def execute_loaders(self, env=None, silent=None, key=None, filename=None): """Execute all internal and registered loaders :param env: The environment to load :param silent: If loading erros is silenced :param key: if provided load a single key :param filename: optional custom filename to load """ if key is None: default_loader(self, self._defaults) env = (env or self.current_env).upper() silent = silent or self.SILENT_ERRORS_FOR_DYNACONF self.pre_load(env, silent=silent, key=key) settings_loader(self, env=env, silent=silent, key=key, filename=filename) self.load_extra_yaml(env, silent, key) # DEPRECATED enable_external_loaders(self) for loader in self.loaders: self.logger.debug("Dynaconf executing: %s", loader.__name__) loader.load(self, env, silent=silent, key=key) self.load_includes(env, silent=silent, key=key) self.logger.debug("Loaded Files: %s", deduplicate(self._loaded_files))
def find_file(filename=".env", project_root=None, skip_files=None, **kwargs): """Search in increasingly higher folders for the given file Returns path to the file if found, or an empty string otherwise. This function will build a `search_tree` based on: - Project_root if specified - Invoked script location and its parents until root - Current working directory For each path in the `search_tree` it will also look for an aditional `./config` folder. """ search_tree = [] work_dir = os.getcwd() skip_files = skip_files or [] if project_root is None: logger.debug("No root_path for %s", filename) else: logger.debug("Got root_path %s for %s", project_root, filename) search_tree.extend(_walk_to_root(project_root, break_at=work_dir)) script_dir = os.path.dirname(os.path.abspath(inspect.stack()[-1].filename)) # Path to invoked script and recursively to root with its ./config dirs search_tree.extend(_walk_to_root(script_dir)) # Path to where Python interpreter was invoked and recursively to root search_tree.extend(_walk_to_root(work_dir)) # Don't look the same place twice search_tree = deduplicate(search_tree) global SEARCHTREE SEARCHTREE != search_tree and logger.debug("Search Tree: %s", search_tree) SEARCHTREE = search_tree logger.debug("Searching for %s", filename) for dirname in search_tree: check_path = os.path.join(dirname, filename) if check_path in skip_files: continue if os.path.exists(check_path): logger.debug("Found: %s", os.path.abspath(check_path)) return check_path # First found will return # return empty string if not found so it can still be joined in os.path return ""
def find_file(filename=".env", project_root=None, skip_files=None, **kwargs): """Search in increasingly higher folders for the given file Returns path to the file if found, or an empty string otherwise. This function will build a `search_tree` based on: - Project_root if specified - Invoked script location and its parents until root - Current working directory For each path in the `search_tree` it will also look for an aditional `./config` folder. """ search_tree = [] work_dir = os.getcwd() skip_files = skip_files or [] # If filename is an absolute path and exists, just return it # if the absolute path does not exist, return empty string so # that it can be joined and avoid IoError if os.path.isabs(filename): return filename if os.path.exists(filename) else "" if project_root is not None: search_tree.extend(_walk_to_root(project_root, break_at=work_dir)) script_dir = os.path.dirname(os.path.abspath(inspect.stack()[-1].filename)) # Path to invoked script and recursively to root with its ./config dirs search_tree.extend(_walk_to_root(script_dir)) # Path to where Python interpreter was invoked and recursively to root search_tree.extend(_walk_to_root(work_dir)) # Don't look the same place twice search_tree = deduplicate(search_tree) global SEARCHTREE SEARCHTREE[:] = search_tree for dirname in search_tree: check_path = os.path.join(dirname, filename) if check_path in skip_files: continue if os.path.exists(check_path): return check_path # First found will return # return empty string if not found so it can still be joined in os.path return ""
def default_loader(obj, defaults=None): """Loads default settings and check if there are overridings exported as environment variables""" defaults = defaults or {} default_settings_values = { key: value for key, value in default_settings.__dict__.items() # noqa if key.isupper() } all_keys = deduplicate( list(defaults.keys()) + list(default_settings_values.keys()) ) for key in all_keys: if not obj.exists(key): value = defaults.get(key, default_settings_values.get(key)) obj.logger.debug("loading: %s:%s", key, value) obj.set(key, value) # start dotenv to get default env vars from there # check overrides in env vars default_settings.start_dotenv(obj) # Deal with cases where a custom ENV_SWITCHER_IS_PROVIDED # Example: Flask and Django Extensions env_switcher = defaults.get( "ENV_SWITCHER_FOR_DYNACONF", "ENV_FOR_DYNACONF" ) for key in all_keys: if key not in default_settings_values.keys(): continue env_value = obj.get_environ( env_switcher if key == "ENV_FOR_DYNACONF" else key, default="_not_found", ) if env_value != "_not_found": obj.logger.debug("overriding from envvar: %s:%s", key, env_value) obj.set(key, env_value, tomlfy=True)