Example #1
0
 def __new__(cls, *args, **kwargs):
     data_sources = [
         EnvSource(),
         YamlSource(FILE_NAME),
         TomlSource(FILE_NAME)
     ]
     kwargs["data_sources"] = data_sources
     return super(Config, cls).__new__(cls, *args, **kwargs)
Example #2
0
    def __new__(cls, *args, **kwargs):
        if "data_sources" not in kwargs.keys():
            yaml_source = YamlSource("n26")
            toml_source = TomlSource("n26")
            data_sources = [EnvSource(), yaml_source, toml_source]
            kwargs["data_sources"] = data_sources

        return super(Config, cls).__new__(cls, *args, **kwargs)
Example #3
0
 def __new__(cls, *args, **kwargs):
     yaml_source = YamlSource(NODE_MAIN)
     toml_source = TomlSource(NODE_MAIN)
     data_sources = [
         EnvSource(),
         yaml_source,
         toml_source
     ]
     return super(Config, cls).__new__(cls, data_sources=data_sources)
Example #4
0
 def __new__(cls, *args, **kwargs):
     yaml_source = YamlSource(CONFIG_NODE_ROOT)
     toml_source = TomlSource(CONFIG_NODE_ROOT)
     data_sources = [
         EnvSource(),
         yaml_source,
         toml_source,
     ]
     return super(AppConfig, cls).__new__(cls, data_sources=data_sources)
    def __new__(cls,
                data_sources: List[DataSource] = None,
                validate: bool = True,
                singleton: bool = True):
        """
        Creates a config object and reads configuration.
        :param data_sources: list of data sources to use. The first value that holds a value for a specific
                             config entry overshadows other data sources.
        :param validate: if validation should be run (can be disabled for tests)
        :param singleton: if the returned instance should be a singleton
        """
        if singleton:
            if cls._instances.get(cls, None) is None:
                instance = super(ConfigBase, cls).__new__(cls)
                cls._instances[cls] = instance
            else:
                instance = cls._instances[cls]
        else:
            instance = super(ConfigBase, cls).__new__(cls)

        self = instance
        self._config_entries = self._find_config_entries()

        if not singleton:
            # copy class attribute to instance to overshadow class attributes
            instance_attributes = {}
            for name, attribute in self._config_entries.items():
                attribute_copy = copy.deepcopy(attribute)
                self.__dict__.setdefault(name, attribute_copy)
                key = "_".join(attribute.key_path)
                instance_attributes[key] = attribute_copy
            # update config_entries list to reflect instance attributes
            self._config_entries = instance_attributes

        if data_sources is None:
            # set default data sources
            from container_app_conf.source.env_source import EnvSource
            from container_app_conf.source.yaml_source import YamlSource

            self.data_sources = [EnvSource(), YamlSource(cls.__name__)]
        else:
            self.data_sources = data_sources

        self.load_config(validate)

        return instance
Example #6
0
 def __new__(cls, *args, **kwargs):
     yaml_source = YamlSource(CONFIG_FILE_NAME)
     data_sources = [EnvSource(), yaml_source]
     return super(AppConfig, cls).__new__(cls, data_sources=data_sources)
    def test_env(self):
        str_entry = StringConfigEntry(
            key_path=["test-ing", "key1"],
            default="value"
        )
        int_entry = IntConfigEntry(
            key_path=["testing", "key2"],
            default=2
        )

        source = EnvSource()
        original_key = EnvSource.env_key(str_entry)
        expected = "expected"
        with mock.patch.dict(os.environ, {original_key: expected}, clear=True):
            source.load()

        self.assertTrue(source.has(str_entry))
        self.assertEqual(source.get(str_entry), expected)
        self.assertFalse(source.has(int_entry))

        normalized_env_key = original_key.replace('-', '_')
        self.assertNotEqual(original_key, normalized_env_key)
        with mock.patch.dict(os.environ, {normalized_env_key: expected + '2'}, clear=True):
            source.load()

        self.assertTrue(source.has(str_entry))
        self.assertEqual(source.get(str_entry), expected + '2')
        self.assertFalse(source.has(int_entry))
Example #8
0
 def __new__(cls, *args, **kwargs):
     yaml_source = YamlSource("deinemudda")
     data_sources = [EnvSource(), yaml_source]
     return super(AppConfig, cls).__new__(cls, data_sources=data_sources)
Example #9
0
 def __new__(cls, *args, **kwargs):
     yaml_source = YamlSource("py_image_dedup")
     data_sources = [EnvSource(), yaml_source]
     return super(DeduplicatorConfig,
                  cls).__new__(cls, data_sources=data_sources)