Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Make the program respect config vars
  • Loading branch information
tdemin committed Jun 13, 2019
1 parent 4148095 commit 575b8b6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -19,6 +19,10 @@ Example config:
"loglevel": 0 // 0: errors, 1: warnings, 2: info
}

If there are environment variables `AMBER_DATABASE` / `AMBER_ALLOW_SIGNUP` /
`AMBER_LOGLEVEL` set, the program will respect them and use over the values
provided with the config file.

#### Dependencies

This app directly depends on `flask`, `flask-sqlalchemy`, and `bcrypt`.
Expand Down
24 changes: 24 additions & 0 deletions project_amber/config.py
Expand Up @@ -37,6 +37,30 @@
if entry in loadedConfig:
config[entry] = loadedConfig[entry]

def string_to_bool(val: str) -> bool:
"""
Converts a string containing a bool value to Python's bool. Serves as a
helper in configuration code.
"""
if val == "1" or val.lower() == "true":
return True
if val == "0" or val.lower() == "false":
return False
return False

# override config with environment variables in need, the first element of a
# tuple is the environment variable itself, the second is the corresponding
# `config` key, and the third one is the function to convert the possible values
for mapping in (
("AMBER_DATABASE", "database", lambda val: val), # str -> str
# pylint: disable=unnecessary-lambda
("AMBER_LOGLEVEL", "loglevel", lambda val: int(val)), # str -> int
("AMBER_ALLOW_SIGNUP", "allow_signup", string_to_bool) # str -> bool
):
env_value = os.getenv(mapping[0])
if not env_value is None:
config[mapping[1]] = mapping[2](env_value)

if config["database"] == "":
print("No database specified. Exiting.")
exit(1)

0 comments on commit 575b8b6

Please sign in to comment.