-
Notifications
You must be signed in to change notification settings - Fork 0
/
mg_receipt.py
executable file
·157 lines (142 loc) · 4.75 KB
/
mg_receipt.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
import argparse
import csv
import random
import yaml
from yaml import CLoader as Loader
from escpos import config
from evdev import InputDevice, categorize, ecodes
config_file = "config.yaml"
c = config.Config()
p = c.printer()
CONFIGS = yaml.load(open(config_file,"r"), Loader)
parser = argparse.ArgumentParser()
parser.add_argument('-i','--index', type=int,
help="row number (zero-indexed starting from first row of data) of the malls.csv file to use, overrides the randomness")
parser.add_argument('-b', '--barcode', action="store_true",
help="if set, waits for a barcode input before printing a receipt")
parser.add_argument('-r', '--random', action="store_true",
help="if set, prints a random barcode each time a barcode is scanned")
parser.add_argument('-k', '--keys', action="store_true",
help="if set, prints the photos, titles, and barcodes of all malls in database")
parser.add_argument('-a', '--all', action="store_true",
help="prints all receipts in database and exits")
DEVICE = CONFIGS['DEVICE']
def print_key(mdb):
for mall in mdb:
p.image("{}/{}".format(CONFIGS['PHOTO_DIR'],mall['photo']))
p.text("\n")
p.text("+"*CONFIGS['WIDTH'])
p.text("\n")
p.set(align="center", text_type="b", height=2)
p.text(mall['title'])
p.text("\n")
p.barcode("{}{}".format("{B",mall['code']), "CODE128", function_type="B")
p.cut()
def read_barcode():
device = InputDevice(DEVICE)
code = ""
for event in device.read_loop():
if event.type == ecodes.EV_KEY:
event = categorize(event)
if event.keystate != 1:
continue
if event.keycode == "KEY_ENTER":
return code
code += event.keycode.split("_")[-1]
def long_print(text):
memory_grafs = text.split('\\n')
first = True
for graf in memory_grafs:
if first:
first = False
else:
p.text("\n")
p.text("\n")
memory_words = graf.split(' ')
line_len = 0
line = ""
for word in memory_words:
if line_len + len(word) + 1 > CONFIGS['WIDTH']:
p.text(line)
p.text("\n")
line = word
line_len = len(word)
else:
line += " "
line += word
line_len += 1
line_len += len(word)
if len(line) > 0:
p.text(line)
def print_receipt(mall_db, index=None, barcode=None):
if index is not None:
mall = mall_db[index]
else:
mall = random.choice(mall_db)
if barcode is not None:
while barcode == mall['code']:
mall = random.choice(mall_db)
head_text = open(CONFIGS['HEAD'],"r").read()
p.image(CONFIGS['LOGO'])
p.set(align="center")
p.text(head_text)
p.set(text_type="normal")
p.text("="*CONFIGS['WIDTH'])
p.text("\n")
p.barcode("{}{}".format("{B",mall['code']), "CODE128", function_type="B")
p.text("\n"*2)
p.set(align="left")
p.text("MALL:{m}\n".format(m=mall['name']))
p.text("{c},{s}\n".format(c=mall['city'],s=mall['state']))
p.text("\n")
p.image("{}/{}".format(CONFIGS['PHOTO_DIR'],mall['photo']))
p.text("\n")
p.text("BIRTH:{:>36}\n".format(mall['birth']))
p.text("DEATH:{:>36}\n".format(mall['death']))
p.text("ABOUT THE MALL:\n")
long_print(mall['anchors'])
p.text("\n")
p.text("="*CONFIGS['WIDTH'])
p.text("\n")
p.set(align="center", text_type="b")
p.text(mall['title'])
p.text("\n")
p.set(text_type="normal")
p.text("="*CONFIGS['WIDTH'])
p.text("\n")
long_print(mall['memory'])
p.text("\n")
p.set(align="right")
p.text("-{}\n".format(mall['memory_author']))
p.text("="*CONFIGS['WIDTH'])
p.text("\n")
p.set(align="center", font="b")
p.text(open(CONFIGS['FOOT'],"r").read())
p.qr(CONFIGS['QRLINK'],size=5)
p.text("\n")
p.set(text_type="normal")
p.cut()
if __name__ == '__main__':
args = parser.parse_args()
reader = csv.DictReader(open(CONFIGS['MALLS'],"r", encoding="utf-8-sig"))
mdb = list()
mall_mapping = dict()
i = 0
for row in reader:
mdb.append(row)
mall_mapping[row['code']] = i
i += 1
if args.all:
for i in range(len(mdb)):
print_receipt(mdb, index=i)
exit()
if args.barcode:
while True:
bc = read_barcode()
ix = mall_mapping.get(bc) if not args.random else None
print_receipt(mdb, barcode=bc, index=ix)
elif args.keys:
print_key(mdb)
else:
print_receipt(mdb, index=args.index)