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

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

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

Loading…
Cancel
Save