expand_jinja.expand_jinja()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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