diff --git a/README.md b/README.md index a6f71ad..e1cc7bc 100644 --- a/README.md +++ b/README.md @@ -293,14 +293,28 @@ Accessing environment in template. ### Accessible objects -You have 4 available objects in your templates. +You have 8 available objects in your templates. - `config` - `links` - `containers` - `environ` - - `yaml` - - `json` + - [`yaml`](https://pypi.org/project/PyYAML/) + - [`json`](https://docs.python.org/fr/3/library/json.html) + - [`toml`](https://pypi.org/project/toml/) + - [`ConfigParser`](https://docs.python.org/3/library/configparser.html) + +#### ConfigParser + +ConfigParser has an extra `to_string` method to write config as string in jinja templace, or can be casted to sting. + +`read*` methods also return self object (unlike the original methods which are retuning `None`) + +```jinja +{% set config = ConfigParser().read_string(env['SOME_CONF_IN_ENVIRONMENT'])} +{# Write parsed config like this #} +{{ str(config) }} +``` #### config diff --git a/common.yml b/common.yml index 05a3ea9..4a0150c 100644 --- a/common.yml +++ b/common.yml @@ -7,3 +7,9 @@ environ: yaml: ok OK: 'true' KO: '0' + TOML: | + [toml] + value = 'ok' + CONFIGPARSER: | + [ini] + value = ok diff --git a/poetry.lock b/poetry.lock index c565e12..b141e8a 100644 --- a/poetry.lock +++ b/poetry.lock @@ -563,12 +563,12 @@ python-versions = "*" version = "1.6.2" [[package]] -category = "dev" +category = "main" description = "Python Library for Tom's Obvious, Minimal Language" name = "toml" optional = false python-versions = "*" -version = "0.10.0" +version = "0.10.1" [[package]] category = "dev" @@ -656,7 +656,7 @@ docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"] testing = ["jaraco.itertools", "func-timeout"] [metadata] -content-hash = "d5b4ce6c6aa76b713725b2fb9726798fffff606425e3434a1511ab75afe6dd56" +content-hash = "fe6f383bd04693549609607dfb8a4bf4605bb3cc36a18aa1ad97f30d8b30788b" python-versions = "^3.6.1" [metadata.files] @@ -963,9 +963,8 @@ texttable = [ {file = "texttable-1.6.2.tar.gz", hash = "sha256:eff3703781fbc7750125f50e10f001195174f13825a92a45e9403037d539b4f4"}, ] toml = [ - {file = "toml-0.10.0-py2.7.egg", hash = "sha256:f1db651f9657708513243e61e6cc67d101a39bad662eaa9b5546f789338e07a3"}, - {file = "toml-0.10.0-py2.py3-none-any.whl", hash = "sha256:235682dd292d5899d361a811df37e04a8828a5b1da3115886b73cf81ebc9100e"}, - {file = "toml-0.10.0.tar.gz", hash = "sha256:229f81c57791a41d65e399fc06bf0848bab550a9dfd5ed66df18ce5f05e73d5c"}, + {file = "toml-0.10.1-py2.py3-none-any.whl", hash = "sha256:bda89d5935c2eac546d648028b9901107a595863cb36bae0c73ac804a9b4ce88"}, + {file = "toml-0.10.1.tar.gz", hash = "sha256:926b612be1e5ce0634a2ca03470f95169cf16f939018233a670519cb4ac58b0f"}, ] urllib3 = [ {file = "urllib3-1.25.9-py2.py3-none-any.whl", hash = "sha256:88206b0eb87e6d677d424843ac5209e3fb9d0190d0ee169599165ec25e9d9115"}, diff --git a/pyentrypoint/configparser.py b/pyentrypoint/configparser.py new file mode 100644 index 0000000..637aeda --- /dev/null +++ b/pyentrypoint/configparser.py @@ -0,0 +1,34 @@ +from configparser import ConfigParser as CP +from io import StringIO + + +class ConfigParser(CP): + ''' + ConfigParser class with to_string method to convert config to string + and write it inside jinja template and read methods which return + self object + ''' + + def to_string(self): + io = StringIO() + self.write(io) + return io.getvalue() + + def read(self, *args, **kwards): + super().read(*args, **kwards) + return self + + def read_dict(self, *args, **kwards): + super().read_dict(*args, **kwards) + return self + + def read_file(self, *args, **kwards): + super().read_file(*args, **kwards) + return self + + def read_string(self, *args, **kwards): + super().read_string(*args, **kwards) + return self + + def __str__(self): + return self.to_string() diff --git a/pyentrypoint/entrypoint.py b/pyentrypoint/entrypoint.py index a14626d..09daab8 100644 --- a/pyentrypoint/entrypoint.py +++ b/pyentrypoint/entrypoint.py @@ -7,12 +7,14 @@ import os from sys import argv from sys import exit +import toml import yaml from jinja2 import Environment from jinja2 import FileSystemLoader from .config import Config from .config import envtobool +from .configparser import ConfigParser from .constants import ENTRYPOINT_FILE from .docker_links import DockerLinks from .logs import Logs @@ -88,6 +90,8 @@ class Entrypoint(object): environ=os.environ, json=json, yaml=yaml, + toml=toml, + ConfigParser=ConfigParser, envtobool=envtobool, containers=DockerLinks().to_containers())) diff --git a/pyproject.toml b/pyproject.toml index 87be715..6fa06d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool] [tool.poetry] name = "pyentrypoint" -version = "0.7.3" +version = "0.7.4" description = "pyentrypoint manages entrypoints in Docker containers." license = "WTFPL" classifiers = ["Programming Language :: Python", "Development Status :: 1 - Planning", "License :: OSI Approved :: BSD License", "Natural Language :: English", "Operating System :: POSIX :: Linux", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.6", "Topic :: System :: Installation/Setup"] @@ -16,6 +16,7 @@ jinja2 = "^2.11" pyyaml = "^5.3" six = "^1.14" watchdog = "^0.10" +toml = "^0.10.1" [tool.poetry.dev-dependencies] diff --git a/tests/configparser_test.py b/tests/configparser_test.py new file mode 100644 index 0000000..8350d67 --- /dev/null +++ b/tests/configparser_test.py @@ -0,0 +1,19 @@ +from commons import clean_env + +from pyentrypoint.configparser import ConfigParser + + +def teardown_function(function): + clean_env() + + +def test_configparser(): + config = ''' +[ini] +content = ok + '''.strip() + + c = ConfigParser() + c.read_string(config) + + assert str(c).strip() == config diff --git a/tests/pyentrypoint_test.py b/tests/pyentrypoint_test.py index 7b020e5..49306cf 100644 --- a/tests/pyentrypoint_test.py +++ b/tests/pyentrypoint_test.py @@ -144,8 +144,8 @@ def test_templates(): int(id, base=16) # test env - assert test['ENV']['SECRET'] == 'nothing' - assert test['ENVIRON']['SECRET'] == 'nothing' + assert test['ENV']['SECRET'].strip() == 'nothing' + assert test['ENVIRON']['SECRET'].strip() == 'nothing' # test yaml assert test['YAML']['yaml'] == 'ok' @@ -157,6 +157,12 @@ def test_templates(): assert test['ENVTOBOOL']['ok'] assert not test['ENVTOBOOL']['ko'] + # test toml + assert test['TOML'].strip() == '[toml]\nvalue = "ok"' + + # test configparser + assert test['CONFIGPARSER'].strip() == '[ini]\nvalue = ok' + def test_conf_commands(): diff --git a/tests/test_template.yml.tpl b/tests/test_template.yml.tpl index bbf1465..4ef1112 100644 --- a/tests/test_template.yml.tpl +++ b/tests/test_template.yml.tpl @@ -20,14 +20,16 @@ All links 2: All environ: {% for link in links.all %} {% for key in link.environ %} - {{key}}: {{link.environ[key]}} + {{key}}: + {{link.environ[key] | indent(8)}} {% endfor %} {% endfor %} All links 2 environ: {% for link in links.test2 %} {% for key in link.environ %} - {{key}}: {{link.environ[key]}} + {{key}}: + {{link.environ[key] | indent(8)}} {% endfor %} {% endfor %} @@ -45,12 +47,14 @@ ID: ENV: {% for e in env %} - '{{e}}': '{{env[e]}}' + '{{e}}': | + {{ env[e] | indent(8) }} {% endfor %} ENVIRON: {% for e in environ %} - '{{e}}': '{{env[e]}}' + '{{e}}': | + {{ env[e] | indent(8) }} {% endfor %} JSON: @@ -66,3 +70,9 @@ YAML: ENVTOBOOL: ok: {{ envtobool('OK', False) }} ko: {{ envtobool('KO', True) }} + +TOML: | + {{ toml.dumps(toml.loads(env['TOML'])) | indent }} + +CONFIGPARSER: | + {{ ConfigParser().read_string(env['CONFIGPARSER']).to_string() | indent }}