Release coccinelle-0.1.10
[bpt/coccinelle.git] / python / coccilib / output_gui.py
1 import pygtk
2 import gtk, gobject
3 import coccilib.coccigui
4 import coccilib.coccigui.coccigui
5 from threading import Thread, Lock
6 import time
7 from copy import deepcopy
8
9 class Output:
10 """In order to implement an output class for use with Coccinelle,
11 one can inherit from this class and overload register_match with
12 the same number of arguments.
13
14 include_match will be overwritten by inheriting from your actual
15 class, and thus if your class is a.b.C then Coccinelle will create
16 a Python class "class Coccinelle(a.b.C)" that hooks include_match
17 into the O'Caml internals.
18 """
19 def include_match(self, b):
20 pass
21
22 def register_match(self, include, messages):
23 pass
24
25 def combine(self, meta_variable, locations):
26 nmv = deepcopy(meta_variable)
27 nloc = [deepcopy(loc) for loc in locations]
28 nmv.location = nloc[0]
29 nmv.locations = nloc
30
31 return nmv
32
33 def finalise(self):
34 pass
35
36 def print_main(self, msg, p) :
37 print "* TODO [[view:%s::face=ovl-face1::linb=%s::colb=%s::cole=%s][%s %s::%s]]" % (p[0].file,p[0].line,p[0].column,p[0].column_end,msg,p[0].file,p[0].line)
38
39 def print_sec(self, msg, p) :
40 print "[[view:%s::face=ovl-face2::linb=%s::colb=%s::cole=%s][%s]]" % (p[0].file,p[0].line,p[0].column,p[0].column_end,msg)
41
42 def print_secs(self, msg, ps) :
43 for i in ps:
44 print "[[view:%s::face=ovl-face2::linb=%s::colb=%s::cole=%s][%s]]" % (i.file,i.line,i.column,i.column_end,msg)
45
46 class Console(Output):
47 def __init__(self):
48 pass
49
50 def register_match(self, include, messages):
51 self.include_match(include)
52 if include:
53 for variable, message in messages:
54 print "%s:%s:%s: %s - %s" % (variable.location.file, variable.location.line, variable.location.column, message, variable)
55
56 class GtkRunner(Thread):
57 def __init__(self):
58 Thread.__init__(self)
59 self.lock = Lock()
60 self.rows = []
61
62 def add_row(self, cocci, l):
63 for i in xrange(0, len(l)):
64 l[i] = (l[i][1], l[i][0].location.file, l[i][0].location.line, l[i][0].location.column)
65
66 self.lock.acquire()
67 try:
68 self.rows.append((cocci, l))
69 finally:
70 self.lock.release()
71
72 def has_row(self):
73 self.lock.acquire()
74 try:
75 return len(self.rows) > 0
76 finally:
77 self.lock.release()
78
79 def get_row(self):
80 self.lock.acquire()
81 try:
82 return self.rows.pop(0)
83 finally:
84 self.lock.release()
85
86 def update(self):
87 while self.has_row():
88 cocci, l = self.get_row()
89 self.gui.add_result(cocci, l)
90 gobject.timeout_add(1000, self.update)
91
92 def run(self):
93 self.gui = coccilib.coccigui.coccigui.pycocci()
94 globals()['gtk_sock'] = self.gui
95 gobject.timeout_add(1000, self.update)
96
97 gtk.gdk.threads_init()
98 gtk.gdk.threads_enter()
99
100 gtk.main()
101
102 gtk.gdk.threads_leave()
103
104 globals().pop('gtk_thread')
105 globals().pop('gtk_sock')
106
107 class Gtk(Output):
108 def check_availability(self):
109 if not globals().has_key('gtk_sock'):
110 t = GtkRunner()
111 globals()['gtk_thread'] = t
112 globals()['gtk_thread'].start()
113 time.sleep(2)
114
115 def register_match(self, include, messages):
116 self.check_availability()
117
118 self.include_match(include)
119 if include:
120 globals()['gtk_thread'].add_row(self.cocci_file, messages)
121
122 def finalise(self):
123 self.check_availability()
124
125 globals()['gtk_thread'].join()