expand_jinja   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 56
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0
wmc 5

2 Functions

Rating   Name   Duplication   Size   Complexity  
A _get_output_filepath() 0 19 4
A expand_jinja() 0 2 1
1
from __future__ import print_function
2
3
import os
4
import sys
5
import argparse
6
7
import ssg.jinja
8
9
10
SUBS_DICT = ssg.jinja.load_macros()
11
12
13
def expand_jinja(filepath):
14
    return ssg.jinja.process_file(filepath, SUBS_DICT)
15
16
17
def _get_output_filepath(outdir, filepath):
18
    out_filepath = None
19
20
    if filepath.endswith(".jinja"):
21
        out_filepath = filepath.split(".jinja")[0]
22
        filepath = out_filepath
23
24
    if outdir:
25
        filename = os.path.basename(filepath)
26
        out_filepath = os.path.join(outdir, filename)
27
28
    if not out_filepath:
29
        raise RuntimeError(
30
            "Don't know where to save expansion of '{0}', as "
31
            "the output directory has not been supplied, and "
32
            "the file doesn't end with '.jinja'."
33
            .format(filepath)
34
        )
35
    return out_filepath
36
37
38
if __name__ == "__main__":
39
    parser = argparse.ArgumentParser()
40
    parser.add_argument("filename", nargs="+")
41
    parser.add_argument("--outdir")
42
43
    args = parser.parse_args()
44
45
    for filepath in args.filename:
46
        try:
47
            out_filepath = _get_output_filepath(args.outdir, filepath)
48
        except RuntimeError as exc:
49
            print(str(exc), file=sys.stderr)
50
            sys.exit(1)
51
52
        expanded_contents = expand_jinja(filepath)
53
        with open(out_filepath, "w") as f:
54
            f.write(expanded_contents)
55
        print(out_filepath)
56