Commit 987a44bf authored by Lucas Bourneuf's avatar Lucas Bourneuf

bugfixes + handling more types + new params for the export

parent 564f8480
......@@ -23,8 +23,8 @@ def get_pipeline_options(scripts:[Script]) -> dict:
def _standalone_export_pipeline(scripts:[Script], options:dict={}, default_context:str='',
metarg_outfile:str='out.png', metarg_dotfile:str='',
metarg_dot_prog:str='dot', verbosity:int=0) -> [str]:
metarg_withgui:bool=True, metarg_outfile:str='out.png', metarg_dotfile:str='',
metarg_dot_prog:str='dot', verbosity:int=0, name:str='Standalone script') -> [str]:
"""Yield Python code strings, implementing a standalone program reproducing given pipeline.
options -- {option name: bool} indicating whether or not the option
......@@ -33,7 +33,7 @@ def _standalone_export_pipeline(scripts:[Script], options:dict={}, default_conte
verbosity -- verbosity of the standalone program itself.
option_names = {(idx, name): final_name for idx, (_, name, final_name) in enumerate(option_names_from_options(scripts))}
option_names = {(idx, name): final_name for idx, name, final_name in option_names_from_options(scripts)}
# print('OPTIONS NAMES:', option_names)
# print('RECOGNIZED OPTIONS:', options)
options_used = tuple(
......@@ -41,15 +41,17 @@ def _standalone_export_pipeline(scripts:[Script], options:dict={}, default_conte
if options.get(final_name)
# print('OPTIONS USED:', options_used)
yield '"""Standalone script generated by biseau ' + str(__version__) + ', ' + str( + '"""\n'
yield f'"""{name}'
yield f'Generated by biseau {__version__}, {}.'
yield '\n"""\n'
yield 'import os'
yield 'import argparse'
yield 'from clitogui import clitogui'
if metarg_withgui: yield 'from clitogui import clitogui'
yield 'import clyngor'
yield 'import biseau'
yield ''
if options_used:
yield '@clitogui.clitogui'
if metarg_withgui: yield '@clitogui.clitogui'
yield 'def cli():'
yield ' def existing_file(filepath:str) -> str:'
yield ' """Argparse type, raising an error if given file does not exists"""'
......@@ -84,7 +86,7 @@ def _standalone_export_pipeline(scripts:[Script], options:dict={}, default_conte
yield ' return parser'
yield ''
yield '@clitogui.clitogui'
if metarg_withgui: yield '@clitogui.clitogui'
yield 'def cli():'
yield ' parser = argparse.ArgumentParser(description=__doc__)'
yield ' return parser'
......@@ -129,15 +131,17 @@ def _standalone_export_pipeline(scripts:[Script], options:dict={}, default_conte
yield ' run_on(context, args)'
def option_names_from_options(scripts:[Script]) -> [(str, str, str)]:
"""Yield (script name, option name, unambiguous option name)
def option_names_from_options(scripts:[Script]) -> [(int, str, str)]:
"""Yield (script idx, option name, unambiguous option name)
so that unambiguous option name is deterministic and unique
accross all options of given scripts.
scripts = tuple(scripts)
used_names = set() # set of all available options
conflicting_names = set() # names that will need the script name
script_names = tuple( for script in scripts)
script_indexes = tuple(idx for idx, script in enumerate(scripts))
script_name_doublons = {n: itertools.count(1) for n in script_names if script_names.count(n) > 1}
# define names of scripts (append their index when multiple scripts of same name)
script_names = tuple(
......@@ -153,12 +157,12 @@ def option_names_from_options(scripts:[Script]) -> [(str, str, str)]:
# for each script, ensure the use of proper option name
for script_name, script in zip(script_names, scripts):
for idx, script_name, script in zip(script_indexes, script_names, scripts):
for name, type, default, description in script.options:
final_name = name
if name in conflicting_names:
final_name = f'{script_name}_{name}'
yield script_name, name, final_name
yield idx, name, final_name
def argparse_addarg_args_from_option(name:str, option, *, explicit_value=None) -> [str]:
......@@ -175,8 +179,18 @@ def argparse_addarg_args_from_option(name:str, option, *, explicit_value=None) -
ctype = 'writable_file'
elif isinstance(argtype, (list, tuple)):
ctype = 'elem_in_list((' + ', '.join(map(repr, argtype)) + '))'
elif isinstance(argtype, str):
elif argtype is str:
ctype = 'str'
elif argtype is bool:
ctype = 'bool'
default = 'false' if default else 'true'
return 'add_argument', (f"'--{name}'", f'action="store_{default}"', f'help="{description}"')
elif argtype is int:
ctype = 'int'
elif argtype is float:
ctype = 'float'
raise NotImplementedError(f"Option of type {argtype} cannot be transcripted as widget")
return 'add_argument', (f"'{name}'", f'type={ctype}', f'default={repr(default)}', f'help="{description}"')
......@@ -191,8 +205,7 @@ def standalone_export_pipeline(*args, **kwargs) -> [str]:
verbosity -- verbosity of the standalone program itself.
gen = _standalone_export_pipeline(*args, **kwargs)
python_code = '\n'.join(gen)
python_code = standalone_export_pipeline_without_formatting(*args, **kwargs)
if black:
return black.format_str(python_code, mode=black.FileMode())
return python_code
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