~kf5jwc/sms-printer

3e2045c33396aa38eccc5e42b7c3de9a73a907ec — Kyle Jones 2 years ago 06b4d3c
Add text scaling

This smells like I should have a config file, or more environment
configuration
1 files changed, 12 insertions(+), 2 deletions(-)

M sms_printer/application.py
M sms_printer/application.py => sms_printer/application.py +12 -2
@@ 1,4 1,5 @@
import os
from math import floor
from subprocess import CalledProcessError, run
from tempfile import NamedTemporaryFile



@@ 10,6 11,10 @@ ARCHIVE = os.getenv('ARCHIVE_DIR', BaseDirectory.save_data_path(__name__))
ALLOWED_SENDERS = [
    num for num in os.getenv('ALLOWED_SENDERS', '').split(',') if num
]
PRINTER_FORMAT = "\n\n" + "      {message}"*2
TEXT_SCALE = 0.6
CPI_DEFAULT = 10
LPI_DEFAULT = 6


@APP.route("/", methods=['GET'])


@@ 36,11 41,16 @@ def sms_printer():

    def print_message(message):
        with NamedTemporaryFile(mode='w+') as tmp_file:
            print("{}".format(message), file=tmp_file)
            print(PRINTER_FORMAT.format(message=message), file=tmp_file)
            tmp_file.flush()

            command = [
                "lp",
                "-o cpi={}".format(floor(CPI_DEFAULT*(1/TEXT_SCALE))),
                "-o lpi={}".format(floor(LPI_DEFAULT*(1/TEXT_SCALE))),
                tmp_file.name]
            try:
                run(['lp', tmp_file.name], check=True, timeout=10)
                run(command, check=True, timeout=10)

            except CalledProcessError:
                return False