def safecompare(one, two): one = want_bytes(one) two = want_bytes(two) if len(one) != len(two): return False rv = 0 for x, y in izip(one, two): rv |= ord(x) ^ ord(y) return rv == 0
def format_log_argument(value): value = want_bytes(value) max_len = 256 if len(value) <= max_len: return value return '%s...[%d bytes truncated]' % (value[:max_len], len(value) - max_len)
def complex_types(value): if isinstance(value, compat.text_type): return want_bytes(value) elif isinstance(value, compat.primitive_type): return value elif isinstance(value, compat.datetime_type): return value.isoformat() return compat.binary_type(value)
def random_string(length=DEFAULT_LENGTH): h = string.ascii_letters + string.digits + string.punctuation \ + datetime.utcnow().strftime('%Y%m%d%H%M%S%f') if not compat.sys_random: compat.random.seed( compat.SHA256.new( want_bytes(('%s%s%s' % compat.random.getstate(), time.time(), os.urandom(32)))).digest()) return ''.join([compat.random.choice(h) for _ in xrange(length)])
def _exec_loads(serializer, module, value, **kwargs): try: if serializer in constants.JSON_SERIALIZER \ and not compat.ujson_available: kwargs.setdefault('object_hook', parser_types) value = module.loads(want_bytes(value), **kwargs) if serializer not in constants.JSON_SERIALIZER \ or compat.ujson_available: value = deserialize_complex_types(value) return value except: return None
def wrapper(self, *args, **kwargs): args = list(args) value = kwargs.get('host_pattern', args[0]) if not value: value = DEFAULT_HOST if not value.endswith(DEFAULT_HOST_END): value += DEFAULT_HOST_END args[0] = value value = kwargs.get('host_handlers', args[1]) if isinstance(value, compat.string_type): args[1] = [want_bytes(value)] elif not isinstance(value, compat.list_type): raise ConfigurationError('Invalid argument type, "host_pattern" ' 'must be a string or a list') return method(self, *args, **kwargs)
def parser_types(value): if isinstance(value, compat.string_type): if rx_integer.search(value): return int(value) elif rx_float.search(value): return float(value) elif rx_datetime_marker.search(value) \ or rx_isoformat_date.search(value) \ or rx_isoformat_time.search(value) \ or rx_isoformat.search(value): return datetime_parser(value) elif value.lower() in compat.true_values: return True elif value.lower() in compat.false_values: return False elif isinstance(value, compat.list_type): for index, item in enumerate(value): value[index] = parser_types(item) elif isinstance(value, dict): for key, item in value.iteritems(): value[key] = parser_types(item) elif not isinstance(value, compat.primitive_type): value = want_bytes(value) return value
def base64_dumps(value): value = want_bytes(value) return base64.urlsafe_b64encode(value).strip(b'=')
def base64_loads(value): value = want_bytes(value, constants.ASCII, 'ignore') return base64.urlsafe_b64decode(value + b'=' * (-len(value) % 4))