-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.py
85 lines (70 loc) · 3.13 KB
/
render.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
import git
import os
import shutil
import tempfile
import argparse
from mistletoe import Document
from mistletoe_extension.blazor_renderer import MudBlazorCVRenderer
from mistletoe_extension.moderncv_renderer import ModernCVRenderer
from mistletoe_extension.json_renderer import JsonCVRenderer
# Currently this renders the given file to all possible outputs, I should add a second arg for the output type.
def onerror(func, path, exc_info):
"""
Error handler for ``shutil.rmtree``.
If the error is due to an access error (read only file)
it attempts to add write permission and then retries.
If the error is for another reason it re-raises the error.
Usage : ``shutil.rmtree(path, onerror=onerror)``
"""
import stat
# Is the error an access error?
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
def get_file_from_git(gitDir, filePath):
if not os.path.exists('./temp'):
os.mkdir('./temp')
# Create temporary dir
t = tempfile.mkdtemp()
# Clone into temporary dir
git.Repo.clone_from(gitDir, t, branch='main', depth=1)
# Copy desired file from temporary dir
shutil.move(os.path.join(t, filePath), os.path.join('./temp', os.path.basename(filePath)))
# Remove temporary dir
shutil.rmtree(t, onerror=onerror)
def render_file(filePath, outFileName):
rendered = None
if not os.path.exists('./output'):
os.mkdir('./output')
with open(filePath, 'r') as fin:
with ModernCVRenderer() as renderer:
rendered = renderer.render(Document(fin))
with open(os.path.join('./output', outFileName + '.tex'), 'w') as fout:
fout.write(rendered)
rendered_html = None
with open(filePath, 'r') as fin:
with MudBlazorCVRenderer() as renderer:
rendered_html = renderer.render(Document(fin))
with open(os.path.join('./output', outFileName + '.razor'), 'w') as fout:
fout.write(rendered_html)
rendered_json = None
with open(filePath, 'r') as fin:
with JsonCVRenderer() as renderer:
rendered_json = renderer.render(Document(fin))
with open(os.path.join('./output', outFileName + '.json'), 'w') as fout:
fout.write(rendered_json)
def get_file_and_render(input_file, git_dir='', output_file='output'):
if git_dir != '':
get_file_from_git(git_dir, input_file)
input_file = os.path.join('./temp', os.path.basename(input_file))
render_file(input_file, output_file)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Renders the given md file as a modernCV tex file and a blazor file.')
parser.add_argument('-i','--input-file', help='Input md file path', required=True)
parser.add_argument('-g','--git-dir', help='Git directory to pull file from', required=False)
parser.add_argument('-o','--output-file', help='Name of output file', required=False)
args = vars(parser.parse_args())
not_none_args = {k:v for k, v in args.items() if v is not None}
get_file_and_render(**not_none_args)