From 70cffdeb57258ab5ac7f8b853dc028b32bbed109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas?= Date: Wed, 9 Sep 2020 11:48:51 +0200 Subject: [PATCH] Command line switches and new functionality --- requirements.txt | 1 - smallprint.py | 89 ++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 78 insertions(+), 12 deletions(-) diff --git a/requirements.txt b/requirements.txt index 28c9621..59ff2a1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,7 +6,6 @@ feedparser==5.2.1 idna==2.10 importlib-metadata==1.7.0 Pillow==7.2.0 -pkg-resources==0.0.0 pyserial==3.4 python-escpos==2.2.0 pyusb==1.0.2 diff --git a/smallprint.py b/smallprint.py index a2784fb..2d2d320 100644 --- a/smallprint.py +++ b/smallprint.py @@ -1,6 +1,7 @@ #! /usr/bin/env python # python imports +import argparse import logging import datetime import os.path @@ -37,7 +38,7 @@ def initialize(): printer_endpoint = config.get("PRINTER_ENDPOINT") or 0x01 # return Usb(prid1, prid2, printer_interface, printer_endpoint) - return File("/dev/usb/lp2") + return File("/dev/usb/lp0") def reset_defaults(printer): @@ -144,21 +145,87 @@ def _print_rss_item(item): printer.text("\n") +def print_text(printer, text): + """ + Prints a text in the smallest form possible + """ + reset_defaults(printer) + # Set the font to a small one and align to + # the left + printer.set(align="left", + text_type="NORMAL", + font="b", + smooth=True) + printer.text("\n") + printer.text(text) + + +def print_file(printer, file): + """ + Prints a file + """ + reset_defaults(printer) + # Set the font to a small one and align to + # the left + printer.set(align="left", + text_type="NORMAL", + font="b", + smooth=True) + + printer.text("\n") + + for line in file: + printer.text(line) + + file.close() + printer.text("\n\n") + + + def clear_html(text): cleanr = re.compile('<.*?>|&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-f]{1,6});') cleantext = re.sub(cleanr, '', text) return cleantext +def create_parser(): + """ + Create an argpaser for command line interaction + """ + parser = argparse.ArgumentParser() + + parser.add_argument("--weather", default=False) + parser.add_argument("--news", default=False) + parser.add_argument("--text", nargs=1, default=None) + parser.add_argument("--file", default=None, type=open) + + return parser + + if __name__ == "__main__": printer = initialize() - print_weather(printer) - printer.text("\n\n") - print_rss(printer, "https://victorhckinthefreeworld.com/feed/") - printer.text("\n\n") - print_rss(printer, "https://pedalibre.org/feed/") - printer.text("\n\n") - print_rss(printer, "https://skyandtelescope.org/observing/sky-at-a-glance/feed/") - printer.text("\n\n") - print_rss(printer, "https://www.space.com/feeds/all", news=2) - printer.text("\n\n") + + parser = create_parser() + args = parser.parse_args() + + if args.text: + print_text(printer, args.text) + printer.text("\n\n") + + if args.file: + print_file(printer, args.file) + printer.text("\n\n") + + if args.weather: + print_weather(printer) + printer.text("\n\n") + + if args.news: + print_rss(printer, "https://victorhckinthefreeworld.com/feed/") + printer.text("\n\n") + print_rss(printer, "https://pedalibre.org/feed/") + printer.text("\n\n") + print_rss(printer, "https://skyandtelescope.org/observing/sky-at-a-glance/feed/") + printer.text("\n\n") + print_rss(printer, "https://www.space.com/feeds/all", news=2) + printer.text("\n\n")