Commit df0a008c authored by Lucas Bourneuf's avatar Lucas Bourneuf

bugfixes and new test

parent 18784113
......@@ -84,6 +84,7 @@ def _standalone_export_pipeline(scripts:[Script], options:dict={}, default_conte
yield ' return parser'
yield ''
else:
yield '@clitogui.clitogui'
yield 'def cli():'
yield ' parser = argparse.ArgumentParser(description=__doc__)'
yield ' return parser'
......@@ -99,16 +100,17 @@ def _standalone_export_pipeline(scripts:[Script], options:dict={}, default_conte
if script.language == 'python':
for line in script.source_code.splitlines(False):
yield ' ' + line
runon_args = ', '.join(f"{name}={name}" for name, *_ in script.options)
new_context = f"''.join(run_on(context, {runon_args}))"
elif script.language == 'asp':
yield ' context += """\\n' + script.source_code + '"""'
new_context = '"""\\n' + script.source_code + '"""'
elif script.language == 'asp file':
yield f' def run_on(context):'
yield f' with open("{script.source_code}") as fd:'
yield ' return context + "\\n" + fd.read().strip()'
yield r' return context + "\n" + fd.read().strip()'
new_context = f"run_on(context)"
else:
raise ValueError(f"unhandled export of language '{script.language}'")
runon_args = ', '.join(f"{name}={name}" for name, *_ in script.options)
new_context = f"''.join(run_on(context, {runon_args}))"
if not script.erase_context:
new_context = 'context + ' + new_context
yield f' return {new_context}'
......@@ -181,6 +183,7 @@ def argparse_addarg_args_from_option(name:str, option, *, explicit_value=None) -
@wraps(_standalone_export_pipeline)
def standalone_export_pipeline(*args, **kwargs) -> [str]:
"""Return a string of Python code, implementing a standalone program reproducing given pipeline.
Also takes care of formatting it with black, if available.
options -- {option name: bool} indicating whether or not the option
must be exposed as program option.
......@@ -193,3 +196,20 @@ def standalone_export_pipeline(*args, **kwargs) -> [str]:
if black:
return black.format_str(python_code, mode=black.FileMode())
return python_code
@wraps(_standalone_export_pipeline)
def standalone_export_pipeline_without_formatting(*args, **kwargs) -> [str]:
"""Return a string of Python code, implementing a standalone program reproducing given pipeline.
options -- {option name: bool} indicating whether or not the option
must be exposed as program option.
default_context -- the default initial context.
verbosity -- verbosity of the standalone program itself.
"""
gen = _standalone_export_pipeline(*args, **kwargs)
python_code = '\n'.join(gen)
return python_code
standalone_export_pipeline.without_formatting = standalone_export_pipeline_without_formatting
import biseau
from biseau import standalone_export_pipeline
from biseau.module_loader import build_scripts_from_asp_code
......@@ -9,5 +10,36 @@ def test_standalone_export_pipeline():
*build_scripts_from_asp_code(ASP_LINK),
*build_scripts_from_asp_code('link(X,1..3) :- link(X,_).'),
))
expected_lines = []
# assert tuple(standalone_export_pipeline(scripts)) == expected_lines
code = standalone_export_pipeline.without_formatting(scripts)
print('CODE:', code)
env = {}
module = exec(code, env)
# print('ENV:', env) # generetae a lot of warning because of the object printing versions that use outdated code
assert callable(env['run_on'])
assert callable(env['cli'])
assert biseau.__version__ in env['__doc__']
def test_standalone_export_pipeline_with_more_script_format():
scripts = tuple((
biseau.module_loader.build_script_from_json({
'name': 'test-python-1',
'python': 'def run_on(context):\n return "node(a)."\n',
}),
biseau.module_loader.build_script_from_json({
'name': 'test-python-2',
'ASP': 'node(b).',
}),
*build_scripts_from_asp_code('link(X,Y) :- node(X) ; node(Y) ; X<Y.'),
))
final_context = biseau.run(scripts)
print('FINAL CONTEXT:', final_context)
code = standalone_export_pipeline.without_formatting(scripts)
print('\nCODE:', code)
env = {}
module = exec(code, env)
# print('ENV:', env) # generetae a lot of warning because of the object printing versions that use outdated code
assert callable(env['run_on'])
assert callable(env['cli'])
assert biseau.__version__ in env['__doc__']
assert env['run_on']('', args=None) == final_context
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment