Add toml and configparser modules in jinja config templates

pull/27/head
Christophe Mehay 4 years ago
parent 24a83dd226
commit 47a91d8aae

@ -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

@ -7,3 +7,9 @@ environ:
yaml: ok
OK: 'true'
KO: '0'
TOML: |
[toml]
value = 'ok'
CONFIGPARSER: |
[ini]
value = ok

11
poetry.lock generated

@ -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"},

@ -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()

@ -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()))

@ -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]

@ -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

@ -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():

@ -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 }}

Loading…
Cancel
Save