From 7de56666e39dd21164c3a241973b748dc12e5073 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Wed, 15 Jul 2015 10:59:48 +0200 Subject: [PATCH] worker,environment: support scanning of arguments with no default --- artiq/language/environment.py | 19 +++++++++++++++++-- artiq/master/worker_impl.py | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/artiq/language/environment.py b/artiq/language/environment.py index 4065759b2..9d43d8da6 100644 --- a/artiq/language/environment.py +++ b/artiq/language/environment.py @@ -10,12 +10,20 @@ class NoDefault: pass +class DefaultMissing(Exception): + """Raised by the ``default`` method of argument processors when no default + value is available.""" + pass + + class FreeValue: def __init__(self, default=NoDefault): if default is not NoDefault: self.default_value = default def default(self): + if not hasattr(self, "default_value"): + raise DefaultMissing return self.default_value def process(self, x): @@ -32,13 +40,14 @@ class HasEnvironment: """Provides methods to manage the environment of an experiment (devices, parameters, results, arguments).""" 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.__dmgr = dmgr self.__pdb = pdb self.__rdb = rdb self.__param_override = param_override + self.__default_arg_none = default_arg_none self.__kwargs = kwargs self.__in_build = True @@ -65,7 +74,13 @@ class HasEnvironment: try: argval = self.__kwargs[key] except KeyError: - return processor.default() + try: + return processor.default() + except DefaultMissing: + if self.__default_arg_none: + return None + else: + raise return processor.process(argval) def attr_argument(self, key, processor=None): diff --git a/artiq/master/worker_impl.py b/artiq/master/worker_impl.py index 59df48a02..2262f31fb 100644 --- a/artiq/master/worker_impl.py +++ b/artiq/master/worker_impl.py @@ -126,7 +126,7 @@ def examine(dmgr, pdb, rdb, file): name = exp_class.__doc__.splitlines()[0].strip() if 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() for k, v in exp_inst.requested_args.items()} register_experiment(class_name, name, arguments)