forked from FOSSRIT/people
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_profiles.py
executable file
·58 lines (43 loc) · 1.74 KB
/
generate_profiles.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
#!/bin/env python3
from __future__ import print_function
from __future__ import absolute_import
import argparse
import hashlib
from jinja2 import Environment, FileSystemLoader
import os
import yaml
def get_template(name):
env = Environment(loader=FileSystemLoader('.'))
def testbio(bio):
assert len(bio) <= 140, "Biography is too long"
return bio
env.filters['md5sum'] = lambda value: hashlib.md5(value.encode('utf-8')).hexdigest()
env.filters['testbio'] = testbio
return env.get_template(name)
def rootedlistdir(pth):
return [os.path.join(pth, name) for name in os.listdir(pth)]
def loadyaml(name):
with open(name) as inf:
return yaml.load(inf, Loader=yaml.FullLoader)
def parse_arguments():
ROLES = ["faculty", "student", "alum", "mentor"]
parser = argparse.ArgumentParser()
parser.add_argument('--template', '-t', default='template.html',
help='Jinja template to use')
parser.add_argument('--roles', '-r', nargs='+',
default=ROLES, help='Ordered list of roles to use')
parser.add_argument('--output', '-o', default='-',
type=argparse.FileType('w'),
help='Output filename')
parser.add_argument('directory', help='Directory to read through')
return parser.parse_args()
def main():
args = parse_arguments()
template = get_template(args.template)
rendered = (template.render(person=loadyaml(name), role=role)
for role in args.roles
for name in rootedlistdir(os.path.join(args.directory, role))
if os.path.splitext(name)[-1].lower() == ".yaml")
print("\n".join(rendered), file=args.output)
if __name__ == '__main__':
main()