aboutsummaryrefslogtreecommitdiff
path: root/src/ee/fact
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2018-07-17 12:23:58 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2018-07-17 12:23:58 +0200
commita30b71772e7eb831e8d87759172a02e79f9673c4 (patch)
tree365d292e6ce73341055d888dd80f7e45b445beec /src/ee/fact
parentd72247b46519609fb0b373d34bcc5d5939d7b9c3 (diff)
downloadee-python-a30b71772e7eb831e8d87759172a02e79f9673c4.tar.gz
ee-python-a30b71772e7eb831e8d87759172a02e79f9673c4.tar.bz2
ee-python-a30b71772e7eb831e8d87759172a02e79f9673c4.tar.xz
ee-python-a30b71772e7eb831e8d87759172a02e79f9673c4.zip
wip. pcb.
Diffstat (limited to 'src/ee/fact')
-rw-r--r--src/ee/fact/__init__.py50
1 files changed, 25 insertions, 25 deletions
diff --git a/src/ee/fact/__init__.py b/src/ee/fact/__init__.py
index 959e755..1bdef0d 100644
--- a/src/ee/fact/__init__.py
+++ b/src/ee/fact/__init__.py
@@ -1,12 +1,13 @@
-from typing import Optional, Mapping, List
import configparser
+import logging
import os
-from pathlib import Path
from functools import total_ordering
-import logging
+from pathlib import Path
+from typing import MutableMapping, Optional, Mapping, List
logger = logging.getLogger(__name__)
+
@total_ordering
class ObjectType(object):
def __init__(self, name: str):
@@ -14,28 +15,20 @@ class ObjectType(object):
self._fields = []
self._objects = {}
- def __eq__(self, o: object) -> bool:
- other = o # type ObjectType
+ def __eq__(self, o) -> bool:
+ other = o # type: ObjectType
return isinstance(o, ObjectType) and self._name == other._name
def __lt__(self, o: object) -> bool:
if not isinstance(o, ObjectType):
return True
- other = o # type ObjectType
- return (self._name) < (self._name)
+ other = o # type: ObjectType
+ return self._name < other._name
def __hash__(self) -> int:
return self._name.__hash__()
- def by_key(self, key: str) -> "Object":
- try:
- return self._objects[key]
- except ValueError:
- o = Object(self, key, {}, {})
- self._objects[key] = o
- return o
-
@property
def name(self):
return self._name
@@ -54,6 +47,7 @@ class ObjectType(object):
self._fields.append(field)
return len(self._fields) - 1
+
class Object(object):
def __init__(self, ds: "DataSet", ot: ObjectType, key: str):
self._ds = ds
@@ -81,13 +75,14 @@ class Object(object):
def get(self, key: str) -> Optional[str]:
idx = self._ot.index_of(key)
- return self._data[idx]
+ return self._data[idx] if idx < len(self._data) else None
+
class DataSet(object):
def __init__(self, name):
self._name = name
self._object_types = {}
- self._objects_by_type = {} # type: Mapping[str, Mapping[str, Object]]
+ self._objects_by_type = {} # type: MutableMapping[str, Mapping[str, Object]]
self._frozen = False
self._changed = False
@@ -146,6 +141,7 @@ class DataSet(object):
return ds
+
class DataSetManager(object):
def __init__(self, basedir: Path):
self._basedir = Path(basedir)
@@ -181,7 +177,7 @@ class DataSetManager(object):
ini = self._load_ini(o_path)
o = ds.get_object(ot, key)
for k, v in ini.items("values"):
- o.set(k, v)
+ o.set(k, v)
if freeze:
ds.freeze()
@@ -212,11 +208,13 @@ class DataSetManager(object):
ini.add_section("values")
for k in ot.fields:
v = o.get(k)
- ini.set("values", k, v)
+ if v:
+ ini.set("values", k, str(v))
self._store_ini(ini, ot_dir / "{}.ini".format(key))
- def _blank_ini(self):
- return configparser.ConfigParser(interpolation = None)
+ @staticmethod
+ def _blank_ini():
+ return configparser.ConfigParser(interpolation=None)
def _load_ini(self, path: Path):
ini = self._blank_ini()
@@ -224,10 +222,12 @@ class DataSetManager(object):
raise IOError("Could not load ini file: {}".format(path))
return ini
- def _store_ini(self, ini, path):
+ @staticmethod
+ def _store_ini(ini, path):
with open(path, "w") as f:
ini.write(f)
+
class LazyDataSet(object):
def __init__(self, dsm: DataSetManager, name, inputs):
self._dsm = dsm
@@ -235,7 +235,7 @@ class LazyDataSet(object):
self._inputs = inputs
def __enter__(self):
-# logger.info("enter: name={}, inputs={}".format(self._name, self._inputs))
+ # logger.info("enter: name={}, inputs={}".format(self._name, self._inputs))
ds = DataSet(self._name)
for name in self._inputs:
ds = ds.merge(self._dsm.load(name, freeze=True))
@@ -243,7 +243,7 @@ class LazyDataSet(object):
return self._ds
def __exit__(self, *args):
-# logger.info("exit: name={}, inputs={}".format(self._name, self._inputs))
-# logger.info("ds.size={}".format(len(self._ds.items())))
+ # logger.info("exit: name={}, inputs={}".format(self._name, self._inputs))
+ # logger.info("ds.size={}".format(len(self._ds.items())))
self._dsm.store(self._ds)
return False