-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.py
199 lines (176 loc) · 9.02 KB
/
menu.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import json
import os
import re
from datetime import datetime
from shutil import copy2
from reportlab.graphics import renderPDF, renderPM
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from svglib.svglib import svg2rlg
from utils import printable_date
class MenuGenerator:
def __init__(self, api, options, logger):
self.options = options
self.api = api
self.logger = logger
self.template_dir = os.path.join(os.getcwd(), 'templates')
self.output_dir = options.output_dir or self.template_dir
self.input_file = options.file_template
self.temp_file = os.path.join(self.output_dir, 'temp.svg')
self.output_file = self.input_file.split('.')[0]
self.ext = options.file_format
self.fonts = [json.loads(f.replace("'", '"')) for f in options.fonts]
self.replace_text = options.replace_text
self.seperator = options.seperator
def write_menu(self, recipes):
template = self.open_template()
if any('ingredients' in r for r in self.options.replace_text['recipe_text']):
for r in recipes:
r.addDetails(self.api)
template = self.find_and_replace(recipes, template)
self.write_temp_template(template)
self.convert_svg()
self.cleanup()
def convert_svg(self):
# Register font files
for f in self.fonts:
self.logger.debug(f'Loading font {f["name"]} from {os.path.join(self.template_dir, f["file"])}.')
font = TTFont(f['name'], os.path.join(self.template_dir, f['file']))
pdfmetrics.registerFont(font)
self.logger.debug(f'Font {font.fontName} loaded succesfully.')
# Load the SVG file as a ReportLab graphics object
drawing = svg2rlg(self.temp_file)
temp_output = os.path.join(os.getcwd(), f'temp.{self.ext}')
if self.ext.lower() == 'pdf':
output_file = os.path.join(self.output_dir, f'{self.output_file}.{self.ext}')
self.logger.debug(f'Writing PDF to {output_file}.')
renderPDF.drawToFile(drawing, temp_output)
else:
output_file = os.path.join(self.output_dir, f'{self.output_file}.{self.ext}')
self.logger.debug(f'Writing {self.ext} to {output_file}.')
renderPM.drawToFile(drawing, temp_output, fmt=self.ext)
self.logger.debug(f'Moving file {temp_output} to {output_file}.')
os.rename(temp_output, output_file)
os.chmod(output_file, 0o755)
self.archive(output_file)
def find_and_replace(self, recipes, template):
def _escape_svg_text(text):
escapes = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
}
return re.sub(r'[\&\<\>\"\']', lambda match: escapes[match.group(0)], text)
if date_text := self.replace_text.get('date_text', None):
date, ordinal = printable_date(self.options.mp_date, format=date_text.get('format', None))
# update dates if they exist
if d := date_text.get('date', None):
template = re.sub(re.escape(d), date, template)
if d := date_text.get('ordinal', None):
template = re.sub(re.escape(d), ordinal, template)
# create replacement dict
replacement_dict = self.prepare_replacement(recipes)
for k, v in replacement_dict.items():
self.api.update_progress()
template = re.sub(re.escape(k), _escape_svg_text(v), template)
return template
def prepare_replacement(self, recipes):
def _length_replace_ing(x):
return len(' '.join(x['ingredients']))
def _length_recipe_ing(x):
return len(self.options.seperator.join(x))
def _chunk_ingredients(before, after):
pairs = []
seperator = self.options.seperator.replace(" ", "~|~")
after = f' {seperator} '.join(after).split()
for x in before:
chunk = ''
while after and len(x) >= len(chunk + after[0]):
next_chunk = after.pop(0)
if chunk == '':
if next_chunk == seperator:
next_chunk = after.pop(0)
chunk += next_chunk
else:
chunk += (" " + next_chunk.replace('~|~', ' ')).replace(" ", " ")
if chunk:
pairs.append((x, chunk))
else:
pairs.append((x, ''))
return pairs
# create temporary array to assign before/after values
temp_text = []
for x in range(len(self.options.replace_text['recipe_text'])):
before = self.options.replace_text['recipe_text'][x]
before['after_name'] = recipes[x].name
before['after_ing'] = [x.name for x in recipes[x].ingredients]
temp_text.append(before)
# confirm that all after strings are shorter than before strings.
for y in range(len(temp_text)):
text_fits = len(temp_text[y]['name']) >= len(temp_text[y]['after_name']) and _length_replace_ing(temp_text[y]) >= _length_recipe_ing(temp_text[y]['after_ing'])
if not text_fits:
truncate_text = True
# if there are no other slots that the name and ingredients fit - just truncate the text
if (
any(len(t['name']) >= len(temp_text[y]['after_name']) for t in temp_text) and
any(_length_replace_ing(t) >= _length_recipe_ing(temp_text[y]['after_ing']) for t in temp_text)
):
for z in range(len(temp_text)):
# if swapping the recipes between two positions fits the text then do that
if (
len(temp_text[z]['name']) > len(temp_text[y]['after_name']) and _length_replace_ing(temp_text[z]) >= _length_recipe_ing(temp_text[y]['after_ing']) and
len(temp_text[y]['name']) > len(temp_text[z]['after_name']) and _length_replace_ing(temp_text[y]) >= _length_recipe_ing(temp_text[z]['after_ing'])
):
tmp_name = temp_text[y]['after_name']
tmp_ing = temp_text[y]['after_ing']
temp_text[y]['after_name'] = temp_text[z]['after_name']
temp_text[y]['after_ing'] = temp_text[z]['after_ing']
temp_text[z]['after_name'] = tmp_name
temp_text[z]['after_ing'] = tmp_ing
truncate_text = False
if truncate_text:
temp_text[y]['after_name'] = temp_text[y]['after_name'][:len(temp_text[y]['name'])]
new_ing = []
while temp_text[y]['after_ing'] and _length_replace_ing(temp_text[y]) > _length_recipe_ing(new_ing):
new_ing.append(temp_text[y]['after_ing'].pop(0))
temp_text[y]['after_ing'] = new_ing
# create replacement dict in the form of key:value = before:after
replacements = {}
for y in temp_text:
replacements[y['name']] = y['after_name']
after_chunks = _chunk_ingredients(y['ingredients'], y['after_ing'])
for pair in after_chunks:
replacements[pair[0]] = pair[1]
return replacements
def open_template(self):
# Open file and read contents
self.logger.debug(f'Opening template from {os.path.join(self.template_dir, self.input_file)}.')
with open(os.path.join(self.template_dir, self.input_file)) as f:
return f.read()
def write_temp_template(self, template):
self.logger.debug(f'Writing temporary template to {self.temp_file}.')
with open(self.temp_file, 'w') as f:
f.write(template)
def cleanup(self):
self.archive(self.temp_file, target_name=self.input_file)
self.logger.debug(f'Removing temporary file {self.temp_file}.')
os.remove(self.temp_file)
def archive(self, file, target_name=None):
if not target_name:
target_name = file
if self.logger.loglevel == 10:
archive_dir = os.path.join(self.template_dir, 'archive')
if not os.path.exists(archive_dir):
os.makedirs(archive_dir)
filename, ext = os.path.splitext(os.path.basename(target_name))
archive_file = (a_file := f"{filename}-{(datetime.now().strftime('%y%m%d'))}")
count = 1
while os.path.exists(os.path.join(archive_dir, f"{archive_file}{ext}")):
archive_file = a_file + '_' + str(count)
count += 1
# os.rename(self.temp_file, os.path.join(archive_dir, "{archive_file}.{ext}"))
af = os.path.join(archive_dir, f"{archive_file}{ext}")
self.logger.debug(f'Archiving file {file} to {af}.')
copy2(file, af)