def extract_options(cls, options, options_prefix="", source_options_prefix="", error_if_missing=True): """Copy the set of registered options for this class from an existing options block and return a new options block with only those values. This method will preserve the _userSet status of all options. Args: options: The options block to extract options from. options_prefix (str): A string to prefix the name of all options added by this class. source_options_prefix (str): A string prefix to use when looking for this class's options on the :attr:`options` argument. error_if_missing (bool): Controls whether or not an exception is raised when registered options are missing from the input options object. Default is :const:`True`. Returns: a PySPConfigBlock with values copied from the \ source options block but with only the options \ associated with this class """ assert isinstance(options_prefix, six.string_types) bare_options = cls.register_options(options_prefix="") return_options = cls.register_options(options_prefix=options_prefix) for name in bare_options.keys(): configval = None if source_options_prefix + name in options: configval = options.get(source_options_prefix + name) elif error_if_missing: raise KeyError(source_options_prefix + name) else: continue assert configval is not None this_configval = return_options.get(options_prefix + name) include_argparse = False check_options_match(this_configval, configval, include_default=True, include_argparse=include_argparse, include_value=False, include_accessed=False) if configval._userSet: this_configval.set_value(configval.value()) return return_options
def extract_options(cls, options, options_prefix="", source_options_prefix="", error_if_missing=True): """Copy the set of registered options for this class from an existing options block and return a new options block with only those values. This method will preserve the _userSet status of all options. Args: options: The options block to extract options from. options_prefix (str): A string to prefix the name of all options added by this class. source_options_prefix (str): A string prefix to use when looking for this class's options on the :attr:`options` argument. error_if_missing (bool): Controls whether or not an exception is raised when registered options are missing from the input options object. Default is :const:`True`. Returns: a PySPConfigBlock with values copied from the \ source options block but with only the options \ associated with this class """ assert isinstance(options_prefix, six.string_types) bare_options = cls.register_options(options_prefix="") return_options = cls.register_options(options_prefix=options_prefix) for name in bare_options.keys(): configval = None if source_options_prefix+name in options: configval = options.get(source_options_prefix+name) elif error_if_missing: raise KeyError(source_options_prefix+name) else: continue assert configval is not None this_configval = return_options.get(options_prefix+name) include_argparse = False check_options_match(this_configval, configval, include_default=True, include_argparse=include_argparse, include_value=False, include_accessed=False) if configval._userSet: this_configval.set_value(configval.value()) return return_options
def validate_options(cls, options, prefix="", error_if_missing=True): """Validate that all registered options can be found in the options block and that their option definitions are the same. The optional flag 'prefix' can be set to indicate that all registered class options will have a name predended with the given prefix. The optional flag 'error_if_missing' can be used to control whether or not an exception is raised when registered options are missing.""" assert isinstance(prefix, six.string_types) bases = inspect.getmro(cls) first = defaultdict(lambda: True) for base in bases: # The check here is that PySPConfiguredObject needs to # appear as an immediate base in a class definition for us # to check the list of _registered options declared in # that class definitions immediate scope. This allows # _declared_options to be distributed across different # containers in the class hierarchy, while leaving the # ability for derived classes to NOT have to define an # empty _declared_options block if they don't have any # new options to add but are derived from some class which # does use PySPConfiguredObject as a base class. By not # declaring PySPConfiguredObject as an immediate base # class, we know not to check for a _declared_options # data member in this derived class's scope (because # otherwise we'd be getting some base class's definition) # if any(base is PySPConfiguredObject for base in base.__bases__): for name in base._declared_options: configval = None try: configval = options.get(prefix + name) except KeyError: configval = None if error_if_missing: raise else: continue assert configval is not None this_configval = base._declared_options.get(name) include_argparse = False if this_configval._argparse is not None: include_argparse = True check_options_match(this_configval, configval, include_argparse=include_argparse, include_default=first[prefix + name], include_value=False, include_accessed=False) first[prefix + name] = False
def extract_options(cls, options, prefix="", srcprefix="", error_if_missing=True): """Copy the set of registered options for this class from an existing options block and return a new options block with only those values. This method will preserve the _userSet status of all options. The optional flag 'prefix' can be set to indicate that all registered class options in the returned options object will have a name predended with the given prefix. The optional flag 'srcprefix' can be set to indicate that all registered class options on the input options object have a named prepended with the given prefix. The optional flag 'error_if_missing' controls whether or not an exception is raised when registered options are missing from the input options object.""" assert isinstance(prefix, six.string_types) bare_options = cls.register_options(prefix="") return_options = cls.register_options(prefix=prefix) for name in bare_options.keys(): configval = None try: configval = options.get(srcprefix + name) except KeyError: configval = None if error_if_missing: raise else: continue assert configval is not None this_configval = return_options.get(prefix + name) check_options_match(this_configval, configval, include_default=True, include_value=False, include_accessed=False) if configval._userSet: this_configval.set_value(configval.value()) return return_options
def extract_options(cls, options, prefix="", srcprefix="", error_if_missing=True): """Copy the set of registered options for this class from an existing options block and return a new options block with only those values. This method will preserve the _userSet status of all options. The optional flag 'prefix' can be set to indicate that all registered class options in the returned options object will have a name predended with the given prefix. The optional flag 'srcprefix' can be set to indicate that all registered class options on the input options object have a named prepended with the given prefix. The optional flag 'error_if_missing' controls whether or not an exception is raised when registered options are missing from the input options object.""" assert isinstance(prefix, six.string_types) bare_options = cls.register_options(prefix="") return_options = cls.register_options(prefix=prefix) for name in bare_options.keys(): configval = None try: configval = options.get(srcprefix+name) except KeyError: configval = None if error_if_missing: raise else: continue assert configval is not None this_configval = return_options.get(prefix+name) check_options_match(this_configval, configval, include_default=True, include_value=False, include_accessed=False) if configval._userSet: this_configval.set_value(configval.value()) return return_options
def validate_options(cls, options, options_prefix="", error_if_missing=True): """Validate that all registered options can be found in the options block and that their option definitions are the same. Args: options: The options block to validate. options_prefix (str): A string to prefix the name of all options registered for class. error_if_missing (bool): Controls whether or not an exception is raised when registered options are missing from the input Namespace object. Default is :const:`True`. """ assert isinstance(options_prefix, six.string_types) bases = inspect.getmro(cls) first = defaultdict(lambda: True) for base in bases: # The check here is that PySPConfiguredObject needs to # appear as an immediate base in a class definition for us # to check the list of _registered options declared in # that class definitions immediate scope. This allows # _declared_options to be distributed across different # containers in the class hierarchy, while leaving the # ability for derived classes to NOT have to define an # empty _declared_options block if they don't have any # new options to add but are derived from some class which # does use PySPConfiguredObject as a base class. By not # declaring PySPConfiguredObject as an immediate base # class, we know not to check for a _declared_options # data member in this derived class's scope (because # otherwise we'd be getting some base class's definition) # if any(base is PySPConfiguredObject for base in base.__bases__): # make sure we've removed this older form assert not hasattr(base, "_declared_options") if "_declare_options" in base.__dict__: _declared_options = base._declare_options() for name in _declared_options: configval = None if options_prefix+name in options: configval = options.get(options_prefix+name) elif error_if_missing: raise KeyError(options_prefix+name) else: continue assert configval is not None this_configval = _declared_options.get(name) include_argparse = False if this_configval._argparse is not None: include_argparse = True check_options_match(this_configval, configval, include_argparse=include_argparse, include_default=first[options_prefix + name], include_value=False, include_accessed=False) first[options_prefix + name] = False
def validate_options(cls, options, options_prefix="", error_if_missing=True): """Validate that all registered options can be found in the options block and that their option definitions are the same. Args: options: The options block to validate. options_prefix (str): A string to prefix the name of all options registered for class. error_if_missing (bool): Controls whether or not an exception is raised when registered options are missing from the input Namespace object. Default is :const:`True`. """ assert isinstance(options_prefix, six.string_types) bases = inspect.getmro(cls) first = defaultdict(lambda: True) for base in bases: # The check here is that PySPConfiguredObject needs to # appear as an immediate base in a class definition for us # to check the list of _registered options declared in # that class definitions immediate scope. This allows # _declared_options to be distributed across different # containers in the class hierarchy, while leaving the # ability for derived classes to NOT have to define an # empty _declared_options block if they don't have any # new options to add but are derived from some class which # does use PySPConfiguredObject as a base class. By not # declaring PySPConfiguredObject as an immediate base # class, we know not to check for a _declared_options # data member in this derived class's scope (because # otherwise we'd be getting some base class's definition) # if any(base is PySPConfiguredObject for base in base.__bases__): # make sure we've removed this older form assert not hasattr(base, "_declared_options") if "_declare_options" in base.__dict__: _declared_options = base._declare_options() for name in _declared_options: configval = None if options_prefix + name in options: configval = options.get(options_prefix + name) elif error_if_missing: raise KeyError(options_prefix + name) else: continue assert configval is not None this_configval = _declared_options.get(name) include_argparse = False if this_configval._argparse is not None: include_argparse = True check_options_match( this_configval, configval, include_argparse=include_argparse, include_default=first[options_prefix + name], include_value=False, include_accessed=False) first[options_prefix + name] = False