| 1 |
# mako/cmd.py |
| 2 |
# Copyright 2006-2022 the Mako authors and contributors <see AUTHORS file> |
| 3 |
# |
| 4 |
# This module is part of Mako and is released under |
| 5 |
# the MIT License: http://www.opensource.org/licenses/mit-license.php |
| 6 |
from argparse import ArgumentParser |
| 7 |
from os.path import dirname |
| 8 |
from os.path import isfile |
| 9 |
import sys |
| 10 |
|
| 11 |
from mako import exceptions |
| 12 |
from mako.lookup import TemplateLookup |
| 13 |
from mako.template import Template |
| 14 |
|
| 15 |
|
| 16 |
def varsplit(var): |
| 17 |
if "=" not in var: |
| 18 |
return (var, "") |
| 19 |
return var.split("=", 1) |
| 20 |
|
| 21 |
|
| 22 |
def _exit(): |
| 23 |
sys.stderr.write(exceptions.text_error_template().render()) |
| 24 |
sys.exit(1) |
| 25 |
|
| 26 |
|
| 27 |
def cmdline(argv=None): |
| 28 |
|
| 29 |
parser = ArgumentParser() |
| 30 |
parser.add_argument( |
| 31 |
"--var", |
| 32 |
default=[], |
| 33 |
action="append", |
| 34 |
help="variable (can be used multiple times, use name=value)", |
| 35 |
) |
| 36 |
parser.add_argument( |
| 37 |
"--template-dir", |
| 38 |
default=[], |
| 39 |
action="append", |
| 40 |
help="Directory to use for template lookup (multiple " |
| 41 |
"directories may be provided). If not given then if the " |
| 42 |
"template is read from stdin, the value defaults to be " |
| 43 |
"the current directory, otherwise it defaults to be the " |
| 44 |
"parent directory of the file provided.", |
| 45 |
) |
| 46 |
parser.add_argument( |
| 47 |
"--output-encoding", default=None, help="force output encoding" |
| 48 |
) |
| 49 |
parser.add_argument( |
| 50 |
"--output-file", |
| 51 |
default=None, |
| 52 |
help="Write to file upon successful render instead of stdout", |
| 53 |
) |
| 54 |
parser.add_argument("input", nargs="?", default="-") |
| 55 |
|
| 56 |
options = parser.parse_args(argv) |
| 57 |
|
| 58 |
output_encoding = options.output_encoding |
| 59 |
output_file = options.output_file |
| 60 |
|
| 61 |
if options.input == "-": |
| 62 |
lookup_dirs = options.template_dir or ["."] |
| 63 |
lookup = TemplateLookup(lookup_dirs) |
| 64 |
try: |
| 65 |
template = Template( |
| 66 |
sys.stdin.read(), |
| 67 |
lookup=lookup, |
| 68 |
output_encoding=output_encoding, |
| 69 |
) |
| 70 |
except: |
| 71 |
_exit() |
| 72 |
else: |
| 73 |
filename = options.input |
| 74 |
if not isfile(filename): |
| 75 |
raise SystemExit("error: can't find %s" % filename) |
| 76 |
lookup_dirs = options.template_dir or [dirname(filename)] |
| 77 |
lookup = TemplateLookup(lookup_dirs) |
| 78 |
try: |
| 79 |
template = Template( |
| 80 |
filename=filename, |
| 81 |
lookup=lookup, |
| 82 |
output_encoding=output_encoding, |
| 83 |
) |
| 84 |
except: |
| 85 |
_exit() |
| 86 |
|
| 87 |
kw = dict(varsplit(var) for var in options.var) |
| 88 |
try: |
| 89 |
rendered = template.render(**kw) |
| 90 |
except: |
| 91 |
_exit() |
| 92 |
else: |
| 93 |
if output_file: |
| 94 |
open(output_file, "wt", encoding=output_encoding).write(rendered) |
| 95 |
else: |
| 96 |
sys.stdout.write(rendered) |
| 97 |
|
| 98 |
|
| 99 |
if __name__ == "__main__": |
| 100 |
cmdline() |