-
Notifications
You must be signed in to change notification settings - Fork 0
/
latex_data.py
309 lines (238 loc) · 11 KB
/
latex_data.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""
LatexDate provides a mechanism for mainting the integrity and atomicity of the latex string, template, and image
Example:
python latex_template.py --verbose --keep --pattern '\\si{\\km}^3' --show
python latex_template.py --verbose --keep
Database Errors and Warnings: https://www.tutorialspoint.com/database-handling-errors-in-python
"""
__author__ = "William DeShazer"
__version__ = "0.1.0"
__license__ = "MIT"
import os
import subprocess
import argparse
import pprint
from warnings import warn
from io import BytesIO
from datetime import datetime
from dataclasses import dataclass
from typing import NewType, List, Optional
from pathlib import Path
from pickle import dumps
from psycopg2 import DatabaseError
from psycopg2.extras import NamedTupleCursor
from PIL import Image
from time_logging import TimeLogger
from template import TemplateTable
from db_utils import my_connect
TemplateID = NewType("TemplateID", int)
TemplateIDs = NewType("Templates", List[TemplateID])
def available_templates(my_conn: Optional[dict] = None, t_log: Optional[TimeLogger] = None,
verbose: bool = False) -> TemplateIDs:
"""Function to pull available templates from ddtatbase"""
if verbose is True and t_log is None:
t_log = TimeLogger()
my_conn = my_connect(my_conn=my_conn, t_log=t_log, verbose=verbose)
conn = my_conn['conn']
if verbose is True:
print('Extracting available Templates')
if verbose is True:
t_log.new_event('Connecting to Templates on Database')
cur = conn.cursor()
try:
cur.execute('SELECT id FROM template ORDER BY created_at')
except DatabaseError as error:
print("Couldn't retrieve templates", error)
output = cur.fetchall()
cur.close()
template_ids = TemplateIDs([x[0] for x in output])
if verbose is True:
t_log.new_event('Finished Extracting Data')
return template_ids
def template(my_conn: Optional[dict] = None, t_log: Optional[TimeLogger] = None,
version: int = None, verbose: bool = False) -> NamedTupleCursor:
"""Function to pull the data for a specified template"""
if verbose is True and t_log is None:
t_log = TimeLogger()
my_conn = my_connect(my_conn=my_conn, t_log=t_log, verbose=verbose)
conn = my_conn['conn']
cur = conn.cursor(cursor_factory=NamedTupleCursor)
if version is None:
if verbose:
t_log.new_event('Extracting Latest Template from Database')
cur.execute('SELECT * FROM template ORDER BY created_at DESC LIMIT 1')
else:
try:
if verbose:
t_log.new_event('Extracting Template with id: {} from Database'.format(version))
cur.execute('SELECT * FROM template WHERE ID=%s', (version,))
except DatabaseError as error:
print("Couldn't retrieve that template.", error)
print("Returning latest template.")
cur.execute('SELECT * FROM template ORDER BY created_at DESC LIMIT 1')
if verbose:
t_log.new_event('Starting Template Extraction')
the_template = cur.fetchone()
cur.close()
if verbose:
t_log.new_event('Finished Template Extraction')
return the_template
def compile_pattern(pattern: str = 'm^3', keep: bool = True, temp_fname: str = "eq_db", version: int = None,
a_template: str = None, verbose: bool = False,
my_conn: Optional[dict] = None, t_log: Optional[TimeLogger] = None) -> bytes:
"""General Latex Compile Function"""
if verbose:
if t_log is None:
t_log = TimeLogger()
print('\tArguments:')
pprint.PrettyPrinter(indent=12).pprint(locals())
if a_template is None:
a_template_record = template(version=version, my_conn=my_conn, t_log=t_log, verbose=verbose)
a_template = a_template_record.data
this_path = Path.cwd()
if this_path.name == 'CustomWidgets':
os.chdir('../LaTeX')
elif this_path.name == 'equation_database':
os.chdir('LaTeX')
else:
warn('Unrecognized Directory')
# preprocess pattern to make sure it conforms to latex
processed_pattern = pattern.strip()
text_file = open(temp_fname+'.tex', "w")
text_file.write(a_template.replace('%__REPLACEMENT__TEXT', processed_pattern))
text_file.close()
# try:
if verbose:
print("Operating System: ", os.name)
t_log.new_event('Executing XeLaTeX:')
shell = os.name == 'nt'
try:
p_1 = subprocess.run(['xelatex.exe', temp_fname + '.tex', '-interaction=batchmode'],
shell=shell, capture_output=True, text=True, check=True)
if verbose:
pprint.PrettyPrinter(indent=8).pprint(p_1)
except subprocess.CalledProcessError as error:
print(error.output)
try:
if verbose:
t_log.new_event('Converting to png')
p_2 = subprocess.run(['convert.exe', '-density', '300', '-depth', '8', '-quality', '85', temp_fname + '.pdf',
'png32:'+temp_fname+'.png'], shell=shell, capture_output=True, text=True, check=True)
if verbose:
t_log.new_event('Finished converting png')
pprint.PrettyPrinter(indent=8).pprint(p_2)
except subprocess.CalledProcessError as error:
t_log.new_event('Failed to make png:')
print(error.output)
if verbose:
t_log.new_event('Pulling png data into python')
with open(temp_fname + '.png', 'rb') as file:
png_data: bytes = file.read()
os.chdir(this_path)
if keep is False:
clean_files(temp_fname)
return png_data
def clean_files(temp_fname="LaTeX/eq_db"):
"""Removes associated LaTeX Files. Unactive by default"""
os.remove(temp_fname+'.aux')
os.remove(temp_fname+'.log')
os.remove(temp_fname+'.pdf')
os.remove(temp_fname+'.tex')
os.remove(temp_fname+'.png')
def main(pattern: str = 'm^3', keep: bool = False, temp_fname: str = "eq_db", version: int = None,
a_template: str = None, template_file: str = None, verbose: bool = False, show: bool = False):
"""Main function for executing a compile"""
if verbose:
print('\tArguments:')
pprint.PrettyPrinter(indent=12).pprint(locals())
# Both aTemplate and template_file are provided here, because programmatically specifying the string seems
# reasonable in addition to specifying a filename. However, it doesn't seem like a reasonable thing to do on the
# command line. It does make sense as a redirected pipe, but I don't have interest in making that happen,
# because it's not even my preferred way of interracting
if template_file is not None:
if verbose:
print('Reading LaTeX Template File: {}'.format(template_file))
with open(template_file, 'r') as my_file:
a_template = my_file.readlines()
compile_pattern(pattern=pattern, keep=keep, version=version, temp_fname=temp_fname, a_template=a_template,
verbose=verbose)
if show is True:
print('add show function')
# noinspection PyMethodParameters
@dataclass
# pylint: disable=too-many-instance-attributes
class LatexData:
"""Class for managing LaTeX data, including image"""
latex: str = r"a^2 + b^2 = c^2"
template_id: TemplateID = None
image: bytes = None
compiled_at: datetime = None
image_is_dirty: bool = False
my_conn: dict = None
t_log: TimeLogger = None
verbose: bool = False # I don't like this, but I verbosity during initialization and at the moment __post_init__
# takes no arguments
def __post_init__(self):
"""Template ID is set during Initialization and an update/potential
recompile if either there is no image or the image is inconsistent with the LaTeX.
An inconsistency can only happen when the LaTeX field is update manually with an SQL query"""
if self.image is None:
if self.template_id is None or self.template_id == 'latest':
template_ids: TemplateIDs = available_templates()
self.template_id = template_ids[-1] # pylint: disable=unsubscriptable-object
self.update(image=self.image, image_is_dirty=self.image_is_dirty, my_conn=self.my_conn,
t_log=self.t_log, verbose=self.verbose)
def update(self, latex: str = None, template_id: TemplateID = None, image: bytes = None,
image_is_dirty: bool = False, my_conn: Optional[dict] = None, t_log: Optional[TimeLogger] = None,
verbose: bool = False):
"""Main method. Maintains integrity of latex text and image by recompiling if core data gets updated"""
if image is None:
if my_conn is None:
my_conn = self.my_conn
else:
self.my_conn = my_conn
my_conn = my_connect(my_conn=my_conn, t_log=t_log, verbose=verbose)
self.my_conn = my_conn
if latex is not None:
self.latex = latex
if template_id is not None:
self.template_id = template_id
if image is not None and image_is_dirty is False:
self.image = image
else:
self.image = \
compile_pattern(pattern=self.latex, version=self.template_id, my_conn=my_conn,
t_log=t_log, verbose=verbose)
self.compiled_at = datetime.now()
self.image_is_dirty = False
def template_table(self):
return TemplateTable(my_conn=self.my_conn)
def show(self):
"""Display associated image"""
stream = BytesIO(self.image)
image = Image.open(stream).convert("RGBA")
stream.close()
image.show()
def as_binary(self) -> bytes:
"""Convience function to store data"""
return dumps(self)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='LaTeX Template Resources')
parser.add_argument("--pattern", dest='pattern', help='str: Valid LaTeX Pattern', default="m^3")
parser.add_argument("--keep", dest='keep', help='bool: Remove Support Files',
default=False, action='store_true')
parser.add_argument("--temp_fname", dest='temp_fname', help='str: Filename Token for Temp Files', default="eq_db")
parser.add_argument("--version", dest='version',
help='int: Template Version. None value or non-existent returns latest',
default=None)
parser.add_argument("--template_file", dest='template_file', help='User specified LaTeX template file',
default=None)
parser.add_argument("--verbose", dest='verbose', help='Output run messages', action='store_true')
parser.add_argument("--show", dest='show', help='Show image file', action='store_true')
args = parser.parse_args()
if args.verbose:
print(args)
main(pattern=args.pattern, keep=args.keep, temp_fname=args.temp_fname,
version=args.version, template_file=args.template_file, verbose=args.verbose, show=args.show)
if args.verbose:
print('Total Elapsed Time:')