Maciej Fijalkowski avatar Maciej Fijalkowski committed 7d2ec21

simplify and reintroduce cssclass, this time saneish

Comments (0)

Files changed (2)

_jitviewer/parser.py

 import cgi
 from pypy.tool.jitlogparser import parser
 
-class Html(str):
-    def __html__(self):
-        return self
-
-    def plaintext(self):
-        # This is not a general way to strip tags, but it's good enough to use
-        # in tests
-        s = re.sub('<.*?>', '', self)
-        s = s.replace("&lt;", "<")
-        s = s.replace("&gt;", ">")
-        s = s.replace("&amp;", "&")
-        return s
-
-
 def cssclass(cls, s, **kwds):
     cls = re.sub("[^\w]", "_", cls)
     attrs = ['%s="%s"' % (name, value) for name, value in kwds.iteritems()]
 def _new_binop(name):
     name = cgi.escape(name)
     def f(self):
-        return '%s = %s %s %s' % (self.getres(), self.getarg(0), name, self.getarg(1))
+        return '%s = %s %s %s' % (self.wrap_html(self.res),
+                                  self.wrap_html(self.args[0]),
+                                  name, self.wrap_html(self.args[1]))
     return f
 
+class Html(str):
+    def __html__(self):
+        return self
+
 class OpHtml(parser.Op):
     """
     Subclass of Op with human-friendly html representation
             return "single-operation"
 
     def html_repr(self):
-        s = getattr(self, 'repr_' + self.name, self.repr)()
+        s = getattr(self, 'repr_' + self.name, self.default_repr)()
         return Html(s)
 
-    #def _getvar(self, v):
-    #    return cssclass(v, v, onmouseover='highlight_var(this)', onmouseout='disable_var(this)')
+    def wrap_html(self, v):
+        return cssclass(v, v, onmouseover='highlight_var(this)', onmouseout='disable_var(this)')
 
     for bin_op, name in [('==', 'int_eq'),
                          ('!=', 'int_ne'),
         locals()['repr_' + name] = _new_binop(bin_op)
 
     def repr_guard_true(self):
-        return 'guard(%s is true)' % self.getarg(0)
+        return 'guard(%s is true)' % self.wrap_html(self.args[0])
 
     def repr_guard_false(self):
-        return 'guard(%s is false)' % self.getarg(0)
+        return 'guard(%s is false)' % self.wrap_html(self.args[0])
 
     def repr_guard_value(self):
-        return 'guard(%s == %s)' % (self.getarg(0), self.getarg(1))
+        return 'guard(%s == %s)' % (self.wrap_html(self.args[0]),
+                                    self.wrap_html(self.args[1]))
 
     def repr_guard_isnull(self):
-        return 'guard(%s is null)' % self.getarg(0)
+        return 'guard(%s is null)' % self.wrap_html(self.args[0])
 
     def repr_getfield_raw(self):
         name, field = self.descr.split(' ')[1].rsplit('.', 1)
-        return '%s = ((%s)%s).%s' % (self.getres(), name, self.getarg(0), field[2:])
+        return '%s = ((%s)%s).%s' % (self.wrap_html(self.res), name,
+                                     self.wrap_html(self.args[0]), field[2:])
 
     def repr_getfield_gc(self):
         fullname, field = self.descr.split(' ')[1].rsplit('.', 1)
         field = cssclass('fieldname', field)
             
         obj = self.getarg(0)
-        return '%s = ((%s.%s)%s).%s' % (self.getres(), namespace, classname, obj, field)
+        return '%s = ((%s.%s)%s).%s' % (self.wrap_html(self.res),
+                                        namespace, classname, obj, field)
 
     def repr_getfield_gc_pure(self):
         return self.repr_getfield_gc() + " [pure]"
 
     def repr_setfield_raw(self):
         name, field = self.descr.split(' ')[1].rsplit('.', 1)
-        return '((%s)%s).%s = %s' % (name, self.getarg(0), field[2:], self.getarg(1))
+        return '((%s)%s).%s = %s' % (name, self.wrap_html(self.args[0]),
+                                     field[2:], self.wrap_html(self.args[1]))
 
     def repr_setfield_gc(self):
         name, field = self.descr.split(' ')[1].rsplit('.', 1)
-        return '((%s)%s).%s = %s' % (name, self.getarg(0), field, self.getarg(1))
+        return '((%s)%s).%s = %s' % (name, self.wrap_html(self.args[0]),
+                                     field, self.wrap_html(self.args[1]))
 
     def repr_jump(self):
         no = int(re.search("\d+", self.descr).group(0))
         return ("<a href='' onclick='show_loop(%d);return false'>" % no +
-                self.repr() + "</a>")
+                self.default_repr() + "</a>")
+
+    def default_repr(self):
+        args = [self.wrap_html(arg) for arg in self.args]
+        if self.descr is not None:
+            args.append('descr=%s' % cgi.escape(self.descr))
+        arglist = ', '.join(args)
+        if self.res is not None:
+            return '%s = %s(%s)' % (self.wrap_html(self.res), self.name,
+                                    arglist)
+        else:
+            return '%s(%s)' % (self.name, arglist)
 
     repr_call_assembler = repr_jump
 
-    def getdescr(self):
-        return cgi.escape(self.descr)
-
     #def repr_call_assembler(self):
     #    xxxx
 

_jitviewer/test/test_parser.py

-from _jitviewer.parser import ParserWithHtmlRepr, parse_log_counts, cssclass
+from _jitviewer.parser import ParserWithHtmlRepr, cssclass
 import py
 
 def parse(input):
     guard_true(i9, descr=<Guard2>) []
     i13 = getfield_raw(151937600, descr=<SignedFieldDescr pypysig_long_struct.c_value 0>)
     ''').operations
-    assert ops[0].html_repr().plaintext() == 'i9 = i7 < 1003'
-    assert ops[2].html_repr().plaintext() == 'i13 = ((pypysig_long_struct)151937600).value'
-
-def test_parse_log_count():
-    py.test.skip('fixme')
-    class Loop(object):
-        pass
-    
-    loops = [Loop() for i in range(13)]
-    nums = parse_log_counts(LINES, loops)
-    assert nums[5] == 2000
-    assert loops[9].count == 2000
+    assert '<' in ops[0].html_repr()
+    assert 'pypysig_long_struct' in ops[2].html_repr()
 
 def test_highlight_var():
     ops = parse('''
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.