Skip to content

Commit 345a58c

Browse files
author
Dr. Bert Besser
committed
feat: hocon reads env vars
1 parent f214011 commit 345a58c

File tree

9 files changed

+27
-6
lines changed

9 files changed

+27
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Unfortunately, cookiecutter does not allow us to show any description of the opt
4747
* `create_cli` (yes or no): if you plan to build an application with a command line interface (CLI), select *yes* here. This will integrate a template for the CLI into your project - minimal boilerplate guaranteed! (We're leveraging the awesome [typer](https://typer.tiangolo.com/) library for this.)
4848
* `config_file`: select your preferred config format. It is best practice to store your configuration separate from your code, even for small projects, but because there are a gazillion ways to do this, each project seems to reinvents the wheel. We want to provide a few options to set you up with a working configuration:
4949
- `yaml`: use [YAML](https://yaml.org/) as your configuration file format. Easy to read and write, widely adopted, relies on the [PyYAML](https://pyyaml.org/) package.
50-
- `hocon`: use [HOCON](https://github.com/lightbend/config/blob/master/HOCON.md) as your configuration file format. It is a superset of JSON, very resilient (it's really hard to make a breaking syntax error) and comes with powerful functions, e.g. for inheritance or variable substitution. In this example you can find two environment configurations (`dev.conf`, `prod.conf`) that override parts of the default configuration. Relies on the [pyhocon](https://github.com/chimpler/pyhocon/) package.
50+
- `hocon`: use [HOCON](https://github.com/lightbend/config/blob/master/HOCON.md) as your configuration file format. It is a superset of JSON, very resilient (it's really hard to make a breaking syntax error) and comes with powerful functions, e.g. for inheritance or variable substitution. In this example you can find two environment configurations (`dev.conf`, `prod.conf`) that override parts of the default configuration. In particular, parts of the configuration are populated from environment variables, which are commonly used to configure processes running in containers (such as e.g. Kubeflow pipeline operators). Relies on the [pyhocon](https://github.com/chimpler/pyhocon/) package.
5151
- `none`: don't need any configuration or want to do your own thing? choose this option.
5252
* `code_formatter`: a code formatter is a powerful tool that can help teams to stick to a common code style. However, a formatter cannot solve every code style problem for you and it may lead to issues for users that are not aware of how it works. Always talk to your team about [PEP 8](https://www.python.org/dev/peps/pep-0008/) and a common code style, then choose the right formatter for you (or none at all):
5353
- [`black`](https://github.com/psf/black): *the uncompromising Python code formatter*.

{{cookiecutter.project_slug}}/Dockerfile__conda

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ SHELL ["conda", "run", "-n", "{{cookiecutter.module_name}}_env", "/bin/bash", "-
1717

1818
RUN python setup.py install
1919

20+
WORKDIR /
21+
COPY ./config ./config
22+
2023
# ENTRYPOINT doesn't use the same shell as RUN so you need the conda stuff
21-
ENTRYPOINT ["conda", "run", "-n", "{{cookiecutter.module_name}}_env", "python", "-OO", "-m", "{{ cookiecutter.module_name }}"]
24+
ENTRYPOINT ["conda", "run", "-n", "{{cookiecutter.module_name}}_env", "python", "-OO", "-m", "{{ cookiecutter.module_name }}.main"]

{{cookiecutter.project_slug}}/Dockerfile__pip

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ RUN [ -n "$(ls -A deps)" ] && pip install deps/*.whl && rm -rf deps || echo "no
3030
# install the application from a wheel package in the 'dist' folder
3131
COPY --from=py-build /build/dist/ dist/
3232
RUN pip install dist/*.whl && rm -rf dist
33-
ENTRYPOINT ["python", "-OO", "-m", "{{ cookiecutter.module_name }}"]
33+
34+
WORKDIR /
35+
COPY ./config ./config
36+
37+
ENTRYPOINT ["python", "-OO", "-m", "{{ cookiecutter.module_name }}.main"]

{{cookiecutter.project_slug}}/Dockerfile__poetry

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ RUN poetry install --only main --no-root --no-interaction && \
1010
COPY ./src /app/src
1111
RUN poetry install --only-root
1212

13-
ENTRYPOINT ["python", "-OO", "-m", "{{ cookiecutter.module_name }}"]
13+
WORKDIR /
14+
COPY ./config ./config
15+
16+
ENTRYPOINT ["python", "-OO", "-m", "{{ cookiecutter.module_name }}.main"]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
environment = "dev"
22
logging.level = DEBUG # overrides the log level that is specified in res/default.conf
3+
username: ${USERNAME}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
environment = "prod"
2+
username: ${USERNAME}

{{cookiecutter.project_slug}}/docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ services:
77
context: .{% if cookiecutter.package_manager != 'poetry' %}
88
args:
99
PYTHON_IMAGE_TAG: {% if cookiecutter.package_manager == 'conda' %}"4.8.2"{% else %}"3.7-stretch"{% endif %}{% endif %}
10+
{% if cookiecutter.config_file == 'hocon' %}
11+
environment:
12+
ENV: dev
13+
USERNAME: ${USERNAME:-dear user of the at-python-template}
14+
{% endif %}
1015
command: '{% if cookiecutter.create_cli == 'yes' %}--help{% endif %}'
1116
tty: true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[virtualenvs]
22
create = true
33
in-project = true
4+

{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
{% if cookiecutter.config_file == 'hocon' %}import os
2+
from pyhocon import ConfigFactory
13

2-
def main():
4+
{% endif %}def main():
35
# TODO your journey starts here
4-
print("hello :)")
6+
{% if cookiecutter.config_file == 'hocon' %}config = ConfigFactory.parse_file(f"config/{os.environ['ENV']}.conf")
7+
print(f"hello {config.get('username')} :)"){% else %}print("hello :)"){% endif %}
58

69

710
if __name__ == "__main__":

0 commit comments

Comments
 (0)