Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hocon reads env vars #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Unfortunately, cookiecutter does not allow us to show any description of the opt
* `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.)
* `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:
- `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.
- `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.
- `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.
- `none`: don't need any configuration or want to do your own thing? choose this option.
* `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):
- [`black`](https://github.com/psf/black): *the uncompromising Python code formatter*.
Expand Down
5 changes: 4 additions & 1 deletion {{cookiecutter.project_slug}}/Dockerfile__conda
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ SHELL ["conda", "run", "-n", "{{cookiecutter.module_name}}_env", "/bin/bash", "-

RUN python setup.py install

WORKDIR /
COPY ./config ./config

# ENTRYPOINT doesn't use the same shell as RUN so you need the conda stuff
ENTRYPOINT ["conda", "run", "-n", "{{cookiecutter.module_name}}_env", "python", "-OO", "-m", "{{ cookiecutter.module_name }}"]
ENTRYPOINT ["conda", "run", "-n", "{{cookiecutter.module_name}}_env", "python", "-OO", "-m", "{{ cookiecutter.module_name }}.main"]
6 changes: 5 additions & 1 deletion {{cookiecutter.project_slug}}/Dockerfile__pip
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ RUN [ -n "$(ls -A deps)" ] && pip install deps/*.whl && rm -rf deps || echo "no
# install the application from a wheel package in the 'dist' folder
COPY --from=py-build /build/dist/ dist/
RUN pip install dist/*.whl && rm -rf dist
ENTRYPOINT ["python", "-OO", "-m", "{{ cookiecutter.module_name }}"]

WORKDIR /
COPY ./config ./config

ENTRYPOINT ["python", "-OO", "-m", "{{ cookiecutter.module_name }}.main"]
5 changes: 4 additions & 1 deletion {{cookiecutter.project_slug}}/Dockerfile__poetry
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ RUN poetry install --only main --no-root --no-interaction && \
COPY ./src /app/src
RUN poetry install --only-root

ENTRYPOINT ["python", "-OO", "-m", "{{ cookiecutter.module_name }}"]
WORKDIR /
COPY ./config ./config

ENTRYPOINT ["python", "-OO", "-m", "{{ cookiecutter.module_name }}.main"]
1 change: 1 addition & 0 deletions {{cookiecutter.project_slug}}/config/dev.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
environment = "dev"
logging.level = DEBUG # overrides the log level that is specified in res/default.conf
username: ${USERNAME}
1 change: 1 addition & 0 deletions {{cookiecutter.project_slug}}/config/prod.conf
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
environment = "prod"
username: ${USERNAME}
5 changes: 5 additions & 0 deletions {{cookiecutter.project_slug}}/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ services:
context: .{% if cookiecutter.package_manager != 'poetry' %}
args:
PYTHON_IMAGE_TAG: {% if cookiecutter.package_manager == 'conda' %}"4.8.2"{% else %}"3.7-stretch"{% endif %}{% endif %}
{% if cookiecutter.config_file == 'hocon' %}
environment:
ENV: dev
USERNAME: ${USERNAME:-dear user of the at-python-template}
{% endif %}
command: '{% if cookiecutter.create_cli == 'yes' %}--help{% endif %}'
tty: true
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{% if cookiecutter.config_file == 'hocon' %}import os
from pyhocon import ConfigFactory

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


if __name__ == "__main__":
Expand Down