예제 #1
0
 def test_parse(self):
     merger = SettingMerger(
         None,
         [DictProvider({"X": 1, "Y": "x{X}"}), DictProvider({"X": 2})],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     merger.process()
     self.assertEqual({"X": 2, "Y": "x2", "DF_MODULE_NAME": "test"}, merger.settings)
 def test_merge(self):
     merger = SettingMerger(
         None,
         [DictProvider({"X": SettingReference("Y"), "Y": 42, "Z": "{Y}"})],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     merger.process()
     self.assertEqual(
         {"X": 42, "DF_MODULE_NAME": "test", "Y": 42, "Z": "42"}, merger.settings
     )
 def test_merge(self):
     merger = SettingMerger(
         None,
         [DictProvider({"X": RawValue("A{Y}C"), "Y": "B"})],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     merger.process()
     self.assertEqual(
         {"X": "A{Y}C", "DF_MODULE_NAME": "test", "Y": "B"}, merger.settings
     )
 def test_no_interpolation(self):
     merger = SettingMerger(
         None,
         [DictProvider({"X": "A{{Y}}C", "Y": "B"})],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     merger.process()
     self.assertEqual(
         {"X": "A{Y}C", "DF_MODULE_NAME": "test", "Y": "B"}, merger.settings
     )
 def test_merge(self):
     x = CallableSetting(self.callable_config, "A", "Z")
     y = CallableSetting(self.callable_config_with_required)
     merger = SettingMerger(
         None,
         [DictProvider({"A": "a", "X": x, "Y": y, "Z": "z"})],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     merger.process()
     self.assertEqual(
         {"A": "a", "X": "az", "DF_MODULE_NAME": "test", "Y": "az", "Z": "z"},
         merger.settings,
     )
예제 #6
0
 def test_priority(self):
     merger = SettingMerger(
         None,
         [DictProvider({"X": 1}),
          DictProvider({"X": 2})],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     merger.process()
     self.assertEqual({"X": 2, "DF_MODULE_NAME": "test"}, merger.settings)
     self.assertEqual(OrderedDict({None: "test"}),
                      merger.raw_settings["DF_MODULE_NAME"])
     self.assertEqual(OrderedDict([("{'X': 1}", 1), ("{'X': 2}", 2)]),
                      merger.raw_settings["X"])
예제 #7
0
 def test_priority(self):
     merger = SettingMerger(
         None,
         [DictProvider({"X": 1}), DictProvider({"X": 2})],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     merger.process()
     self.assertEqual({"X": 2, "DF_MODULE_NAME": "test"}, merger.settings)
     self.assertEqual(
         OrderedDict({None: "test"}), merger.raw_settings["DF_MODULE_NAME"]
     )
     self.assertEqual(
         OrderedDict([("{'X': 1}", 1), ("{'X': 2}", 2)]), merger.raw_settings["X"]
     )
 def test_merge(self):
     merger = SettingMerger(
         None,
         [DictProvider({
             "X": RawValue("A{Y}C"),
             "Y": "B"
         })],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     merger.process()
     self.assertEqual({
         "X": "A{Y}C",
         "DF_MODULE_NAME": "test",
         "Y": "B"
     }, merger.settings)
 def test_no_interpolation(self):
     merger = SettingMerger(
         None,
         [DictProvider({
             "X": "A{{Y}}C",
             "Y": "B"
         })],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     merger.process()
     self.assertEqual({
         "X": "A{Y}C",
         "DF_MODULE_NAME": "test",
         "Y": "B"
     }, merger.settings)
예제 #10
0
 def test_parse(self):
     merger = SettingMerger(
         None,
         [DictProvider({
             "X": 1,
             "Y": "x{X}"
         }), DictProvider({"X": 2})],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     merger.process()
     self.assertEqual({
         "X": 2,
         "Y": "x2",
         "DF_MODULE_NAME": "test"
     }, merger.settings)
예제 #11
0
 def test_merge(self):
     merger = SettingMerger(
         None,
         [DictProvider({
             "X": SettingReference("Y"),
             "Y": 42,
             "Z": "{Y}"
         })],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     merger.process()
     self.assertEqual(
         {
             "X": 42,
             "DF_MODULE_NAME": "test",
             "Y": 42,
             "Z": "42"
         }, merger.settings)
예제 #12
0
 def test_loop(self):
     merger = SettingMerger(
         None,
         [DictProvider({
             "X": "{Y}",
             "Y": "{Z}",
             "Z": "{X}"
         })],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     self.assertRaises(ValueError, merger.process)
예제 #13
0
 def test_merge(self):
     merger = SettingMerger(
         None,
         [
             DictProvider({
                 "A": [1, ExpandIterable("X"), 4, "{Y}"],
                 "X": [2, 3, "{Y}"],
                 "Y": 32,
             })
         ],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     merger.process()
     self.assertEqual(
         {
             "A": [1, 2, 3, "32", 4, "32"],
             "X": [2, 3, "32"],
             "Y": 32,
             "DF_MODULE_NAME": "test",
         },
         merger.settings,
     )
예제 #14
0
 def test_merge(self):
     x = CallableSetting(self.callable_config, "A", "Z")
     y = CallableSetting(self.callable_config_with_required)
     merger = SettingMerger(
         None,
         [DictProvider({
             "A": "a",
             "X": x,
             "Y": y,
             "Z": "z"
         })],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     merger.process()
     self.assertEqual(
         {
             "A": "a",
             "X": "az",
             "DF_MODULE_NAME": "test",
             "Y": "az",
             "Z": "z"
         },
         merger.settings,
     )
 def test_merge(self):
     merger = SettingMerger(
         None,
         [
             DictProvider(
                 {
                     "A": [1, ExpandIterable("X"), 4, "{Y}"],
                     "X": [2, 3, "{Y}"],
                     "Y": 32,
                 }
             )
         ],
         extra_values={"DF_MODULE_NAME": "test"},
     )
     merger.process()
     self.assertEqual(
         {
             "A": [1, 2, 3, "32", 4, "32"],
             "X": [2, 3, "32"],
             "Y": 32,
             "DF_MODULE_NAME": "test",
         },
         merger.settings,
     )
예제 #16
0
def get_merger_from_env() -> SettingMerger:
    """ Should be used after set_env(); determine all available settings in this order:

   * djangofloor.defaults
   * {project_name}.defaults (overrides djangofloor.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)

    """
    # required if set_env is not called
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangofloor.conf.settings")
    if "PYCHARM_DJANGO_MANAGE_MODULE" in os.environ:
        # noinspection EmptyAlternationBranch
        pycharm_matcher = re.match(
            r"^([\w_\-.]+)-(\w+)(\.py|\.pyc|)$",
            os.environ["PYCHARM_DJANGO_MANAGE_MODULE"],
        )
        if pycharm_matcher:
            os.environ.setdefault(
                "DF_CONF_NAME", "%s:%s" % pycharm_matcher.groups()[:2]
            )
    os.environ.setdefault("DF_CONF_NAME", "%s:%s" % ("django", "django"))
    module_name, sep, script = os.environ["DF_CONF_NAME"].partition(":")
    module_name = module_name.replace("-", "_")
    if sep != ":":
        script = None

    prefix = os.path.abspath(sys.prefix)
    if prefix == "/usr":
        prefix = ""

    def search_providers(basename, suffix, cls):
        default_ini_filename = "%s/etc/%s/%s.%s" % (
            prefix,
            module_name,
            basename,
            suffix,
        )
        ini_filenames = [default_ini_filename]
        ini_filenames.sort()
        return [cls(x) for x in ini_filenames]

    local_conf_filename = os.path.abspath("local_settings.ini")
    # global_conf_filename = '%s/etc/%s/settings.ini' % (prefix, module_name)

    config_providers = [PythonModuleProvider("djangofloor.conf.defaults")]
    if module_name != "djangofloor":
        config_providers.append(PythonModuleProvider("%s.defaults" % module_name))
        mapping = "%s.iniconf:INI_MAPPING" % module_name
    else:
        mapping = "djangofloor.conf.mapping:INI_MAPPING"
    config_providers += search_providers("settings", "ini", IniConfigProvider)
    config_providers += search_providers("settings", "py", PythonFileProvider)
    if script:
        config_providers += search_providers(script, "ini", IniConfigProvider)
        config_providers += search_providers(script, "py", PythonFileProvider)
    config_providers += [IniConfigProvider(local_conf_filename)]
    config_providers += [PythonFileProvider(os.path.abspath("local_settings.py"))]
    fields_provider = PythonConfigFieldsProvider(mapping)
    extra_values = {"DF_MODULE_NAME": module_name}
    if script:
        extra_values["SCRIPT_NAME"] = script
    else:
        extra_values["SCRIPT_NAME"] = "noscript"
    return SettingMerger(fields_provider, config_providers, extra_values=extra_values)