Ejemplo n.º 1
0
def pbkdf2(password, salt, iterations, dklen=0, digest=None):
    """Return the hash of password using pbkdf2."""
    if digest is None:
        digest = hashlib.sha256
    dklen = dklen or None
    password = force_bytes(password)
    salt = force_bytes(salt)
    return hashlib.pbkdf2_hmac(digest().name, password, salt, iterations,
                               dklen)
Ejemplo n.º 2
0
 def test_force_bytes_exception(self):
     """
     force_bytes knows how to convert to bytes an exception
     containing non-ASCII characters in its args.
     """
     error_msg = "This is an exception, voilà"
     exc = ValueError(error_msg)
     self.assertEqual(force_bytes(exc), error_msg.encode())
     self.assertEqual(force_bytes(exc, encoding='ascii', errors='ignore'), b'This is an exception, voil')
Ejemplo n.º 3
0
def _generate_cache_key(request, method, headerlist, key_prefix):
    """Return a cache key from the headers given in the header list."""
    ctx = hashlib.md5()
    for header in headerlist:
        value = request.META.get(header)
        if value is not None:
            ctx.update(force_bytes(value))
    url = hashlib.md5(force_bytes(iri_to_uri(request.build_absolute_uri())))
    cache_key = 'views.decorators.cache.cache_page.%s.%s.%s.%s' % (
        key_prefix, method, url.hexdigest(), ctx.hexdigest())
    return _i18n_cache_key_suffix(request, cache_key)
Ejemplo n.º 4
0
    def __init__(self, param, cursor, strings_only=False):
        # With raw SQL queries, datetimes can reach this function
        # without being converted by DateTimeField.get_db_prep_value.
        if settings.USE_TZ and (isinstance(param, datetime.datetime)
                                and not isinstance(param, Oracle_datetime)):
            param = Oracle_datetime.from_datetime(param)

        string_size = 0
        # Oracle doesn't recognize True and False correctly.
        if param is True:
            param = 1
        elif param is False:
            param = 0
        if hasattr(param, 'bind_parameter'):
            self.force_bytes = param.bind_parameter(cursor)
        elif isinstance(param, (Database.Binary, datetime.timedelta)):
            self.force_bytes = param
        else:
            # To transmit to the database, we need Unicode if supported
            # To get size right, we must consider bytes.
            self.force_bytes = force_text(param, cursor.charset, strings_only)
            if isinstance(self.force_bytes, str):
                # We could optimize by only converting up to 4000 bytes here
                string_size = len(
                    force_bytes(param, cursor.charset, strings_only))
        if hasattr(param, 'input_size'):
            # If parameter has `input_size` attribute, use that.
            self.input_size = param.input_size
        elif string_size > 4000:
            # Mark any string param greater than 4000 characters as a CLOB.
            self.input_size = Database.CLOB
        elif isinstance(param, datetime.datetime):
            self.input_size = Database.TIMESTAMP
        else:
            self.input_size = None
Ejemplo n.º 5
0
 def generic(self, method, path, data='',
             content_type='application/octet-stream', secure=False,
             **extra):
     """Construct an arbitrary HTTP request."""
     parsed = urlparse(str(path))  # path can be lazy
     data = force_bytes(data, settings.DEFAULT_CHARSET)
     r = {
         'PATH_INFO': self._get_path(parsed),
         'REQUEST_METHOD': method,
         'SERVER_PORT': '443' if secure else '80',
         'wsgi.url_scheme': 'https' if secure else 'http',
     }
     if data:
         r.update({
             'CONTENT_LENGTH': str(len(data)),
             'CONTENT_TYPE': content_type,
             'wsgi.input': FakePayload(data),
         })
     r.update(extra)
     # If QUERY_STRING is absent or empty, we want to extract it from the URL.
     if not r.get('QUERY_STRING'):
         # WSGI requires latin-1 encoded strings. See get_path_info().
         query_string = parsed[4].encode().decode('iso-8859-1')
         r['QUERY_STRING'] = query_string
     return self.request(**r)
Ejemplo n.º 6
0
 def attr_value(self, target, index=0):
     """
     The attribute value for the given target node (e.g. 'PROJCS'). The index
     keyword specifies an index of the child node to return.
     """
     if not isinstance(target, str) or not isinstance(index, int):
         raise TypeError
     return capi.get_attr_value(self.ptr, force_bytes(target), index)
