diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0fbaae23ed5fb2e2924e72981e2615e526ab2f0a..ca51e18665ffb1b19793a60c779086e700052561 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,8 +1,14 @@ -- repo: git://github.com/pre-commit/pre-commit-hooks - sha: v0.8.0 +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + sha: v1.2.3 hooks: - id: check-added-large-files - id: check-yaml - id: check-merge-conflict - id: flake8 - id: check-symlinks +- repo: https://github.com/ambv/black + sha: 18.5b0 + hooks: + - id: black + args: [--line-length=79] diff --git a/setup.py b/setup.py index 3e24a6475aabf4436840ec21f7670078de8ae66e..1532a73c5aec8d86d4a95565733113a39df67255 100644 --- a/setup.py +++ b/setup.py @@ -2,29 +2,26 @@ from setuptools import setup, find_packages setup( - name='template', - version=open('VERSION', 'r').read(), - description='''A CLI tool for generating files from Jinja2 templates and - environment variables.''', - long_description=open('README.rst', 'r').read(), - url='https://www.shore.co.il/git/template', - author='Nimrod Adar', - author_email='nimrod@shore.co.il', - license='MIT', + name="template", + version=open("VERSION", "r").read(), + description="""A CLI tool for generating files from Jinja2 templates and + environment variables.""", + long_description=open("README.rst", "r").read(), + url="https://www.shore.co.il/git/template", + author="Nimrod Adar", + author_email="nimrod@shore.co.il", + license="MIT", classifiers=[ - 'Development Status :: 4 - Beta', - 'Intended Audience :: Developers', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 2', - 'Intended Audience :: System Administrators', - 'Topic :: Utilities', + "Development Status :: 4 - Beta", + "Intended Audience :: Developers", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 2", + "Intended Audience :: System Administrators", + "Topic :: Utilities", ], - keywords='config configuration jinja template environment', + keywords="config configuration jinja template environment", packages=find_packages(), - install_requires=['Jinja2', 'PyYAML', 'jmespath', 'toml'], - extras_require={ - 'dev': ['tox'], }, - entry_points={ - 'console_scripts': [ - 'template=template:main'], }, + install_requires=["Jinja2", "PyYAML", "jmespath", "toml"], + extras_require={"dev": ["tox"]}, + entry_points={"console_scripts": ["template=template:main"]}, ) diff --git a/template/__init__.py b/template/__init__.py index ccd38c9999b9f855794ca1915babed38aa3101d0..95394146a4f9d0d8c53066f4acaff22c1bee0fd3 100755 --- a/template/__init__.py +++ b/template/__init__.py @@ -1,8 +1,12 @@ #!/usr/bin/env python -'''Generate files from Jinja2 templates and environment variables.''' +"""Generate files from Jinja2 templates and environment variables.""" -from __future__ import (absolute_import, division, - print_function, unicode_literals) +from __future__ import ( + absolute_import, + division, + print_function, + unicode_literals, +) from jinja2 import Environment from os import environ from sys import stdin, stdout @@ -12,9 +16,10 @@ import template.filters def render(template_string): + """Render the template.""" env = Environment(autoescape=True) # Add all functions in template.filters as Jinja filters. - for tf in filter(lambda x: not x.startswith('_'), dir(template.filters)): + for tf in filter(lambda x: not x.startswith("_"), dir(template.filters)): env.filters[tf] = template.filters.__getattribute__(tf) t = env.from_string(template_string) return t.render(environ) @@ -22,20 +27,26 @@ def render(template_string): def main(): parser = ArgumentParser( - description='''A CLI tool for generating files from Jinja2 templates - and environment variables.''') - parser.add_argument('filename', - help='Input filename', - type=argparse.FileType('r'), - nargs='?') - parser.add_argument('-o', '--output', - help='Output to filename', - type=argparse.FileType('w')) + description="""A CLI tool for generating files from Jinja2 templates + and environment variables.""" + ) + parser.add_argument( + "filename", + help="Input filename", + type=argparse.FileType("r"), + nargs="?", + ) + parser.add_argument( + "-o", + "--output", + help="Output to filename", + type=argparse.FileType("w"), + ) args = parser.parse_args() infd = args.filename if args.filename else stdin outfd = args.output if args.output else stdout print(render(infd.read()), file=outfd) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/template/filters.py b/template/filters.py index ab57b9b0522baad90356f2306ca8ec4825ad7a5f..7ad2b6bb6b9560a1741bec72b9bd39a687a46931 100644 --- a/template/filters.py +++ b/template/filters.py @@ -1,10 +1,14 @@ #!/usr/bin/env python -from __future__ import (absolute_import, division, - print_function, unicode_literals) +from __future__ import ( + absolute_import, + division, + print_function, + unicode_literals, +) def to_yaml(value): - ''' + """ Converts given data structure to YAML form. Examples: @@ -16,13 +20,14 @@ def to_yaml(value): '1:\\n a: [1, 2, 3]\\n' >>> to_yaml("abc") 'abc\\n...\\n' - ''' + """ from yaml import safe_dump + return safe_dump(value) def to_json(value): - ''' + """ Converts given data structure to JSON form. Examples: @@ -34,13 +39,14 @@ def to_json(value): '2' >>> to_json({1: {'a': [1,2,3]}}) '{"1": {"a": [1, 2, 3]}}' - ''' + """ from json import dumps + return dumps(value) def from_json(value): - ''' + """ Returns native data structure from the given JSON string. Examples: @@ -51,13 +57,14 @@ def from_json(value): True >>> from_json('{"1": {"a": [1, 2, 3]}}') == {'1': {'a': [1, 2, 3]}} True - ''' + """ from json import loads + return loads(value) def from_yaml(value): - ''' + """ Returns native data structure from the given YAML string. Examples: @@ -67,49 +74,52 @@ def from_yaml(value): [1, 2, 3] >>> from_yaml('{"1": {"a": [1, 2, 3]}}') {'1': {'a': [1, 2, 3]}} - ''' + """ from yaml import safe_load + return safe_load(value) def combine(default, override): - ''' + """ Returns a combined dictionary of the 2 dictionaries given (with the 2nd overriding the 1st). Examples: >>> combine({'a': 1, 'b': 2}, {'b': 3, 'c': 4}) == {'a': 1, 'b': 3, 'c': 4} True - ''' + """ combined = default.copy() combined.update(override) return combined def from_toml(value): - ''' + """ Returns a data structure from the TOML string given. Examples: >>> from_toml('[table]\\nkey = "value"\\n') == {'table': {'key': 'value'}} True - ''' + """ from toml import loads + return loads(value) def to_toml(value): - ''' + """ Returns a string of the TOML representation for the data structure given. Examples: >>> import six >>> to_toml({'key': [1, 2]}) == six.text_type("key = [ 1, 2,]\\n") True - ''' + """ from toml import dumps + return dumps(value) def jmespath(value, query): - ''' + """ Queries the data using the JMESPath query language. Examples: >>> import six @@ -125,6 +135,7 @@ def jmespath(value, query): ... WACities == {'WashingtonCities': 'Bellevue, Olympia, Seattle'} ... True - ''' + """ import jmespath + return jmespath.search(query, value)