-
Notifications
You must be signed in to change notification settings - Fork 2
/
dyn.py
376 lines (328 loc) · 11.1 KB
/
dyn.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 21 12:14:52 2011
@author: Pietro Zambelli
Dynamic LaTex Generator
"""
import configparser
import os
import shutil
import sys
from datetime import datetime
from optparse import OptionParser
from filters import Environment
# print "where we are: ", os.getcwd()
# print "where dyn.py is", os.path.abspath(__file__)
sys.path.append(os.path.abspath(__file__))
def makedir(path):
"""Create directories and subdirectories like `mkdir -p` """
directories = path.split(os.sep)
origdir = os.path.abspath(os.path.curdir)
# print 'makedir: ', path
for _ in directories:
if os.path.exists(_) == False:
os.mkdir(_)
os.chdir(_)
# return to the previous directory
os.chdir(origdir)
def renderfile(_src, _dst, kargs):
"""Generate a rendred file from a jinja template.
>>> makedir('build')
>>> opt = {'info': {'surname': 'Bonaparte', 'name': 'Napoleone'},
... 'tab' : {'add_hline': '0,1,-1', 'col_layout': '0:l'}}
>>> renderfile('examples/main.tex', 'build', opt)
>>> os.path.isfile('build/main.tex')
True
>>> shutil.rmtree('build/')
"""
# load the template
with open(_src, "r") as srcfile:
srcbytes = srcfile.read()
template = Environment.from_string(srcbytes)
srcname = os.path.split(_src)[1]
# get the new paths, for directory and file
# newdirpath, newfilepath = get_newpath(_src, _dst)
# no error if existing, make parent directories as needed
newfilepath = os.path.join(_dst, srcname)
makedir(_dst) # like in a shell the comand "make -p"
with open(newfilepath, "w") as newf:
# record path, where we are as original directory
_odir = os.path.abspath(os.path.curdir)
# move into the directory contain the template
# in order to respect the path contained in the tempalate
os.chdir(os.path.split(_src)[0])
# do a render and write it to a file
# import pdb; pdb.set_trace()
try:
newf.write(template.render(**kargs))
newf.close()
except TypeError:
print(
("\n".join(["{0} : {1}".format(k, v) for k, v in list(kargs.items())]))
)
raise TypeError(
"Are you calling in your template a dictionary or \
obj not define in your configuration file?"
)
# could be a variable that as been wrong defined,
# for example if you delete the int() trasformation of col
# variable in filter.do_figure
# move back to original directory
os.chdir(_odir)
def istemplate(_src, extensions=[".tex"]):
"""Return True the file is a template
>>> istemplate('examples/main.tex')
True
>>> istemplate('examples/main.txt')
False
>>> istemplate('examples/main.txt', extensions=['.tex','.txt'])
True
"""
ext = os.path.splitext(_src)[-1]
# src is a template file
return os.path.isfile(_src) and (ext in extensions)
def copy(_src, _dst, link):
"""Make a copy or a link to a file
>>> copy('examples/copy.txt','examples/copy_no_link.txt', False)
>>> os.path.islink('examples/copy_no_link.txt')
False
>>> copy('examples/copy.txt','examples/copy_link.txt', True)
>>> os.path.islink('examples/copy_link.txt')
True
>>> os.remove('examples/copy_no_link.txt')
>>> os.remove('examples/copy_link.txt')
"""
if link: # make a link
os.symlink(_src, _dst)
else: # copy
shutil.copy(_src, _dst)
def get_filelist(_src):
"""Return a filelist given a path
>>> get_filelist('examples/') # doctest:+ELLIPSIS
['examples/style.tex', ..., 'examples/some.csv']
"""
slist = os.listdir(_src)
filelist = []
for _ in slist:
filelist.append(os.path.join(_src, _))
return filelist
def processrc(srclist, default, _dst="build", srcext=[".tex",], link=False):
"""Process a list of file, understand if is a source file
and using as a template for jinja, It work recursively in the directories
>>> opt = {'info': {'surname': 'Bonaparte', 'name': 'Napoleone'},
... 'tab' : {'add_hline': '0,1,-1', 'col_layout': '0:l'},
... 'euro': {'add_hline': '0,1,-1', 'col_layout': '0:l',
... 'numberformat' : '{0:.2f} euro'}, #.decode('utf-8')},
... 'dollar': {'add_hline': '0,1,-1', 'col_layout': '0:l',
... 'numberformat' : '{0:.2f} dollar'}, #.decode('utf-8')},
... }
>>> processrc(['examples/main.tex',], opt, srcext=['.tex',], link=True)
>>> os.listdir('build/')
['main.tex']
>>> processrc(['examples/',], opt, srcext=['.tex',], link=True)
>>> directory = os.listdir('build/examples/')
>>> directory.sort()
>>> directory # doctest:+ELLIPSIS
['copy.txt', ..., 'test.cfg']
>>> shutil.rmtree('build/')"""
if type(srclist) == list:
# start the cicle to render all files
for _src in srclist:
if _src[-1] == os.sep:
_src = _src[:-1]
# check if src is a file or a directory
if istemplate(_src, srcext):
# print 'is a template src: ', _src
makedir(_dst)
# src is a file
renderfile(_src, _dst, default)
elif os.path.isfile(_src):
# src is file but not a templpate _dst
# check, if not unix link=False
# print 'is a file: ', _src
if os.name != "posix":
link = False
srcdir, srcname = os.path.split(_src)
try:
makedir(_dst)
copy(_src, os.path.join(_dst, srcname), link)
except OSError:
# print "OSError: File exists ", os.path.join(_dst, srcname)
# print " the file will not be copied
# import pdb; pdb.set_trace()
pass
else:
# src is a directory
srcdir = os.path.split(_src)[1]
_dst = os.path.join(_dst, srcdir)
# import pdb; pdb.set_trace()
makedir(_dst)
# get a list of new sources
slist = get_filelist(_src)
# and then process
processrc(slist, default, _dst=_dst, srcext=srcext, link=link)
else:
raise TypeError("Srclist must be a list here! ;-)")
class Option:
"""Define an empty object"""
pass
def readgeneral(config):
"""Return an Option object with general information"""
general = Option()
items = list(dict(config.items("general")).keys())
if "imgext" in items:
general.imgext = config.get("general", "imgext")
else:
general.imgext = ".png, .pdf, .jpg"
if "srcext" in items:
general.srcext = config.get("general", "srcext")
else:
general.srcext = ".tex"
if "verbose" in items:
general.verbose = config.getboolean("general", "verbose")
else:
general.verbose = False
if "compile" in items:
general.compile = config.getboolean("general", "compile")
else:
general.compile = False
if "link" in items:
general.link = config.getboolean("general", "link")
else:
general.link = True
if "pdfcommand" in items:
general.pdfcommand = config.get("general", "pdfcommand")
else:
general.pdfcommand = "pdflatex build/main.tex"
if "dest" in items:
general.dest = config.get("general", "dest")
else:
general.dest = "build"
if "source" in items:
general.source = config.get("general", "source").replace(" ", "").split(",")
return general
def readcfg(cfg):
"""Return a dictionary with the information read from a
configuration file"""
config = configparser.ConfigParser()
config.readfp(open(cfg))
sections = config.sections()
# import pdb; pdb.set_trace()
opt = {}
opt["general"] = readgeneral(config)
sections.remove("general")
for sec in sections:
# build a dictionary
opt[sec] = dict(config.items(sec))
return opt
if __name__ == "__main__":
usage = "usage: %prog [options] SOURCE"
parser = OptionParser(usage)
parser.add_option(
"-d",
"--dest",
dest="dest",
default="build",
help="Default is put inside a 'build' directory",
metavar="DIRECTORY",
)
parser.add_option(
"-c",
"--cfg",
dest="cfg",
default="",
help="Define your configure file",
metavar="FILE",
)
parser.add_option(
"-s",
"--srcext",
dest="srcext",
default=".tex",
help="Source valid extensions like:'.tex, .txt' ",
metavar="STRING",
)
parser.add_option(
"-i",
"--imgext",
dest="imgext",
default=".png, .pdf, .jpg",
help="Images valid extensions like:'.png, .pdf, .jpg' ",
metavar="STRING",
)
parser.add_option(
"-x",
"--compile",
action="store_true",
dest="compile",
default=False,
metavar="BOOLEAN",
help="Compile generated latex to produce pdf",
)
parser.add_option(
"-p",
"--pdfcommand",
dest="pdfcommand",
default="pdflatex build/main.tex",
help="Define the comand that you want to use to compile\
LaTex files",
metavar="STRING",
)
parser.add_option(
"-l",
"--link",
action="store_true",
dest="link",
default=False,
metavar="BOOLEAN",
help="For not source files, make a link the build\
directory",
)
parser.add_option(
"-v",
"--verbose",
action="store_true",
dest="verbose",
default=False,
metavar="BOOLEAN",
help="Get more Info",
)
(options, args) = parser.parse_args()
optcompile = options.compile
# print '\n\n dyn: ', os.getcwd()
odir = os.path.abspath(os.path.curdir)
if options.cfg:
print((options.cfg))
opt = readcfg(options.cfg)
options = opt.pop("general")
else:
opt = {}
if args:
options.source = args
if options.source:
if options.dest == None:
# destination is not define, then make a "build" directory
# in the folder where we run the program
options.dest = os.path.join(os.getcwd(), "build")
# Start to process sources
processrc(
options.source,
opt,
_dst=options.dest,
srcext=options.srcext.replace(" ", "").split(","),
link=options.link,
)
else:
print("Give me a latex source! Use cfg file or cmd line")
if optcompile:
# change dir
odir = os.path.abspath(os.path.curdir)
print(("moving into: ", options.dest))
os.chdir(options.dest)
# run compile comand
print("Start to compile using: ")
print((options.pdfcommand))
print(("=" * 50))
os.system(options.pdfcommand)
os.chdir(odir)
# ===============================================================================