Beispiel #1
0
def generate(url):
    parts = [
        '''\
"""

    webencodings.labels
    ~~~~~~~~~~~~~~~~~~~

    Map encoding labels to their name.

    :copyright: Copyright 2012 by Simon Sapin
    :license: BSD, see LICENSE for details.

"""

# XXX Do not edit!
# This file is automatically generated by mklabels.py

LABELS = {
'''
    ]
    labels = [
        (repr(assert_lower(label)).lstrip('u'),
         repr(encoding['name']).lstrip('u'))
        for category in json_01.loads(urlopen(url).read().decode('ascii'))
        for encoding in category['encodings'] for label in encoding['labels']
    ]
    max_len = max(len(label) for label, name in labels)
    parts.extend('    %s:%s %s,\n' % (label, ' ' *
                                      (max_len - len(label)), name)
                 for label, name in labels)
    parts.append('}')
    return ''.join(parts)
Beispiel #2
0
 def bound_data(self, data, initial):
     if self.disabled:
         return initial
     try:
         return json_01.loads(data)
     except json_01.JSONDecodeError:
         return InvalidJSONInput(data)
Beispiel #3
0
 def _parse_json(self, response, **extra):
     if not hasattr(response, '_json'):
         if not JSON_CONTENT_TYPE_RE.match(response.get('Content-Type')):
             raise ValueError(
                 'Content-Type header is "{0}", not "application/json"'.
                 format(response.get('Content-Type')))
         response._json = json_01.loads(
             response.content.decode(response.charset), **extra)
     return response._json
Beispiel #4
0
 def to_python(self, value):
     if isinstance(value, str):
         # Assume we're deserializing
         vals = json_01.loads(value)
         for end in ('lower', 'upper'):
             if end in vals:
                 vals[end] = self.base_field.to_python(vals[end])
         value = self.range_type(**vals)
     elif isinstance(value, (list, tuple)):
         value = self.range_type(value[0], value[1])
     return value
Beispiel #5
0
def Deserializer(stream_or_string, **options):
    """Deserialize a stream or string of JSON data."""
    if not isinstance(stream_or_string, (bytes, str)):
        stream_or_string = stream_or_string.read()
    if isinstance(stream_or_string, bytes):
        stream_or_string = stream_or_string.decode()
    try:
        objects = json_01.loads(stream_or_string)
        yield from PythonDeserializer(objects, **options)
    except (GeneratorExit, DeserializationError):
        raise
    except Exception as exc:
        raise DeserializationError() from exc
Beispiel #6
0
 def load_manifest(self):
     content = self.read_manifest()
     if content is None:
         return {}
     try:
         stored = json_01.loads(content)
     except json_01.JSONDecodeError:
         pass
     else:
         version = stored.get('version')
         if version == '1.0':
             return stored.get('paths', {})
     raise ValueError("Couldn't load manifest '%s' (version %s)" %
                      (self.manifest_name, self.manifest_version))
Beispiel #7
0
 def __init__(self,
              path=None,
              fileobj=None,
              mapping=None,
              scheme='default'):
     if [path, fileobj, mapping].count(None) < 2:
         raise TypeError('path, fileobj and mapping are exclusive')
     self._legacy = None
     self._data = None
     self.scheme = scheme
     #import pdb; pdb.set_trace()
     if mapping is not None:
         try:
             self._validate_mapping(mapping, scheme)
             self._data = mapping
         except MetadataUnrecognizedVersionError:
             self._legacy = LegacyMetadata(mapping=mapping, scheme=scheme)
             self.validate()
     else:
         data = None
         if path:
             with open(path, 'rb') as f:
                 data = f.read()
         elif fileobj:
             data = fileobj.read()
         if data is None:
             # Initialised with no args - to be added
             self._data = {
                 'metadata_version': self.METADATA_VERSION,
                 'generator': self.GENERATOR,
             }
         else:
             if not isinstance(data, text_type):
                 data = data.decode('utf-8')
             try:
                 self._data = json_01.loads(data)
                 self._validate_mapping(self._data, scheme)
             except ValueError:
                 # Note: MetadataUnrecognizedVersionError does not
                 # inherit from ValueError (it's a DistlibException,
                 # which should not inherit from ValueError).
                 # The ValueError comes from the json.load - if that
                 # succeeds and we get a validation error, we want
                 # that to propagate
                 self._legacy = LegacyMetadata(fileobj=StringIO(data),
                                               scheme=scheme)
                 self.validate()
Beispiel #8
0
    def _loads_v2(self, request, data):
        try:
            cached = json_01.loads(zlib.decompress(data).decode("utf8"))
        except (ValueError, zlib.error):
            return

        # We need to decode the items that we've base64 encoded
        cached["response"]["body"] = _b64_decode_bytes(
            cached["response"]["body"])
        cached["response"]["headers"] = dict(
            (_b64_decode_str(k), _b64_decode_str(v))
            for k, v in cached["response"]["headers"].items())
        cached["response"]["reason"] = _b64_decode_str(
            cached["response"]["reason"])
        cached["vary"] = dict(
            (_b64_decode_str(k), _b64_decode_str(v) if v is not None else v)
            for k, v in cached["vary"].items())

        return self.prepare_response(request, cached)
