From 8a51b97cb4ff995475e9e46dbc432b545619c27c Mon Sep 17 00:00:00 2001 From: tomaszdrozdz Date: Tue, 4 Aug 2020 18:30:35 +0200 Subject: [PATCH] New aproach for uploading sanic app config. --- sanic/app.py | 21 ++++++++++++--------- sanic/config.py | 27 ++++++++++++++++----------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index 9b600b60..29aaf43b 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -1460,25 +1460,28 @@ class Sanic: # -------------------------------------------------------------------- # def update_config(self, config: Union[bytes, str, dict, Any]): """Update app.config. - + Note:: only upper case settings are considered. - + You can upload app config by providing path to py file holding settings. - + # /some/py/file A = 1 B = 2 - - app.update_config("/some/py/file") - + + app.update_config("${some}/py/file") + + Yes you can put environment variable here, but they must be provided in format: ${some_env_var}, + and mark that $some_env_var is treated as plain string. + You can upload app config by providing dict holding settings. - + d = {"A": 1, "B": 2} app.update_config(d) - + You can upload app config by providing any object holding settings, but in such case config.__dict__ will be used as dict holding settings. - + class C: A = 1 B = 2 diff --git a/sanic/config.py b/sanic/config.py index 78ea97c4..32de41b2 100644 --- a/sanic/config.py +++ b/sanic/config.py @@ -81,25 +81,28 @@ class Config(dict): def update_config(self, config: Union[bytes, str, dict, Any]): """Update app.config. - + Note only upper case settings are considered. - + You can upload app config by providing path to py file holding settings. - + # /some/py/file A = 1 B = 2 - - config.update_config("/some/py/file") - + + config.update_config("${some}/py/file") + + Yes you can put environment variable here, but they must be provided in format: ${some_env_var}, + and mark that $some_env_var is treated as plain string. + You can upload app config by providing dict holding settings. - + d = {"A": 1, "B": 2} config.update_config(d) - + You can upload app config by providing any object holding settings, but in such case config.__dict__ will be used as dict holding settings. - + class C: A = 1 B = 2 @@ -139,12 +142,14 @@ def strtobool(val): def load_module_from_file_location(*args, **kwargs): """Returns loaded module provided as a file path. - + :param args: look for importlib.util.spec_from_file_location parameters specification :param kwargs: look for importlib.util.spec_from_file_location parameters specification - + So for example You can: + some_module = load_module_from_file_location("some_module_name", "/some/path/${some_env_var}) + Yes you can put environment variable here, but they must be provided in format: ${some_env_var}, and mark that $some_env_var is treated as plain string."""