def json_string_normalizer(self, normalize, code, message, chunk):
     if normalize:
         data = self.json_normalizer(code, message, '{response}')
         if code < 300:
             chunk = compat.binary_type(data).replace("'{response}'", chunk)
         else:
             del data['response']
             chunk = compat.binary_type(data)
         normalize = False
     return normalize, chunk
 def complex_response(self,
                      chunk,
                      code=None,
                      message=None,
                      normalize=False,
                      **kwargs):
     try:
         normalize = normalize or self._json_forcing_normalization
         if isinstance(chunk, Exception):
             trace([self.request, self.request.arguments])
             code = getattr(chunk, 'code', code) or ERROR_CODE
             message = getattr(chunk, 'message', compat.binary_type(chunk))\
                 or message or ERROR_MESSAGE
             normalize = True
             chunk = None
         elif isinstance(chunk, Cursor):
             chunk = mongodb_dumps(chunk)
             normalize, chunk = \
                 self.json_string_normalizer(normalize, code, message, chunk)
         elif hasattr(chunk, 'to_json'):
             chunk = getattr(chunk, 'to_json')()
             normalize, chunk = \
                 self.json_string_normalizer(normalize, code, message, chunk)
         elif not isinstance(chunk, compat.string_type):
             for func in constants.PRIMITIVE_METHODS:
                 if hasattr(chunk, func):
                     chunk = getattr(chunk, func)()
                     break
         if not normalize:
             return self.json_response(chunk, **kwargs)
         self.normalize_response(code, message, chunk, **kwargs)
     except Exception, e:
         error = ApiResponseError(e.message)
         self.complex_response(error)
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 check_requirements(file_name):
    errors = []
    for item in parse_requirements(file_name):
        item.check_if_exists()
        if not item.satisfied_by:
            errors.append(item)
    if errors:
        errors = [compat.binary_type(e) for e in errors]
        raise RuntimeError('Requirements not installed: %s' %
                           ', '.join(errors))
def want_bytes(value, encoding=constants.ENCODING, errors='strict'):
    if isinstance(value, compat.text_type):
        value = value.encode(encoding, errors)
    return compat.binary_type(value)
def pack_placemark(iso_country, country, state, locality, postal_code):
    values = [
        base64_hex_encode(compat.binary_type(want_bytes(item)), False)
        for item in (iso_country, country, state, locality, postal_code)
    ]
    return ','.join(values)
Example #7
0
def to_string(value):
    if isinstance(value, float):
        value = repr(value)
    return compat.binary_type(value)