Beispiel #1
0
 def getBreadcrumbsHTML(self):
     html = []
     for name, url in self.getBreadcrumbs():
         if url:
             html.append('<a href="%s">%s</a>' % (escape(url, True),
                                                  escape(name, False)))
         else:
             html.append(escape(name, False))
     return ''.join(html)
Beispiel #2
0
 def getBreadcrumbsHTML(self):
     html = []
     for name, url in self.getBreadcrumbs():
         if url:
             html.append('<a href="%s">%s</a>' %
                         (escape(url, True), escape(name, False)))
         else:
             html.append(escape(name, False))
     return ''.join(html)
Beispiel #3
0
 def render(self, tid=None, can_link=True, limit=200, threshold=4):
     newline = b'\n' if isinstance(self.context, bytes) else u'\n'
     if self.context.count(newline) <= threshold:
         return GenericValue.render(self,
                                    tid,
                                    can_link=can_link,
                                    limit=limit)
     else:
         if isinstance(self.context, bytes):
             context = self.context.decode('latin-1').encode(
                 'ascii', 'backslashreplace')
         else:
             context = self.context
         if isinstance(self.context, str):
             prefix = ''
         else:
             prefix = 'u'
         lines = [
             re.sub(r'^[ \t]+',
                    lambda m: '&nbsp;' * len(m.group(0).expandtabs()),
                    escape(line, False)) for line in context.splitlines()
         ]
         nl = '<br />'  # hm, maybe '\\n<br />'?
         if sum(map(len, lines)) > limit:
             head = nl.join(lines[:5])
             tail = nl.join(lines[5:])
             id = truncate(tail)
             return (prefix + "'<span class=\"struct\">" + head + nl +
                     '<span id="%s" class="truncated">...</span>' % id +
                     "'</span>")
         else:
             return (prefix + "'<span class=\"struct\">" + nl.join(lines) +
                     "'</span>")
Beispiel #4
0
 def render(self, tid=None, can_link=True, limit=200, threshold=4):
     newline = b'\n' if isinstance(self.context, bytes) else u'\n'
     if self.context.count(newline) <= threshold:
         return GenericValue.render(self, tid, can_link=can_link,
                                    limit=limit)
     else:
         if isinstance(self.context, bytes):
             context = self.context.decode('latin-1').encode('ascii',
                                                         'backslashreplace')
         else:
             context = self.context
         if isinstance(self.context, str):
             prefix = ''
         else:
             prefix = 'u'
         lines = [re.sub(r'^[ \t]+',
                         lambda m: '&nbsp;' * len(m.group(0).expandtabs()),
                         escape(line, False))
                  for line in context.splitlines()]
         nl = '<br />' # hm, maybe '\\n<br />'?
         if sum(map(len, lines)) > limit:
             head = nl.join(lines[:5])
             tail = nl.join(lines[5:])
             id = truncate(tail)
             return (prefix + "'<span class=\"struct\">" + head + nl
                     + '<span id="%s" class="truncated">...</span>' % id
                     + "'</span>")
         else:
             return (prefix + "'<span class=\"struct\">" + nl.join(lines)
                     + "'</span>")
Beispiel #5
0
def compareDictsHTML(new, old, tid=None, indent=''):
    """Compare two state dictionaries, return HTML."""
    html = [indent + '<div class="diff">\n']
    diff = compareDicts(new, old)
    for key, (action, newvalue, oldvalue) in sorted(diff.items(),
                                                    key=lambda kv:
                                                    (str(type(kv[0])), kv[0])):
        what = action.split()[0]
        html.append(indent +
                    '  <div class="diffitem %s">\n' % escape(what, True))
        if isinstance(key, basestring) and isascii(key):
            html.append(indent +
                        '    <strong>%s</strong>: ' % escape(key, False))
        else:
            html.append(indent + '    <strong>%s</strong>: ' %
                        IValueRenderer(key).render(tid))
        if (action == CHANGED and isinstance(oldvalue, dict)
                and isinstance(newvalue, dict)):
            html.append('dictionary changed:\n')
            html.append(
                compareDictsHTML(newvalue,
                                 oldvalue,
                                 tid,
                                 indent=indent + '    '))
        elif (action == CHANGED and isinstance(oldvalue, tuple)
              and isinstance(newvalue, tuple)):
            html.append('tuple changed:\n')
            html.append(
                compareTuplesHTML(newvalue,
                                  oldvalue,
                                  tid,
                                  indent=indent + '    '))
        else:
            html.append(action)
            html.append(' ')
            if action == REMOVED:
                value = oldvalue
            else:
                value = newvalue
            html.append(IValueRenderer(value).render(tid))
            html.append('\n')
        html.append(indent + '  </div>\n')
    html.append(indent + '</div>\n')
    return ''.join(html)
Beispiel #6
0
 def render(self, tid=None, can_link=True, limit=200):
     text = self._repr()
     if len(text) > limit:
         id = truncate(text[limit:])
         text = '%s<span id="%s" class="truncated">...</span>' % (escape(
             text[:limit], False), id)
     else:
         text = escape(text, False)
     if not isinstance(self.context, (basestring, bytes)):
         try:
             n = len(self.context)
         except Exception:
             pass
         else:
             if n == 1:  # this is a crime against i18n, but oh well
                 text += ' (%d item)' % n
             else:
                 text += ' (%d items)' % n
     return text
