-
Notifications
You must be signed in to change notification settings - Fork 1
/
glader_templates.py
71 lines (60 loc) · 2.15 KB
/
glader_templates.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
#!/usr/bin/env python3
""" Glader - Templates
Helpers for retrieving Glader template files/content.
-Christopher Welborn 03-14-20
"""
import os.path
import sys
SCRIPT = os.path.split(os.path.abspath(sys.argv[0]))[1]
SCRIPTDIR = os.path.abspath(sys.path[0])
TEMPLATEDIR = os.path.join(SCRIPTDIR, 'templates')
# A dict of {name: full_path}, like: {'body': './templates/body.py'}
TEMPLATE_FILES = {
os.path.splitext(s)[0]: os.path.join(TEMPLATEDIR, s)
for s in os.listdir(TEMPLATEDIR)
if s.endswith('.py')
}
def fatal_err(*args, **kwargs):
""" Print a message to stderr and exit(1). """
print_err(*args, **kwargs)
sys.exit(1)
def get_template(name, indent=0):
""" Retrieve template content by name ('body', 'cls', ...) """
filepath = TEMPLATE_FILES.get(name, None)
if filepath is None:
print_err(f'\nUnknown template name: {name!r}')
sys.exit(1)
try:
with open(filepath, 'r') as f:
content = ''.join(parse_template(f, indent=indent))
except FileNotFoundError:
fatal_err(f'File was deleted before it was read!: {filepath}')
except EnvironmentError as ex:
fatal_err(f'Unable to read template file: {filepath}\n{ex}')
return content
def parse_template(lines, indent=0):
""" Parse an open file object, or an iterable of lines to make a "usable"
templates.
Yields usable lines.
"""
spaces = ' ' * indent
yielded = 0
for line in lines:
stripped = line.rstrip()
if stripped.lower().endswith('# ignore'):
# Ignore this line.
continue
if not (yielded or stripped):
# Only blank lines so far (from ignoring lines).
continue
yield f'{spaces}{line}'
yielded += 1
def print_err(*args, **kwargs):
kwargs['file'] = kwargs.get('file', sys.stderr)
print(*args, **kwargs)
if not os.path.exists(TEMPLATEDIR):
fatal_err(f'\nMissing ./templates directory for Glader in: {SCRIPTDIR}')
# Ensure all template files are present.
for name, path in TEMPLATE_FILES.items():
if not os.path.exists(path):
fatal_err(f'Missing template file for {name!r}: {path}')