diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-01-08 09:20:04 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-01-08 11:41:21 +0200 |
commit | 432a88a05bec355aff948cdbfef0bacd9c57db8e (patch) | |
tree | e51cdacf6bb1b0d8bbd4d20807c706e4b3534a4c /templates | |
parent | fbcc54cde9a2c77dc2cfd00ce95c4d2a2eebc243 (diff) | |
download | subplot-432a88a05bec355aff948cdbfef0bacd9c57db8e.tar.gz |
Change: use tera templates for generating Python code
Diffstat (limited to 'templates')
-rw-r--r-- | templates/python.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/templates/python.py b/templates/python.py new file mode 100644 index 0000000..c9d2018 --- /dev/null +++ b/templates/python.py @@ -0,0 +1,77 @@ +############################################################################# +# Functions that implement steps. From {{ functions_filename }}. + +{{ functions }} + + +############################################################################# +# Helper code generated by Subplot. + +import base64 +import logging +import os +import shutil +import tempfile + +# Store context between steps. +class Context: + + def __init__(self): + self._vars = {} + + def get(self, key, default=None): + return self._vars.get(key, default) + + def __getitem__(self, key): + return self._vars[key] + + def __setitem__(self, key, value): + self._vars[key] = value + +# Test data files that were embedded in the source document. Base64 +# encoding is used to allow arbitrary data. +_files = {} +{% for file in files %} +_files['''{{ file.filename }}'''] = '''{{ file.contents | base64 }}''' +{% endfor %} + + +# Retrieve an embedded test data file using filename. +def get_file(filename): + return base64.b64decode(_files[filename]) + +# Check two values for equality and give error if they +def assert_eq(a, b): + assert a == b, 'expected %r == %r' % (a, b) + +# Create a new temporary directory and chdir there. This allows step +# functions to create new files in the current working directory +# without having to be so careful. +_datadir = tempfile.mkdtemp() +print('datadir', _datadir) +os.chdir(_datadir) + +############################################################################# +# Code to implement the scenarios. + +{% for scenario in scenarios %} +###################################### +print('''scenario: {{ scenario.title }}''') +ctx = Context() +{% for step in scenario.steps %} +print(''' step: {{ step.kind | lower }} {{ step.text }}''') +args = {} +{% for part in step.parts %}{% if part.CapturedText is defined -%} +args['''{{ part.CapturedText.name }}'''] = '''{{ part.CapturedText.text }}''' +{% endif -%} +{% endfor -%} +{{ step.function }}(ctx, **args) +{% endfor %} +{% endfor %} + + +############################################################################# +# Clean up temporary directory and report success. + +shutil.rmtree(_datadir) +print('OK, all scenarios finished successfully') |