summaryrefslogtreecommitdiffstats
path: root/ipapython/ipaldap.py
diff options
context:
space:
mode:
authorJan Cholasta <jcholast@redhat.com>2013-09-10 10:20:24 +0000
committerPetr Viktorin <pviktori@redhat.com>2013-11-05 13:56:55 +0100
commitdf5f4ee81d1aff1122dd92ab1b56eb335294c3a7 (patch)
treeb112b429a896789029038bd3e25218495b647d56 /ipapython/ipaldap.py
parent989493979da3ef1136a9b346cace5689ef22eed8 (diff)
downloadfreeipa-df5f4ee81d1aff1122dd92ab1b56eb335294c3a7.tar.gz
freeipa-df5f4ee81d1aff1122dd92ab1b56eb335294c3a7.tar.xz
freeipa-df5f4ee81d1aff1122dd92ab1b56eb335294c3a7.zip
Turn LDAPEntry.single_value into a dictionary-like property.
This change makes single_value consistent with the raw property. https://fedorahosted.org/freeipa/ticket/3521
Diffstat (limited to 'ipapython/ipaldap.py')
-rw-r--r--ipapython/ipaldap.py52
1 files changed, 30 insertions, 22 deletions
diff --git a/ipapython/ipaldap.py b/ipapython/ipaldap.py
index b54299a23..339da25ce 100644
--- a/ipapython/ipaldap.py
+++ b/ipapython/ipaldap.py
@@ -620,7 +620,7 @@ class IPASimpleLDAPObject(object):
# r[1] == r.data
class LDAPEntry(collections.MutableMapping):
__slots__ = ('_conn', '_dn', '_names', '_nice', '_raw', '_sync',
- '_not_list', '_orig', '_raw_view')
+ '_not_list', '_orig', '_raw_view', '_single_value_view')
def __init__(self, _conn, _dn=None, _obj=None, **kwargs):
"""
@@ -638,6 +638,8 @@ class LDAPEntry(collections.MutableMapping):
Keyword arguments can be used to override values of specific attributes.
"""
+ super(LDAPEntry, self).__init__()
+
if isinstance(_conn, LDAPEntry):
assert _dn is None
_dn = _conn
@@ -662,6 +664,7 @@ class LDAPEntry(collections.MutableMapping):
self._not_list = set()
self._orig = self
self._raw_view = None
+ self._single_value_view = None
if isinstance(_obj, LDAPEntry):
#pylint: disable=E1103
@@ -700,6 +703,12 @@ class LDAPEntry(collections.MutableMapping):
return self._raw_view
@property
+ def single_value(self):
+ if self._single_value_view is None:
+ self._single_value_view = SingleValueLDAPEntryView(self)
+ return self._single_value_view
+
+ @property
def data(self):
# FIXME: for backwards compatibility only
return self
@@ -911,27 +920,6 @@ class LDAPEntry(collections.MutableMapping):
return self._get_nice(name)
- def single_value(self, name, default=_missing):
- """Return a single attribute value
-
- Checks that the attribute really has one and only one value
-
- If the entry is missing and default is given, return the default.
- If the entry is missing and default is not given, raise KeyError.
- """
- try:
- values = self[name]
- except KeyError:
- if default is _missing:
- raise
- return default
- if not isinstance(values, list): # TODO: remove when we enforce lists
- return values
- if len(values) != 1:
- raise ValueError(
- '%s has %s values, one expected' % (name, len(values)))
- return values[0]
-
def __delitem__(self, name):
name = self._get_attr_name(name)
@@ -1047,6 +1035,26 @@ class RawLDAPEntryView(LDAPEntryView):
def __setitem__(self, name, value):
self._entry._set_raw(name, value)
+class SingleValueLDAPEntryView(LDAPEntryView):
+ def __getitem__(self, name):
+ value = self._entry[name]
+ if not isinstance(value, list):
+ # FIXME: remove when we enforce lists
+ return value
+ elif not value:
+ return None
+ elif len(value) == 1:
+ return value[0]
+ else:
+ raise ValueError(
+ '%s has %s values, one expected' % (name, len(value)))
+
+ def __setitem__(self, name, value):
+ if value is None:
+ self._entry[name] = None
+ else:
+ self._entry[name] = [value]
+
class LDAPClient(object):
"""LDAP backend class