From 1b0b9645d197f512eee61051775414ca35ee7f6d Mon Sep 17 00:00:00 2001 From: Martin Kosek Date: Tue, 29 Nov 2011 09:59:11 +0100 Subject: Add --delattr option to complement --setattr/--addattr Add a --delattr option to round out multi-valued attribute manipulation. The new option is available for all LDAPUpdate based commands. --delattr is evaluated last, it can remove any value present either in --addattr/--setattr option or in current LDAP object. --*attr processing was completely refactored and placed to one independent function available for all baseldap commands. For this purpose a missing common base class for all baseldap commands has been implemented. The new class should serve not only for --*attr processing but also for other common baseldap methods and attributes. This approach will also benefit other custom commands based neither on LDAPCreate nor LDAPUpdate. They can easily integrate --*attr option processing when needed. https://fedorahosted.org/freeipa/ticket/1929 --- ipalib/frontend.py | 62 +----------------------------------------------------- 1 file changed, 1 insertion(+), 61 deletions(-) (limited to 'ipalib/frontend.py') diff --git a/ipalib/frontend.py b/ipalib/frontend.py index 2ab457c3d..b79aad955 100644 --- a/ipalib/frontend.py +++ b/ipalib/frontend.py @@ -30,7 +30,7 @@ from util import make_repr from output import Output, Entry, ListOfEntries from text import _, ngettext -from errors import ZeroArgumentError, MaxArgumentError, OverlapError, RequiresRoot, VersionError, RequirementError, ValidationError +from errors import ZeroArgumentError, MaxArgumentError, OverlapError, RequiresRoot, VersionError, RequirementError from errors import InvocationError from constants import TYPE_ERROR from ipapython.version import API_VERSION @@ -535,45 +535,6 @@ class Command(HasParam): kw = self.args_options_2_params(*args, **options) return dict(self.__attributes_2_entry(kw)) - def __convert_2_dict(self, attrs, append=True): - """ - Convert a string in the form of name/value pairs into - a dictionary. The incoming attribute may be a string or - a list. - - Any attribute found that is also a param is validated. - - append controls whether this returns a list of values or a single - value. - """ - newdict = {} - if not type(attrs) in (list, tuple): - attrs = [attrs] - for a in attrs: - m = re.match("\s*(.*?)\s*=\s*(.*?)\s*$", a) - attr = str(m.group(1)).lower() - value = m.group(2) - if len(value) == 0: - # None means "delete this attribute" - value = None - if attr in self.params: - try: - value = self.params[attr](value) - except ValidationError, err: - (name, error) = str(err.strerror).split(':') - raise ValidationError(name=attr, error=error) - if append and attr in newdict: - if type(value) in (tuple,): - newdict[attr] += list(value) - else: - newdict[attr].append(value) - else: - if type(value) in (tuple,): - newdict[attr] = list(value) - else: - newdict[attr] = [value] - return newdict - def __attributes_2_entry(self, kw): for name in self.params: if self.params[name].attribute and name in kw: @@ -583,27 +544,6 @@ class Command(HasParam): else: yield (name, kw[name]) - adddict = {} - if kw.get('setattr'): - adddict = self.__convert_2_dict(kw['setattr'], append=False) - - if kw.get('addattr'): - for (k, v) in self.__convert_2_dict(kw['addattr']).iteritems(): - if k in adddict: - adddict[k] += v - else: - adddict[k] = v - - for name in adddict: - value = adddict[name] - if isinstance(value, list): - if len(value) == 1: - yield (name, value[0]) - else: - yield (name, [v for v in value]) - else: - yield (name, value) - def params_2_args_options(self, **params): """ Split params into (args, options). -- cgit