def test_has_value(self):
     provider = EnvironmentConfigProvider(prefix="DF_")
     with EnvPatch(DF_UNITTEST="on"):
         self.assertTrue(
             provider.has_value(BooleanConfigField("test.test",
                                                   "UNITTEST")))
         self.assertFalse(
             provider.has_value(
                 BooleanConfigField("test.test2", "UNITTEST_2")))
Example #2
0
 def show_env_config(self, verbosity):
     prefix = None
     for provider in merger.providers:
         if not isinstance(provider, EnvironmentConfigProvider):
             continue
         prefix = provider.prefix
     if not prefix:
         self.stderr.write("Environment variables are not used•")
         return
     if verbosity >= 2:
         self.stdout.write(
             self.style.SUCCESS("# read environment variables:"))
     provider = EnvironmentConfigProvider(prefix)
     merger.write_provider(provider, include_doc=verbosity >= 2)
     self.stdout.write(provider.to_str())
Example #3
0
def get_merger_from_env() -> SettingMerger:
    """ Should be used after set_env(); determine all available settings in this order:

   * df_config.config.defaults
   * {project_name}.defaults (overrides df_config.config.defaults)
   * {root}/etc/{project_name}/settings.ini (overrides {project_name}.settings)
   * {root}/etc/{project_name}/settings.py (overrides {root}/etc/{project_name}/settings.ini)
   * ./local_settings.ini (overrides {root}/etc/{project_name}/settings.py)
   * ./local_settings.py (overrides ./local_settings.ini)
   * environment variables (overrides ./local_settings.py)
    """
    # required if set_env is not called
    module_name = set_env()
    prefix = os.path.abspath(sys.prefix)
    if prefix == "/usr":
        prefix = ""
    mapping = "%s.iniconf:INI_MAPPING" % module_name
    config_providers = [
        DictProvider({"DF_MODULE_NAME": module_name}, name="default values"),
        PythonModuleProvider("df_config.config.defaults"),
        PythonModuleProvider("%s.defaults" % module_name),
        IniConfigProvider("%s/etc/%s/settings.ini" % (
            prefix,
            module_name,
        )),
        PythonFileProvider("%s/etc/%s/settings.py" % (
            prefix,
            module_name,
        )),
        IniConfigProvider(os.path.abspath("local_settings.ini")),
        PythonFileProvider(os.path.abspath("local_settings.py")),
        EnvironmentConfigProvider("%s_" % module_name.upper()),
    ]
    fields_provider = PythonConfigFieldsProvider(mapping)
    return SettingMerger(fields_provider, config_providers)
 def test_to_str(self):
     provider = EnvironmentConfigProvider(prefix="DF_")
     with EnvPatch():
         provider.set_value(
             BooleanConfigField("test.test", "UNITTEST", default=True))
         provider.set_value(
             CharConfigField("test.test2",
                             "UNITTEST2",
                             default="$KEY {VALUE} 'SPECIAL \"CHARS"))
     content = provider.to_str()
     self.assertEqual(
         "DF_UNITTEST=true\nDF_UNITTEST2='$KEY {VALUE} '\"'\"'SPECIAL \"CHARS'\n",
         content,
     )
 def test_is_valid(self):
     provider = EnvironmentConfigProvider(prefix="DF_")
     self.assertEqual(True, provider.is_valid())
 def test_get_extra_settings(self):
     provider = EnvironmentConfigProvider(prefix="DF_")
     self.assertEqual([], provider.get_extra_settings())
 def test_get_value(self):
     provider = EnvironmentConfigProvider(prefix="DF_")
     with EnvPatch(DF_UNITTEST="off"):
         v = provider.get_value(
             BooleanConfigField("test.test", "UNITTEST", default=True))
         self.assertEqual(False, v)