diff --git a/.travis.yml b/.travis.yml index 1c90cea60547bf8e2f375e7a7f0d43acc3c97764..0d07079ece25ee8cc4550e39d76df7cc2f444b3e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,10 @@ --- language: python -python: ["2.7", "3.2", "3.3", "3.4", "3.5"] +python: ["2.7", "3.3", "3.4", "3.5"] dist: trusty sudo: false cache: - pip -matrix: - allow_failure: - - python: "3.2" install: - pip install tox-travis diff --git a/README.rst b/README.rst index f5af29cf6993e950047103048f55b85a3dbf6acf..e222e914faa295104a99ae3462634b54d58d053c 100644 --- a/README.rst +++ b/README.rst @@ -79,9 +79,6 @@ at: https://www.shore.co.il/git/. TODO ---- -- Add unit tests of filters using doctest. -- Fix combining dictionaries test. -- Fix Travis CI test on Python 3.2 (https://travis-ci.org/adarnimrod/template/jobs/187388235). - Release on tagged commits to PyPI in Travis CI (https://docs.travis-ci.com/user/deployment/pypi/ and https://docs.travis-ci.com/user/encryption-keys/). diff --git a/setup.py b/setup.py index 68c110afa8b5485f2ca86cd5ae5c449e9f3fddd2..7abfd3bcff29369202de230eacb8355750b384e8 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ setup( ], keywords='config configuration jinja template environment', packages=find_packages(), - install_requires=['Jinja2', 'PyYAML'], + install_requires=['Jinja2', 'PyYAML', 'six'], extras_require={ 'dev': ['tox'], }, entry_points={ diff --git a/template/filters.py b/template/filters.py index 018c9ac0422ab8a6a5ebf2e5949a4ce689a088ee..133ecbbbaac71916a11b84475754b02490eed07d 100644 --- a/template/filters.py +++ b/template/filters.py @@ -1,32 +1,103 @@ #!/usr/bin/env python +from __future__ import (absolute_import, division, + print_function, unicode_literals) +import six # noqa: F401 def to_yaml(value): + ''' + Converts given data structure to YAML form. + Examples: + + >>> to_yaml([1,2,3]) + '[1, 2, 3]\\n' + >>> to_yaml({'a': 1, 'b': 2}) + '{a: 1, b: 2}\\n' + >>> to_yaml({1: {'a': [1,2,3]}}) + '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: + + >>> to_json([1,2,3]) + '[1, 2, 3]' + >>> to_json({'b':2}) + '{"b": 2}' + >>> to_json(2) + '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: + + >>> from_json('[1, 2, 3]') + [1, 2, 3] + >>> from_json('"a"') == six.text_type(u'a') + True + >>> from_json('{"1": {"a": [1, 2, 3]}}') == {u'1': {u'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: + + >>> from_yaml('a') + 'a' + >>> from_yaml('[1, 2, 3]') + [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 pprint(value): + ''' + Returns a pretty string representation of the data structure given. + Examples: + >>> pprint(1) + '1' + >>> output = pprint([{'first_name': 'John', 'last_name': 'Doe'}, {'first_name': 'Jane', 'last_name': 'Doe'}]) # noqa: E501 + >>> if six.PY3: + ... output == "[{'first_name': 'John', 'last_name': 'Doe'},\\n {'first_name': 'Jane', 'last_name': 'Doe'}]" + ... elif six.PY2: + ... output == "[{u'first_name': u'John', u'last_name': u'Doe'},\\n {u'first_name': u'Jane', u'last_name': u'Doe'}]" + ... + True + ''' from pprint import pformat return pformat(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 diff --git a/tests.sh b/tests.sh index 3b25f336f9806e14faa0e1a0459d84375d545efb..d329d33f939fd7cfe4975a21a033357c9b3c433b 100755 --- a/tests.sh +++ b/tests.sh @@ -14,31 +14,4 @@ export name='John' template --output "$outfile" "$infile" test "$(cat $outfile)" = "$name" -echo Testing JSON parsing. -export json='{"a": 1, "b": 2}' -echo '{{ (json|from_json)["a"] }}' > "$infile" -test "$(template $infile)" = "1" - -echo Testing JSON output. -echo '{{ [1, 1+2, 3] | to_json }}' > "$infile" -test "$(template $infile)" = '[1, 3, 3]' - -echo Testing YAML parsing. -export yaml='a: 1 -b: 2' -echo '{{ (yaml|from_yaml)["a"] }}' > "$infile" -test "$(template $infile)" = "1" - -echo Testing YAML output. -echo '{{ [1, 1+2, 3] | to_yaml }}' > "$infile" -test "$(template $infile)" = '[1, 3, 3]' - -echo Testing pprint. -echo '{{ [1, ] + [2, ] }}' > "$infile" -test "$(template $infile)" = "[1, 2]" - -# echo Testing combining dictionaries. -# echo '{{ {"a": 1, "b": 2}|combine({"a": 11, "c": 33}) }}' > "$infile" -# test "$(template $infile)" = "{'a': 11, 'c': 33, 'b': 2}" - rm "$infile" "$outfile" diff --git a/tox.ini b/tox.ini index 719a30a718fdc97aa0bcfb6d9ec7e9bfb9820fa7..ab86304e8a82c503e9937d09e25c4baf70b99c68 100644 --- a/tox.ini +++ b/tox.ini @@ -21,6 +21,7 @@ commands = check-manifest --ignore tox.ini,tests* python setup.py check -m -r -s flake8 . + python -m doctest template/filters.py template/__init__.py ./tests.sh [testenv:release]