Beispiel #9
0
 def to_python(self, value):
     if self.disabled:
         return value
     if value in self.empty_values:
         return None
     elif isinstance(value, (list, dict, int, float, JSONString)):
         return value
     try:
         converted = json_01.loads(value)
     except json_01.JSONDecodeError:
         raise forms.ValidationError(
             self.error_messages['invalid'],
             code='invalid',
             params={'value': value},
         )
     if isinstance(converted, str):
         return JSONString(converted)
     else:
         return converted
Beispiel #10
0
    def get_change_message(self):
        """
        If self.change_message is a JSON structure, interpret it as a change
        string, properly translated.
        """
        if self.change_message and self.change_message[0] == '[':
            try:
                change_message = json_01.loads(self.change_message)
            except json_01.JSONDecodeError:
                return self.change_message
            messages = []
            for sub_message in change_message:
                if 'added' in sub_message:
                    if sub_message['added']:
                        sub_message['added']['name'] = gettext(sub_message['added']['name'])
                        messages.append(gettext('Added {name} “{object}”.').format(**sub_message['added']))
                    else:
                        messages.append(gettext('Added.'))

                elif 'changed' in sub_message:
                    sub_message['changed']['fields'] = get_text_list(
                        [gettext(field_name) for field_name in sub_message['changed']['fields']], gettext('and')
                    )
                    if 'name' in sub_message['changed']:
                        sub_message['changed']['name'] = gettext(sub_message['changed']['name'])
                        messages.append(gettext('Changed {fields} for {name} “{object}”.').format(
                            **sub_message['changed']
                        ))
                    else:
                        messages.append(gettext('Changed {fields}.').format(**sub_message['changed']))

                elif 'deleted' in sub_message:
                    sub_message['deleted']['name'] = gettext(sub_message['deleted']['name'])
                    messages.append(gettext('Deleted {name} “{object}”.').format(**sub_message['deleted']))

            change_message = ' '.join(msg[0].upper() + msg[1:] for msg in messages)
            return change_message or gettext('No fields changed.')
        else:
            return self.change_message
Beispiel #11
0
    def _decode(self, data):
        """
        Safely decode an encoded text stream back into a list of messages.

        If the encoded text stream contained an invalid hash or was in an
        invalid format, return None.
        """
        if not data:
            return None
        bits = data.split('$', 1)
        if len(bits) == 2:
            hash, value = bits
            if constant_time_compare(hash, self._hash(value)):
                try:
                    # If we get here (and the JSON decode works), everything is
                    # good. In any other case, drop back and return None.
                    return json_01.loads(value, cls=MessageDecoder)
                except json_01.JSONDecodeError:
                    pass
        # Mark the data as used (so it gets removed) since something was wrong
        # with the data.
        self.used = True
        return None
Beispiel #12
0
    def to_python(self, value):
        if not value:
            return {}
        if not isinstance(value, dict):
            try:
                value = json_01.loads(value)
            except json_01.JSONDecodeError:
                raise ValidationError(
                    self.error_messages['invalid_json'],
                    code='invalid_json',
                )

        if not isinstance(value, dict):
            raise ValidationError(
                self.error_messages['invalid_format'],
                code='invalid_format',
            )

        # Cast everything to strings for ease.
        for key, val in value.items():
            if val is not None:
                val = str(val)
            value[key] = val
        return value
Beispiel #13
0
 def loads(self, data):
     return json_01.loads(data.decode('latin-1'))
Beispiel #14
0
d = {c: 0 for c in a}
# print(d)

# for c in enumerate(a):
#     print(c)
# for i, c in enumerate(a):
#     print(i, c)
d = {i: c for i, c in enumerate(a)}
# print(d)
# -----------------------------

import json_01
j1 = '{"ip": "8.8.8.8"}'
# print(j1)
# loads = 문자열일때 , load = 파일일때
d1 = json_01.loads(j1)
# print(d1, type(d1))
# print(d1['ip'])

