-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.py
292 lines (245 loc) · 8.24 KB
/
patch.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
from genericpath import isfile
from typing import Callable
import win32com.client
import datetime
import zipfile
import os
import re
def resolveTextFile(contents_dir: str, file_name: str) -> str:
while True:
path = os.path.join(contents_dir, file_name)
with open(path, 'r', encoding='utf-8') as f:
text = f.readline().rstrip('\r\n')
if not text.startswith('@'):
return path
contents_dir = os.path.relpath(text[1:])
def process_lines(path: str) -> list[list[str]]:
with open(path, 'r', encoding='utf-8') as f:
data_lines = f.readlines() + ['\n']
data = list[list[str]]()
prev_i = 0
for i, l in enumerate(data_lines):
if l != '\n':
continue
data.append([
l.rstrip('\r\n').lstrip('- ')
for l in data_lines[prev_i:i]
if not l.startswith('#')
])
prev_i = i + 1
return data
SKILL_TEMPLATE = """
<text:list-item>
<text:p text:style-name="tech-skill">{tech_skill}</text:p>
</text:list-item>
""".lstrip('\n')
def make_skills(contents_dir: str) -> list[str]:
data = process_lines(resolveTextFile(contents_dir, 'skills.txt'))
fill = '\n'.join(
SKILL_TEMPLATE.format_map({
'tech_skill': l,
})
for d in data for l in d
)
return fill.split('\n')
EDUCATION_TEMPLATE = """
<table:table-row table:style-name="sect-row">
<table:table-cell table:style-name="sect-cell">
<text:p text:style-name="edu-title">{educ_title}<text:span text:style-name="edu-sub"> – {educ_sub}</text:span>
</text:p>
<text:list
text:style-name="edu-list"
text:continue-numbering="true">
{educ_descs}
</text:list>
</table:table-cell>
<table:table-cell table:style-name="date-cell">
<text:p text:style-name="date">{educ_date}</text:p>
</table:table-cell>
</table:table-row>
""".lstrip('\n')
EDUCATION_TEMPLATE_DESC = """
<text:list-item>
<text:p text:style-name="edu-desc">{educ_desc}</text:p>
</text:list-item>
""".lstrip('\n')
def make_education(contents_dir: str) -> list[str]:
data = process_lines(resolveTextFile(contents_dir, 'education.txt'))
fill = '\n'.join(
EDUCATION_TEMPLATE.format_map({
'educ_date': d[0],
'educ_title': d[1],
'educ_sub': d[2],
'educ_descs': ''.join(
EDUCATION_TEMPLATE_DESC.format_map({
'educ_desc': desc
})
for desc in d[3:]
),
})
for d in data
)
return fill.split('\n')
PROJECT_TEMPLATE = """
<table:table-row table:style-name="sect-row">
<table:table-cell table:style-name="sect-cell">
<text:p text:style-name="proj-head">
<text:a xlink:href="{proj_link}">
<text:span text:style-name="proj-title">
{proj_title}</text:span>
</text:a>{proj_sub}</text:p>
<text:list
text:style-name="proj-list"
text:continue-numbering="true">
{proj_descs}
</text:list>
</table:table-cell>
<table:table-cell table:style-name="date-cell">
<text:p text:style-name="date">{proj_date}</text:p>
</table:table-cell>
</table:table-row>
""".lstrip('\n')
PROJECT_TEMPLATE_DESC = """
<text:list-item>
<text:p text:style-name="proj-desc">{proj_desc}</text:p>
</text:list-item>
""".lstrip('\n')
def make_projects(contents_dir: str) -> list[str]:
data = process_lines(resolveTextFile(contents_dir, 'projects.txt'))
fill = '\n'.join(
PROJECT_TEMPLATE.format_map({
'proj_sub': d[3],
'proj_link': d[2],
'proj_title': d[1],
'proj_date': d[0],
'proj_descs': ''.join(
PROJECT_TEMPLATE_DESC.format_map({
'proj_desc': desc
})
for desc in d[4:]
),
})
for d in data
)
return fill.split('\n')
WORK_TEMPLATE = """
<table:table-row table:style-name="sect-row">
<table:table-cell table:style-name="sect-cell">
<text:p text:style-name="work-title">
{work_title}<text:span text:style-name="work-sub"> {work_sub}</text:span>
</text:p>
{work_descs}
</table:table-cell>
<table:table-cell table:style-name="date-cell">
<text:p text:style-name="date">{work_date}</text:p>
</table:table-cell>
</table:table-row>
""".lstrip('\n')
WORK_TEMPLATE_DESC = """
<text:p text:style-name="work-desc">
{work_desc}
</text:p>
""".lstrip('\n')
def make_work_exp(contents_dir: str) -> list[str]:
data = process_lines(resolveTextFile(contents_dir, 'work.txt'))
project_fill = '\n'.join(
WORK_TEMPLATE.format_map({
'work_date': d[0],
'work_title': d[1],
'work_sub': d[2],
'work_descs': ''.join(
WORK_TEMPLATE_DESC.format_map({
'work_desc': desc
})
for desc in d[3:]
),
})
for d in data
)
return project_fill.split('\n')
def update_date(contents_dir: str) -> list[str]:
date = datetime.datetime.fromtimestamp(
max(
os.path.getmtime(path)
for fn in os.listdir(contents_dir)
if (path := os.path.join(contents_dir, fn))
and path.endswith('.txt')
),
tz=datetime.UTC,
)
date_str = date.strftime('%Y-%m-%d %HZ')
return [
f'<text:p text:style-name="foot-date">{date_str}</text:p>',
]
def xml_mod(contents_dir: str, source_dir: str, file_name: str, methods: dict[str, Callable]):
xml_path = f'{source_dir}/{file_name}'
with open(xml_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
ranges = {}
for i, l in enumerate(lines):
# Captures XML comments.
m = re.match(r'^(\t*)<!-- (.+) (.+) -->\s*$', l)
if not m:
continue
typ = m.group(2)
mode = m.group(3)
if mode == 'START':
ranges[typ] = {
'start': i + 1,
'end': i + 1,
'tab_prefix': m.group(1),
}
elif mode == 'END':
ranges[typ]['end'] = i
# Replaces text between the delimiting lines with what we return from calls to functions within METHODS.
for (i, d) in reversed(ranges.items()):
lines[d['start']:d['end']] = [
f"{d['tab_prefix']}{l}\n"
for l in methods[i](contents_dir)
]
with open(xml_path, 'w', encoding='utf-8') as f:
f.writelines(lines)
def dir_mod(contents_dir: str, source_dir: str):
xml_mod(contents_dir, source_dir, 'content.xml', {
'SKILLS': make_skills,
'EDUCATION': make_education,
'PROJECTS': make_projects,
'WORK': make_work_exp,
})
xml_mod(contents_dir, source_dir, 'styles.xml', {
'UPDATE-DATE': update_date,
})
def dir2odt(contents_dir: str, odt_path: str):
with zipfile.ZipFile(odt_path, 'w') as archive:
for f in os.listdir(contents_dir):
archive.write(f'{contents_dir}/{f}', f)
def odt2pdf(odt_path: str, pdf_path: str):
word = win32com.client.Dispatch('Word.Application')
doc = word.Documents.Open(os.path.abspath(odt_path))
# 17 is an alias for PDF.
doc.SaveAs(os.path.abspath(pdf_path), 17)
doc.Close()
word.Quit()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
'contents_dirs',
default=[
f.name
for f in os.scandir()
if f.is_dir(follow_symlinks=False)
and f.name != 'src'
and not f.name.startswith('.')
],
nargs='*',
type=str,
)
args = parser.parse_args()
contents_dirs = args.contents_dirs
source_dir = f'{os.path.abspath(os.path.dirname(__file__))}/src'
for contents_dir in contents_dirs:
dir_mod(contents_dir, source_dir)
dir2odt(source_dir, f'{contents_dir}/cv.odt')
odt2pdf(f'{contents_dir}/cv.odt', f'{contents_dir}/cv.pdf')
print(f'Finished "{contents_dir}"')