def execute(self, prompt, args): # Override from Okaara to prevent any non-kwargs from being passed # through to the underlying extensions, which have thus far always # been told to expect only kwargs. There should be a cleaner way of # overriding this in Okaara, but that would require a new build of # Okaara and I'm (currently) addressing a CR-2 blocker. Going forward, # I'll refactor Okaara and come back here to override the appropriate # smaller call. jdob, Sep 4, 2012 # Parse the command arguments into a dictionary try: arg_list, kwarg_dict = self.parse_arguments(prompt, args) except OptionValidationFailed: return os.EX_DATAERR # Pulp-specific logic of indicating a problem if there are non-kwargs if len(arg_list) > 0: raise CommandUsage() # Make sure all of the required arguments have been specified. This is # different from the Okaara standard version which does not include '' # as not fulfilling the required contract. Like the comment above, I'll # refactor Okaara to make this easier to override in a subclass so we # can remove the bulk of this method from being copied. jdob, Sep 4, 2012 missing_required = [ o for o in self.all_options() if o.required and (o.name not in kwarg_dict or kwarg_dict[o.name] is None or kwarg_dict[o.name] == '') ] if len(missing_required) > 0: raise CommandUsage(missing_required) # Flag entries that are not specified are parsed as None, but I'd rather # them explicitly be set to false. Iterate through each flag explicitly # setting the value to false if it was not specified for o in self.options: if isinstance(o, Flag) and kwarg_dict[o.name] is None: kwarg_dict[o.name] = False # Clean up option names clean_kwargs = dict([(k.lstrip('-'), v) for k, v in kwarg_dict.items()]) return self.method(*arg_list, **clean_kwargs)
def ensure_criteria(kwargs): """ Ensures at least one of the criteria options is specified in the given arguments. Other values may be specified in here and not affect the outcome. @param kwargs: keyword arguments parsed by the framework @raise CommandUsage: if there isn't at least one criteria argument """ criteria_args = [k for k, v in kwargs.items() if k in ALL_CRITERIA_ARGS and v is not None] if len(criteria_args) == 0: raise CommandUsage()
def run(self, **kwargs): content_type = kwargs.get('type') unit_id = kwargs.get('unit-id') if unit_id and not content_type: raise CommandUsage([OPTION_TYPE]) if kwargs.get('all'): response = self.context.server.content_orphan.remove_all() elif content_type and unit_id: response = self.context.server.content_orphan.remove(content_type, unit_id) elif content_type: response = self.context.server.content_orphan.remove_by_type(content_type) else: raise CommandUsage self.poll(response.response_body, kwargs)
def _parse_sort(cls, sort_args): """ Parse the sort argument to a search command @param sort_args: list of search arguments. Each is in the format 'field_name,direction' where direction is 'ascending' or 'descending'. @type sort_args: list @return: list of sort arguments in the format expected by Criteria @rtype: list """ ret = [] for value in sort_args: field_name, direction = cls._explode_sort_arg_pieces(value) if direction not in ('ascending', 'descending'): # validation should have caught this raise CommandUsage() ret.append((field_name, direction)) return ret