Esempio n. 1
0
 def readArrayChunk(self, arrayType='d'):
     """writeArray doc..."""
     length = self.readUint32()
     data = self.read(length, decode=False)
     data = StringUtils.toBytes(data)
     a = array(arrayType)
     a.fromstring(data)
     return a
Esempio n. 2
0
 def readArrayChunk(self, arrayType ='d'):
     """writeArray doc..."""
     length = self.readUint32()
     data   = self.read(length, decode=False)
     data   = StringUtils.toBytes(data)
     a = array(arrayType)
     a.fromstring(data)
     return a
Esempio n. 3
0
    def sha256hmac(cls, key, text):
        """Returns a HMAC-SHA256 hex hash of the specified text based on the specified key."""

        msg = cls.base64Encode(text)
        hash = hmac.new(StringUtils.toBytes(key),
                        msg=msg,
                        digestmod=hashlib.sha256).digest()
        return cls.base64Encode(hash)
Esempio n. 4
0
    def _generateHeaders(cls,
                         keyName,
                         expires=None,
                         eTag=None,
                         maxAge=-1,
                         gzipped=False):
        """Doc..."""
        headers = dict()

        if expires:
            if StringUtils.isStringType(expires):
                headers['Expires'] = StringUtils.toBytes(expires)
            elif StringUtils.isBinaryType(expires):
                headers['Expires'] = expires
            else:
                headers['Expires'] = StringUtils.toBytes(
                    TimeUtils.dateTimeToWebTimestamp(expires))
        elif eTag:
            headers['ETag'] = StringUtils.toBytes(eTag)

        if maxAge > -1:
            headers['Cache-Control'] = StringUtils.toBytes(
                'max-age=%s; public' % maxAge)

        if keyName.endswith('.jpg'):
            contentType = MIME_TYPES.JPEG_IMAGE
        elif keyName.endswith('.png'):
            contentType = MIME_TYPES.PNG_IMAGE
        elif keyName.endswith('.gif'):
            contentType = MIME_TYPES.GIF_IMAGE
        else:
            contentType = FileUtils.getMimeType(keyName)
        if StringUtils.begins(contentType, ('text/', 'application/')):
            contentType = '%s; charset=UTF-8' % contentType
        headers['Content-Type'] = contentType

        if gzipped:
            headers['Content-Encoding'] = 'gzip'

        return headers
Esempio n. 5
0
    def _generateHeaders(
            cls, keyName, expires =None, eTag =None, maxAge =-1, gzipped =False
    ):
        """Doc..."""
        headers = dict()

        if expires:
            if StringUtils.isStringType(expires):
                headers['Expires'] = StringUtils.toBytes(expires)
            elif StringUtils.isBinaryType(expires):
                headers['Expires'] = expires
            else:
                headers['Expires'] = StringUtils.toBytes(
                    TimeUtils.dateTimeToWebTimestamp(expires))
        elif eTag:
            headers['ETag'] = StringUtils.toBytes(eTag)

        if maxAge > -1:
            headers['Cache-Control'] = StringUtils.toBytes(
                'max-age=%s; public' % maxAge)

        if keyName.endswith('.jpg'):
            contentType = MIME_TYPES.JPEG_IMAGE
        elif keyName.endswith('.png'):
            contentType = MIME_TYPES.PNG_IMAGE
        elif keyName.endswith('.gif'):
            contentType = MIME_TYPES.GIF_IMAGE
        else:
            contentType = FileUtils.getMimeType(keyName)
        if StringUtils.begins(contentType, ('text/', 'application/')):
            contentType = '%s; charset=UTF-8' % contentType
        headers['Content-Type'] = contentType

        if gzipped:
            headers['Content-Encoding'] = 'gzip'

        return headers
Esempio n. 6
0
File: JSON.py Progetto: sernst/PyAid
 def toFile(cls, path, value, pretty =False, gzipped =False, throwError =False):
     try:
         res = StringUtils.toStr2(cls.asString(value, pretty=pretty))
         if gzipped:
             f = gzip.open(path, 'wb')
             f.write(StringUtils.toBytes(res))
         else:
             f = open(path, 'w+')
             f.write(res)
         f.close()
         return True
     except Exception as err:
         if throwError:
             raise
         else:
             print(err)
         return False
Esempio n. 7
0
 def toFile(cls,
            path,
            value,
            pretty=False,
            gzipped=False,
            throwError=False):
     try:
         res = StringUtils.toStr2(cls.asString(value, pretty=pretty))
         if gzipped:
             f = gzip.open(path, 'wb')
             f.write(StringUtils.toBytes(res))
         else:
             f = open(path, 'w+')
             f.write(res)
         f.close()
         return True
     except Exception as err:
         if throwError:
             raise
         else:
             print(err)
         return False
Esempio n. 8
0
    def sha256hmac(cls, key, text):
        """Returns a HMAC-SHA256 hex hash of the specified text based on the specified key."""

        msg  = cls.base64Encode(text)
        hash = hmac.new(StringUtils.toBytes(key), msg=msg, digestmod=hashlib.sha256).digest()
        return cls.base64Encode(hash)