-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlabgen-module-prepare-doc
executable file
·130 lines (96 loc) · 4.44 KB
/
mlabgen-module-prepare-doc
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import mlabgen
import sys
import os
import shutil
import subprocess
import jinja2
from jinja2 import Template
import csv, json
import re
import kicad_netlist_reader
LATEX_SUBS = (
(re.compile(r'\\'), r'\\textbackslash'),
(re.compile(r'([{}_#%&$])'), r'\\\1'),
(re.compile(r'~'), r'\~{}'),
(re.compile(r'\^'), r'\^{}'),
(re.compile(r'"'), r"''"),
(re.compile(r'\.\.\.+'), r'\\ldots'),
(re.compile(r'°'), r'$^\\circ$')
)
def escape_tex(value):
for pattern, replacement in LATEX_SUBS:
value = pattern.sub(replacement, value)
return value
latex_jinja_env = jinja2.Environment(
block_start_string = '\BLOCK{',
block_end_string = '}',
variable_start_string = '\VAR{',
variable_end_string = '}',
comment_start_string = '\#{',
comment_end_string = '}',
line_statement_prefix = '%%',
line_comment_prefix = '%#',
trim_blocks = True,
autoescape = True,
loader = jinja2.FileSystemLoader(os.path.abspath('/'))
)
latex_jinja_env.filters['escape_tex'] = escape_tex
class PrepareDoc():
def __init__(self):
self.newDoc()
def bom_list(self, name):
net = kicad_netlist_reader.netlist('/home/roman/repos/Modules/clock/RTC03A/hw/sch_pcb/RTC03A.net')
components = net.getInterestingComponents()
print(components)
def newDoc(self):
options = mlabgen.getModuleInfo()
print(options)
options['bom'] = self.bom_list(options['name'])
try:
with open('./hw/bom_base.csv') as csvfile:
bom = csv.reader(csvfile, delimiter=';')
for x in bom:
options['bom'] += [[', '.join(eval(x[3]))]+ x + [len(eval(x[3]))]]
except Exception as e:
print("Chybí BOM soubor")
sys.exit(-1)
print (options['bom'])
#if not os.path.exists('doc/img/%s_QRcode.png'%options['name']): # create the build directory if not existing
mlabgen.QRpermalink(options['name'])
if not os.path.exists('doc/src/'): # create the build directory if not existing
os.makedirs('doc/src')
os.chdir('doc/src')
shutil.copyfile('/home/roman/repos/mlabgen/templates/MLABdoc.sty', 'MLABdoc.sty')
for lang in ['cs', 'en']:
if not os.path.exists("{}.{}.tex".format(options['name'], lang)):
template = latex_jinja_env.get_template(os.path.realpath('/home/roman/repos/mlabgen/templates/module.{}.tex'.format(lang)))
renderer_template = template.render(**options)
with open("{}.{}.tex".format(options['name'], lang), "w") as f: # saves tex_code to outpout file
f.write(renderer_template)
template = latex_jinja_env.get_template(os.path.realpath('/home/roman/repos/mlabgen/templates/module.{}.assembly.tex'.format(lang) ))
renderer_template = template.render(**options)
with open("{}.{}.assembly.tex".format(options['name'], lang), "w") as f: # saves tex_code to outpout file
f.write(renderer_template)
template = latex_jinja_env.get_template(os.path.realpath('/home/roman/repos/mlabgen/templates/module.{}.title.tex'.format(lang) ))
renderer_template = template.render(**options)
with open("{}.{}.title.tex".format(options['name'], lang), "w") as f: # saves tex_code to outpout file
f.write(renderer_template)
template = latex_jinja_env.get_template(os.path.realpath('/home/roman/repos/mlabgen/templates/README.md'))
renderer_template = template.render(**options)
with open('../../README.md', "w") as f: # saves tex_code to outpout file
f.write(renderer_template)
#template = latex_jinja_env.get_template(os.path.realpath('/home/roman/repos/mlabgen/templates/README.cs.md'))
#renderer_template = template.render(**options)
#with open('../../README.cs.md', "w") as f: # saves tex_code to outpout file
# f.write(renderer_template)
proc = subprocess.Popen(["pdflatex", "{}.{}.tex".format(options['name'], lang) , "; cd ../.."])
proc.wait()
shutil.copyfile("{}.{}.pdf".format(options['name'], lang), "../{}.{}.pdf".format(options['name'], lang))
os.chdir('../..')
print(options['bom'])
def main():
pd = PrepareDoc()
if __name__ == '__main__':
main()