mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
Redo boolean value encoding.
Move the code for encoding boolean values to LDAP boolean syntax from the Parameter class to the Encoder class, where the rest of LDAP encoding takes place. Remove encoding code from the Parameter class altogether, as all LDAP encoding should be done in the Encoder class.
This commit is contained in:
parent
abef5e8c02
commit
d9d1967989
@ -79,7 +79,15 @@ class Encoder(object):
|
||||
return self.encoder_settings.encode_postprocessor(
|
||||
var.encode(self.encoder_settings.encode_to)
|
||||
)
|
||||
elif isinstance(var, (bool, float, Decimal, int, long)):
|
||||
elif isinstance(var, bool):
|
||||
if var:
|
||||
var = 'TRUE'
|
||||
else:
|
||||
var = 'FALSE'
|
||||
return self.encoder_settings.encode_postprocessor(
|
||||
var.encode(self.encoder_settings.encode_to)
|
||||
)
|
||||
elif isinstance(var, (float, Decimal, int, long)):
|
||||
return self.encoder_settings.encode_postprocessor(
|
||||
str(var).encode(self.encoder_settings.encode_to)
|
||||
)
|
||||
|
@ -429,8 +429,6 @@ class Command(HasParam):
|
||||
if not self.api.env.in_server and 'version' not in params:
|
||||
params['version'] = API_VERSION
|
||||
self.validate(**params)
|
||||
if self.api.env.in_server:
|
||||
params = self.encode(**params)
|
||||
(args, options) = self.params_2_args_options(**params)
|
||||
ret = self.run(*args, **options)
|
||||
if (
|
||||
@ -615,14 +613,6 @@ class Command(HasParam):
|
||||
(k, self.params[k].convert(v)) for (k, v) in kw.iteritems()
|
||||
)
|
||||
|
||||
def encode(self, **kw):
|
||||
"""
|
||||
Return a dictionary of encoded values.
|
||||
"""
|
||||
return dict(
|
||||
(k, self.params[k].encode(v)) for (k, v) in kw.iteritems()
|
||||
)
|
||||
|
||||
def __convert_iter(self, kw):
|
||||
for param in self.params():
|
||||
if kw.get(param.name, None) is None:
|
||||
|
@ -306,8 +306,6 @@ class Param(ReadOnly):
|
||||
- primary_key: Command's parameter primary key is used for unique
|
||||
identification of an LDAP object and for sorting
|
||||
- normalizer: a custom function for Param value normalization
|
||||
- encoder: a custom function used to override Param subclass default
|
||||
encoder
|
||||
- default_from: a custom function for generating default values of
|
||||
parameter instance
|
||||
- autofill: by default, only `required` parameters get a default value
|
||||
@ -381,7 +379,6 @@ class Param(ReadOnly):
|
||||
('multivalue', bool, False),
|
||||
('primary_key', bool, False),
|
||||
('normalizer', callable, None),
|
||||
('encoder', callable, None),
|
||||
('default_from', DefaultFrom, None),
|
||||
('autofill', bool, False),
|
||||
('query', bool, False),
|
||||
@ -901,36 +898,6 @@ class Param(ReadOnly):
|
||||
rule=rule,
|
||||
)
|
||||
|
||||
def encode(self, value, force=False):
|
||||
"""
|
||||
Encode Python native type value to chosen backend format. Encoding is
|
||||
applied for parameters representing actual attributes (attribute=True).
|
||||
|
||||
The default encode method `Param._encode` can be overriden in a `Param`
|
||||
instance with `encoder` attribute:
|
||||
|
||||
>>> s = Str('my_str', encoder=lambda x:encode(x))
|
||||
|
||||
Note that the default method of encoding values is defined in
|
||||
`Param._encode()`.
|
||||
|
||||
:param value: Encoded value
|
||||
:param force: If set to true, encoding takes place even for Params
|
||||
not marked as attribute
|
||||
"""
|
||||
if not self.attribute and not force: #pylint: disable=E1101
|
||||
return value
|
||||
if self.encoder is not None: #pylint: disable=E1101
|
||||
return self.encoder(value) #pylint: disable=E1101
|
||||
|
||||
return self._encode(value)
|
||||
|
||||
def _encode(self, value):
|
||||
"""
|
||||
Encode a value to backend format.
|
||||
"""
|
||||
return value
|
||||
|
||||
def get_default(self, **kw):
|
||||
"""
|
||||
Return the static default or construct and return a dynamic default.
|
||||
|
@ -933,12 +933,6 @@ last, after all sets and adds."""),
|
||||
raise errors.ValidationError(name=attr, error=err.error)
|
||||
except errors.ConversionError, err:
|
||||
raise errors.ConversionError(name=attr, error=err.error)
|
||||
# FIXME: We use `force` when encoding because we know this is
|
||||
# an attribute, even if it does not have the `attribute` flag
|
||||
# set. This happens with no_update attributes, which are
|
||||
# not cloned to Update commands. This cloning is where the flag
|
||||
# gets set.
|
||||
value = param.encode(value, force=True)
|
||||
entry_attrs[attr] = value
|
||||
else:
|
||||
# unknown attribute: remove duplicite and invalid values
|
||||
|
@ -59,7 +59,6 @@ except ImportError:
|
||||
from ldap.functions import explode_dn
|
||||
from ipalib.dn import DN
|
||||
from ipalib import _
|
||||
from ipalib.parameters import Bool
|
||||
|
||||
import krbV
|
||||
|
||||
@ -76,23 +75,6 @@ MEMBERS_INDIRECT = 2
|
||||
# SASL authentication mechanism
|
||||
SASL_AUTH = _ldap_sasl.sasl({}, 'GSSAPI')
|
||||
|
||||
# OID 1.3.6.1.4.1.1466.115.121.1.7 (Boolean) syntax encoding
|
||||
def _encode_bool(self, value):
|
||||
def encode_bool_value(value):
|
||||
if value is None:
|
||||
return None
|
||||
if value:
|
||||
return u'TRUE'
|
||||
return u'FALSE'
|
||||
|
||||
if type(value) in (tuple, list):
|
||||
return tuple(encode_bool_value(v) for v in value)
|
||||
else:
|
||||
return encode_bool_value(value)
|
||||
|
||||
# set own Bool parameter encoder
|
||||
Bool._encode = _encode_bool
|
||||
|
||||
class IPASimpleLDAPObject(SimpleLDAPObject):
|
||||
'''
|
||||
This is a thin layer over SimpleLDAPObject which allows us to utilize
|
||||
|
1
makeapi
1
makeapi
@ -43,7 +43,6 @@ API_DOC_ERROR = 8
|
||||
PARAM_IGNORED_KW_ATTRIBUTES = ('label',
|
||||
'doc',
|
||||
'normalizer',
|
||||
'encoder',
|
||||
'default_from',
|
||||
'hint',
|
||||
'flags',
|
||||
|
@ -49,7 +49,7 @@ class test_Encoder(ClassChecker):
|
||||
assert_equal(o.encode('ahoj'), 'ahoj'.encode(encode_to))
|
||||
assert_equal(o.encode(_test_str_d), _test_str_e)
|
||||
# bool, float, int, long
|
||||
assert_equal(o.encode(True), str(True).encode(encode_to))
|
||||
assert_equal(o.encode(True), 'TRUE'.encode(encode_to))
|
||||
assert_equal(o.encode(1.01), str(1.01).encode(encode_to))
|
||||
assert_equal(o.encode(1000), str(1000).encode(encode_to))
|
||||
assert_equal(o.encode(long(1)), str(long(1)).encode(encode_to))
|
||||
|
Loading…
Reference in New Issue
Block a user