refactor fedi and twitter to use central small print config

workspace
Brendan Howell 4 years ago
parent f9a24c458b
commit c2addd9cbd

@ -356,6 +356,20 @@ class Bureau(object):
self.log.debug(str(self.commands)) self.log.debug(str(self.commands))
self.log.debug(str(self.api)) self.log.debug(str(self.api))
def _get_small_printer(self):
"""
returns an instance of the small escpos printer
"""
if (self.smprint["inep"] is None) and (self.smprint["outep"] is None):
prn = printer.Usb(self.smprint["vendorid"], self.smprint["productid"])
else:
prn = printer.Usb(self.smprint["vendorid"],
self.smprint["productid"],
in_ep=self.smprint["inep"],
out_ep=self.smprint["outep"])
return prn
def print_full(self, template, **kwargs): def print_full(self, template, **kwargs):
"""print a full page (A4) document """ """print a full page (A4) document """
# TODO: look up the printer LPR name / allow multiple printers/non-default # TODO: look up the printer LPR name / allow multiple printers/non-default
@ -376,13 +390,7 @@ class Bureau(object):
""" """
print on Thermal Line printer. print on Thermal Line printer.
""" """
if (self.smprint["inep"] is None) and (self.smprint["outep"] is None): prn = self._get_small_printer()
prn = printer.Usb(self.smprint["vendorid"], self.smprint["productid"])
else:
prn = printer.Usb(self.smprint["vendorid"],
self.smprint["productid"],
in_ep=self.smprint["inep"],
out_ep=self.smprint["outep"])
text = textwrap.fill(text, width=self.smprint["textwidth"]) text = textwrap.fill(text, width=self.smprint["textwidth"])
text += "\r\n" * 2 text += "\r\n" * 2
prn.text(text + "\r\n\r\n") prn.text(text + "\r\n\r\n")
@ -393,13 +401,7 @@ class Bureau(object):
""" """
print an image on the mini thermal printer. print an image on the mini thermal printer.
""" """
if (self.smprint["inep"] is None) and (self.smprint["outep"] is None): prn = self._get_small_printer()
prn = printer.Usb(self.smprint["vendorid"], self.smprint["productid"])
else:
prn = printer.Usb(self.smprint["vendorid"],
self.smprint["productid"],
in_ep=self.smprint["inep"],
out_ep=self.smprint["outep"])
if type(img) is PIL.Image.Image: if type(img) is PIL.Image.Image:
im = img im = img

@ -145,7 +145,8 @@ class PublicRelations(Bureau):
else: else:
count = 10 count = 10
prn = printer.Usb(0x416, 0x5011, in_ep=0x81, out_ep=0x03) prn = self._get_small_printer()
# TODO: abstract this to use a simple templating system instead of raw
prn.codepage = "cp437" 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
@ -159,7 +160,8 @@ class PublicRelations(Bureau):
prn.text("\r\n") 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=48) + "\r\n" t_wrapped = textwrap.fill(twtext, width=self.smprint["textwidth"])\
+ "\r\n"
t_enc = t_wrapped.encode("cp437", "ignore") t_enc = t_wrapped.encode("cp437", "ignore")
prn._raw(t_enc) prn._raw(t_enc)
@ -173,7 +175,7 @@ class PublicRelations(Bureau):
im = PIL.Image.open(filename) im = PIL.Image.open(filename)
if im.mode in ("L", "RGB", "P"): if im.mode in ("L", "RGB", "P"):
im = PIL.ImageOps.equalize(im) im = PIL.ImageOps.equalize(im)
im.thumbnail((576, 576), PIL.Image.ANTIALIAS) im.thumbnail((self.smprint["width"], 576), PIL.Image.ANTIALIAS)
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"])
@ -190,7 +192,7 @@ class PublicRelations(Bureau):
shortcode, _ = data.split(".") shortcode, _ = data.split(".")
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 = printer.Usb(0x416, 0x5011, in_ep=0x81, out_ep=0x03) prn = self._get_small_printer()
prn.codepage = "cp437" prn.codepage = "cp437"
prn.set(text_type="U") prn.set(text_type="U")
@ -199,7 +201,8 @@ class PublicRelations(Bureau):
prn.text("\r\n") 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=48) + "\r\n" t_wrapped = textwrap.fill(twtext, width=self.smprint["textwidth"])\
+ "\r\n"
t_enc = t_wrapped.encode("cp437", "ignore") t_enc = t_wrapped.encode("cp437", "ignore")
prn._raw(t_enc) prn._raw(t_enc)
@ -213,7 +216,7 @@ class PublicRelations(Bureau):
im = PIL.Image.open(filename) im = PIL.Image.open(filename)
if im.mode in ("L", "RGB", "P"): if im.mode in ("L", "RGB", "P"):
im = PIL.ImageOps.equalize(im) im = PIL.ImageOps.equalize(im)
im.thumbnail((576, 576), PIL.Image.ANTIALIAS) im.thumbnail((self.smprint["width"], 576), PIL.Image.ANTIALIAS)
prn.image(im, impl="bitImageColumn") prn.image(im, impl="bitImageColumn")
tw_shortcode = self.short_tweet_id(tweet["id_str"]) tw_shortcode = self.short_tweet_id(tweet["id_str"])
@ -297,7 +300,7 @@ class PublicRelations(Bureau):
else: else:
count = 10 count = 10
prn = printer.Usb(0x416, 0x5011, in_ep=0x81, out_ep=0x03) prn = self._get_small_printer()
prn.codepage = "cp437" 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
@ -313,7 +316,8 @@ class PublicRelations(Bureau):
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=48) + "\r\n" t_wrapped = textwrap.fill(ttext, width=self.smprint["textwidth"])\
+ "\r\n"
t_enc = t_wrapped.encode("cp437", "ignore") t_enc = t_wrapped.encode("cp437", "ignore")
prn._raw(t_enc) prn._raw(t_enc)
@ -331,7 +335,7 @@ class PublicRelations(Bureau):
im = PIL.Image.open(io.BytesIO(req_data.content)) im = PIL.Image.open(io.BytesIO(req_data.content))
if im.mode in ("L", "RGB", "P"): if im.mode in ("L", "RGB", "P"):
im = PIL.ImageOps.equalize(im) im = PIL.ImageOps.equalize(im)
im.thumbnail((576, 576), PIL.Image.ANTIALIAS) im.thumbnail((self.smprint["width"], 576), PIL.Image.ANTIALIAS)
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"]))

Loading…
Cancel
Save