Example #1
0
    def test_20_dump_and_multi_load(self):
        a = dict(name="a", a=1, b=dict(b=[0, 1], c="C"))
        b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D"))

        a_path = os.path.join(self.workdir, "a.json")
        b_path = os.path.join(self.workdir, "b.json")

        A.dump(a, a_path)
        self.assertTrue(os.path.exists(a_path))

        A.dump(b, b_path)
        self.assertTrue(os.path.exists(b_path))

        a1 = A.multi_load([a_path, b_path], merge=A.MS_DICTS)

        self.assertEquals(a1["name"], a["name"])
        self.assertEquals(a1["a"], b["a"])
        self.assertEquals(a1["b"]["b"], b["b"]["b"])
        self.assertEquals(a1["b"]["c"], a["b"]["c"])
        self.assertEquals(a1["b"]["d"], b["b"]["d"])

        a2 = A.multi_load([a_path, b_path], merge=A.MS_DICTS_AND_LISTS)

        self.assertEquals(a2["name"], a["name"])
        self.assertEquals(a2["a"], b["a"])
        self.assertEquals(a2["b"]["b"], [0, 1, 2, 3, 4, 5])
        self.assertEquals(a2["b"]["c"], a["b"]["c"])
        self.assertEquals(a2["b"]["d"], b["b"]["d"])
Example #2
0
    def test_20_dump_and_multi_load(self):
        obja = dict(name="a", a=1, b=dict(b=[0, 1], c="C"))
        objb = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D"))

        a_path = os.path.join(self.workdir, "a.json")
        b_path = os.path.join(self.workdir, "b.json")

        TT.dump(obja, a_path)
        self.assertTrue(os.path.exists(a_path))

        TT.dump(objb, b_path)
        self.assertTrue(os.path.exists(b_path))

        obja1 = TT.multi_load([a_path, b_path], ac_merge=TT.MS_DICTS)

        self.assertEqual(obja1["name"], obja["name"])
        self.assertEqual(obja1["a"], objb["a"])
        self.assertEqual(obja1["b"]["b"], objb["b"]["b"])
        self.assertEqual(obja1["b"]["c"], obja["b"]["c"])
        self.assertEqual(obja1["b"]["d"], objb["b"]["d"])

        obja2 = TT.multi_load([a_path, b_path], ac_merge=TT.MS_DICTS_AND_LISTS)

        self.assertEqual(obja2["name"], obja["name"])
        self.assertEqual(obja2["a"], objb["a"])
        self.assertEqual(obja2["b"]["b"], [0, 1, 2, 3, 4, 5])
        self.assertEqual(obja2["b"]["c"], obja["b"]["c"])
        self.assertEqual(obja2["b"]["d"], objb["b"]["d"])
Example #3
0
    def test_20_dump_and_multi_load(self):
        obja = dict(name="a", a=1, b=dict(b=[0, 1], c="C"))
        objb = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D"))

        a_path = os.path.join(self.workdir, "a.json")
        b_path = os.path.join(self.workdir, "b.json")

        TT.dump(obja, a_path)
        self.assertTrue(os.path.exists(a_path))

        TT.dump(objb, b_path)
        self.assertTrue(os.path.exists(b_path))

        obja1 = TT.multi_load([a_path, b_path], ac_merge=TT.MS_DICTS)

        self.assertEqual(obja1["name"], obja["name"])
        self.assertEqual(obja1["a"], objb["a"])
        self.assertEqual(obja1["b"]["b"], objb["b"]["b"])
        self.assertEqual(obja1["b"]["c"], obja["b"]["c"])
        self.assertEqual(obja1["b"]["d"], objb["b"]["d"])

        obja2 = TT.multi_load([a_path, b_path], ac_merge=TT.MS_DICTS_AND_LISTS)

        self.assertEqual(obja2["name"], obja["name"])
        self.assertEqual(obja2["a"], objb["a"])
        self.assertEqual(obja2["b"]["b"], [0, 1, 2, 3, 4, 5])
        self.assertEqual(obja2["b"]["c"], obja["b"]["c"])
        self.assertEqual(obja2["b"]["d"], objb["b"]["d"])
Example #4
0
    def test_20_dump_and_multi_load(self):
        a = dict(name="a", a=1, b=dict(b=[0, 1], c="C"))
        b = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d="D"))

        a_path = os.path.join(self.workdir, "a.json")
        b_path = os.path.join(self.workdir, "b.json")

        A.dump(a, a_path)
        self.assertTrue(os.path.exists(a_path))

        A.dump(b, b_path)
        self.assertTrue(os.path.exists(b_path))

        a1 = A.multi_load([a_path, b_path], merge=A.MS_DICTS)

        self.assertEquals(a1["name"],   a["name"])
        self.assertEquals(a1["a"],      b["a"])
        self.assertEquals(a1["b"]["b"], b["b"]["b"])
        self.assertEquals(a1["b"]["c"], a["b"]["c"])
        self.assertEquals(a1["b"]["d"], b["b"]["d"])

        a2 = A.multi_load([a_path, b_path], merge=A.MS_DICTS_AND_LISTS)

        self.assertEquals(a2["name"],   a["name"])
        self.assertEquals(a2["a"],      b["a"])
        self.assertEquals(a2["b"]["b"], [0, 1, 2, 3, 4, 5])
        self.assertEquals(a2["b"]["c"], a["b"]["c"])
        self.assertEquals(a2["b"]["d"], b["b"]["d"])
