~kf5jwc/sms-printer

ref: 001bc477efa7f19caabd0709e22738bcb00bb077 sms-printer/sms_printer/config.py -rw-r--r-- 2.5 KiB
001bc477 — Kyle Jones Create a namespace for parsers 1 year, 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from math import floor
from os import getenv
from typing import List

from xdg import BaseDirectory


def first_defined_argument(*args):
    return next((item for item in args if item is not None), None)


first = first_defined_argument


class Environment(object):
    """
    This is where all of the configuration options (should) live.

    Please review the class variables for available configuration
    options through their respective environment variables.
    """

    ARCHIVE = getenv("ARCHIVE_DIR", BaseDirectory.save_data_path(__package__))
    ARCHIVE_SUFFIX_LENGTH = int(getenv("SUFFIX_LENGTH", "6"))
    ALLOWED_SENDERS = [num for num in getenv("ALLOWED_SENDERS", "").split(",") if num]

    MESSAGE_FORMAT = getenv("MESSAGE_FORMAT", "{message}")

    PRINT_COMMAND = getenv("PRINT_COMMAND", "lp")
    PRINTER_NAME = getenv("PRINTER_NAME", None)
    PRINT_JOB_TIMEOUT = int(getenv("PRINT_JOB_TIMEOUT", "5"))  # seconds

    CPI = int(getenv("CPI", "10"))
    LPI = int(getenv("LPI", "6"))
    TEXT_SCALE = float(getenv("TEXT_SCALE", "1"))

    # Set in points; each point is 1/72 inch or 0.35mm.
    MARGINS = getenv("MARGINS", None)
    MARGIN_SIDES = first(getenv("MARGIN_SIDES", None), MARGINS)
    MARGIN_TOP = first(getenv("MARGIN_TOP", None), MARGINS)
    MARGIN_BOTTOM = first(getenv("MARGIN_BOTTOM", None), MARGINS)
    MARGIN_LEFT = first(getenv("MARGIN_LEFT", None), MARGIN_SIDES, MARGINS)
    MARGIN_RIGHT = first(getenv("MARGIN_RIGHT", None), MARGIN_SIDES, MARGINS)

    def print_command(self, file_to_print=None) -> List[str]:
        command = [self.PRINT_COMMAND]

        if self.PRINTER_NAME is not None:
            command.append("-d {}".format(self.PRINTER_NAME))

        if self.TEXT_SCALE is not 1.0:
            # Scale horizontally
            cpi = floor(self.CPI * (1 / self.TEXT_SCALE))
            command.append("-o cpi={}".format(cpi))

            # Scale vertically
            lpi = floor(self.LPI * (1 / self.TEXT_SCALE))
            command.append("-o lpi={}".format(lpi))

        if self.MARGIN_TOP is not None:
            command.append("-o page-top={}".format(self.MARGIN_TOP))

        if self.MARGIN_BOTTOM is not None:
            command.append("-o page-bottom={}".format(self.MARGIN_BOTTOM))

        if self.MARGIN_LEFT is not None:
            command.append("-o page-left={}".format(self.MARGIN_LEFT))

        if self.MARGIN_RIGHT is not None:
            command.append("-o page-right={}".format(self.MARGIN_RIGHT))

        if file_to_print is not None:
            command.append(file_to_print)

        return command