def test_tolist(): assert [1] == tolist(1) l = [1, 2] assert l is tolist(l) t = (1, 2) # TODO: this is wrong I think as we could actually be wanting a mutable list assert t == tolist(t)
def process_args(self): # noqa had_strict_arg_failure = False for argname, processor, required, takes_list, list_item_invalidates, \ strict, show_msg, custom_msg, pass_as in self._processors: is_invalid = False pass_as = pass_as or argname argval = self.calling_args.get(argname, None) try: if isinstance(argval, list): if takes_list is False: raise formencode.Invalid('multiple values not allowed', argval, None) if takes_list is None: self.calling_args[pass_as] = argval = argval[0] elif takes_list: self.calling_args[pass_as] = argval = tolist(argval) if pass_as != argname: # catches a couple cases where a replacement doesn't # already happen above self.calling_args[pass_as] = argval # delete the old value if it exists if argname in self.calling_args: del self.calling_args[argname] if processor: if takes_list: processor = formencode.ForEach(processor) try: if required: processor = formencode.All(formencode.validators.NotEmpty, processor) processed_val = processor.to_python(argval) except formencode.Invalid as e: """ do a second round of processing for list values """ if not takes_list or not e.error_list or list_item_invalidates: raise """ only remove the bad values, keep the good ones """ new_list = [] for index, error in enumerate(e.error_list): if error is None: new_list.append(argval[index]) # revalidate for conversion and required processed_val = processor.to_python(new_list) self.calling_args[pass_as] = processed_val except formencode.Invalid as e: is_invalid = True if self.strict_args or strict: had_strict_arg_failure = True self.invalid_arg_keys.append(argname) if show_msg: invalid_msg = '%s: %s' % (argname, custom_msg or str(e)) user.add_message('error', invalid_msg) try: if is_invalid or self.calling_args[pass_as] is None or \ self.calling_args[pass_as] == '': del self.calling_args[pass_as] except KeyError: pass if len(self.invalid_arg_keys) > 0: log.debug('%s had bad args: %s', self.__class__.__name__, self.invalid_arg_keys) if had_strict_arg_failure: raise BadRequest('strict arg failure w/ invalid keys: %s' % self.invalid_arg_keys)
def tb_depth_in(depths): """ looks at the current traceback to see if the depth of the traceback matches any number in the depths list. If a match is found, returns True, else False. """ depths = tolist(depths) if traceback_depth() in depths: return True return False
def list_active(cls, include_ids=None, order_by=None): if order_by is None: order_by = cls.label if include_ids: include_ids = tolist(include_ids) clause = sasql.or_( cls.active_flag == 1, cls.id.in_(include_ids) ) else: clause = cls.active_flag == 1 return cls.list_where(clause, order_by=order_by)
def auth_calculate_any_all(self, any, all): # if require_all is given and there are any failures, deny authorization for perm in tolist(all): if not user.has_perm(perm): return False # if there was at least one value for require_all and not values for # require any, then the user is authorized if all and not any: return True # at this point, require_all passed or was empty and require_any has # at least one value, so this is the final check to determine # authorization if user.has_any_perm(any): return True return False
def decorate(f): lrule = rule fname = f.__name__ getargs = options.pop('getargs', []) component_prefix = _calc_component_name(f.__module__) # calculate the endpoint endpoint = fname if component_prefix is not None: endpoint = '%s:%s' % (component_prefix, endpoint) # setup the routing if lrule is None: lrule = '/%s' % fname log.debug('@asview adding route "%s" to endpoint "%s"', lrule, endpoint) ag.route_map.add(Rule(lrule, endpoint=endpoint), **options) # cache key for this object cachekey = '%s:%s' % (f.__module__, fname) # create the class that will handle this function if it doesn't already # exist in the cache if cachekey not in CLASS_CACHE: fvh = type(fname, (_AsViewHandler, ), {}) fvh.__module__ = f.__module__ # make the getargs available fvh._asview_getargs = tolist(getargs) # store this class object in the cache so we don't have to # recreate next time CLASS_CACHE[cachekey] = fvh else: fvh = CLASS_CACHE[cachekey] # assign the default method. This can't be cached because on a reload # of the views module, the first decorated function will become # None. So we have to recreate the defmethod wrapper with the # function object that is being created/recreated and decorated. def defmethod(self): return self._call_with_expected_args(f, method_is_bound=False) fvh.default = defmethod # return the class instead of the function return fvh
def filtering_col_inputs1(self, col): filter = col.filter field_name = 'v1({0})'.format(col.key) field_name = self.grid.prefix_qs_arg_key(field_name) inputs = '' if 'input' in filter.input_types: ident = '{0}_input1'.format(col.key) inputs += _HTML.input(name=field_name, type='text', value=filter.value1_set_with, id=ident) if 'select' in filter.input_types: ident = '{0}_select1'.format(col.key) current_selected = tolist(filter.value1) or [] multiple = None if len(current_selected) > 1: multiple = 'multiple' inputs += tags.select(field_name, current_selected, self.filtering_filter_options(filter), multiple=multiple) inputs += self.filtering_toggle_image() return inputs
def _perform_override(self): if not self._override_added and settings.emails.override: self._override_added = True body_prepend = '%s\n\nTo: %s \nCc: %s \nBcc: %s\n\n%s\n\n' % ( '-' * 70, ', '.join(self.to), ', '.join(self.cc), ', '.join( self.bcc), '-' * 70) self.to = tolist(settings.emails.override) self.cc = [] self.bcc = [] if self.content_subtype == 'html': body_prepend = markdown(body_prepend) self.body = self._insert_after_html_body( body_prepend, self.body) else: self.body = body_prepend + self.body # take care of any text/html alternative types for attachment in self.attachments: filename, content, mimetype = attachment if not filename and content and mimetype == 'text/html': attachment[1] = self._insert_after_html_body( markdown(body_prepend), content)
def query_base(self, has_sort, has_filters): cols = [col.expr for col in self.columns if col.expr is not None] query = self.manager.sa_query(*cols) for join_terms in (tolist(self.query_joins) or tuple()): query = query.join(*tolist(join_terms)) for join_terms in (tolist(self.query_outer_joins) or tuple()): query = query.outerjoin(*tolist(join_terms)) if self.query_filter: query = query.filter(*tolist(self.query_filter)) if not has_sort and self.query_default_sort is not None: query = query.order_by(*tolist(self.query_default_sort)) return query
def update(cls, oid=None, **kwargs): from compstack.auth.model.orm import Group if oid is None: u = cls() db.sess.add(u) # when creating a new user, if the password is not set, assign it as # a random string assuming the email will get sent out to the user # and they will change it when they login if not kwargs.get('password', None): kwargs['password'] = randchars(8) else: u = cls.get(oid) # automatically turn on reset_password when the password get set manually # (i.e. when an admin does it), unless told not to (when a user does it # for their own account) if kwargs.get('password') and kwargs.get('pass_reset_ok', True): kwargs['reset_required'] = True for k, v in six.iteritems(kwargs): try: # some values can not be set directly if k not in ('pass_hash', 'pass_salt', 'assigned_groups', 'approved_permissions', 'denied_permissions'): setattr(u, k, v) except AttributeError: pass if 'assigned_groups' in kwargs: u.groups = [ Group.get(gid) for gid in tolist(kwargs['assigned_groups']) ] db.sess.flush() if 'approved_permissions' in kwargs: u.set_permissions(kwargs['approved_permissions'], True) if 'denied_permissions' in kwargs: u.set_permissions(kwargs['denied_permissions'], False) return u
def _perform_override(self): if not self._override_added and settings.emails.override: self._override_added = True body_prepend = '%s\n\nTo: %s \nCc: %s \nBcc: %s\n\n%s\n\n' % ( '-' * 70, ', '.join(self.to), ', '.join(self.cc), ', '.join(self.bcc), '-' * 70 ) self.to = tolist(settings.emails.override) self.cc = [] self.bcc = [] if self.content_subtype == 'html': body_prepend = markdown(body_prepend) self.body = self._insert_after_html_body(body_prepend, self.body) else: self.body = body_prepend + self.body # take care of any text/html alternative types for attachment in self.attachments: filename, content, mimetype = attachment if not filename and content and mimetype == 'text/html': attachment[1] = self._insert_after_html_body(markdown(body_prepend), content)
def test_tolist_nonmutable_default(): x = tolist(None) assert x == [] x.append(1) assert tolist(None) == []
def email_error_to(self): error_to = self.get('KEG_EMAIL_ERROR_TO') override_to = self.get('KEG_EMAIL_OVERRIDE_TO') if override_to: return tolist(override_to) return tolist(error_to)
def order_by_helper(cls, query, order_by): if order_by is not None: return query.order_by(*tolist(order_by)) pk_cols = sa_inspect(cls).primary_key return query.order_by(*pk_cols)
def _has_any(self, haystack, needles, arg_needles): needles = set(tolist(needles)) if len(arg_needles) > 0: needles |= set(arg_needles) return bool(haystack.intersection(needles))
def render_in(self): resolved = self._render_in if callable(resolved): resolved = resolved(self) return tuple(tolist(resolved))
def add_processor(self, argname, processor=None, required=None, takes_list=None, list_item_invalidates=False, strict=False, show_msg=False, custom_msg=None, pass_as=None): """ Sets up filtering & validation on the calling_args to be used before any methods in the call stack are called. The default arguments will cause an invalid argument to be ignored silently. However, you can also setup the validation process to show the user error messages and/or raise a 400 Bad Request. argname = the key that corresponds to the argument you want to validate. If this key isn't found as a URL argument, it is assumed that it is a get argument and expect_getargs(argname) is called. processor = a callable which takes the arg value to validate as its single argument. Should raise ValueError if the value is invalid. It may also be a Formencode validator. The processor should also return the value to use as the calling arg. If validation fails and strict is not True, then the value is set to None. required = requires the argument to be present and to have a non-None value. Does not alter the value. Since the default behavior is to ignore a value when validation has failed, which results in a None value being set, it only makes sense to use required when strict should be True. Therefore, strict is set to true implicitely when using require. takes_list = if True, validates multiple values and ensures that the calling arg associated with argname is a list even if only one item was sent. If None or False, only the first value will be validated and the calling arg associated with argname will not be a list, even if multiple values are present in calling_args. If None, having more than one value for argname is not considered a validation error. If False, it is considered a validation error. strict = If validation fails, raise a 400 Bad Request exception. The exception is raised after all validation has taken place, so user messages will still be set for all errors if applicable. Therefore, if your error doc handler shows user messages, the user can still be informed of the reason why they received the error. list_item_invalidates = When set to False and a list item fails validation, it is simply ignored. If strict_list == True, then a single validation error is considered a validation error for the whole argument, causing the value to be set to []. show_msg = should the user be shown an error message if validation on this argument fails? If custom_msg == True, show_msg also gets set to True. custom_msg = A custom error msg to show the user if validation fails. If not given, and if validator is a Formencode validator, then the message will be take from the formencode validator. If not given and the validator is a callable, then the message will be taken from the ValueError exception that is raised. If given, show_msg is implicitly set to True. pass_as = When the argname is not a valid python variable (e.g. ?listvalues[]=1&listvalues[]=2), then set pass_as to a string that corresponds to the variable name that should be used when passing this value to the action methods. """ if argname not in self.urlargs: self.expect_getargs(argname) if custom_msg: show_msg = True if required: if not processor: processor = formencode.validators.NotEmpty() strict = True if processor: for proc in tolist(processor): if not formencode.is_validator(proc): if not hasattr(proc, '__call__'): raise TypeError('processor must be a Formencode validator or a callable') proc = _ProcessorWrapper(to_python=proc) self._processors.append(( argname, proc, required, takes_list, list_item_invalidates, strict, show_msg, custom_msg, pass_as )) else: self._processors.append(( argname, None, required, takes_list, list_item_invalidates, strict, show_msg, custom_msg, pass_as ))