Exemplo n.º 1
0
def generate_guid():
    """
    Generates a GUID string.

    The GUID length is 32 characters. It is used by the
    session manager to generate session IDs.

    @rtype: str
    """
    return hashlib.md5(str(time.time() + time.clock() * 1000)).hexdigest()
Exemplo n.º 2
0
def generate_guid():
    """
    Generates a GUID string.

    The GUID length is 32 characters. It is used by the
    session manager to generate session IDs.

    @rtype: str
    """
    return hashlib.md5(str(time.time() + time.clock() * 1000)).hexdigest()
    def write(self, s):
        """Appends a string to the response sent to the client.

        @param s: string to write
        @type s: str

        @return: None
        """
        if isinstance(s, str):
            self._body.write(s.encode(self.charset))
        elif isinstance(s, bytes):
            self._body.write(s)
        else:
            self._body.write(str(s))
Exemplo n.º 4
0
def execute(context, filename):
    try:
        # get modification date
        sMod = os.stat(filename)[8]
    except OSError:
        raise exceptions.NotFound(
            'The file "%s" can not be found' % filename)

    sMod = hex(sMod)[2:]
    magic = str(binascii.hexlify(imp.get_magic()), 'latin-1')

    sFileWithoutExtension = \
        re.search(PSP_REMOVE_EXTENSION, filename).groups()[0]
    compiledFileName = '{0}#{1}-{2}.bin'.format(sFileWithoutExtension,
                                                sMod, magic)

    try:
        pspBinaryFile = open(compiledFileName, 'rb')
    except IOError:
        # remove old compiled files
        oldFiles = glob.glob(sFileWithoutExtension + '*.bin')
        for oldFile in oldFiles:
            os.remove(oldFile)
        # start compilation
        oFile = open(filename, 'rU')
        pspCode = oFile.read()

        # truncate utf-8 file encoding identifier for NT
        if pspCode[:3] == NT_UTF_8_IDENTIFIER:
            pspCode = pspCode[3:]

        execCode = ''

        pspCode = re.split(PSP_TAGS, pspCode)
        intend = ''
        for codeFragment in pspCode:
            if codeFragment != '':
                codeMatch = re.match(PSP_TAGS, codeFragment)
                if codeMatch is None:
                    # pure HTML
                    execCode += intend + 'Response.write(%r)\n' % codeFragment
                else:
                    # pure Python
                    # remove PSP tags
                    codeFragment = codeFragment[2:len(codeFragment) - 2]
                    linesOfCode = codeFragment.split('\n')
                    formattedCode = ''
                    for line in linesOfCode:
                        # remove whitespaces
                        line = line.strip()
                        # set intendation
                        if line != '':
                            if line != 'end':
                                if re.match(PSP_DECREASE_INDENT, line):
                                    intend = intend[0:len(intend) - 1]
                                formattedCode += intend + line + '\n'
                                if line[-1] == ':':
                                    intend += '\t'
                            else:
                                intend = intend[0:len(intend) - 1]
                    execCode += formattedCode

        oCode = compile(execCode, filename, 'exec')
        pspBinaryFile = open(compiledFileName, 'w+b')
        marshal.dump(oCode, pspBinaryFile)
        pspBinaryFile.seek(0)

    psp_globals = {'Server': context.server,
                   'Session': context.session,
                   'Response': context.response,
                   'Request': context.request}

    code = marshal.load(pspBinaryFile)
    try:
        exec(code, psp_globals)
    finally:
        pspBinaryFile.close()
Exemplo n.º 5
0
    def format(self, format, locale='*'):
        """
        Convert the date to a string as specified by the format argument.

        @param format: The following directives can be embedded in the
                       format string::

                -yyyy  four digit year
                -yy    two digit year
                -month full month
                -mmm   short month
                -mm    month as a decimal number
                -dd    day of the month as a decimal number
                -day   full week day
                -ddd   short week day
                -h24   24-based hours
                -h12   12-based hours
                -min   minutes
                -sec   seconds
                -MM    locale's AM or PM
        @type format: str
        @param locale: The locale string. If ommited the '*' is used.
        @type locale: str

        @return: The formatted string
        @rtype: str
        """
        tupTime = time.localtime(self.value)
        sYear = str(tupTime[0])
        iMonth = tupTime[1] - 1
        iDate = tupTime[2]
        iHours = tupTime[3]
        if iHours > 12:
            iHours12 = iHours - 12
            ampm = self.resources.get_resource('PM', locale)
        else:
            iHours12 = iHours
            ampm = self.resources.get_resource('AM', locale)
        if type(ampm) == bytes:
            # python 2.6
            ampm = ampm.decode('utf-8')

        iMins = tupTime[4]
        iSecs = tupTime[5]
        iWeekday = tupTime[6]

        sMonth = self.resources.get_resource('MONTHS', locale)[iMonth]
        if type(sMonth) == bytes:
            # python 2.6
            sMonth = sMonth.decode('utf-8')

        sDay = self.resources.get_resource('DAYS', locale)[iWeekday]
        if type(sDay) == bytes:
            # python 2.6
            sDay = sDay.decode('utf-8')

        format = format.replace('yyyy', sYear)
        format = format.replace('yy', sYear[2:4])

        format = format.replace('month', sMonth)
        format = format.replace('mmm', sMonth[:3])
        format = format.replace('mm', str(iMonth + 1))

        format = format.replace('min', '%02d' % iMins)
        format = format.replace('sec', '%02d' % iSecs)

        format = format.replace('ddd', sDay[:3])
        format = format.replace('dd', str(iDate))
        format = format.replace('day', sDay)

        format = format.replace('h24', '%02d' % iHours)
        format = format.replace('h12', str(iHours12))
        format = format.replace('MM', ampm)

        return format
Exemplo n.º 6
0
 def set_value(self, value):
     if value != self._value:
         if type(value) == str:
             value = value.encode('utf-8')
         self._value = str(hashlib.md5(value).hexdigest())
Exemplo n.º 7
0
 def set_value(self, value):
     if value != self._value:
         if type(value) == str:
             value = value.encode('utf-8')
         self._value = str(hashlib.md5(value).hexdigest())
Exemplo n.º 8
0
    def format(self, format, locale='*'):
        """
        Convert the date to a string as specified by the format argument.

        @param format: The following directives can be embedded in the
                       format string::

                -yyyy  four digit year
                -yy    two digit year
                -month full month
                -mmm   short month
                -mm    month as a decimal number
                -dd    day of the month as a decimal number
                -day   full week day
                -ddd   short week day
                -h24   24-based hours
                -h12   12-based hours
                -min   minutes
                -sec   seconds
                -MM    locale's AM or PM
        @type format: str
        @param locale: The locale string. If ommited the '*' is used.
        @type locale: str

        @return: The formatted string
        @rtype: str
        """
        tupTime = time.localtime(self.value)
        sYear = str(tupTime[0])
        iMonth = tupTime[1] - 1
        iDate = tupTime[2]
        iHours = tupTime[3]
        if iHours > 12:
            iHours12 = iHours - 12
            ampm = self.resources.get_resource('PM', locale)
        else:
            iHours12 = iHours
            ampm = self.resources.get_resource('AM', locale)
        if type(ampm) == bytes:
            # python 2.6
            ampm = ampm.decode('utf-8')

        iMins = tupTime[4]
        iSecs = tupTime[5]
        iWeekday = tupTime[6]

        sMonth = self.resources.get_resource('MONTHS', locale)[iMonth]
        if type(sMonth) == bytes:
            # python 2.6
            sMonth = sMonth.decode('utf-8')

        sDay = self.resources.get_resource('DAYS', locale)[iWeekday]
        if type(sDay) == bytes:
            # python 2.6
            sDay = sDay.decode('utf-8')

        format = format.replace('yyyy', sYear)
        format = format.replace('yy', sYear[2:4])

        format = format.replace('month', sMonth)
        format = format.replace('mmm', sMonth[:3])
        format = format.replace('mm', str(iMonth + 1))

        format = format.replace('min', '%02d' % iMins)
        format = format.replace('sec', '%02d' % iSecs)

        format = format.replace('ddd', sDay[:3])
        format = format.replace('dd', str(iDate))
        format = format.replace('day', sDay)

        format = format.replace('h24', '%02d' % iHours)
        format = format.replace('h12', str(iHours12))
        format = format.replace('MM', ampm)

        return format
Exemplo n.º 9
0
def execute(context, filename):
    try:
        # get modification date
        sMod = os.stat(filename)[8]
    except OSError:
        raise exceptions.NotFound('The file "%s" can not be found' % filename)

    sMod = hex(sMod)[2:]
    magic = str(binascii.hexlify(imp.get_magic()), 'latin-1')

    sFileWithoutExtension = \
        re.search(PSP_REMOVE_EXTENSION, filename).groups()[0]
    compiledFileName = '{0}#{1}-{2}.bin'.format(sFileWithoutExtension, sMod,
                                                magic)

    try:
        pspBinaryFile = open(compiledFileName, 'rb')
    except IOError:
        # remove old compiled files
        oldFiles = glob.glob(sFileWithoutExtension + '*.bin')
        for oldFile in oldFiles:
            os.remove(oldFile)
        # start compilation
        oFile = open(filename, 'rU')
        pspCode = oFile.read()

        # truncate utf-8 file encoding identifier for NT
        if pspCode[:3] == NT_UTF_8_IDENTIFIER:
            pspCode = pspCode[3:]

        execCode = ''

        pspCode = re.split(PSP_TAGS, pspCode)
        intend = ''
        for codeFragment in pspCode:
            if codeFragment != '':
                codeMatch = re.match(PSP_TAGS, codeFragment)
                if codeMatch is None:
                    # pure HTML
                    execCode += intend + 'Response.write(%r)\n' % codeFragment
                else:
                    # pure Python
                    # remove PSP tags
                    codeFragment = codeFragment[2:len(codeFragment) - 2]
                    linesOfCode = codeFragment.split('\n')
                    formattedCode = ''
                    for line in linesOfCode:
                        # remove whitespaces
                        line = line.strip()
                        # set intendation
                        if line != '':
                            if line != 'end':
                                if re.match(PSP_DECREASE_INDENT, line):
                                    intend = intend[0:len(intend) - 1]
                                formattedCode += intend + line + '\n'
                                if line[-1] == ':':
                                    intend += '\t'
                            else:
                                intend = intend[0:len(intend) - 1]
                    execCode += formattedCode

        oCode = compile(execCode, filename, 'exec')
        pspBinaryFile = open(compiledFileName, 'w+b')
        marshal.dump(oCode, pspBinaryFile)
        pspBinaryFile.seek(0)

    psp_globals = {
        'Server': context.server,
        'Session': context.session,
        'Response': context.response,
        'Request': context.request
    }

    code = marshal.load(pspBinaryFile)
    try:
        exec(code, psp_globals)
    finally:
        pspBinaryFile.close()