j2 = '''{
   "Accept-Language": "en-US,en;q=0.8",
   "Host": "headers.jsontest.com",
   "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
   "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
}'''
print(j2, type(j2))
d2 = json_01.loads(j2)
print(d2, type(d2))
print('-' * 30)
jj2 = json_01.dumps(d2)
print(jj2, type(jj2))
Beispiel #15
0
    def __init__(self, ds_input, write=False):
        self._write = 1 if write else 0
        Driver.ensure_registered()

        # Preprocess json inputs. This converts json strings to dictionaries,
        # which are parsed below the same way as direct dictionary inputs.
        if isinstance(ds_input, str) and json_regex.match(ds_input):
            ds_input = json_01.loads(ds_input)

        # If input is a valid file path, try setting file as source.
        if isinstance(ds_input, str):
            try:
                # GDALOpen will auto-detect the data source type.
                self._ptr = capi.open_ds(force_bytes(ds_input), self._write)
            except GDALException as err:
                raise GDALException(
                    'Could not open the datasource at "{}" ({}).'.format(
                        ds_input, err))
        elif isinstance(ds_input, bytes):
            # Create a new raster in write mode.
            self._write = 1
            # Get size of buffer.
            size = sys.getsizeof(ds_input)
            # Pass data to ctypes, keeping a reference to the ctypes object so
            # that the vsimem file remains available until the GDALRaster is
            # deleted.
            self._ds_input = c_buffer(ds_input)
            # Create random name to reference in vsimem filesystem.
            vsi_path = os.path.join(VSI_FILESYSTEM_BASE_PATH,
                                    str(uuid.uuid4()))
            # Create vsimem file from buffer.
            capi.create_vsi_file_from_mem_buffer(
                force_bytes(vsi_path),
                byref(self._ds_input),
                size,
                VSI_TAKE_BUFFER_OWNERSHIP,
            )
            # Open the new vsimem file as a GDALRaster.
            try:
                self._ptr = capi.open_ds(force_bytes(vsi_path), self._write)
            except GDALException:
                # Remove the broken file from the VSI filesystem.
                capi.unlink_vsi_file(force_bytes(vsi_path))
                raise GDALException(
                    'Failed creating VSI raster from the input buffer.')
        elif isinstance(ds_input, dict):
            # A new raster needs to be created in write mode
            self._write = 1

            # Create driver (in memory by default)
            driver = Driver(ds_input.get('driver', 'MEM'))

            # For out of memory drivers, check filename argument
            if driver.name != 'MEM' and 'name' not in ds_input:
                raise GDALException(
                    'Specify name for creation of raster with driver "{}".'.
                    format(driver.name))

            # Check if width and height where specified
            if 'width' not in ds_input or 'height' not in ds_input:
                raise GDALException(
                    'Specify width and height attributes for JSON or dict input.'
                )

            # Check if srid was specified
            if 'srid' not in ds_input:
                raise GDALException('Specify srid for JSON or dict input.')

            # Create null terminated gdal options array.
            papsz_options = []
            for key, val in ds_input.get('papsz_options', {}).items():
                option = '{}={}'.format(key, val)
                papsz_options.append(option.upper().encode())
            papsz_options.append(None)

            # Convert papszlist to ctypes array.
            papsz_options = (c_char_p * len(papsz_options))(*papsz_options)

            # Create GDAL Raster
            self._ptr = capi.create_ds(
                driver._ptr,
                force_bytes(ds_input.get('name', '')),
                ds_input['width'],
                ds_input['height'],
                ds_input.get('nr_of_bands', len(ds_input.get('bands', []))),
                ds_input.get('datatype', 6),
                byref(papsz_options),
            )

            # Set band data if provided
            for i, band_input in enumerate(ds_input.get('bands', [])):
                band = self.bands[i]
                if 'nodata_value' in band_input:
                    band.nodata_value = band_input['nodata_value']
                    # Instantiate band filled with nodata values if only
                    # partial input data has been provided.
                    if band.nodata_value is not None and (
                            'data' not in band_input or 'size' in band_input
                            or 'shape' in band_input):
                        band.data(data=(band.nodata_value, ), shape=(1, 1))
                # Set band data values from input.
                band.data(
                    data=band_input.get('data'),
                    size=band_input.get('size'),
                    shape=band_input.get('shape'),
                    offset=band_input.get('offset'),
                )

            # Set SRID
            self.srs = ds_input.get('srid')

            # Set additional properties if provided
            if 'origin' in ds_input:
                self.origin.x, self.origin.y = ds_input['origin']

            if 'scale' in ds_input:
                self.scale.x, self.scale.y = ds_input['scale']

            if 'skew' in ds_input:
                self.skew.x, self.skew.y = ds_input['skew']
        elif isinstance(ds_input, c_void_p):
            # Instantiate the object using an existing pointer to a gdal raster.
            self._ptr = ds_input
        else:
            raise GDALException('Invalid data source input type: "{}".'.format(
                type(ds_input)))
Beispiel #16
0
 def deserialize_messages(self, data):
     if data and isinstance(data, str):
         return json_01.loads(data, cls=MessageDecoder)
     return data
Beispiel #17
0
 def to_python(self, value):
     if isinstance(value, str):
         value = json_01.loads(value)
     return value
Beispiel #18
0
 def to_python(self, value):
     if isinstance(value, str):
         # Assume we're deserializing
         vals = json_01.loads(value)
         value = [self.base_field.to_python(val) for val in vals]
     return value