Beispiel #7
0
 def render(self, tid=None, can_link=True, limit=200):
     text = self._repr()
     if len(text) > limit:
         id = truncate(text[limit:])
         text = '%s<span id="%s" class="truncated">...</span>' % (
             escape(text[:limit], False), id)
     else:
         text = escape(text, False)
     if not isinstance(self.context, (basestring, bytes)):
         try:
             n = len(self.context)
         except Exception:
             pass
         else:
             if n == 1: # this is a crime against i18n, but oh well
                 text += ' (%d item)' % n
             else:
                 text += ' (%d items)' % n
     return text
Beispiel #8
0
def compareDictsHTML(new, old, tid=None, indent=''):
    """Compare two state dictionaries, return HTML."""
    html = [indent + '<div class="diff">\n']
    diff = compareDicts(new, old)
    for key, (action, newvalue, oldvalue) in sorted(
            diff.items(), key=lambda kv: (str(type(kv[0])), kv[0])):
        what = action.split()[0]
        html.append(indent + '  <div class="diffitem %s">\n'
                    % escape(what, True))
        if isinstance(key, basestring) and isascii(key):
            html.append(indent + '    <strong>%s</strong>: '
                        % escape(key, False))
        else:
            html.append(indent + '    <strong>%s</strong>: '
                        % IValueRenderer(key).render(tid))
        if (action == CHANGED and isinstance(oldvalue, dict) and
                isinstance(newvalue, dict)):
            html.append('dictionary changed:\n')
            html.append(compareDictsHTML(newvalue, oldvalue, tid,
                                         indent=indent + '    '))
        elif (action == CHANGED and isinstance(oldvalue, tuple) and
              isinstance(newvalue, tuple)):
            html.append('tuple changed:\n')
            html.append(compareTuplesHTML(newvalue, oldvalue, tid,
                                          indent=indent + '    '))
        else:
            html.append(action)
            html.append(' ')
            if action == REMOVED:
                value = oldvalue
            else:
                value = newvalue
            html.append(IValueRenderer(value).render(tid))
            html.append('\n')
        html.append(indent + '  </div>\n')
    html.append(indent + '</div>\n')
    return ''.join(html)
Beispiel #9
0
 def render(self, tid=None, can_link=True):
     obj = self.context
     url = '%s?oid=0x%x' % (self.view_name, u64(self.context._p_oid))
     if tid is not None:
         url += "&tid=0x%x" % u64(tid)
         try:
             oldstate = IObjectHistory(self.context).loadState(tid)
             clone = self.context.__class__.__new__(self.context.__class__)
             clone.__setstate__(oldstate)
             clone._p_oid = self.context._p_oid
             obj = clone
         except Exception:
             log.debug('Could not load old state for %s 0x%x',
                       self.context.__class__, u64(self.context._p_oid))
     value = self.delegate_to(obj).render(tid, can_link=False)
     if can_link:
         return '<a class="objlink" href="%s">%s</a>' % (escape(
             url, True), value)
     else:
         return value
Beispiel #10
0
 def render(self, tid=None, can_link=True):
     obj = self.context
     url = '%s?oid=0x%x' % (self.view_name, u64(self.context._p_oid))
     if tid is not None:
         url += "&tid=0x%x" % u64(tid)
         try:
             oldstate = IObjectHistory(self.context).loadState(tid)
             clone = self.context.__class__.__new__(self.context.__class__)
             clone.__setstate__(oldstate)
             clone._p_oid = self.context._p_oid
             obj = clone
         except Exception:
             log.debug('Could not load old state for %s 0x%x',
                       self.context.__class__, u64(self.context._p_oid))
     value = self.delegate_to(obj).render(tid, can_link=False)
     if can_link:
         return '<a class="objlink" href="%s">%s</a>' % (escape(url, True),
                                                         value)
     else:
         return value
def fixupWhitespace(element, indent=0, step=2, split_if_longer=38):
    """Normalize whitespace on lxml elements."""
    # Input:
    #   <tag ...>[text]<children ...></tag>[tail]
    # Output:
    #   {indent}<tag ...>\n
    #   [indent+2][text]\n
    #             <children>
    #   [indent]</tag>\n
    #   [indent][text]

    children = element.getchildren()

    element.text = stripify(element.text)
    # heuristic for splitting long elements
    should_split = (len(str(element.attrib)) > split_if_longer or
                    len(escape(element.text, False)) > split_if_longer)
    if should_split and element.text:
        element.text = ('\n' + ' ' * (indent + step) + element.text.lstrip())
    if children:
        element.text += '\n' + ' ' * (indent + step)
    else:
        if '\n' in element.text:
            element.text += '\n' + ' ' * indent

    for idx, child in enumerate(children):
        fixupWhitespace(child, indent + step, step)
        if idx == len(children) - 1:
            child.tail += '\n' + ' ' * indent
        else:
            child.tail += '\n' + ' ' * (indent + step)

    if indent == 0:
        element.tail = None
    element.tail = stripify(element.tail)
    if element.tail and element.tail.startswith(' '):
        element.tail = '\n' + ' ' * indent + element.tail.lstrip()