def resolve_base(self, options): """ Resolves a base object from a set of parent identifier options. """ collection_candidate = self.check_connection() parnet_candidate_locator = 0 base = None parnet_candidates = [ key for key in options.keys() if OptionHelper.is_parent_id_option(key) ] parnet_candidates_permutations = list(itertools.permutations(parnet_candidates)) if parnet_candidates_permutations[0]: for combination in parnet_candidates_permutations: for item in combination: key = item val = options[key] parnet_candidate_locator += 1 typename = OptionHelper.get_parent_id_type(key) coll = TypeHelper.to_plural(typename) if not (TypeHelper.isKnownType(typename) or TypeHelper.isKnownType(coll)): self.error(Messages.Error.NO_SUCH_TYPE % typename) if hasattr(collection_candidate, coll): coll_ins = getattr(collection_candidate, coll) if hasattr(coll_ins, 'get'): if not val: self.error(Messages.Error.INVALID_OPTION % key) if key.endswith('-identifier'): base = coll_ins.get(id=val) elif key.endswith('-name'): base = coll_ins.get(name=val) if not base: if len(combination) > parnet_candidate_locator: continue else: self.error(Messages.Error.NO_SUCH_OBJECT % (typename, val)) if len(parnet_candidates) == parnet_candidate_locator: return base else: collection_candidate = base self.error(Messages.Error.CANNOT_CONSTRUCT_COLLECTION_MEMBER_VIEW % str(parnet_candidates_permutations))
def validate_options(self, opts, options): for opt in opts.keys(): if opt.startswith('--'): opt_item = opt[2:] else: opt_item = opt if opt_item not in options and not OptionHelper.is_parent_id_option(opt): self.error(Messages.Error.NO_SUCH_OPTION % opt)
def validate_options(self, opts, options): for opt in opts.keys(): if opt.startswith('--'): opt_item = opt[2:] else: opt_item = opt if opt_item not in options and not OptionHelper.is_parent_id_option( opt): self.error(Messages.Error.NO_SUCH_OPTION % opt)
def _resolve_base(self, args): """ Resolves a base object from a set of '--parent-type-identifier' or '--parent-type-name' options. """ parent_candidates = [item for item in args if OptionHelper.is_parent_id_option(item)] parent_candidates_permutations = list(itertools.permutations(parent_candidates)) if parent_candidates_permutations[0]: for combination in parent_candidates_permutations: candidates = [OptionHelper.get_parent_id_type(item) for item in combination if OptionHelper.is_parent_id_option(item)] candidate = (''.join(candidates) + args[0]).lower() dt = TypeHelper.getDecoratorType(candidate) if dt: return dt return None
def _resolve_base(self, args): """ Resolves a base object from a set of '--parent-type-identifier' or '--parent-type-name' options. """ parent_candidates = [ item for item in args if OptionHelper.is_parent_id_option(item) ] parent_candidates_permutations = list( itertools.permutations(parent_candidates)) if parent_candidates_permutations[0]: for combination in parent_candidates_permutations: candidates = [ OptionHelper.get_parent_id_type(item) for item in combination if OptionHelper.is_parent_id_option(item) ] candidate = (''.join(candidates) + args[0]).lower() dt = TypeHelper.getDecoratorType(candidate) if dt: return dt return None
def update_object_data(self, obj, options, scope=None): """Updates object properties with values from `options'.""" for key in options.keys(): if OptionHelper.is_parent_id_option(key): continue prop = key.replace('--', '') val = options[key] if type(val) == types.ListType: for item in val: self.__do_set_data(obj=obj, prop=prop, fq_prop=key, val=item) else: self.__do_set_data(obj=obj, prop=prop, fq_prop=key, val=val) return obj
def _get_query_params(self, opts, query_arg='--query', kwargs_arg='--kwargs'): """INTERNAL: retrieves query and kwargs from attribute options""" query = opts[query_arg] if opts.has_key(query_arg) else None kw = {} empty = True if opts.has_key(kwargs_arg): if opts[kwargs_arg]: for item in opts[kwargs_arg].split(';'): if item.find("=") != -1: k, v = item.split('=') kw[k.replace('-', '.')] = v empty = False elif empty: self.error(Messages.Error.INVALID_KWARGS_FORMAT % item) else: self.error(Messages.Error.INVALID_KWARGS_CONTENT) mopts = {} for k, v in opts.iteritems(): if k != query_arg and k != kwargs_arg and not OptionHelper.is_parent_id_option(k): mopts[k if not k.startswith('--') else k[2:]] = v kw.update(mopts) return query, kw
def resolve_base(self, options): """ Resolves a base object from a set of parent identifier options. """ # Initially the base is the connection: connection = self.check_connection() base = connection # Find all the options that are parent identifiers, and sort them # so that the process will be always the same regardless of the # order of the options dictionary: identifiers = [ key for key in options.keys() if OptionHelper.is_parent_id_option(key) ] identifiers.sort() # Calculate the set of permutations of all the parent identifiers, as # we are going to try each permutation till we find one that results # in a valid base: permutations = list(itertools.permutations(identifiers)) for permutation in permutations: # Restart the search from the connection for each permutation: base = connection for item in permutation: # Get the name and value of the current parent identifier: key = item value = options[key] # Calculate the type and collection names: type_name = OptionHelper.get_parent_id_type(key) collection_name = TypeHelper.to_plural(type_name) if not (TypeHelper.isKnownType(type_name) or TypeHelper.isKnownType(collection_name)): self.error(Messages.Error.NO_SUCH_TYPE % type_name) # Try to extract the next base from the current one: if hasattr(base, collection_name): collection = getattr(base, collection_name) if hasattr(collection, 'get'): if not value: self.error(Messages.Error.INVALID_OPTION % key) if key.endswith('-identifier'): base = collection.get(id=value) elif key.endswith('-name'): base = collection.get(name=value) else: base = None else: base = None else: base = None # If we haven't been able to find a valid base for the current # parent identifier, then we should discard this permutation: if base is None: break # If we already found a valid base, then we should discard the # rest of the permutations, i.e., the first valid permutation is # the winner: if base != None: break # Generate an error message if no permutation results in a valid base: if base is None: self.error(Messages.Error.CANNOT_CONSTRUCT_COLLECTION_MEMBER_VIEW % str(permutations)) # The connection is the starting point for the search, so if the # result is the connection we can conclude that there is no # base: if base == connection: base = None return base