-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathgenerate.py
executable file
·53 lines (40 loc) · 1.58 KB
/
generate.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
#!/usr/bin/env python
# encoding: utf-8
import os
import logging
logger = logging.getLogger(__name__)
from jinja2 import Environment, FileSystemLoader
def get_args():
from argparse import ArgumentParser
parser = ArgumentParser(description="TODO: description of the utility")
parser.add_argument("-v", "--verbose", action="count", help="the logging verbosity (more gives more detail)")
parser.add_argument("-t", "--template_dir", default="templates", help="Location of the template directory (default: %(default)s)")
parser.add_argument("-o", "--output_dir", default="html", help="Location of the output directory (default: %(default)s)")
args = parser.parse_args()
if args.verbose == 1:
level = logging.DEBUG
else:
level = logging.INFO
logging.basicConfig(format="%(levelname)s %(asctime)s: %(message)s")
logger.setLevel(level)
return args
def main():
args = get_args()
env = Environment(loader=FileSystemLoader(args.template_dir))
try:
os.makedirs(args.output_dir)
except FileExistsError:
pass
for dirpath, dirnames, filenames in os.walk(args.template_dir):
for filename in filenames:
if not filename.endswith(".html"):
continue
input_path = filename
template = env.get_template(input_path)
result = template.render()
path = os.path.join(args.output_dir, input_path)
with open(path, "w") as of:
of.write(result)
break # just want the top level dir
if __name__ == '__main__':
main()