Example #5
0
    def read(self):
        """
        Parse the configs and validate it.

        It could be either local or from a local services
        (first local then packages by alphabetical order).
        """
        schema = anyconfig.multi_load(self.spec_files)
        config = anyconfig.multi_load(self.config_files)
        # Make sure the compiled configuration is valid
        try:
            anyconfig.validate(config, schema, safe=False)
        except _Error as error:
            self.error = '{} ({})'.format(error.message, ' -> '.join(map(str, error.path)))
            return False

        config['project_dir'] = path.realpath(path.dirname(self.config_file))
        if config['project_name'] == '':
            config['project_name'] = path.basename(config['project_dir'])

        return config
Example #6
0
    def test_20_dump_and_multi_load(self):
        obj_diff = dict(a=2, b=dict(b=[1, 2, 3, 4, 5], d='D'))

        with tempfile.TemporaryDirectory() as tmpdir:
            a_path = pathlib.Path(tmpdir) / 'a.json'
            b_path = pathlib.Path(tmpdir) / 'b.json'

            TT.dump(self.obj, a_path)
            self.assertTrue(a_path.exists())

            TT.dump(obj_diff, b_path)
            self.assertTrue(b_path.exists())

            ref = copy.copy(self.obj)
            obj_1 = TT.multi_load([a_path, b_path], ac_merge=TT.MS_DICTS)
            TT.merge(ref, obj_diff, ac_merge=TT.MS_DICTS)
            self.assertEqual(obj_1, ref)
Example #7
0
    def load_config(self):
        """
        Load configuration from configuration files and environment variables.

        Search order, latest has presedence:

          1. hard coded defaults
          2. `/etc/phabfive.yaml`
          3. `/etc/phabfive.d/*.yaml`
          4. `~/.config/phabfive.yaml`
          5. `~/.config/phabfive.d/*.yaml`
          6. environment variables
        """
        environ = os.environ.copy()

        log.debug("Loading configuration defaults")
        conf = copy.deepcopy(DEFAULTS)

        os.environ["XDG_CONFIG_DIRS"] = "/etc"

        site_conf_file = os.path.join(
            appdirs.site_config_dir("phabfive") + ".yaml")
        log.debug("Loading configuration file: {}".format(site_conf_file))
        anyconfig.merge(
            conf,
            {
                k: v
                for k, v in dict(
                    anyconfig.load(site_conf_file, ac_ignore_missing=True)).
                items() if k in CONFIGURABLES
            },
        )

        site_conf_dir = os.path.join(
            appdirs.site_config_dir("phabfive") + ".d", "*.yaml")
        log.debug("Loading configuration files: {}".format(site_conf_dir))
        anyconfig.merge(
            conf,
            {
                k: v
                for k, v in dict(anyconfig.multi_load(site_conf_dir)).items()
                if k in CONFIGURABLES
            },
        )

        user_conf_file = os.path.join(
            appdirs.user_config_dir("phabfive")) + ".yaml"
        log.debug("Loading configuration file: {}".format(user_conf_file))
        anyconfig.merge(
            conf,
            {
                k: v
                for k, v in dict(
                    anyconfig.load(user_conf_file, ac_ignore_missing=True)).
                items() if k in CONFIGURABLES
            },
        )

        user_conf_dir = os.path.join(
            appdirs.user_config_dir("phabfive") + ".d", "*.yaml")
        log.debug("Loading configuration files: {}".format(user_conf_dir))
        anyconfig.merge(
            conf,
            {
                k: v
                for k, v in dict(anyconfig.multi_load(user_conf_dir)).items()
                if k in CONFIGURABLES
            },
        )

        log.debug("Loading configuration from environment")
        anyconfig.merge(
            conf, {k: v
                   for k, v in environ.items() if k in CONFIGURABLES})

        return conf
Example #8
0
 def _load_config(dir, file_name, file_suffix):
     """Loads the project configuration file."""
     project_dir_path = Path(dir)
     project_file = project_dir_path / (file_name + file_suffix)
     project_secret_file = project_dir_path / (file_name + '.secret' + file_suffix)
     return anyconfig.multi_load([project_file, project_secret_file], ac_ignore_missing=True)