summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-05-16 10:18:52 +0300
committerLars Wirzenius <liw@liw.fi>2020-05-17 12:11:02 +0300
commit6fe21a9836eb85fbda8cf26bdabc6b56131f79aa (patch)
treec2e50a8a03b9abd7576583e85baef6ca1ad7dd0b
parent8e8a6ee6249b365b3d50c64c442141d704e59f62 (diff)
downloadsubplot-6fe21a9836eb85fbda8cf26bdabc6b56131f79aa.tar.gz
refactor(template.py): make Python template easier to follow
It had become hard for me to follow the actual logic of the Python code running scenarios. This commit changes things so that the logic is clean Python, without any templates, using classes for steps and scenarios. The code that instantiates those classes still needs templates, but is correspondingly simpler and easier to follow.
-rw-r--r--templates/python/template.py119
1 files changed, 87 insertions, 32 deletions
diff --git a/templates/python/template.py b/templates/python/template.py
index 62b1fc9..86396ff 100644
--- a/templates/python/template.py
+++ b/templates/python/template.py
@@ -82,36 +82,91 @@ os.chdir(_datadir)
#############################################################################
# Code to implement the scenarios.
+
+class Step:
+
+ def __init__(self):
+ self._kind = None
+ self._text = None
+ self._args = {}
+ self._function = None
+
+ def set_kind(self, kind):
+ self._kind = kind
+
+ def set_text(self, text):
+ self._text = text
+
+ def set_arg(self, name, value):
+ self._args[name] = value
+
+ def set_function(self, function):
+ self._function = function
+
+ def do(self, ctx):
+ print(' step: {} {}'.format(self._kind, self._text))
+ logging.info(' step: {} {}'.format(self._kind, self._text))
+ self._function(ctx, **self._args)
+
+
+class Scenario:
+
+ def __init__(self):
+ self._title = None
+ self._steps = []
+
+ def get_title(self):
+ return self._title
+
+ def set_title(self, title):
+ self._title = title
+
+ def append_step(self, step):
+ self._steps.append(step)
+
+ def run(self):
+ print('scenario: {}'.format(self._title))
+ logging.info("Scenario: {}".format(self._title))
+
+ scendir = tempfile.mkdtemp(dir=_datadir)
+ os.chdir(scendir)
+
+ ctx = Context()
+ for step in self._steps:
+ step.do(ctx)
+
+
{% for scenario in scenarios %}
######################################
# Scenario: {{ scenario.title }}
-def scenario_{{ loop.index }}():
- title = decode_str('{{ scenario.title | base64 }}')
- print('scenario: {}'.format(title))
- logging.info("Scenario: {}".format(title))
- _scendir = tempfile.mkdtemp(dir=_datadir)
- os.chdir(_scendir)
- ctx = Context()
- {% for step in scenario.steps %}
- # Step: {{ step.text }}
- step = decode_str('{{ step.text | base64 }}')
- print(' step: {{ step.kind | lower }} {}'.format(step))
- logging.info(' step: {{ step.kind | lower }} {}'.format(step))
- args = {}
- {% for part in step.parts %}{% if part.CapturedText is defined -%}
- name = decode_str('{{ part.CapturedText.name | base64 }}')
- text = decode_str('{{ part.CapturedText.text | base64 }}')
- args[name] = text
- {% endif -%}
- {% endfor -%}
- {{ step.function }}(ctx, **args)
+class Scenario_{{ loop.index }}():
+ def __init__(self):
+ self._scenario = Scenario()
+ self._scenario.set_title(decode_str('{{ scenario.title | base64 }}'))
+ {% for step in scenario.steps %}
+ # Step: {{ step.text }}
+ step = Step()
+ step.set_kind('{{ step.kind | lower }}')
+ step.set_text(decode_str('{{ step.text | base64 }}'))
+ step.set_function({{ step.function }})
+ self._scenario.append_step(step)
+ {% for part in step.parts %}{% if part.CapturedText is defined -%}
+ name = decode_str('{{ part.CapturedText.name | base64 }}')
+ text = decode_str('{{ part.CapturedText.text | base64 }}')
+ step.set_arg(name, text)
+ {% endif -%}
+ {% endfor -%}
{% endfor %}
-{% endfor %}
-_scenarios = {
-{% for scenario in scenarios %}
- '{{ scenario.title }}': scenario_{{ loop.index }},
+ def get_title(self):
+ return self._scenario.get_title()
+
+ def run(self):
+ self._scenario.run()
{% endfor %}
+
+_scenarios = { {% for scenario in scenarios %}
+ Scenario_{{ loop.index }}(),{% endfor %}
}
@@ -156,20 +211,20 @@ def main():
logging.info("patterns: {}".format(args.patterns))
if len(args.patterns) == 0:
logging.info("Executing all scenarios")
- funcs = list(_scenarios.values())
- random.shuffle(funcs)
+ todo = list(_scenarios)
+ random.shuffle(todo)
else:
logging.info("Executing requested scenarios only: {}".format(args.patterns))
patterns = [arg.lower() for arg in args.patterns]
- funcs = [
- func
- for title, func in _scenarios.items()
- if any(pattern in title.lower() for pattern in patterns)
+ todo = [
+ scen
+ for scen in _scenarios
+ if any(pattern in scen.get_title().lower() for pattern in patterns)
]
try:
- for func in funcs:
- func()
+ for scen in todo:
+ scen.run()
except Exception as e:
logging.error(str(e), exc_info=True)
if args.save_on_failure: