Ejemplo n.º 1
0
    def update_canvas(self):
        """ Updates canvas """

        httplog = os.path.join(self.options.folder, self.options.log_filename)
        try:
            t1 = os.path.getsize(httplog)
        except:
            self.canvas.after(1000, self.update_canvas)
            return

        try:
            fp = open(httplog, 'r')
            fp.seek(self.t0)
            data = fp.read(t1 - self.t0)
            fp.close()
            value = self.p0[1:] + [10 + 90.0 / math.sqrt(1 + data.count('\n'))]
            self.p0 = value

            for i in xrange(len(self.p0) - 1):
                c = self.canvas.coords(self.q0[i])
                self.canvas.coords(self.q0[i],
                                   (c[0],
                                    self.p0[i],
                                    c[2],
                                    self.p0[i + 1]))
            self.t0 = t1
        except BaseException:
            self.t0 = time.time()
            self.t0 = t1
            self.p0 = [100] * 400
            self.q0 = [self.canvas.create_line(i, 100, i + 1, 100,
                       fill='green') for i in xrange(len(self.p0) - 1)]

        self.canvas.after(1000, self.update_canvas)
Ejemplo n.º 2
0
    def update_canvas(self):
        """ Updates canvas """

        httplog = os.path.join(self.options.folder, self.options.log_filename)
        try:
            t1 = os.path.getsize(httplog)
        except:
            self.canvas.after(1000, self.update_canvas)
            return

        try:
            fp = open(httplog, 'r')
            fp.seek(self.t0)
            data = fp.read(t1 - self.t0)
            fp.close()
            value = self.p0[1:] + [10 + 90.0 / math.sqrt(1 + data.count('\n'))]
            self.p0 = value

            for i in xrange(len(self.p0) - 1):
                c = self.canvas.coords(self.q0[i])
                self.canvas.coords(self.q0[i],
                                   (c[0], self.p0[i], c[2], self.p0[i + 1]))
            self.t0 = t1
        except BaseException:
            self.t0 = time.time()
            self.t0 = t1
            self.p0 = [100] * 400
            self.q0 = [
                self.canvas.create_line(i, 100, i + 1, 100, fill='green')
                for i in xrange(len(self.p0) - 1)
            ]

        self.canvas.after(1000, self.update_canvas)
Ejemplo n.º 3
0
    def update_canvas(self):
        """ Updates canvas """
        httplog = os.path.join(self.options.folder, self.options.log_filename)
        canvas = self.canvas
        try:
            t1 = os.path.getsize(httplog)
        except OSError:
            canvas.after(1000, self.update_canvas)
            return

        points = 400
        try:
            pvalues = self.p0[1:]
            with open(httplog, 'r') as fp:
                fp.seek(self.t0)
                data = fp.read(t1 - self.t0)
            self.p0 = pvalues + [10 + 90.0 / math.sqrt(1 + data.count('\n'))]

            for i in xrange(points - 1):
                c = canvas.coords(self.q0[i])
                canvas.coords(self.q0[i],
                                   (c[0], self.p0[i],
                                    c[2], self.p0[i + 1]))
            self.t0 = t1
        except AttributeError:
            self.t0 = time.time()
            self.t0 = t1
            self.p0 = [100] * points
            self.q0 = [canvas.create_line(i, 100, i + 1, 100,
                       fill='green') for i in xrange(points - 1)]

        canvas.after(1000, self.update_canvas)
Ejemplo n.º 4
0
def compare(a, b):
    """ Compares two strings and not vulnerable to timing attacks """
    if HAVE_COMPARE_DIGEST:
        return hmac.compare_digest(a, b)
    result = len(a) ^ len(b)
    for i in xrange(len(b)):
        result |= ord(a[i % len(a)]) ^ ord(b[i])
    return result == 0
Ejemplo n.º 5
0
def compare(a, b):
    """ Compares two strings and not vulnerable to timing attacks """
    if HAVE_COMPARE_DIGEST:
        return hmac.compare_digest(a, b)
    result = len(a) ^ len(b)
    for i in xrange(len(b)):
        result |= ord(a[i%len(a)]) ^ ord(b[i])
    return result == 0
Ejemplo n.º 6
0
def fast_urandom16(urandom=[], locker=threading.RLock()):
    """
    This is 4x faster than calling os.urandom(16) and prevents
    the "too many files open" issue with concurrent access to os.urandom()
    """
    try:
        return urandom.pop()
    except IndexError:
        try:
            locker.acquire()
            ur = os.urandom(16 * 1024)
            urandom += [ur[i:i + 16] for i in xrange(16, 1024 * 16, 16)]
            return ur[0:16]
        finally:
            locker.release()
Ejemplo n.º 7
0
def fast_urandom16(urandom=[], locker=threading.RLock()):
    """
    This is 4x faster than calling os.urandom(16) and prevents
    the "too many files open" issue with concurrent access to os.urandom()
    """
    try:
        return urandom.pop()
    except IndexError:
        try:
            locker.acquire()
            ur = os.urandom(16 * 1024)
            urandom += [ur[i:i + 16] for i in xrange(16, 1024 * 16, 16)]
            return ur[0:16]
        finally:
            locker.release()
Ejemplo n.º 8
0
def highlight(
    code,
    language,
    link='/examples/globals/vars/',
    counter=1,
    styles=None,
    highlight_line=None,
    context_lines=None,
    attributes=None,
):
    styles = styles or {}
    attributes = attributes or {}
    code_style = styles.get('CODE', None) or '''
font-size: 11px;
font-family: Bitstream Vera Sans Mono,monospace;
background-color: transparent;
margin: 0;
padding: 5px;
border: none;
overflow: auto;
white-space: pre !important;
'''
    linenumbers_style = styles.get('LINENUMBERS', None) or '''
font-size: 11px;
font-family: Bitstream Vera Sans Mono,monospace;
background-color: transparent;
margin: 0;
padding: 5px;
border: none;
color: #A0A0A0;
'''
    linehighlight_style = styles.get('LINEHIGHLIGHT', None) or \
        'background-color: #EBDDE2;'

    if language and language.upper() in [
            'PYTHON', 'C', 'CPP', 'HTML', 'WEB2PY'
    ]:
        code = Highlighter(language, link, styles).highlight(code)
    else:
        code = local_html_escape(code, quote=False)
    lines = code.split('\n')

    if counter is None:
        linenumbers = [''] * len(lines)
    elif isinstance(counter, str):
        linenumbers = [local_html_escape(counter, quote=False)] * len(lines)
    else:
        linenumbers = [str(i + counter) + '.' for i in xrange(len(lines))]

    if highlight_line:
        if counter and not isinstance(counter, str):
            lineno = highlight_line - counter
        else:
            lineno = highlight_line
        if lineno < len(lines):
            lines[lineno] = '<span style="%s">%s</span>' % (
                linehighlight_style, lines[lineno])
            linenumbers[lineno] = '<span style="%s">%s</span>' % (
                linehighlight_style, linenumbers[lineno])

        if context_lines:
            if lineno + context_lines < len(lines):
                delslice = slice(lineno + context_lines + 1, len(lines))
                del lines[delslice]
                del linenumbers[delslice]
            if lineno - context_lines > 0:
                delslice = slice(0, lineno - context_lines)
                del lines[delslice]
                del linenumbers[delslice]

    code = '<br/>'.join(lines)
    numbers = '<br/>'.join(linenumbers)

    items = attributes.items()
    fa = ' '.join([
        key[1:].lower()
        for (key, value) in items if key[:1] == '_' and value is None
    ] + [
        '%s="%s"' % (key[1:].lower(), str(value).replace('"', "'"))
        for (key, value) in attributes.items() if key[:1] == '_' and value
    ])
    if fa:
        fa = ' ' + fa
    return '<table%s><tr style="vertical-align:top;">' \
           '<td style="min-width:40px; text-align: right;"><pre style="%s">%s</pre></td>' \
           '<td><pre style="%s">%s</pre></td></tr></table>' % (fa, linenumbers_style, numbers, code_style, code)
Ejemplo n.º 9
0
def highlight(
    code,
    language,
    link='/examples/globals/vars/',
    counter=1,
    styles=None,
    highlight_line=None,
    context_lines=None,
    attributes=None,
):
    styles = styles or {}
    attributes = attributes or {}
    if 'CODE' not in styles:
        code_style = """
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        overflow: auto;
        white-space: pre !important;\n"""
    else:
        code_style = styles['CODE']
    if 'LINENUMBERS' not in styles:
        linenumbers_style = """
        font-size: 11px;
        font-family: Bitstream Vera Sans Mono,monospace;
        background-color: transparent;
        margin: 0;
        padding: 5px;
        border: none;
        color: #A0A0A0;\n"""
    else:
        linenumbers_style = styles['LINENUMBERS']
    if 'LINEHIGHLIGHT' not in styles:
        linehighlight_style = "background-color: #EBDDE2;"
    else:
        linehighlight_style = styles['LINEHIGHLIGHT']

    if language and language.upper() in ['PYTHON', 'C', 'CPP', 'HTML',
                                         'WEB2PY']:
        code = Highlighter(language, link, styles).highlight(code)
    else:
        code = local_html_escape(code, quote=False)
    lines = code.split('\n')

    if counter is None:
        linenumbers = [''] * len(lines)
    elif isinstance(counter, str):
        linenumbers = [local_html_escape(counter, quote=False)] * len(lines)
    else:
        linenumbers = [str(i + counter) + '.' for i in
                       xrange(len(lines))]

    if highlight_line:
        if counter and not isinstance(counter, str):
            lineno = highlight_line - counter
        else:
            lineno = highlight_line
        if lineno < len(lines):
            lines[lineno] = '<div style="%s">%s</div>' % (
                linehighlight_style, lines[lineno])
            linenumbers[lineno] = '<div style="%s">%s</div>' % (
                linehighlight_style, linenumbers[lineno])

        if context_lines:
            if lineno + context_lines < len(lines):
                del lines[lineno + context_lines:]
                del linenumbers[lineno + context_lines:]
            if lineno - context_lines > 0:
                del lines[0:lineno - context_lines]
                del linenumbers[0:lineno - context_lines]

    code = '<br/>'.join(lines)
    numbers = '<br/>'.join(linenumbers)

    items = attributes.items()
    fa = ' '.join([key[1:].lower() for (key, value) in items if key[:1]
                   == '_' and value is None] + ['%s="%s"'
                                                % (key[1:].lower(), str(value).replace('"', "'"))
                  for (key, value) in attributes.items() if key[:1]
                  == '_' and value])
    if fa:
        fa = ' ' + fa
    return '<table%s><tr style="vertical-align:top;">' \
           '<td style="min-width:40px; text-align: right;"><pre style="%s">%s</pre></td>' \
           '<td><pre style="%s">%s</pre></td></tr></table>' % (fa, linenumbers_style, numbers, code_style, code)