Config improved, input from stdin with -
This commit is contained in:
parent
8bb547cfb4
commit
30c3d61974
4 changed files with 52 additions and 15 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,6 +1,5 @@
|
|||
.pyc
|
||||
.pyo
|
||||
config.py
|
||||
__pycache__
|
||||
.ropeproject/
|
||||
bin/
|
||||
|
|
35
config.py
Normal file
35
config.py
Normal file
|
@ -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
|
|
@ -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,
|
||||
}
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue