refactor to take advantage of built-in barcode and encoding in escpos library

workspace
Brendan Howell 4 years ago
parent 6c67985b42
commit c52486b91d

@ -92,6 +92,7 @@ class LogPrinter(logging.Handler):
self.printer["productid"],
in_ep=self.printer["inep"],
out_ep=self.printer["outep"])
prn.set()
msg = self.format(record)
#text = textwrap.fill(msg, width=self.printer["textwidth"])
splitter_regex = r'.{1,' + str(self.printer["textwidth"]) + '}(?:\s+|$)'
@ -100,6 +101,7 @@ class LogPrinter(logging.Handler):
out_text += "\r\n" * 4
prn.text(out_text)
prn.cut()
prn.close()
class KeyValStore(object):
@ -445,22 +447,26 @@ class Bureau(object):
print on Thermal Line printer.
"""
prn = self._get_small_printer()
prn.set()
splitter_regex = r'.{1,' + str(self.smprint["textwidth"]) + '}(?:\s+|$)'
out_text = "\r\n".join(line.strip() for line in
re.findall(splitter_regex, text))
prn.text(out_text + "\r\n")
prn.text(out_text)
prn.ln()
if linefeed:
prn.text("\r\n\r\n")
prn.ln(2)
if cut:
prn.cut()
prn.close()
def print_small_image(self, img, linefeed=True):
"""
print an image on the mini thermal printer.
"""
prn = self._get_small_printer()
prn.set()
if type(img) is PIL.Image.Image:
im = img
@ -474,7 +480,7 @@ class Bureau(object):
# not using this bitImageColumn crashes some printers, sigh
prn.image(im, impl="bitImageColumn")
if linefeed:
prn.text("\r\n")
prn.ln()
@add_command("test")
def test(self, data=None):

@ -136,6 +136,7 @@ class Publications(Bureau):
Use the small printer to output the current local weather forecast
pulled from met.no api.
"""
#TODO: refactor this to not mess with opening and closing the printer
lat, lon = self.config["latlon"]
forecast = weather.get_forecast(lat, lon)
prn = self._get_small_printer()
@ -155,7 +156,7 @@ class Publications(Bureau):
prn.text(period["mintemp"] + " - " + period["maxtemp"] + "C\r\n")
if day_count > 4:
break
prn.text("\r\n")
prn.ln()
prn.cut()
prn.close()

