summaryrefslogtreecommitdiff
path: root/qface/generator.py
diff options
context:
space:
mode:
Diffstat (limited to 'qface/generator.py')
-rw-r--r--qface/generator.py21
1 files changed, 17 insertions, 4 deletions
diff --git a/qface/generator.py b/qface/generator.py
index ac1baeb..ef02062 100644
--- a/qface/generator.py
+++ b/qface/generator.py
@@ -156,16 +156,18 @@ class Generator(object):
class RuleGenerator(Generator):
"""Generates documents based on a rule YAML document"""
- def __init__(self, search_path: str, destination: Path, context: dict= {}):
+ def __init__(self, search_path: str, destination:Path, context:dict={}, features:set=set()):
super().__init__(search_path, context)
self.context.update({
'dst': destination,
'project': Path(destination).name,
})
self.destination = '{{dst}}'
+ self.features = features
- def process_rules(self, path: Path, system: System):
+ def process_rules(self, path: Path, system: System, features:set=set()):
"""writes the templates read from the rules document"""
+ self.features.update(features)
self.context.update({'system': system})
document = FileSystem.load_yaml(path, required=True)
for module, rules in document.items():
@@ -174,6 +176,8 @@ class RuleGenerator(Generator):
def _process_rules(self, rules: dict, system: System):
""" process a set of rules for a target """
+ if not self._shall_proceed(rules):
+ return
self.context.update(rules.get('context', {}))
self.destination = rules.get('destination', '{{dst}}')
self._process_rule(rules.get('system', None), {'system': system})
@@ -188,7 +192,7 @@ class RuleGenerator(Generator):
def _process_rule(self, rule: dict, context: dict):
""" process a single rule """
- if not rule:
+ if not rule or not self._shall_proceed(rule):
return
self.context.update(context)
self.context.update(rule.get('context', {}))
@@ -198,6 +202,15 @@ class RuleGenerator(Generator):
preserve = target in preserved
self.write(target, source, preserve=preserve)
+ def _shall_proceed(self, obj):
+ conditions = obj.get('when', [])
+ if not conditions:
+ return True
+ if not isinstance(conditions, list):
+ conditions = [conditions]
+ result = self.features.intersection(set(conditions))
+ return bool(len(result))
+
class FileSystem(object):
"""QFace helper functions to work with the file system"""
@@ -302,5 +315,5 @@ class FileSystem(object):
try:
return yaml.load(document.text(), Loader=Loader)
except yaml.YAMLError as exc:
- click.secho(exc, fg='red')
+ click.secho(str(exc), fg='red')
return {}