From 30c3d619740fed18df3cd2652f700e46136343c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Cuevas?= Date: Wed, 11 Aug 2021 12:48:42 +0200 Subject: [PATCH] Config improved, input from stdin with - --- .gitignore | 1 - config.py | 35 +++++++++++++++++++++++++++++++++++ config.py.example | 9 --------- smallprint.py | 22 +++++++++++++++++----- 4 files changed, 52 insertions(+), 15 deletions(-) create mode 100644 config.py delete mode 100644 config.py.example diff --git a/.gitignore b/.gitignore index f05e58d..8a77a23 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ .pyc .pyo -config.py __pycache__ .ropeproject/ bin/ diff --git a/config.py b/config.py new file mode 100644 index 0000000..c509eae --- /dev/null +++ b/config.py @@ -0,0 +1,35 @@ +from pathlib import Path +import os +import yaml + +CONFIG_PATH = "~/.config/smallprint/config.yml" + +config = { + "OWM": "Your OpenWeatherMap token goes here", + "CITY": "City name", + "PRINTER_FILE": "/dev/usb/lp0", + "PRINTER_USB_ID": "", + "PRINTER_INTERFACE": 0, + "PRINTER_ENDPOINT": 0x01, +} + + +def load_config(): + global config + path = Path(CONFIG_PATH).expanduser() # Generate the path to the config file + + if not path.parent.is_dir(): + os.mkdir(path.parent) + + if not path.is_file(): + # Create sample file + with path.open("w") as cfg_file: + cfg_file.write(yaml.dump(config)) + + return False + + else: + with path.open("r") as cfg_file: + config = yaml.load(cfg_file.read(), Loader=yaml.FullLoader) + + return True diff --git a/config.py.example b/config.py.example deleted file mode 100644 index bfdc214..0000000 --- a/config.py.example +++ /dev/null @@ -1,9 +0,0 @@ -# Simple configuration example - -config = { - "OWM": "Your OpenWeatherMap token geos here", - "CITY": "City name", - "PRINTER_USB_ID": "", - "PRINTER_INTERFACE": 0, - "PRINTER_ENDPOINT": 0x01, -} diff --git a/smallprint.py b/smallprint.py index 9a9338c..179067a 100644 --- a/smallprint.py +++ b/smallprint.py @@ -7,6 +7,7 @@ import math import datetime import os import os.path +import sys # 3rd party imports from escpos.printer import Usb, Dummy, File @@ -16,7 +17,7 @@ from PIL import Image import re import requests -from config import config +from config import config, load_config, CONFIG_PATH # Enable logging @@ -31,6 +32,12 @@ def initialize(): Initializes the printer and returns a printer object to generate the print """ + if not load_config(): + print(f"Config file created, check {CONFIG_PATH}") + + if os.path.exists(config.get("PRINTER_FILE")): + return File(config.get("PRINTER_FILE")) + printer_id = config.get("PRINTER_USB_ID") if not printer_id: @@ -41,8 +48,7 @@ def initialize(): printer_interface = config.get("PRINTER_INTERFACE") or 0 printer_endpoint = config.get("PRINTER_ENDPOINT") or 0x01 - # return Usb(prid1, prid2, printer_interface, printer_endpoint) - return File("/dev/usb/lp4") + return Usb(prid1, prid2, printer_interface, printer_endpoint) def reset_defaults(printer): @@ -53,14 +59,15 @@ def reset_defaults(printer): height=1, density=9, invert=False, smooth=False, flip=False) -def print_weather(printer): +def print_weather(printer, city=None): reset_defaults(printer) appkey = config.get("OWM") if not appkey: logger.error("Open Weather key not set!") return - city = config.get("CITY") + if not city: + city = config.get("CITY") if not city: logger.error("No city set") @@ -248,6 +255,11 @@ def print_text(printer, text): font="b", smooth=True) printer.text("\n") + if text.strip() == "-": + # Load stdin data + for line in sys.stdin: + printer.text(line) + printer.text(text)