@ -150,23 +150,18 @@ class PublicRelations(Bureau):
prn = self._get_small_printer()
# TODO: abstract this to use a simple templating system instead of raw
prn.codepage = "cp437"
# TODO: add fancier formatting i.e. inverted text for username/handle
tweets = self.t.t.statuses.home_timeline(count=count,
tweet_mode="extended")
out = ""
for t in tweets:
prn.set(text_type="U")
username = t["user"]["name"].encode("cp437", "ignore")
prn._raw(username)
prn.text("\r\n")
username = t["user"]["name"]
prn.textln(username)
prn.set(text_type="NORMAL")
twtext = html.unescape(t["full_text"])
t_wrapped = textwrap.fill(twtext, width=self.smprint["textwidth"])\
+ "\r\n"
t_enc = t_wrapped.encode("cp437", "ignore")
prn._raw(t_enc)
prn.block_text(twtext)
prn.ln()
if "media" in t["entities"]:
if len(t["entities"]["media"]) > 0:
@ -182,10 +177,12 @@ class PublicRelations(Bureau):
prn.image(im, impl="bitImageColumn")
tw_shortcode = self.short_tweet_id(t["id_str"])
prn.barcode("PRtwd." + tw_shortcode, "CODE128", function_type="B")
prn.text("\r\n\r\n")
#prn.barcode("PRtwd." + tw_shortcode, "CODE128", function_type="B")
prn.soft_barcode("CODE128", "PRtwd." + tw_shortcode)
prn.ln(2)
prn.cut()
prn.close()
@add_command("twd", "Print Tweet Details")
def tw_details(self, data):
@ -196,18 +193,13 @@ class PublicRelations(Bureau):
tweet_id = self.get_tweet_id(shortcode)
tweet = self.t.t.statuses.show(id=tweet_id, tweet_mode="extended")
prn = self._get_small_printer()
prn.codepage = "cp437"
prn.set(text_type="U")
username = tweet["user"]["name"].encode("cp437", "ignore")
prn._raw(username)
prn.text("\r\n")
username = tweet["user"]["name"]
prn.textln(username)
prn.set(text_type="NORMAL")
twtext = html.unescape(tweet["full_text"])
t_wrapped = textwrap.fill(twtext, width=self.smprint["textwidth"])\
+ "\r\n"
t_enc = t_wrapped.encode("cp437", "ignore")
prn._raw(t_enc)
prn.(twtext)
if "media" in tweet["entities"]:
for entity in tweet["entities"]["media"]:
@ -224,14 +216,17 @@ class PublicRelations(Bureau):
tw_shortcode = self.short_tweet_id(tweet["id_str"])
prn.text("retweet\r\n")
prn.barcode("PRtwrt." + tw_shortcode, "CODE128", function_type="B")
#prn.barcode("PRtwrt." + tw_shortcode, "CODE128", function_type="B")
prn.soft_barcode("CODE128", "PRtwrt." + tw_shortcode)
prn.text("like\r\n")
prn.barcode("PRtwlk." + tw_shortcode, "CODE128", function_type="B")
#prn.barcode("PRtwlk." + tw_shortcode, "CODE128", function_type="B")
prn.soft_barcode("CODE128", "PRtwlk." + tw_shortcode)
prn.text("\r\n\r\n")
prn.barcode("PRtwre." + tw_shortcode, "CODE128", function_type="B")
prn.text("\r\n\r\n")
#prn.barcode("PRtwre." + tw_shortcode, "CODE128", function_type="B")
prn.soft_barcode("CODE128", "PRtwre." + tw_shortcode)
prn.ln(2)
prn.cut()
prn.close()
@add_command("twrt", "Re-Tweet")
def tw_retweet(self, data):
@ -288,7 +283,6 @@ class PublicRelations(Bureau):
photo = self.send("PX", "photo")["photo"]
media = self.masto.media_post(photo)
post = self.masto.status_post("", media_ids=[media])
#self.log.debug(str(post))
@add_command("tootline", "Print Recent Toots")
def tootline(self, data=None):
@ -313,16 +307,12 @@ class PublicRelations(Bureau):
out = ""
for t in toots:
prn.set(text_type="U")
username = t.account.display_name.encode("cp437", "ignore")
prn._raw(username)
prn.text("\r\n")
username = t.account.display_name
prn.textln(username)
prn.set(text_type="NORMAL")
ttext = bleach.clean(t.content, tags=[], strip=True)
ttext = html.unescape(ttext)
t_wrapped = textwrap.fill(ttext, width=self.smprint["textwidth"])\
+ "\r\n"
t_enc = t_wrapped.encode("cp437", "ignore")
prn._raw(t_enc)
prn.block_text(ttext)
if len(t.media_attachments) > 0:
img = None
@ -342,12 +332,13 @@ class PublicRelations(Bureau):
prn.image(im, impl="bitImageColumn")
tw_shortcode = self.short_tweet_id(str(t["id"]))
prn.barcode("PRmad." + tw_shortcode, "CODE128", function_type="B")
#prn.barcode("PRmad." + tw_shortcode, "CODE128", function_type="B")
prn.soft_barcode("CODE128", "PRmad." + tw_shortcode)
notifications = self.masto.notifications(since_id=self.last_mast_notif)
if len(notifications) > 0:
prn.set(text_type="B")
prn.text("NOTIFICATIONS:\r\n")
prn.textln("NOTIFICATIONS:")
prn.set(text_type="NORMAL")
# store the last notification id
@ -355,16 +346,16 @@ class PublicRelations(Bureau):
with self.dbenv.begin(db=self.tweetdb, write=True) as txn:
txn.put(b"last_mast_notif", self.last_mast_notif.encode())
for note in notifications:
username = note.account.display_name.encode("cp437", "ignore") + \
b" (" + note.account.acct.encode("cp437", "ignore") + b")"
username = note.account.display_name + " (" + note.account.acct + ")"
prn.text(note["type"] + " " + str(note["created_at"]) + " from ")
prn._raw(username)
prn.text(":\r\n" + str(note["status"]) + "\r\n")
prn.textln(username + ":")
prn.textln(str(note["status"]))
with self.dbenv.begin(db=self.tweetdb, write=True) as txn:
txn.put(shortcode.encode(), tweet_id.encode())
prn.text("\r\n\r\n")
prn.ln(2)
prn.cut()
prn.close()
def main():

Loading…
Cancel
Save