Merge pull request #332 from pacien/env-override

Allow config key override through env var
This commit is contained in:
Tulir Asokan
2019-06-07 17:10:10 +03:00
committed by GitHub
2 changed files with 9 additions and 2 deletions
+2 -1
View File
@@ -22,6 +22,7 @@ import logging.config
import sys
import copy
import signal
import os
import sqlalchemy as sql
@@ -62,7 +63,7 @@ parser.add_argument("-r", "--registration", type=str, default="registration.yaml
metavar="<path>", help="the path to save the generated registration to")
args = parser.parse_args()
config = Config(args.config, args.registration, args.base_config)
config = Config(args.config, args.registration, args.base_config, os.environ)
config.load()
config.update()
+7 -1
View File
@@ -103,12 +103,18 @@ class DictWithRecursion:
class Config(DictWithRecursion):
def __init__(self, path: str, registration_path: str, base_path: str) -> None:
def __init__(self, path: str, registration_path: str, base_path: str,
overrides: Dict[str, Any] = {}) -> None:
super().__init__()
self.path = path # type: str
self.registration_path = registration_path # type: str
self.base_path = base_path # type: str
self._registration = None # type: Optional[Dict]
self._overrides = overrides # type: Dict[str, Any]
def __getitem__(self, key: str, prefix: str = 'MAUTRIX_TELEGRAM_') -> Any:
env_key = prefix + key.replace('.', '_').upper()
return self._overrides[env_key] or super(Config, self).__getitem__(key)
def load(self) -> None:
with open(self.path, 'r') as stream: