forked from M-Labs/artiq
1
0
Fork 0

worker,environment: support scanning of arguments with no default

This commit is contained in:
Sebastien Bourdeauducq 2015-07-15 10:59:48 +02:00
parent 9ed4dcd7d1
commit 7de56666e3
2 changed files with 18 additions and 3 deletions

View File

@ -10,12 +10,20 @@ class NoDefault:
pass pass
class DefaultMissing(Exception):
"""Raised by the ``default`` method of argument processors when no default
value is available."""
pass
class FreeValue: class FreeValue:
def __init__(self, default=NoDefault): def __init__(self, default=NoDefault):
if default is not NoDefault: if default is not NoDefault:
self.default_value = default self.default_value = default
def default(self): def default(self):
if not hasattr(self, "default_value"):
raise DefaultMissing
return self.default_value return self.default_value
def process(self, x): def process(self, x):
@ -32,13 +40,14 @@ class HasEnvironment:
"""Provides methods to manage the environment of an experiment (devices, """Provides methods to manage the environment of an experiment (devices,
parameters, results, arguments).""" parameters, results, arguments)."""
def __init__(self, dmgr=None, pdb=None, rdb=None, *, def __init__(self, dmgr=None, pdb=None, rdb=None, *,
param_override=dict(), **kwargs): param_override=dict(), default_arg_none=False, **kwargs):
self.requested_args = dict() self.requested_args = dict()
self.__dmgr = dmgr self.__dmgr = dmgr
self.__pdb = pdb self.__pdb = pdb
self.__rdb = rdb self.__rdb = rdb
self.__param_override = param_override self.__param_override = param_override
self.__default_arg_none = default_arg_none
self.__kwargs = kwargs self.__kwargs = kwargs
self.__in_build = True self.__in_build = True
@ -65,7 +74,13 @@ class HasEnvironment:
try: try:
argval = self.__kwargs[key] argval = self.__kwargs[key]
except KeyError: except KeyError:
try:
return processor.default() return processor.default()
except DefaultMissing:
if self.__default_arg_none:
return None
else:
raise
return processor.process(argval) return processor.process(argval)
def attr_argument(self, key, processor=None): def attr_argument(self, key, processor=None):

View File

@ -126,7 +126,7 @@ def examine(dmgr, pdb, rdb, file):
name = exp_class.__doc__.splitlines()[0].strip() name = exp_class.__doc__.splitlines()[0].strip()
if name[-1] == ".": if name[-1] == ".":
name = name[:-1] name = name[:-1]
exp_inst = exp_class(dmgr, pdb, rdb) exp_inst = exp_class(dmgr, pdb, rdb, default_arg_none=True)
arguments = {k: v.describe() arguments = {k: v.describe()
for k, v in exp_inst.requested_args.items()} for k, v in exp_inst.requested_args.items()}
register_experiment(class_name, name, arguments) register_experiment(class_name, name, arguments)