Esempio n. 1
0
def _setupNewButler(butler: Butler, outputLocation: ResourcePath, dirExists: bool) -> Butler:
    # Set up the new butler object at the specified location
    if dirExists:
        # Remove the existing table, if the code got this far and this exists
        # clobber must be true
        executionRegistry = outputLocation.join("gen3.sqlite3")
        if executionRegistry.exists():
            executionRegistry.remove()
    else:
        outputLocation.mkdir()

    # Copy the existing butler config, modifying the location of the
    # registry to the specified location.
    # Preserve the root path from the existing butler so things like
    # file data stores continue to look at the old location.
    config = Config(butler._config)
    config["root"] = outputLocation.geturl()
    config["allow_put_of_predefined_dataset"] = True
    config["registry", "db"] = "sqlite:///<butlerRoot>/gen3.sqlite3"

    # Remove any namespace that may be set in main registry.
    config.pop(("registry", "namespace"), None)

    # record the current root of the datastore if it is specified relative
    # to the butler root
    if config.get(("datastore", "root")) == BUTLER_ROOT_TAG and butler._config.configDir is not None:
        config["datastore", "root"] = butler._config.configDir.geturl()
    config["datastore", "trust_get_request"] = True

    # Requires that we use the dimension configuration from the original
    # butler and not use the defaults.
    config = Butler.makeRepo(
        root=outputLocation,
        config=config,
        dimensionConfig=butler.registry.dimensions.dimensionConfig,
        overwrite=True,
        forceConfigRoot=False,
    )

    # Return a newly created butler
    return Butler(config, writeable=True)
Esempio n. 2
0
    def testBasics(self):
        c = Config({"1": 2, "3": 4, "key3": 6, "dict": {"a": 1, "b": 2}})
        pretty = c.ppprint()
        self.assertIn("key3", pretty)
        r = repr(c)
        self.assertIn("key3", r)
        regex = r"^Config\(\{.*\}\)$"
        self.assertRegex(r, regex)
        c2 = eval(r)
        self.assertIn("1", c)
        for n in c.names():
            self.assertEqual(c2[n], c[n])
        self.assertEqual(c, c2)
        s = str(c)
        self.assertIn("\n", s)
        self.assertNotRegex(s, regex)

        self.assertCountEqual(c.keys(), ["1", "3", "key3", "dict"])
        self.assertEqual(list(c), list(c.keys()))
        self.assertEqual(list(c.values()), [c[k] for k in c.keys()])
        self.assertEqual(list(c.items()), [(k, c[k]) for k in c.keys()])

        newKeys = ("key4", ".dict.q", ("dict", "r"), "5")
        oldKeys = ("key3", ".dict.a", ("dict", "b"), "3")
        remainingKey = "1"

        # Check get with existing key
        for k in oldKeys:
            self.assertEqual(c.get(k, "missing"), c[k])

        # Check get, pop with nonexistent key
        for k in newKeys:
            self.assertEqual(c.get(k, "missing"), "missing")
            self.assertEqual(c.pop(k, "missing"), "missing")

        # Check setdefault with existing key
        for k in oldKeys:
            c.setdefault(k, 8)
            self.assertNotEqual(c[k], 8)

        # Check setdefault with nonexistent key (mutates c, adding newKeys)
        for k in newKeys:
            c.setdefault(k, 8)
            self.assertEqual(c[k], 8)

        # Check pop with existing key (mutates c, removing newKeys)
        for k in newKeys:
            v = c[k]
            self.assertEqual(c.pop(k, "missing"), v)

        # Check deletion (mutates c, removing oldKeys)
        for k in ("key3", ".dict.a", ("dict", "b"), "3"):
            self.assertIn(k, c)
            del c[k]
            self.assertNotIn(k, c)

        # Check that `dict` still exists, but is now empty (then remove
        # it, mutatic c)
        self.assertIn("dict", c)
        del c["dict"]

        # Check popitem (mutates c, removing remainingKey)
        v = c[remainingKey]
        self.assertEqual(c.popitem(), (remainingKey, v))

        # Check that c is now empty
        self.assertFalse(c)