def _fix_value(self, value): value = super(DomainNameSubstringField, self)._fix_value(value) try: value = value.encode('idna') except ValueError: raise FieldValueError( public_message=(u'"{}" could not be encoded using the ' u'IDNA encoding'.format(ascii_str(value)))) value = ascii_str(value) return unicode(value.lower()) #3: `unicode`--
def default_public_message(self): """The aforementioned property.""" messages = [] if self.illegal_keys: messages.append(self.illegal_keys_msg_template.format( u', '.join(sorted('"{}"'.format(ascii_str(k)) for k in self.illegal_keys)))) if self.missing_keys: messages.append(self.missing_keys_msg_template.format( u', '.join(sorted('"{}"'.format(ascii_str(k)) for k in self.missing_keys)))) return u' '.join(messages)
def get_n6_default_client_properties_for_amqp_connection(): return { 'information': 'Host: {hostname}, ' 'PID: {pid_str}, ' 'script: {script_name}, ' 'args: {args!r}, ' 'modified: {mtime_str}'.format( hostname=ascii_str(n6lib.const.HOSTNAME), pid_str=str(os.getpid()), script_name=ascii_str(n6lib.const.SCRIPT_BASENAME), args=sys.argv[1:], mtime_str=_get_script_mtime_str(), ), }
def _clean_result_list(self, value, do_clean): checked_value_list = [] too_long = False for v in value: try: v = do_clean(v) except FieldValueTooLongError as exc: if exc.field is not self: raise too_long = True assert hasattr(self, 'max_length') assert exc.max_length == self.max_length v = exc.checked_value checked_value_list.append(v) if too_long: raise FieldValueTooLongError( field=self, checked_value=checked_value_list, max_length=self.max_length, public_message=( u'Length of at least one item of ' u'list {} is greater than {}'.format( ascii_str(value), self.max_length))) return checked_value_list
def _validate_value(self, value): super(UnicodeEnumField, self)._validate_value(value) if value not in self.enum_values: raise FieldValueError( public_message=(u'"{}" is not one of: {}'.format( ascii_str(value), u', '.join(u'"{}"'.format(v) for v in self.enum_values))))
def clean_param_value(self, value): """ The input `value` should be such a (:class:`str` or :class:`unicode`) string that ``value.lower()`` is equal to one of: * ``""`` or ``"yes"``, or ``"y"``, or ``"true"``, or ``"t"``, or ``"1"``, or ``"on"`` -- then the resultant cleaned value will be :obj:`True`; * ``"no"`` or ``"n"``, or ``"false"``, or ``"f"``, or ``"0"``, or ``"off"`` -- then the resultant cleaned value will be :obj:`False`; Note that when an empty string is given the resultant cleaned value will be :obj:`True` (!); thanks to this rule, a flag can be set by specifying the apropriate URL query parameter with no value (i.e., by using just its name). Returns: a :class:`bool` object (:obj:`True` or :obj:`False`). """ value = super(FlagField, self).clean_param_value(value) if not value: return True value = value.lower() try: value = string_to_bool(value) except ValueError: raise FieldValueError( public_message=(string_to_bool.PUBLIC_MESSAGE_PATTERN.format( ascii_str(value)))) return value
def _validate_value(self, value): super(UnicodeEnumField, self)._validate_value(value) if value not in self.enum_values: raise FieldValueError(public_message=( u'"{}" is not one of: {}'.format( ascii_str(value), u', '.join(u'"{}"'.format(v) for v in self.enum_values))))
def _parse_datetime_string(value): try: return parse_iso_datetime_to_utc(value) except Exception: raise FieldValueError(public_message=( u'"{}" is not a valid date + ' u'time specification'.format(ascii_str(value))))
def _clean_result_list(self, value, do_clean): checked_value_list = [] too_long = False for v in value: try: v = do_clean(v) except FieldValueTooLongError as exc: if exc.field is not self: raise too_long = True assert hasattr(self, 'max_length') assert exc.max_length == self.max_length v = exc.checked_value checked_value_list.append(v) if too_long: raise FieldValueTooLongError( field=self, checked_value=checked_value_list, max_length=self.max_length, public_message=(u'Length of at least one item of ' u'list {} is greater than {}'.format( ascii_str(value), self.max_length))) if isinstance(self.sort_result_list, collections.Mapping): checked_value_list.sort(**self.sort_result_list) elif self.sort_result_list: checked_value_list.sort() return checked_value_list
def default_public_message(self): """The aforementioned property.""" messages = [] for key, values, exc in self.error_info_seq: if isinstance(values, basestring): values = (values, ) assert isinstance(values, collections.Sequence) msg = self.msg_template.format( key=ascii_str(key), values_repr=u', '.join(u'"{}"'.format(ascii_str(val)) for val in values), optional_exc_public_message=(u' ({})'.format( exc.public_message.rstrip(u'.')) if isinstance( exc, _ErrorWithPublicMessageMixin) else u'')) messages.append(msg) return u' '.join(messages)
def _fix_value(self, value): value = super(IPv6Field, self)._fix_value(value) try: ipv6_obj = ipaddr.IPv6Address(value) except Exception: raise FieldValueError(public_message=( self.error_msg_template.format(ascii_str(value)))) return ipv6_obj
def default_public_message(self): """The aforementioned property.""" messages = [] for key, values, exc in self.error_info_seq: if isinstance(values, basestring): values = (values,) assert isinstance(values, collections.Sequence) msg = self.msg_template.format( key=ascii_str(key), values_repr=u', '.join( u'"{}"'.format(ascii_str(val)) for val in values), optional_exc_public_message=( u' ({})'.format(exc.public_message.rstrip(u'.')) if isinstance(exc, _ErrorWithPublicMessageMixin) else u'')) messages.append(msg) return u' '.join(messages)
def _validate_value(self, value): super(UnicodeLimitedField, self)._validate_value(value) if len(value) > self.max_length: raise FieldValueTooLongError( field=self, checked_value=value, max_length=self.max_length, public_message=(u'Length of "{}" is greater than {}'.format( ascii_str(value), self.max_length)))
def _prepare_single_raw_value(val): if isinstance(val, basestring): return val if isinstance(val, (bool, int, long, float)): return str(val) if isinstance(val, datetime.datetime): return str(datetime_utc_normalize(val)) raise TypeError('unsupported class {} of parameter value {!r}'.format( ascii_str(val.__class__.__name__), val))
def _fix_value(self, value): value = super(DomainNameSubstringField, self)._fix_value(value) try: ascii_value = value.encode('idna') except ValueError: raise FieldValueError(public_message=( u'"{}" could not be encoded using the ' u'IDNA encoding'.format(ascii_str(value)))) return unicode(ascii_value.lower())
def iter_deduplicated_params(self): assert isinstance(self, AbstractViewBase) params = super(SingleParamValuesViewMixin, self).iter_deduplicated_params() for param_name, values in params: if len(values) != 1: raise ParamCleaningError(public_message=( 'Received a request with more than or less than exactly one ' 'value of the parameter "{}".'.format(ascii_str(param_name)))) yield (param_name, values[0]) # <- here: the value itself (not a list of values)
def _clean_value(self, value): try: value = self._coerce_value(value) self._check_range(value) except FieldValueError: if self.error_msg_template is None: raise raise FieldValueError(public_message=( self.error_msg_template.format(ascii_str(value)))) return value
def _fix_value(self, value): value = super(IPv6NetField, self)._fix_value(value) try: if '/' not in value: raise ValueError ipv6_network_obj = ipaddr.IPv6Network(value) except Exception: raise FieldValueError(public_message=( self.error_msg_template.format(ascii_str(value)))) return ipv6_network_obj
def _fix_value(self, value): if isinstance(value, str): try: value = value.decode(self.encoding, self.decode_error_handling) except UnicodeError: raise FieldValueError(public_message=( u'"{}" cannot be decoded with encoding "{}"'.format( ascii_str(value), self.encoding))) assert isinstance(value, unicode) return value
def _validate_value(self, value): super(HexDigestField, self)._validate_value(value) try: value.decode('hex') if len(value) != self.num_of_characters: raise ValueError except (TypeError, ValueError): raise FieldValueError( public_message=(u'"{}" is not a valid {} hash'.format( ascii_str(value), self.hash_algo_descr)))
def clean_result_value(self, value): if not isinstance(value, basestring): try: ip, net = value value = '{}/{}'.format(ip, net) except (ValueError, TypeError): raise FieldValueError(public_message=( self.error_msg_template.format(ascii_str(value)))) # returning a unicode string return super(IPv4NetField, self).clean_result_value(value)
def _validate_value(self, value): super(HexDigestField, self)._validate_value(value) try: value.decode('hex') if len(value) != self.num_of_characters: raise ValueError except (TypeError, ValueError): raise FieldValueError(public_message=( u'"{}" is not a valid {} hash'.format( ascii_str(value), self.hash_algo_descr)))
def iter_deduplicated_params(self): params = self.get_params_from_request() for key in params: if not isinstance(key, str): raise AssertionError('{!a} is not a str'.format(key)) values = params.getall(key) if not (values and isinstance(values, list) and all(isinstance(val, str) for val in values)): raise AssertionError('{}={!a}, *not* being a non-empty list ' 'of str!'.format(ascii_str(key), values)) yield key, self.preprocess_param_values(key, values)
def _prepare_single_raw_value(val): if isinstance(val, str): return val if isinstance(val, (bytes, bytearray)): return val.decode('utf-8') if isinstance(val, (bool, int, float)): return str(val) if isinstance(val, datetime.datetime): return str(datetime_utc_normalize(val)) raise TypeError('unsupported class {} of parameter value {!a}'.format( ascii_str(val.__class__.__name__), val))
def iter_deduplicated_params(self): assert isinstance(self, AbstractViewBase) params = super(OmittingEmptyParamsViewMixin, self).iter_deduplicated_params() for param_name, values in params: if not (isinstance(values, list) and all(isinstance(val, basestring) for val in values)): raise TypeError('{}={!r}, not being a list of strings' .format(ascii_str(param_name), values)) nonempty_values = list(filter(None, values)) if nonempty_values: yield param_name, nonempty_values
def clean_result_value(self, value): str = unicode #3-- if not isinstance(value, (str, bytes, bytearray)): try: ip, net = value value = '{}/{}'.format(as_unicode(ip), as_unicode(net)) except (ValueError, TypeError): raise FieldValueError(public_message=( self.error_msg_template.format(ascii_str(value)))) # returning a str return super(IPv4NetField, self).clean_result_value(value)
def iter_deduplicated_params(self): chain_iterables = itertools.chain.from_iterable params = self.request.params for key in params: values = params.getall(key) if not (values and all(isinstance(val, basestring) for val in values)): raise AssertionError( '{}={!r}, *not* being a non-empty sequence ' 'of strings!'.format(ascii_str(key), values)) yield key, list(chain_iterables(self.iter_values_from_param_value(val) for val in values))
def exc_to_http_exc(exc): """ Takes any :exc:`~exceptions.Exception` instance, returns a :exc:`pyramid.httpexceptions.HTTPException` instance. """ if isinstance(exc, HTTPException): code = getattr(exc, 'code', None) if isinstance(code, (int, long)) and 200 <= code < 500: LOGGER.debug( 'HTTPException: %r ("%s", code: %s)', exc, ascii_str(exc), code) else: LOGGER.error( 'HTTPException: %r ("%s", code: %r)', exc, ascii_str(exc), code, exc_info=True) http_exc = exc elif isinstance(exc, AuthorizationError): LOGGER.debug( 'Authorization not successful: %r (public message: "%s")', exc, ascii_str(exc.public_message)) http_exc = HTTPForbidden(exc.public_message) elif isinstance(exc, ParamCleaningError): LOGGER.debug( 'Request parameters not valid: %r (public message: "%s")', exc, ascii_str(exc.public_message)) http_exc = HTTPBadRequest(exc.public_message) elif isinstance(exc, TooMuchDataError): LOGGER.debug( 'Too much data requested: %r (public message: "%s")', exc, ascii_str(exc.public_message)) http_exc = HTTPForbidden(exc.public_message) else: if isinstance(exc, DataAPIError): if isinstance(exc, ResultCleaningError): LOGGER.error( 'Result cleaning error: %r (public message: "%s")', exc, ascii_str(exc.public_message), exc_info=True) else: LOGGER.error( '%r (public message: "%s")', exc, ascii_str(exc.public_message), exc_info=True) public_message = ( None if exc.public_message == DataAPIError.default_public_message else exc.public_message) else: LOGGER.error( 'Non-HTTPException/DataAPIError exception: %r', exc, exc_info=True) public_message = None http_exc = HTTPServerError(public_message) return http_exc
def exc_to_http_exc(cls, exc): """ Takes any :exc:`~exceptions.Exception` instance, returns a :exc:`pyramid.httpexceptions.HTTPException` instance. """ if isinstance(exc, HTTPException): code = getattr(exc, 'code', None) if isinstance(code, (int, long)) and 200 <= code < 500: LOGGER.debug( 'HTTPException: %r ("%s", code: %s)', exc, ascii_str(exc), code) else: LOGGER.error( 'HTTPException: %r ("%s", code: %r)', exc, ascii_str(exc), code, exc_info=True) http_exc = exc elif isinstance(exc, AuthorizationError): LOGGER.debug( 'Authorization not successful: %r (public message: "%s")', exc, ascii_str(exc.public_message)) http_exc = HTTPForbidden(exc.public_message) elif isinstance(exc, ParamCleaningError): LOGGER.debug( 'Request parameters not valid: %r (public message: "%s")', exc, ascii_str(exc.public_message)) http_exc = HTTPBadRequest(exc.public_message) elif isinstance(exc, TooMuchDataError): LOGGER.debug( 'Too much data requested: %r (public message: "%s")', exc, ascii_str(exc.public_message)) http_exc = HTTPForbidden(exc.public_message) else: if isinstance(exc, DataAPIError): if isinstance(exc, ResultCleaningError): LOGGER.error( 'Result cleaning error: %r (public message: "%s")', exc, ascii_str(exc.public_message), exc_info=True) else: LOGGER.error( '%r (public message: "%s")', exc, ascii_str(exc.public_message), exc_info=True) public_message = ( None if exc.public_message == DataAPIError.default_public_message else exc.public_message) else: LOGGER.error( 'Non-HTTPException/DataAPIError exception: %r', exc, exc_info=True) public_message = None http_exc = HTTPServerError(public_message) return http_exc
def __init__(self, **kwargs): super(UnicodeLimitedField, self).__init__(**kwargs) if self.max_length is None: raise TypeError("'max_length' not specified for {} " "(neither as a class attribute nor " "as a constructor argument)" .format(self.__class__.__name__)) if self.max_length < 1: raise ValueError("'max_length' specified for {} should " "not be lesser than 1 ({} given)" .format(self.__class__.__name__, ascii_str(self.max_length)))
def _coerce_value(self, value): try: coerced_value = self._do_coerce(value) # e.g. float is OK *only* if it is an integer number (such as 42.0) if not isinstance(value, basestring) and coerced_value != value: raise ValueError except (TypeError, ValueError): raise FieldValueError( public_message=(u'"{}" cannot be interpreted as an ' u'integer number'.format(ascii_str(value)))) assert isinstance(coerced_value, (int, long)) # long if > sys.maxint return coerced_value
def __init__(self, **kwargs): super(UnicodeLimitedField, self).__init__(**kwargs) if self.max_length is None: raise TypeError("'max_length' not specified for {} " "(neither as a class attribute nor " "as a constructor argument)".format( self.__class__.__name__)) if self.max_length < 1: raise ValueError("'max_length' specified for {} should " "not be lesser than 1 ({} given)".format( self.__class__.__name__, ascii_str(self.max_length)))
def _coerce_value(self, value): try: coerced_value = self._do_coerce(value) # e.g. float is OK *only* if it is an integer number (such as 42.0) if not isinstance(value, basestring) and coerced_value != value: raise ValueError except (TypeError, ValueError): raise FieldValueError(public_message=( u'"{}" cannot be interpreted as an ' u'integer number'.format(ascii_str(value)))) assert isinstance(coerced_value, (int, long)) # long if > sys.maxint return coerced_value
def _fix_value(self, value): if isinstance(value, (bytes, bytearray)): try: value = value.decode(self.encoding, self.decode_error_handling) except UnicodeError: raise FieldValueError(public_message=( u'"{}" cannot be decoded with encoding "{}"'.format( ascii_str(value), self.encoding))) assert isinstance(value, unicode) #3: `unicode`->`str` if self.auto_strip: value = value.strip() return value
def _validate_value(self, value): super(UnicodeLimitedField, self)._validate_value(value) if self.checking_bytes_length: value = value.encode(self.encoding) if len(value) > self.max_length: raise FieldValueTooLongError( field=self, checked_value=value, max_length=self.max_length, public_message=( u'Length of "{}" is greater than {}'.format( ascii_str(value), self.max_length)))
def _fix_value(self, value): value = super(IPv6Field, self)._fix_value(value) try: _, _, last_segment = value.rpartition(':') if '.' in last_segment and not IPv4_STRICT_DECIMAL_REGEX.search( last_segment): raise ValueError('{!r} is not a valid IPv4-like suffix'.format( last_segment)) ipv6_obj = ipaddress.IPv6Address(value) except Exception: raise FieldValueError(public_message=( self.error_msg_template.format(ascii_str(value)))) return ipv6_obj
def _validate_value(self, value): super(HexDigestField, self)._validate_value(value) try: if hasattr(bytes, 'fromhex'): #3-- bytes.fromhex(value) #3: keep just `bytes.fromhex(value)` else: value.decode('hex') #3-- if len(value) != self.num_of_characters: raise ValueError('expected length: {!r} (got: {!r})'.format( self.num_of_characters, len(value))) except (TypeError, ValueError): raise FieldValueError( public_message=(u'"{}" is not a valid {} hash'.format( ascii_str(value), self.hash_algo_descr)))
def _coerce_value(self, value): try: coerced_value = self._do_coerce(value) # e.g. float is OK *only* if it is an integer number (such as 42.0) str = unicode #3-- if not isinstance( value, (str, bytes, bytearray)) and coerced_value != value: raise ValueError except (TypeError, ValueError): raise FieldValueError( public_message=(u'"{}" cannot be interpreted as an ' u'integer number'.format(ascii_str(value)))) assert isinstance(coerced_value, (int, long)) #3: `long`-- return coerced_value
def clean_result_value(self, value): str = unicode #3-- if not isinstance(value, (str, bytes, bytearray)): try: ipv6_raw, prefixlen_raw = value value = '{}/{}'.format(as_unicode(ipv6_raw), as_unicode(prefixlen_raw)) except (ValueError, TypeError): raise FieldValueError(public_message=( self.error_msg_template.format(ascii_str(value)))) ipv6_obj, prefixlen = super(IPv6NetField, self).clean_result_value(value) assert isinstance(ipv6_obj, ipaddress.IPv6Address) ipv6 = str(ipv6_obj.compressed) #3: `str(`-- `)`-- assert isinstance(ipv6, str) assert isinstance(prefixlen, int) and 0 <= prefixlen <= 128, prefixlen # returning a str return str('{}/{}'.format(ipv6, prefixlen)) #3: `str(`-- `)`--
def _validate_value(self, value): super(UnicodeRegexField, self)._validate_value(value) if self.regex.search(value) is None: raise FieldValueError(public_message=( self.error_msg_template.format(ascii_str(value))))