aboutsummaryrefslogtreecommitdiff
path: root/src/ee/fact/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/ee/fact/__init__.py')
-rw-r--r--src/ee/fact/__init__.py92
1 files changed, 55 insertions, 37 deletions
diff --git a/src/ee/fact/__init__.py b/src/ee/fact/__init__.py
index 5c6d789..00035a6 100644
--- a/src/ee/fact/__init__.py
+++ b/src/ee/fact/__init__.py
@@ -1,49 +1,67 @@
-class Fact(object):
- def __init__(self, kind):
- self._kind = kind
- self._domains = {}
+from typing import Optional
+import os.path
+import configparser
- @property
- def kind() -> str:
- return self._kind
+class ObjectDescriptor(object):
+ def __init__(self):
+ self._keys = []
- def attr(self, domain: str, key: str):
+ def index_of(self, key, create: bool = False) -> int:
try:
- return self._domains[domain][key]
- except KeyError:
- pass
+ return self._keys.index(key)
+ except ValueError as e:
+ if not create:
+ raise e
- def set(self, domain: str, key: str, value: str):
- _kv(domain)[key] = value
+ self._keys.append(key)
+ return len(self._keys) - 1
- def _kv(self, domain: str):
- try:
- kv = self._domains[domain]
- except KeyError:
- kv = {}
- self._domains[domain] = kv
+ @property
+ def keys(self):
+ return self._keys
+
+class Object(object):
+ def __init__(self, key, descriptor):
+ self._key = key
+ self._descriptor = descriptor
+ self._data = []
+
+ @property
+ def key(self):
+ return self._key
+
+ def set(self, key: str, value: str):
+ idx = self._descriptor.index_of(key, create=True)
+ self._data.insert(idx, value)
- return kv
+ def get(self, key: str) -> Optional[str]:
+ return self._data[self._descriptor.index_of(key)]
- def view(self, domain: str):
- return FactKv(self, domain, self._kv(domain))
+class ObjectSet(object):
+ def __init__(self, meta = dict()):
+ self._objects = {}
+ self._meta = meta
+ self._descriptor = ObjectDescriptor()
- def write(self, output):
- import configparser
- ini = configparser.ConfigParser(interpolation = None)
- for domain, kv in self._domains.items():
- ini.add_section(domain)
- for key, value in kv.items():
- ini.set(domain, key, value)
+ def create_object(self, key: str):
+ o = Object(key, self._descriptor)
+ self._objects[key] = o
+ return o
- ini.write(output)
+ def read(self, path):
+ print("Reading objects from {}".format(path))
+ pass
+ def write(self, path):
+ print("Writing {} objects".format(len(self._objects)))
-class FactKv(object):
- def __init__(self, fact, domain, kv):
- self._fact = fact
- self._domain = domain
- self._kv = kv
+ for o in self._objects.values():
+ ini = configparser.ConfigParser(interpolation = None)
+ ini.add_section("values")
+ for key in sorted(self._descriptor.keys):
+ value = o.get(key)
+ if value:
+ ini.set("values", key, value)
- def set(self, key, value):
- self._kv[key] = value
+ with open(os.path.join(path, "{}.ini".format(o.key)), "w") as f:
+ ini.write(f)