def test_operator(self): self.assertFalse(self.ks == kdb.KeySet(0)) self.assertEqual(len(self.ks), 4) self.assertEqual(len(kdb.KeySet(0)), 0) self.assertTrue(kdb.Key("user:/key3") in self.ks) self.assertFalse(kdb.Key("user:/foo") in self.ks) self.assertEqual(self.ks[0], kdb.Key("user:/key3")) self.assertEqual(self.ks[-1], kdb.Key("system:/key2")) with self.assertRaises(IndexError): self.assertIsNone(self.ks[100]) with self.assertRaises(IndexError): self.assertIsNone(self.ks[-100]) self.assertEqual(self.ks[1:3], [ self.ks[1], self.ks[2] ]) self.assertEqual(self.ks["user:/key3"], kdb.Key("user:/key3")) with self.assertRaises(KeyError): self.assertIsNone(self.ks["user:/doesnt_exist"]) self.assertEqual(self.ks[kdb.Key("system:/key2")], kdb.Key("system:/key2")) with self.assertRaises(KeyError): self.ks[kdb.Key("user:/doesnt_exist")] self.assertEqual(str(self.ks), "['user:/key3', 'user:/key4', 'system:/key1', 'system:/key2']") self.assertEqual(repr(self.ks), "kdb.KeySet(4, kdb.Key('user:/key3'), kdb.Key('user:/key4'), kdb.Key('system:/key1'), kdb.Key('system:/key2'))") self.assertIsInstance(hash(self.ks[0]), int) self.assertTrue(self.ks[0].isNameLocked()) self.assertFalse(self.ks[0].isValueLocked()) self.assertFalse(self.ks[0].isMetaLocked())
def ls(os_path): path = os_path_to_elektra_path(os_path) root = kdb.Key(path) is_root_level = len(path) > 1 and path.endswith("/") # special case with _get_kdb_instance() as db: ks = kdb.KeySet() db.get(ks, root) #only retain keys that are below the root (kdb.get does not gurantee this property) ks_filtered = kdb.KeySet() for key in ks: if key.isBelowOrSame(root): ks_filtered.append(key) path_without_namespace = _remove_namespace_prefix(path) result_keys_without_namespace = map(_remove_namespace_prefix, ks_filtered.unpack_names()) below = {name.split(path_without_namespace)[1] for name in result_keys_without_namespace if is_path_prefix(path_without_namespace, name)} dirs = {name.split("/")[0 if is_root_level else 1] for name in below if "/" in name} files = {name for name in below if not "/" in name}.difference(dirs) if '' in files: files.remove('') files.add(dir_file_special_name) return (dirs, files)
def test_ctor(self): self.assertIsInstance(self.ks, kdb.KeySet) ks = kdb.KeySet(0) self.assertIsInstance(ks, kdb.KeySet) ks = kdb.KeySet(self.ks) self.assertIsInstance(ks, kdb.KeySet)
def test_functions(self): self.assertEqual(self.ks.lookup("user/key3"), kdb.Key("user/key3")) self.assertEqual(self.ks.lookup(kdb.Key("system/key2")), kdb.Key("system/key2")) self.assertEqual(self.ks.lookup(0), kdb.Key("system/key1")) self.assertEqual(self.ks.lookup(-1), kdb.Key("user/key4")) ks = kdb.KeySet(0) ks.append(kdb.Key("user/foo")) ks.append(kdb.Key("user/bar"), kdb.Key("user/bar2")) ks.append("user/nice") ks.extend([kdb.Key("user/baz"), kdb.Key("user/baz2")]) self.assertEqual(len(ks), 6) ' test clear ' ks = kdb.KeySet(0) ks.append(kdb.Key("user/test1")) ks.append(kdb.Key("user/test2")) ks.append(kdb.Key("user/test3")) ks.clear() self.assertEqual(len(ks), 0) ' test pop ' ks = kdb.KeySet(0) ks.append(kdb.Key("user/test1")) ks.append(kdb.Key("user/test2")) ks.append(kdb.Key("user/test3")) self.assertEqual(ks.pop(), kdb.Key("user/test3")) self.assertEqual(len(ks), 2) ' test cut ' ks = kdb.KeySet(0) ks.append(kdb.Key("user/level11")) ks.append(kdb.Key("user/level13")) ks.append(kdb.Key("user/level13/level21")) ks.append(kdb.Key("user/level13/level22/level31")) ks.append(kdb.Key("user/level13/level23")) ks.append(kdb.Key("user/level15")) cutresult = ks.cut(kdb.Key("user/level13")) self.assertEqual(len(cutresult), 4) self.assertTrue(kdb.Key("user/level13") in cutresult) self.assertTrue(kdb.Key("user/level13/level21") in cutresult) self.assertTrue(kdb.Key("user/level13/level22/level31") in cutresult) self.assertTrue(kdb.Key("user/level13/level23") in cutresult) self.assertEqual(len(ks), 2) self.assertTrue(kdb.Key("user/level11") in ks) self.assertTrue(kdb.Key("user/level15") in ks) cutresult = ks.cut(kdb.Key("user/does/not/exist")) self.assertEqual(len(cutresult), 0) self.assertEqual(len(ks), 2)
def test_ctor(self): self.assertIsInstance(self.ks, kdb.KeySet) ks = kdb.KeySet(0) self.assertIsInstance(ks, kdb.KeySet) ks = kdb.KeySet(self.ks) self.assertIsInstance(ks, kdb.KeySet) ks = kdb.KeySet(self.ks.dup()) self.assertIsInstance(ks, kdb.KeySet) self.assertEqual(len(ks), len(self.ks)) ks.pop() self.assertEqual(len(ks), len(self.ks) - 1)
def set_key(self, key): if not key.HasField('name'): return False name = str(key.name) value = None if key.type == PARAM_STRING: if not key.HasField('paramstring'): return False value = str(key.paramstring) elif key.type == PARAM_BINARY: if not key.HasField('parambinary'): return False value = str(key.parambinary) elif key.type == PARAM_INTEGER: if not key.HasField('paramint'): return False value = str(key.paramint) elif key.type == PARAM_DOUBLE: if not key.HasField('paramdouble'): return False value = str(key.paramdouble) elif key.type == PARAM_DOUBLE: if not key.HasField('paramlist'): return False return False # TODO: implement for s in self.subscriptions: if name.startswith(s): with kdb.KDB() as db: ks = kdb.KeySet() db.get(ks, s) ks[name].string = value db.set(ks, s) return True return False
def test_get(self): with kdb.KDB() as db: ks = kdb.KeySet() db.get(ks, "system/elektra") key = ks["system/elektra/version/constants/KDB_VERSION"] self.assertEqual(key.value, kdb.VERSION)
def test_properties(self): config = kdb.KeySet(1, kdb.Key("system/module", "without config")) spec = kdb.tools.PluginSpec("somename", config) self.assertEqual(spec.name, "somename") self.assertEqual(spec.fullname, "somename#somename") self.assertEqual(spec.refName, "somename") self.assertEqual(spec.config, config) self.assertFalse(spec.refNumber) spec.name = "other" self.assertEqual(spec.name, "other") spec.refName = "myref" self.assertEqual(spec.refName, "myref") spec.fullname = "fullname" self.assertEqual(spec.fullname, "fullname#myref") spec.refNumber = 5 self.assertTrue(spec.refNumber) config.append(kdb.Key("system/modules/2")) self.assertNotEqual(spec.config, config) spec.config = config self.assertEqual(spec.config, config) with self.assertRaises(kdb.tools.BadPluginName): spec.fullname = "this is invalid"
def tearDownClass(cls): # cleanup with kdb.KDB() as db: ks = kdb.KeySet(100) db.get(ks, TEST_NS) ks.cut(kdb.Key(TEST_NS)) db.set(ks, TEST_NS)
def complete_long_options(): global start_of_current_input, mount_point, last_command completion_options = [] k = kdb.KDB() ks = kdb.KeySet() k.get(ks, mount_point) word = start_of_current_input.strip() while word.startswith('-'): word = word[1:] for key in ks: if last_command and last_command != str(key): continue long_opt = key.getMeta(name='opt/long') opt = key.getMeta(name='opt') if long_opt: if long_opt.value.startswith(word): completion_options.append('--' + long_opt.value) elif opt and opt.value.startswith('#'): try: len_opts = int((opt.value)[1:]) except ValueError: continue for i in range(len_opts+1): long_opt = None long_opt = key.getMeta(name='opt/#{}/long'.format(i)) if long_opt and long_opt.value.startswith(word): completion_options.append('--' + long_opt.value) k.close() return completion_options
def elektraMount(mountpoint, filename, resolver, plugins, recommends): with kdb.KDB() as db: ks = kdb.KeySet(0) mountpoints = "system/elektra/mountpoints" rc = 0 try: rc = db.get(ks, mountpoints) except kdb.KDBException as e: raise ElektraReadException("KDB.get failed: {}".format(e)) if rc == -1: raise ElektraMountException( "Failed to fetch elektra facts: failed to read system/elektra/mountpoints." ) searchKey = mountpoints + '/' + mountpoint.replace('/', '\/') try: key = ks[searchKey] return (0, True) except KeyError: command = [] command.append("kdb") command.append("mount") if recommends: command.append("--with-recommends") command.append("-R") command.append(resolver) command.append(filename) command.append(mountpoint) for item in plugins: for cname, cvalue in item.items(): command.append(str(cname)) # plugin if isinstance(cvalue, dict): # iterate plugin meta for scname, scvalue in cvalue.items(): command.append(str(scname) + "=" + str(scvalue)) return execute(command)
def test_helpers(self): self.assertEqual( self.ks.unpack_names(), set(['system:/key1', 'system:/key2', 'user:/key3', 'user:/key4'])) self.assertEqual(self.ks.unpack_basenames(), set(['key1', 'key2', 'key3', 'key4'])) self.assertEqual(self.ks.filter_below(kdb.Key('user:/')), kdb.KeySet(2, self.ks[0], self.ks[1]))
def test_python_head(self): ks = kdb.KeySet(0) ks.append(kdb.Key("user:/foo")) self.assertEqual(ks.head(), ks[0]) ks.append(kdb.Key("user:/bar")) self.assertEqual(ks.head(), ks[0]) ks.remove("user:/foo") self.assertEqual(ks.head(), ks[0])
def setUp(self): self.parent_key = kdb.Key("user:/python") self.plugin = dns_plugin.ElektraPlugin() self.localhost_key_with_plugin = kdb.Key("user:/python/hostname", kdb.KEY_VALUE, "localhost", kdb.KEY_META, "check/dns", "") self.valid_key_with_plugin = kdb.Key("user:/python/hostname", kdb.KEY_VALUE, VALID_DOMAIN, kdb.KEY_META, "check/dns", "") self.invalid_key_with_plugin = kdb.Key("user:/python/hostname", kdb.KEY_VALUE, INVALID_DOMAIN, kdb.KEY_META, "check/dns", "") self.valid_key_without_plugin = kdb.Key("user:/foo/bar", kdb.KEY_VALUE, "val") self.invalid_ks = kdb.KeySet(10, self.invalid_key_with_plugin, self.valid_key_without_plugin, kdb.KS_END) self.valid_ks = kdb.KeySet(10, self.valid_key_with_plugin, self.valid_key_without_plugin, kdb.KS_END) self.localhost_ks = kdb.KeySet(10, self.localhost_key_with_plugin, kdb.KS_END)
def get_key_and_keyset(os_path): path = os_path_to_elektra_path(os_path) with _get_kdb_instance() as db: ks = kdb.KeySet() db.get(ks, path) key = ks[path] return (key, ks)
def test_ensure(self): with kdb.KDB() as db: ks = kdb.KeySet() k = kdb.Key("system:/elektra/ensure/plugins/global/gopts", kdb.KEY_VALUE, "mounted") parent = kdb.Key("system:/elektra") rc = db.ensure(ks, parent) self.assertEqual(rc, 0) db.get(ks, parent)
def test_set(self): with kdb.KDB() as db: ks = kdb.KeySet(100) db.get(ks, TEST_NS) try: key = ks[TEST_NS + "/mykey"] except KeyError: key = kdb.Key(TEST_NS + "/mykey") ks.append(key) key.value = "new_value" db.set(ks, TEST_NS) with kdb.KDB() as db: ks = kdb.KeySet(100) db.get(ks, TEST_NS) self.assertEqual(ks[TEST_NS + "/mykey"].value, "new_value")
def setUp(self): self.ks = kdb.KeySet(100, kdb.Key("system:/key1"), kdb.Key("system:/key2"), kdb.Key("user:/key3"), kdb.Key("user:/key4"), kdb.KS_END, kdb.Key("user:/lost") )
def test_get(self): with kdb.KDB() as db: ks = kdb.KeySet() db.get(ks, "system/elektra") import os if os.getenv("CHECK_VERSION") is None: key = ks["system/elektra/version/constants/KDB_VERSION"] self.assertEqual(key.value, kdb.VERSION)
def full_update(self, basekey): with kdb.KDB() as db: ks = kdb.KeySet() db.get(ks, basekey) for k in ks: key = self.tx.key.add() self.convert_key(key, k) self.add_pparams() self.send_param_msg(basekey, MT_PARAM_FULL_UPDATE)
def setupSpec(): with kdb.KDB() as db: ks = kdb.KeySet(10) db.get(ks, spec_base_key) if len(ks.cut(kdb.Key(spec_base_key))) > 0: print("ERROR: Couldn't setup spec, keys exist!", file=sys.stderr) exit(1) ks.extend(spec) db.set(ks, spec_base_key)
def create_key(os_path): path = os_path_to_elektra_path(os_path) with _get_kdb_instance() as db: ks = kdb.KeySet() db.get(ks, path) if not path in ks: key = kdb.Key(path) ks.append(key) keys_modified = db.set(ks, path) if keys_modified != 1: raise OSError(errno.EIO)
def test_load(self): modules = kdb.tools.Modules() config = kdb.KeySet(1, kdb.Key("system:/module", "without config")) spec = kdb.tools.PluginSpec("dump", config) plugin = modules.load(spec) self.assertIsInstance(plugin, kdb.tools.Plugin) self.assertEqual(plugin.name, "dump") self.assertEqual(config, plugin.getConfig()) with self.assertRaises(kdb.tools.NoPlugin): modules.load(kdb.tools.PluginSpec("does_not_exist"))
def _get_kdb_instance(): config = kdb.KeySet(0) contract = kdb.KeySet(0) custom_envp = [ "%s=%s" % (k, v) for (k, v) in os.environ.items() ] kdb.goptsContract (contract, sys.argv, custom_envp, parent_key, config) db = kdb.KDB(contract) #monkey patch db.get as #- proc:/ keys are only available through a cascading lookup (See manpage elektra-namespaces: "Keys in the namespace proc ... are ignored by kdbGet ... ") #- we don't want spec: keys to appear in the cascading namespace orig_get = db.get def patched_get(ks, orig_root): justified_root = re.sub("^proc:/", "/", str(orig_root)) status = orig_get(ks, justified_root) if kdb.Key(orig_root).isCascading(): for key_to_remove in ks.filter(lambda key: key.isSpec()): ks.remove(key_to_remove) return status db.get = patched_get return db
def test_spec_reader(self): spec = kdb.KeySet( 1, kdb.Key("spec:/example/xx", {"mountpoint": "/example/xx"})) sr = kdb.tools.SpecReader() sr.readSpecification(spec) backends = sr.getBackends() self.assertIsInstance(backends, kdb.tools.SpecBackendBuilderMap) self.assertEqual(len(backends), 1) self.assertIsInstance(backends[spec[0]], kdb.tools.SpecBackendBuilder) self.assertEqual(backends.keys(), [k for k in spec]) self.assertIsInstance(backends.values()[0], kdb.tools.SpecBackendBuilder)
def incremental_update(self, key, basekey='', delete=False): if not delete: with kdb.KDB() as db: ks = kdb.KeySet() db.get(ks, basekey) k = ks[key] paramKey = self.tx.key.add() self.convert_key(paramKey, k) else: paramKey = self.tx.key.add() paramKey.name = key paramKey.deleted = True self.send_param_msg(basekey, MT_PARAM_INCREMENTAL_UPDATE)
def test_gopts(self): try: setupSpec() custom_argv = ["test", "get", "-v", "user:/"] custom_envp = [] config = kdb.KeySet(0) contract = kdb.KeySet(0) kdb.goptsContract(contract, custom_argv, custom_envp, kdb.Key(base_key), config) with kdb.KDB(contract) as db: ks = kdb.KeySet(0) db.get(ks, base_key) self.assertTrue((base_key in ks)) self.assertEqual(ks[base_key].value, "getter") self.assertEqual(ks[base_key + "/getter/keyname"].value, "user:/") self.assertTrue((base_key + "/getter/verbose" in ks)) self.assertEqual(ks[base_key + "/getter/verbose"].value, "1") finally: removeSpec()
def elektraSet(mountpoint, keyset, keeporder): with kdb.KDB() as db: ks = kdb.KeySet(0) rc = 0 try: rc = db.get(ks, mountpoint) except kdb.KDBException as e: raise ElektraReadException("KDB.get failed: {}".format(e)) if rc == -1: raise ElektraReadException( "Failed to read keyset below {}".format(mountpoint)) flattenedKeyset = flatten_json(keyset) i = 0 for name, value in flattenedKeyset.items(): key = None kname = None try: key = ks[mountpoint + "/" + name] if keeporder and key.hasMeta("order"): i = int((key.getMeta("order").value)) + 1 if keeporder: key.setMeta("order", str(i)) i += 1 except KeyError: key = kdb.Key(mountpoint + "/" + name) if keeporder: key.setMeta("order", str(i)) i += 1 ks.append(key) if isinstance(value, dict): for sname, svalue in value.items(): if sname == 'value': key.value = svalue elif sname == 'meta': for mname, mvalue in svalue.items(): key.setMeta(mname, str(mvalue)) else: key.value = value try: rc = db.set(ks, mountpoint) except kdb.KDBException as e: raise ElektraWriteException("KDB.set failed: {}".format(e)) if rc == -1: raise ElektraWriteException( "Failed to write keyset to {}".format(mountpoint)) return rc
def complete_commands(start_of_input): global mount_point, root with kdb.KDB() as k: ks = kdb.KeySet() k.get(ks, mount_point) len_path_mount_point = len(mount_point.split('/')) completion_commands = [] for key in ks: path = str(key).split('/') if len(path) <= len_path_mount_point: continue if start_of_input is None: completion_commands.append(path[-1]) elif path[-1].startswith(start_of_input): completion_commands.append(path[-1]) k.close() return completion_commands
def update_meta_map(os_path, new_meta_map): path = os_path_to_elektra_path(os_path) with _get_kdb_instance() as db: ks = kdb.KeySet() db.get(ks, path) key = ks[path] #delete old meta keys for meta_key in key.getMeta(): key.delMeta(meta_key.name) #insert new meta keys for keyname in new_meta_map.keys(): key.setMeta(keyname, new_meta_map[keyname]) db.set(ks, path)