Ejemplo n.º 7
0
 def test_capability(self, capability):
     """
     Return a bool indicating whether the this Layer supports the given
     capability (a string).  Valid capability strings include:
       'RandomRead', 'SequentialWrite', 'RandomWrite', 'FastSpatialFilter',
       'FastFeatureCount', 'FastGetExtent', 'CreateField', 'Transactions',
       'DeleteFeature', and 'FastSetNextByIndex'.
     """
     return bool(capi.test_capability(self.ptr, force_bytes(capability)))
Ejemplo n.º 8
0
 def _hash_generator(*args):
     """
     Generate a 32-bit digest of a set of arguments that can be used to
     shorten identifying names.
     """
     h = hashlib.md5()
     for arg in args:
         h.update(force_bytes(arg))
     return h.hexdigest()[:6]
Ejemplo n.º 9
0
 def relate_pattern(self, other, pattern):
     """
     Return true if the elements in the DE-9IM intersection matrix for the
     two Geometries match the elements in pattern.
     """
     if not isinstance(pattern, str) or len(pattern) > 9:
         raise GEOSException('invalid intersection matrix pattern')
     return capi.geos_relatepattern(self.ptr, other.ptr,
                                    force_bytes(pattern))
Ejemplo n.º 10
0
def urlsafe_base64_decode(s):
    """
    Decode a base64 encoded string. Add back any trailing equal signs that
    might have been stripped.
    """
    s = force_bytes(s)
    try:
        return base64.urlsafe_b64decode(s.ljust(len(s) + len(s) % 4, b'='))
    except (LookupError, BinasciiError) as e:
        raise ValueError(e)
Ejemplo n.º 11
0
 def _encode_data(self, data, content_type):
     if content_type is MULTIPART_CONTENT:
         return encode_multipart(BOUNDARY, data)
     else:
         # Encode the content so that the byte representation is correct.
         match = CONTENT_TYPE_RE.match(content_type)
         if match:
             charset = match.group(1)
         else:
             charset = settings.DEFAULT_CHARSET
         return force_bytes(data, encoding=charset)
Ejemplo n.º 12
0
 def vsi_buffer(self):
     if not self.is_vsi_based:
         return None
     # Prepare an integer that will contain the buffer length.
     out_length = c_int()
     # Get the data using the vsi file name.
     dat = capi.get_mem_buffer_from_vsi_file(
         force_bytes(self.name),
         byref(out_length),
         VSI_DELETE_BUFFER_ON_READ,
     )
     # Read the full buffer pointer.
     return string_at(dat, out_length.value)
Ejemplo n.º 13
0
def salted_hmac(key_salt, value, secret=None):
    """
    Return the HMAC-SHA1 of 'value', using a key generated from key_salt and a
    secret (which defaults to settings.SECRET_KEY).

    A different key_salt should be passed in for every application of HMAC.
    """
    if secret is None:
        secret = settings.SECRET_KEY

    key_salt = force_bytes(key_salt)
    secret = force_bytes(secret)

    # We need to generate a derived key from our base key.  We can do this by
    # passing the key_salt and our base key through a pseudo-random function and
    # SHA1 works nicely.
    key = hashlib.sha1(key_salt + secret).digest()

    # If len(key_salt + secret) > sha_constructor().block_size, the above
    # line is redundant and could be replaced by key = key_salt + secret, since
    # the hmac module does the same thing for keys longer than the block size.
    # However, we need to ensure that we *always* do this.
    return hmac.new(key, msg=force_bytes(value), digestmod=hashlib.sha1)
Ejemplo n.º 14
0
 def from_ewkt(ewkt):
     ewkt = force_bytes(ewkt)
     srid = None
     parts = ewkt.split(b';', 1)
     if len(parts) == 2:
         srid_part, wkt = parts
         match = re.match(br'SRID=(?P<srid>\-?\d+)', srid_part)
         if not match:
             raise ValueError('EWKT has invalid SRID part.')
         srid = int(match.group('srid'))
     else:
         wkt = ewkt
     if not wkt:
         raise ValueError('Expected WKT but got an empty string.')
     return GEOSGeometry(GEOSGeometry._from_wkt(wkt), srid=srid)
