Commit 899821ea authored by Lucas Bourneuf's avatar Lucas Bourneuf

works for #3 on option types

parent 4cbf4c71
......@@ -21,8 +21,8 @@ from biseau.script import Script, Module
DEFAULT_DOC = 'NO SCRIPT DOC PROVIDED.\nFix this by writing a module documentation inside script definition.'
RETURNS_TYPES = {iter, str}
OPTIONS_TYPES = {int, float, bool, str, list, open, partial}
TYPE_DEFAULT = {int: 0, float: 0., bool: False, str: '', list: (), open: None, partial: None}
OPTIONS_TYPES = {int, float, bool, str, open, (open, 'r'), (open, 'w')}
TYPE_DEFAULT = {int: 0, float: 0., bool: False, str: '', open: None}
REGEX_OPTION_DESC = re.compile(r'([a-zA-Z0-9_]+)\s*--\s*(.+)$')
class ScriptError(ValueError):
......@@ -52,10 +52,6 @@ def build_scripts_from_file(fname:str, options:dict={}) -> [Script]:
elif ext == '.py':
try:
script = build_python_script_from_name(name)
if script.disabled:
if isinstance(script.disabled, str):
print('SCRIPT {} DISABLED:', script.disabled)
else:
yield script
except ScriptError as err:
print('SCRIPT ERROR:', str(err))
......@@ -265,10 +261,14 @@ def build_script_from_module(module, *, defaults:dict={}) -> Script or ScriptErr
options = [] # list of (arg name, arg type, default, description)
for arg in args.kwonlyargs:
argtype = args.annotations.get(arg)
if argtype not in OPTIONS_TYPES and not isinstance(argtype, partial):
bad_script_error(module, "Option {} do not have a valid annotation "
"({}). Only {} are accepted"
isgroup = isinstance(argtype, (tuple, list, set, frozenset))
if not isgroup and argtype not in OPTIONS_TYPES:
bad_script_error(module, "Option {} does not have a valid annotation "
"({}). Only tuples, lists, (frozen)sets, and primitive types such as {} are accepted"
"".format(arg, argtype, ', '.join(map(str, OPTIONS_TYPES))))
if isgroup: # pick an element or a subset of elements as default
default = argtype[0] if isinstance(argtype, (tuple, list)) else frozenset()
else:
default = args.kwonlydefaults.get(arg, TYPE_DEFAULT.get(argtype))
options.append((arg, argtype, default))
default_options = {arg: default for arg, _, default in options}
......
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