def _filter_param_by_context(self, name, env=None): """ Filter params on attribute named ``name`` by environment ``env``. For example: >>> from ipalib.config import Env >>> class Example(HasParam): ... ... takes_args = ( ... Str('foo_only', include=['foo']), ... Str('not_bar', exclude=['bar']), ... 'both', ... ) ... ... def get_args(self): ... return self._get_param_iterable('args') ... ... >>> eg = Example() >>> foo = Env(context='foo') >>> bar = Env(context='bar') >>> another = Env(context='another') >>> (foo.context, bar.context, another.context) (u'foo', u'bar', u'another') >>> list(eg._filter_param_by_context('args', foo)) [Str('foo_only', include=['foo']), Str('not_bar', exclude=['bar']), Str('both')] >>> list(eg._filter_param_by_context('args', bar)) [Str('both')] >>> list(eg._filter_param_by_context('args', another)) [Str('not_bar', exclude=['bar']), Str('both')] """ env = getattr(self, 'env', env) get_name = 'get_' + name if not hasattr(self, get_name): raise NotImplementedError( '%s.%s()' % (self.name, get_name) ) get = getattr(self, get_name) if not callable(get): raise TypeError( '%s.%s must be a callable; got %r' % (self.name, get_name, get) ) for spec in get(): param = create_param(spec) if env is None or param.use_in_context(env): if env is not None and not hasattr(param, 'env'): # Force specified environment. The way it is done is violation of ReadOnly promise. # Unfortunately, all alternatives are worse from both performance and code complexity # points of view. See following threads on freeipa-devel@ for references: # https://www.redhat.com/archives/freeipa-devel/2011-August/msg00000.html # https://www.redhat.com/archives/freeipa-devel/2011-August/msg00011.html object.__setattr__(param, 'env', env) yield param
def get_params(self): """ This method gets called by `HasParam._create_param_namespace()`. """ for spec in self._get_param_iterable('params'): if type(spec) is str: key = spec.rstrip('?*+') else: assert isinstance(spec, Param) key = spec.name yield create_param(spec) def get_key(p): if p.param.required: if p.param.default_from is None: return 0 return 1 return 2