Ejemplo n.º 15
0
def truncate_name(identifier, length=None, hash_len=4):
    """
    Shorten a SQL identifier to a repeatable mangled version with the given
    length.

    If a quote stripped name contains a namespace, e.g. USERNAME"."TABLE,
    truncate the table portion only.
    """
    namespace, name = split_identifier(identifier)

    if length is None or len(name) <= length:
        return identifier

    digest = hashlib.md5(force_bytes(name)).hexdigest()[:hash_len]
    return '%s%s%s' % ('%s"."' % namespace if namespace else '',
                       name[:length - hash_len], digest)
Ejemplo n.º 16
0
 def __getitem__(self, index):
     "Allows use of the index [] operator to get a layer at the index."
     if isinstance(index, str):
         try:
             layer = capi.get_layer_by_name(self.ptr, force_bytes(index))
         except GDALException:
             raise IndexError('Invalid OGR layer name given: %s.' % index)
     elif isinstance(index, int):
         if 0 <= index < self.layer_count:
             layer = capi.get_layer(self._ptr, index)
         else:
             raise IndexError(
                 'Index out of range when accessing layers in a datasource: %s.'
                 % index)
     else:
         raise TypeError('Invalid index type: %s' % type(index))
     return Layer(layer, self)
Ejemplo n.º 17
0
    def __init__(self,
                 ds_input,
                 ds_driver=False,
                 write=False,
                 encoding='utf-8'):
        # The write flag.
        if write:
            self._write = 1
        else:
            self._write = 0
        # See also http://trac.osgeo.org/gdal/wiki/rfc23_ogr_unicode
        self.encoding = encoding

        Driver.ensure_registered()

        if isinstance(ds_input, str):
            # The data source driver is a void pointer.
            ds_driver = Driver.ptr_type()
            try:
                # OGROpen will auto-detect the data source type.
                ds = capi.open_ds(force_bytes(ds_input), self._write,
                                  byref(ds_driver))
            except GDALException:
                # Making the error message more clear rather than something
                # like "Invalid pointer returned from OGROpen".
                raise GDALException('Could not open the datasource at "%s"' %
                                    ds_input)
        elif isinstance(ds_input, self.ptr_type) and isinstance(
                ds_driver, Driver.ptr_type):
            ds = ds_input
        else:
            raise GDALException('Invalid data source input type: %s' %
                                type(ds_input))

        if ds:
            self.ptr = ds
            self.driver = Driver(ds_driver)
        else:
            # Raise an exception if the returned pointer is NULL
            raise GDALException('Invalid data source file "%s"' % ds_input)
Ejemplo n.º 18
0
    def __init__(self, dr_input):
        """
        Initialize an GDAL/OGR driver on either a string or integer input.
        """
        if isinstance(dr_input, str):
            # If a string name of the driver was passed in
            self.ensure_registered()

            # Checking the alias dictionary (case-insensitive) to see if an
            # alias exists for the given driver.
            if dr_input.lower() in self._alias:
                name = self._alias[dr_input.lower()]
            else:
                name = dr_input

            # Attempting to get the GDAL/OGR driver by the string name.
            for iface in (vcapi, rcapi):
                driver = c_void_p(iface.get_driver_by_name(force_bytes(name)))
                if driver:
                    break
        elif isinstance(dr_input, int):
            self.ensure_registered()
            for iface in (vcapi, rcapi):
                driver = iface.get_driver(dr_input)
                if driver:
                    break
        elif isinstance(dr_input, c_void_p):
            driver = dr_input
        else:
            raise GDALException(
                'Unrecognized input type for GDAL/OGR Driver: %s' %
                type(dr_input))

        # Making sure we get a valid pointer to the OGR Driver
        if not driver:
            raise GDALException(
                'Could not initialize GDAL/OGR Driver on input: %s' % dr_input)
        self.ptr = driver
Ejemplo n.º 19
0
 def __del__(self):
     if self.is_vsi_based:
         # Remove the temporary file from the VSI in-memory filesystem.
         capi.unlink_vsi_file(force_bytes(self.name))
     super().__del__()
