Esempio n. 1
0
    def load_file(self, path=None, env=None, silent=True, key=None):
        """Programmatically load files from ``path``.

        :param path: A single filename or a file list
        :param env: Which env to load from file (default current_env)
        :param silent: Should raise errors?
        :param key: Load a single key?
        """
        env = (env or self.current_env).upper()
        files = ensure_a_list(path)
        if files:
            self.logger.debug("Got %s files to process", files)
            already_loaded = set()
            for _filename in files:
                self.logger.debug("Processing file %s", _filename)
                filepath = os.path.join(self._root_path, _filename)
                self.logger.debug("File path is %s", filepath)
                # Handle possible *.globs sorted alphanumeric
                for path in sorted(glob.glob(filepath)):
                    self.logger.debug("Loading %s", path)
                    if path in already_loaded:  # pragma: no cover
                        self.logger.debug("Skipping %s, already loaded", path)
                        continue
                    settings_loader(
                        obj=self,
                        env=env,
                        silent=silent,
                        key=key,
                        filename=path,
                    )
                    already_loaded.add(path)
            if not already_loaded:
                self.logger.warning("Not able to locate the files %s to load",
                                    files)
Esempio n. 2
0
    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))
Esempio n. 3
0
    def execute_loaders(
        self, env=None, silent=None, key=None, filename=None, loaders=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
        :param loaders: optional list of loader modules
        """
        if key is None:
            default_loader(self, self._defaults)

        env = (env or self.current_env).upper()
        silent = silent or self.SILENT_ERRORS_FOR_DYNACONF

        if loaders is None:
            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)

            loaders = self.loaders

        for loader in loaders:
            loader.load(self, env, silent=silent, key=key)

        self.load_includes(env, silent=silent, key=key)
Esempio n. 4
0
 def load_includes(self, env, silent, key):
     """Do we have any nested includes we need to process?"""
     includes = self.get("DYNACONF_INCLUDE", [])
     includes.extend(self.get("INCLUDES_FOR_DYNACONF", []))
     if includes:
         self.logger.debug("Found %s includes to process", includes)
         already_loaded = set()
         for include in includes:
             self.logger.debug("Processing include %s", include)
             included_filename = os.path.join(self._root_path, include)
             self.logger.debug("Include path is %s", included_filename)
             # Handle possible *.globs sorted alphanumeric
             for path in sorted(glob.glob(included_filename)):
                 self.logger.debug("Loading %s", path)
                 if path in already_loaded:  # pragma: no cover
                     self.logger.debug("Skipping %s, already loaded", path)
                     continue
                 settings_loader(
                     obj=self,
                     env=env,
                     silent=silent,
                     key=key,
                     filename=path,
                 )
                 already_loaded.add(path)
         if not already_loaded:
             self.logger.warning(
                 "Not able to locate the files %s to include", includes)
Esempio n. 5
0
def load(
    obj: Settings,
    env: Optional[str] = None,
    silent: bool = True,
    key: Optional[str] = None,
    filename: Optional[str] = None,
) -> None:
    """Load the appropriate YAML setting file linked to the current env.

    :param obj: the settings instance
    :param env: settings current env (upper case)
    :param silent: if errors should raise
    :param key: if defined load a single key, else load all from `env`
    :param filename: Optional custom filename to load
    :type obj: Settings
    :type env: Optional[str]
    :type silent: bool
    :type key: Optional[str]
    :type filename: Optional[str]
    """
    # pylint: disable=unused-argument
    if env is None:
        return

    settings_loader(obj, filename=f"{env.lower()}.yml")
Esempio n. 6
0
def load_config(config_file: str):
    """
    Load the configuration from the given file

    Args:
        config_file: name of the profile to search for the configuration

    """
    settings_loader(config, filename=config_file)
 def setUpClass(cls) -> None:
     """
     Initialize the test case environment
     """
     container.reset()
     settings_loader(config, filename=TEST_CONFIG_PATH)
     config.rabbit = cls.TEST_QUEUE_CONFIG
     cls.exchange_publisher_mock = Mock(spec=ExchangePublisher)
     container.set('exchange_publisher', cls.exchange_publisher_mock)
Esempio n. 8
0
 def execute_loaders(self, namespace=None, silent=None, key=None):
     """Execute all internal and registered loaders"""
     silent = silent or self.SILENT_ERRORS_FOR_DYNACONF
     if key is None:
         default_loader(self, self._defaults)
     settings_loader(self, namespace=namespace, silent=silent, key=key)
     self.load_extra_yaml(namespace, silent, key)  # DEPRECATED
     for loader in self.loaders:
         self.logger.debug('Loading %s', loader.__name__)
         loader.load(self, namespace, silent=silent, key=key)
Esempio n. 9
0
 def test_init_app(self, view_mock, setup_graphql_mock, load_mock, *_):
     """
     Test if the initialization of the app initializes all the required modules
     """
     settings_loader(config, filename=TEST_CONFIG_PATH)
     base_app = Application()
     init_news_manager(base_app)
     view_mock.setup_routes.assert_called_once()
     setup_graphql_mock.assert_called_once()
     load_mock.assert_called_once()
Esempio n. 10
0
    def load_file(self, path=None, env=None, silent=True, key=None):
        """Programmatically load files from ``path``.

        :param path: A single filename or a file list
        :param env: Which env to load from file (default current_env)
        :param silent: Should raise errors?
        :param key: Load a single key?
        """
        env = (env or self.current_env).upper()
        files = ensure_a_list(path)
        if files:
            already_loaded = set()
            for _filename in files:

                if py_loader.try_to_load_from_py_module_name(
                    obj=self, name=_filename, silent=True
                ):
                    # if it was possible to load from module name
                    # continue the loop.
                    continue

                # python 3.6 does not resolve Pathlib basedirs
                # issue #494
                root_dir = str(self._root_path or os.getcwd())
                if (
                    isinstance(_filename, Path)
                    and str(_filename.parent) in root_dir
                ):  # pragma: no cover
                    filepath = str(_filename)
                else:
                    filepath = os.path.join(
                        self._root_path or os.getcwd(), str(_filename)
                    )

                paths = [
                    p
                    for p in sorted(glob.glob(filepath))
                    if ".local." not in p
                ]
                local_paths = [
                    p for p in sorted(glob.glob(filepath)) if ".local." in p
                ]
                # Handle possible *.globs sorted alphanumeric
                for path in paths + local_paths:
                    if path in already_loaded:  # pragma: no cover
                        continue
                    settings_loader(
                        obj=self,
                        env=env,
                        silent=silent,
                        key=key,
                        filename=path,
                    )
                    already_loaded.add(path)
Esempio n. 11
0
    def load_file(self, path=None, env=None, silent=True, key=None):
        """Programmatically load files from ``path``.

        :param path: A single filename or a file list
        :param env: Which env to load from file (default current_env)
        :param silent: Should raise errors?
        :param key: Load a single key?
        """
        env = (env or self.current_env).upper()
        files = ensure_a_list(path)
        if files:
            self.logger.debug(f"Got {files} files to process")
            already_loaded = set()
            for _filename in files:
                self.logger.debug(f"Processing file {_filename}")

                if py_loader.try_to_load_from_py_module_name(
                    obj=self, name=_filename, silent=True
                ):
                    # if it was possible to load from module name
                    # continue the loop.
                    continue

                filepath = os.path.join(
                    self._root_path or os.getcwd(), _filename
                )
                self.logger.debug(f"File path is {filepath}")
                paths = [
                    p
                    for p in sorted(glob.glob(filepath))
                    if ".local." not in p
                ]
                local_paths = [
                    p for p in sorted(glob.glob(filepath)) if ".local." in p
                ]
                # Handle possible *.globs sorted alphanumeric
                for path in paths + local_paths:
                    self.logger.debug(f"Loading {path}")
                    if path in already_loaded:  # pragma: no cover
                        self.logger.debug(f"Skipping {path}, already loaded")
                        continue
                    settings_loader(
                        obj=self,
                        env=env,
                        silent=silent,
                        key=key,
                        filename=path,
                    )
                    already_loaded.add(path)
            if not already_loaded:
                self.logger.warning(
                    f"Not able to locate the files {files} " "to load"
                )
Esempio n. 12
0
    def test_publish_new(
        self,
        _,
    ):
        """
        Test publishing new declares the exchange, publish the new, sets hydrated of new as true, closes the channel
        and closes the connection
        """
        settings_loader(config, filename=TEST_CONFIG_PATH)

        publish_hydrated_new(asdict(self.TEST_NEW))

        expected_new = asdict(self.TEST_NEW)
        self.exchange_publisher_mock.assert_called_with(expected_new)
Esempio n. 13
0
 def test_init_app(self, auth_view_mock, users_view_mock, init_sql_mock,
                   health_mock, setup_event_bus_mock, load_mock, _):
     """
     Test if the initialization of the app initializes all the required components
     """
     settings_loader(config, filename=TEST_CONFIG_PATH)
     health_mock.return_value = True
     base_app = Application()
     init_uaa(base_app)
     auth_view_mock.setup_routes.assert_called_once()
     users_view_mock.setup_routes.assert_called_once()
     init_sql_mock.assert_called_once()
     setup_event_bus_mock.assert_called()
     load_mock.assert_called()
Esempio n. 14
0
    def test_init_app(self, init_sql_mock, sql_health_mock, setup_graphql_mock,
                      event_bus_mock, load_mock, _):
        """
        Test initializing the app initializes the database the graphql views and all the required services
        """
        settings_loader(config, filename=TEST_CONFIG_PATH)
        sql_health_mock().return_value = True
        base_app = Application()
        init_search_engine(base_app)

        event_bus_mock.assert_called_once()
        load_mock.assert_called_once()

        init_sql_mock.assert_called_once()
        setup_graphql_mock.assert_called_once()
Esempio n. 15
0
    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: optinal custom filename to load
        """
        if key is None:
            default_loader(self, self._defaults)
        silent = silent or self.SILENT_ERRORS_FOR_DYNACONF
        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)

        # Do we have any nested includes we need to process? If so,
        # Remove any duplicate paths (while preserving definition order)
        # by converting to an ordered dict, and then converting back
        # to a list.
        includes = list(OrderedDict.fromkeys(self.get('DYNACONF_INCLUDE', [])))
        if includes:
            already_loaded = set()
            basepath = os.path.split(self.settings_module)[0]

            for include in includes:
                # Handle absolute and relative paths in the configuration.
                if os.path.isabs(include):  # pragma: no cover
                    included_filename = include
                else:
                    included_filename = os.path.join(basepath, include)

                # Handle possible globs
                for path in glob.glob(included_filename):
                    if path in already_loaded:  # pragma: no cover
                        continue

                    settings_loader(obj=self,
                                    env=env,
                                    silent=silent,
                                    key=key,
                                    filename=path)

                    already_loaded.add(path)
Esempio n. 16
0
 def setUpClass(cls) -> None:
     """
     Initialize test case environment
     """
     settings_loader(config, filename=TEST_CONFIG_PATH)