aboutsummaryrefslogtreecommitdiff
path: root/src/ee/fact
diff options
context:
space:
mode:
Diffstat (limited to 'src/ee/fact')
-rw-r--r--src/ee/fact/__init__.py49
-rw-r--r--src/ee/fact/doit.py14
2 files changed, 63 insertions, 0 deletions
diff --git a/src/ee/fact/__init__.py b/src/ee/fact/__init__.py
new file mode 100644
index 0000000..5c6d789
--- /dev/null
+++ b/src/ee/fact/__init__.py
@@ -0,0 +1,49 @@
+class Fact(object):
+ def __init__(self, kind):
+ self._kind = kind
+ self._domains = {}
+
+ @property
+ def kind() -> str:
+ return self._kind
+
+ def attr(self, domain: str, key: str):
+ try:
+ return self._domains[domain][key]
+ except KeyError:
+ pass
+
+ def set(self, domain: str, key: str, value: str):
+ _kv(domain)[key] = value
+
+ def _kv(self, domain: str):
+ try:
+ kv = self._domains[domain]
+ except KeyError:
+ kv = {}
+ self._domains[domain] = kv
+
+ return kv
+
+ def view(self, domain: str):
+ return FactKv(self, domain, self._kv(domain))
+
+ 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)
+
+ ini.write(output)
+
+
+class FactKv(object):
+ def __init__(self, fact, domain, kv):
+ self._fact = fact
+ self._domain = domain
+ self._kv = kv
+
+ def set(self, key, value):
+ self._kv[key] = value
diff --git a/src/ee/fact/doit.py b/src/ee/fact/doit.py
new file mode 100644
index 0000000..f922cf1
--- /dev/null
+++ b/src/ee/fact/doit.py
@@ -0,0 +1,14 @@
+
+
+def uptodate_if_isdir(path):
+ def do_check():
+ return os.path.isdir(path)
+ return do_check
+
+def uptodate_if_older_than(dependency, *targets):
+ def do_check():
+ dep_mtime = os.stat(dependency).st_mtime
+ mtimes = [os.stat(t).st_mtime for t in targets if os.path.isfile(t)]
+ target_mtime = max(mtimes) if mtimes else 0
+ return dep_mtime < target_mtime
+ return do_check