Ejemplo n.º 20
0
 def read(self, wkt):
     if not isinstance(wkt, (bytes, str)):
         raise TypeError
     return wkt_reader_read(self.ptr, force_bytes(wkt))
Ejemplo n.º 21
0
def _generate_cache_header_key(key_prefix, request):
    """Return a cache key for the header cache."""
    url = hashlib.md5(force_bytes(iri_to_uri(request.build_absolute_uri())))
    cache_key = 'views.decorators.cache.cache_header.%s.%s' % (key_prefix,
                                                               url.hexdigest())
    return _i18n_cache_key_suffix(request, cache_key)
Ejemplo n.º 22
0
 def convert_binaryfield_value(self, value, expression, connection):
     if isinstance(value, Database.LOB):
         value = force_bytes(value.read())
     return value
Ejemplo n.º 23
0
def get_cookie_signer(salt='djmodels.core.signing.get_cookie_signer'):
    Signer = import_string(settings.SIGNING_BACKEND)
    key = force_bytes(settings.SECRET_KEY)  # SECRET_KEY may be str or bytes.
    return Signer(b'djmodels.http.cookies' + key, salt=salt)
Ejemplo n.º 24
0
 def to_bytes(s):
     return force_bytes(s, settings.DEFAULT_CHARSET)
Ejemplo n.º 25
0
 def from_json(geom_input):
     return OGRGeometry(OGRGeometry._from_json(force_bytes(geom_input)))
Ejemplo n.º 26
0
 def write(self, content):
     if self.read_started:
         raise ValueError("Unable to write a payload after he's been read")
     content = force_bytes(content)
     self.__content.write(content)
     self.__len += len(content)
Ejemplo n.º 27
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.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)))
Ejemplo n.º 28
0
def constant_time_compare(val1, val2):
    """Return True if the two strings are equal, False otherwise."""
    return hmac.compare_digest(force_bytes(val1), force_bytes(val2))
Ejemplo n.º 29
0
    def __init__(self, geo_input, srid=None):
        """
        The base constructor for GEOS geometry objects. It may take the
        following inputs:

         * strings:
            - WKT
            - HEXEWKB (a PostGIS-specific canonical form)
            - GeoJSON (requires GDAL)
         * buffer:
            - WKB

        The `srid` keyword specifies the Source Reference Identifier (SRID)
        number for this Geometry. If not provided, it defaults to None.
        """
        input_srid = None
        if isinstance(geo_input, bytes):
            geo_input = force_text(geo_input)
        if isinstance(geo_input, str):
            wkt_m = wkt_regex.match(geo_input)
            if wkt_m:
                # Handle WKT input.
                if wkt_m.group('srid'):
                    input_srid = int(wkt_m.group('srid'))
                g = self._from_wkt(force_bytes(wkt_m.group('wkt')))
            elif hex_regex.match(geo_input):
                # Handle HEXEWKB input.
                g = wkb_r().read(force_bytes(geo_input))
            elif json_regex.match(geo_input):
                # Handle GeoJSON input.
                ogr = gdal.OGRGeometry.from_json(geo_input)
                g = ogr._geos_ptr()
                input_srid = ogr.srid
            else:
                raise ValueError(
                    'String input unrecognized as WKT EWKT, and HEXEWKB.')
        elif isinstance(geo_input, GEOM_PTR):
            # When the input is a pointer to a geometry (GEOM_PTR).
            g = geo_input
        elif isinstance(geo_input, memoryview):
            # When the input is a buffer (WKB).
            g = wkb_r().read(geo_input)
        elif isinstance(geo_input, GEOSGeometry):
            g = capi.geom_clone(geo_input.ptr)
        else:
            raise TypeError('Improper geometry input type: %s' %
                            type(geo_input))

        if not g:
            raise GEOSException(
                'Could not initialize GEOS Geometry with given input.')

        input_srid = input_srid or capi.geos_get_srid(g) or None
        if input_srid and srid and input_srid != srid:
            raise ValueError('Input geometry already has SRID: %d.' %
                             input_srid)

        super().__init__(g, None)
        # Set the SRID, if given.
        srid = input_srid or srid
        if srid and isinstance(srid, int):
            self.srid = srid
Ejemplo n.º 30
0
 def from_gml(cls, gml_string):
     return cls(capi.from_gml(force_bytes(gml_string)))