def _validate_dict(self, spec, data): accept_any_key = False any_key_spec = None for k in iterkeys(spec): if isinstance(k, required): if k not in data: self._handle_error("missing '%s', not in %s" % (k, self.context.current_pos)) if isinstance(k, anything): accept_any_key = True any_key_spec = spec[k] for k, v in iteritems(data): if accept_any_key: with self.context.pos('.' + text_type(k)): self._validate_part(any_key_spec, v) else: if k not in spec: self._handle_error("unknown '%s' in %s" % (k, self.context.current_pos), info_only=True) continue with self.context.pos('.' + text_type(k)): self._validate_part(spec[k], v)
def query_string(self): """ The map request as a query string (the order is not guaranteed). >>> qs = RequestParams(dict(foo='egg', bar='ham%eggs', baz=100)).query_string >>> sorted(qs.split('&')) ['bar=ham%25eggs', 'baz=100', 'foo=egg'] """ kv_pairs = [] for key, values in self.params.iteritems(): value = ','.join(text_type(v) for v in values) kv_pairs.append(key + '=' + quote(value.encode('utf-8'), safe=',')) return '&'.join(kv_pairs)
def _validate_dict(self, spec, data): accept_any_key = False any_key_spec = None for k in iterkeys(spec): if isinstance(k, required): if k not in data: self._handle_error("missing '%s' not in %s" % (k, self.context.current_pos)) if isinstance(k, anything): accept_any_key = True any_key_spec = spec[k] for k, v in iteritems(data): if accept_any_key: with self.context.pos('.' + text_type(k)): self._validate_part(any_key_spec, v) else: if k not in spec: self._handle_error("unknown '%s' in %s" % (k, self.context.current_pos), info_only=True) continue with self.context.pos('.' + text_type(k)): self._validate_part(spec[k], v)
def _repr(self, value, pos): __traceback_hide__ = True try: if value is None: return '' if self._unicode: try: value = text_type(value) except UnicodeDecodeError: value = bytes(value) else: if not isinstance(value, basestring_): value = coerce_text(value) if (is_unicode(value) and self.default_encoding): value = value.encode(self.default_encoding) except: exc_info = sys.exc_info() e = exc_info[1] e.args = (self._add_line_info(e.args[0], pos),) reraise((exc_info[0], e, exc_info[2])) else: if self._unicode and isinstance(value, bytes): if not self.default_encoding: raise UnicodeDecodeError( 'Cannot decode bytes value %r into unicode ' '(no default_encoding provided)' % value) try: value = value.decode(self.default_encoding) except UnicodeDecodeError as e: raise UnicodeDecodeError( e.encoding, e.object, e.start, e.end, e.reason + ' in string %r' % value) elif not self._unicode and is_unicode(value): if not self.default_encoding: raise UnicodeEncodeError( 'Cannot encode unicode value %r into bytes ' '(no default_encoding provided)' % value) value = value.encode(self.default_encoding) return value
def _repr(self, value, pos): __traceback_hide__ = True try: if value is None: return '' if self._unicode: try: value = text_type(value) except UnicodeDecodeError: value = bytes(value) else: if not isinstance(value, basestring_): value = coerce_text(value) if (is_unicode(value) and self.default_encoding): value = value.encode(self.default_encoding) except: exc_info = sys.exc_info() e = exc_info[1] e.args = (self._add_line_info(e.args[0], pos), ) reraise((exc_info[0], e, exc_info[2])) else: if self._unicode and isinstance(value, bytes): if not self.default_encoding: raise UnicodeDecodeError( 'Cannot decode bytes value %r into unicode ' '(no default_encoding provided)' % value) try: value = value.decode(self.default_encoding) except UnicodeDecodeError as e: raise UnicodeDecodeError( e.encoding, e.object, e.start, e.end, e.reason + ' in string %r' % value) elif not self._unicode and is_unicode(value): if not self.default_encoding: raise UnicodeEncodeError( 'Cannot encode unicode value %r into bytes ' '(no default_encoding provided)' % value) value = value.encode(self.default_encoding) return value
def iteritems(self): for key, values in self.params.iteritems(): yield key, self.delimiter.join((text_type(x) for x in values))