-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_data.py
255 lines (215 loc) · 8.07 KB
/
generate_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
# python3.8
# Generate all tex file for the resume, including info, education,
# publications, and experiences.
import os
import argparse
import yaml
from easydict import EasyDict
BASE_DIR = 'raw_data/'
INFO_PATH = 'raw_data/info.yml'
EDU_PATH = 'raw_data/education.yml'
PUB_PATH = 'raw_data/publication.md'
EXP_PATH = 'raw_data/experience.yml'
ACT_PATH = 'raw_data/activity.yml'
HONOR_PATH = 'raw_data/honor.yml'
SKILL_PATH = 'raw_data/skill.yml'
STYLE_DIR = 'styles'
_ALLOWED_LANGUAGES = ['chinese', 'english']
def parse_args():
"""Parses arguments."""
parser = argparse.ArgumentParser(description='Prepare necessary files.')
parser.add_argument('--language', type=str, default='english',
help='Choose the language ([chinese | english]).')
parser.add_argument('--style_file', type=str, default='xia.sty',
help='Filename of the target sty file.')
return parser.parse_args()
def process_idx(idx):
"""Process the index."""
if idx % 10 == 1 and idx != 11:
return f'{idx}st'
elif idx % 10 == 2 and idx != 12:
return f'{idx}nd'
elif idx % 10 == 3 and idx != 13:
return f'{idx}rd'
return f'{idx}th'
def generate_style(args):
"""Generate tex file for style."""
if not args.style_file.endswith('.sty'):
raise ValueError(f'Style file must ends with `.sty`, however, '
f'{args.style_file} received. Please use filename '
f'under `styles` folder.')
style_file_path = os.path.join(STYLE_DIR, args.style_file)
if not os.path.isfile(style_file_path):
raise FileNotFoundError(f'Style file {style_file_path} not found! '
f'Please use ONLY filename under `styles` '
f'folder.')
style = style_file_path.replace('.sty', '')
msg = f'\\usepackage{{{style}}}\n'
style_path = os.path.join('data', 'style.tex')
with open(style_path, 'w') as f:
f.write(msg)
def generate_info(args):
"""Generate tex file for info."""
language = args.language
if language not in _ALLOWED_LANGUAGES:
raise ValueError(f'Illegal language {language}, supported languages '
f'is {_ALLOWED_LANGUAGES}.')
with open(INFO_PATH, 'r') as f:
config = EasyDict(yaml.load(f.read(), Loader=yaml.FullLoader))
info_config = config.info
msg = '\\xiasetup{%\n'
for key, val in info_config.items():
msg += f' {key} = {{{val}}},\n'
msg += f' language = {{{language}}},\n'
msg += '}\n'
info_path = os.path.join('data', 'xiasetup.tex')
with open(info_path, 'w') as f:
f.write(msg)
def generate_education():
"""Generate tex file for education."""
with open(EDU_PATH, 'r') as f:
config = EasyDict(yaml.load(f.read(), Loader=yaml.FullLoader))
edu_config = config.education
msg = '\\begin{education}\n\n\n'
for raw_idx, raw_edu in enumerate(edu_config):
idx = raw_idx + 1
msg += f'% {process_idx(idx)} education.\n'
msg += '\\xiasetup{%\n'
edu = raw_edu[f'edu{idx}']
for key, val in edu.items():
msg += f' {key} = {{{val}}},\n'
msg += '}\n'
msg += '\\rendereducation\n\n\n'
msg += '\\end{education}\n'
edu_path = os.path.join('data', 'education.tex')
with open(edu_path, 'w') as f:
f.write(msg)
def parse_md(md_path):
"""Parse the markdown file."""
pub_yml_list = list()
process = lambda s: s[s.index('(') + 1: -1]
with open(md_path, 'r') as f:
raw_md = f.read().split('\n')
for raw_pub_yml in raw_md:
if not raw_pub_yml: # Skip the empty line.
continue
if raw_pub_yml.startswith('#'): # Skip the first line.
continue
if raw_pub_yml.startswith('<!--'): # Skip the commented line.
continue
try:
pub_yml = process(raw_pub_yml)
pub_yml_list.append(pub_yml)
except:
print(raw_pub_yml)
return pub_yml_list
def generate_publication():
"""Generate tex file for publication."""
pub_yml_list = parse_md(PUB_PATH)
msg = '\\begin{publication}\n\n\n'
for raw_idx, pub_yml in enumerate(pub_yml_list):
yml_path = os.path.join(BASE_DIR, pub_yml)
with open(yml_path, 'r') as f:
pub_config = EasyDict(yaml.load(f.read(), Loader=yaml.FullLoader))
pub = list(pub_config.values())[0]
idx = raw_idx + 1
msg += f'% {process_idx(idx)} publication.\n'
msg += '\\xiasetup{%\n'
for key, val in pub.items():
msg += f' {key} = {{{val}}},\n'
msg += '}\n'
msg += '\\renderpublication\n\n\n'
msg += '\\end{publication}\n'
pub_path = os.path.join('data', 'publication.tex')
with open(pub_path, 'w') as f:
f.write(msg)
def generate_experience():
"""Generate tex file for experience."""
with open(EXP_PATH, 'r') as f:
config = EasyDict(yaml.load(f.read(), Loader=yaml.FullLoader))
exp_config = config.experience
msg = '\\begin{experience}\n\n\n'
for raw_idx, raw_exp in enumerate(exp_config):
idx = raw_idx + 1
msg += f'% {process_idx(idx)} experience.\n'
msg += '\\xiasetup{%\n'
exp = raw_exp[f'exp{idx}']
for key, val in exp.items():
msg += f' {key} = {{{val}}},\n'
msg += '}\n'
msg += '\\renderexperience\n\n\n'
msg += '\\end{experience}\n'
exp_path = os.path.join('data', 'experience.tex')
with open(exp_path, 'w') as f:
f.write(msg)
def generate_activity():
"""Generate tex file for activity."""
with open(ACT_PATH, 'r') as f:
config = EasyDict(yaml.load(f.read(), Loader=yaml.FullLoader))
act_config = config.activity
msg = '\\begin{activity}\n\n\n'
for raw_idx, raw_act in enumerate(act_config):
idx = raw_idx + 1
msg += f'% {process_idx(idx)} activity.\n'
msg += '\\xiasetup{%\n'
act = raw_act[f'act{idx}']
for key, val in act.items():
msg += f' {key} = {{{val}}},\n'
msg += '}\n'
msg += '\\renderactivity\n\n\n'
msg += '\\end{activity}\n'
act_path = os.path.join('data', 'activity.tex')
with open(act_path, 'w') as f:
f.write(msg)
def generate_honor():
"""Generate tex file for honor."""
with open(HONOR_PATH, 'r') as f:
config = EasyDict(yaml.load(f.read(), Loader=yaml.FullLoader))
honor_config = config.honor
msg = '\\begin{honor}\n\n\n'
for raw_idx, raw_honor in enumerate(honor_config):
idx = raw_idx + 1
msg += f'% {process_idx(idx)} honor.\n'
msg += '\\xiasetup{%\n'
honor = raw_honor[f'honor{idx}']
for key, val in honor.items():
msg += f' {key} = {{{val}}},\n'
msg += '}\n'
msg += '\\renderhonor\n\n\n'
msg += '\\end{honor}\n'
honor_path = os.path.join('data', 'honor.tex')
with open(honor_path, 'w') as f:
f.write(msg)
def generate_skill():
"""Generate tex file for skill."""
with open(SKILL_PATH, 'r') as f:
config = EasyDict(yaml.load(f.read(), Loader=yaml.FullLoader))
skill_config = config.skill
msg = '\\begin{skill}\n\n\n'
for raw_idx, raw_skill in enumerate(skill_config):
idx = raw_idx + 1
msg += f'% {process_idx(idx)} skill.\n'
msg += '\\xiasetup{%\n'
skill = raw_skill[f'skill{idx}']
for key, val in skill.items():
msg += f' {key} = {{{val}}},\n'
msg += '}\n'
left_flag = 'right' if raw_idx % 2 else 'left'
msg += f'\\renderskill{{{left_flag}}}\n\n\n'
msg += '\\end{skill}\n'
skill_path = os.path.join('data', 'skill.tex')
with open(skill_path, 'w') as f:
f.write(msg)
def main():
"""Main function."""
args = parse_args()
generate_style(args)
generate_info(args)
generate_education()
generate_publication()
generate_experience()
generate_activity()
generate_honor()
generate_skill()
if __name__ == '__main__':
main()