diff options
author | Rob Crittenden <rcritten@redhat.com> | 2010-10-11 22:27:57 -0400 |
---|---|---|
committer | Simo Sorce <ssorce@redhat.com> | 2010-10-28 16:06:06 -0400 |
commit | 33802ab71262e01704d3342761215ea480354e88 (patch) | |
tree | f242d7585a601b51ed576c895b4866c75cbf1a9a /ipalib/parameters.py | |
parent | ff636984abfd2b4a8dff329678df9edab4bc3d52 (diff) | |
download | freeipa-33802ab71262e01704d3342761215ea480354e88.tar.gz freeipa-33802ab71262e01704d3342761215ea480354e88.tar.xz freeipa-33802ab71262e01704d3342761215ea480354e88.zip |
Use context to decide which name to return on RequirementsErrors
When a Requirement fails we throw an exception including the name of the
field that is missing. To make the command-line friendlier we have a
cli_name defined which may or may not match the LDAP attribute. This can
be confusing if you are using ipalib directly because the attribute name
missing may not match what is actually required (desc vs description is
a good example).
If you use the context 'cli' then it will throw exceptions using cli_name.
If you use any other context it will use the name of the attribute.
ticket 187
Diffstat (limited to 'ipalib/parameters.py')
-rw-r--r-- | ipalib/parameters.py | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/ipalib/parameters.py b/ipalib/parameters.py index a06c8da2c..862c7593c 100644 --- a/ipalib/parameters.py +++ b/ipalib/parameters.py @@ -476,7 +476,10 @@ class Param(ReadOnly): value = self.get_default(**kw) else: value = self.convert(self.normalize(value)) - self.validate(value) + if hasattr(self, 'env'): + self.validate(value, self.env.context) + else: + self.validate(value) return value def kw(self): @@ -696,15 +699,19 @@ class Param(ReadOnly): error=ugettext(self.type_error), ) - def validate(self, value): + def validate(self, value, context=None): """ Check validity of ``value``. :param value: A proposed value for this parameter. + :param context: The context we are running in. """ if value is None: if self.required: - raise RequirementError(name=self.cli_name) + if context == 'cli': + raise RequirementError(name=self.cli_name) + else: + raise RequirementError(name=self.name) return if self.query: return @@ -1324,9 +1331,9 @@ class StrEnum(Enum): For example: >>> enum = StrEnum('my_enum', values=(u'One', u'Two', u'Three')) - >>> enum.validate(u'Two') is None + >>> enum.validate(u'Two', 'cli') is None True - >>> enum.validate(u'Four') + >>> enum.validate(u'Four', 'cli') Traceback (most recent call last): ... ValidationError: invalid 'my_enum': must be one of (u'One', u'Two', u'Three') |