diff options
Diffstat (limited to 'src/ee/fact')
-rw-r--r-- | src/ee/fact/__init__.py | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/src/ee/fact/__init__.py b/src/ee/fact/__init__.py index 00035a6..659558f 100644 --- a/src/ee/fact/__init__.py +++ b/src/ee/fact/__init__.py @@ -1,4 +1,4 @@ -from typing import Optional +from typing import Optional, Mapping import os.path import configparser @@ -34,6 +34,10 @@ class Object(object): idx = self._descriptor.index_of(key, create=True) self._data.insert(idx, value) + def merge(self, kv: Mapping[str, str]): + for k, v in kv.items(): + self.set(k, v) + def get(self, key: str) -> Optional[str]: return self._data[self._descriptor.index_of(key)] @@ -43,20 +47,46 @@ class ObjectSet(object): self._meta = meta self._descriptor = ObjectDescriptor() + def items(self): + return self._objects.values() + def create_object(self, key: str): + if key in self._objects: + raise ValueError("Object already exists: {}".format(key)) o = Object(key, self._descriptor) self._objects[key] = o return o def read(self, path): + from pathlib import Path print("Reading objects from {}".format(path)) - pass + for p in Path(path).glob("*.ini"): + if p.name == "object-set.ini": + continue + + with open(p, "r") as f: + ini = configparser.ConfigParser(interpolation = None) + ini.read(p) + + key = ini.get("meta", "key") + + o = self.create_object(key) + o.merge({k:v for k, v in ini.items("values")}) + print("Read {} objects".format(len(self._objects))) def write(self, path): print("Writing {} objects".format(len(self._objects))) + ini = configparser.ConfigParser(interpolation = None) + ini.add_section("object-set") + with open(os.path.join(path, "object-set.ini"), "w") as f: + ini.write(f) + for o in self._objects.values(): ini = configparser.ConfigParser(interpolation = None) + ini.add_section("meta") + ini.set("meta", "key", o.key) + ini.add_section("values") for key in sorted(self._descriptor.keys): value = o.get(key) |