pygdb now saves absolute breakpoint paths
[pygdb.git] / Configuration.py
1 #!/usr/bin/python
2 #shuber, 2008-06-09
3
4 __author__ = "shuber"
5
6
7
8 import re
9 import string
10
11 import StatusWindow
12
13 class Configuration:
14
15 def __init__(self):
16 self.clear()
17
18
19 def clear(self):
20 self.breakpoints = []
21 self.watches = []
22 self.ints = []
23 self.currfile, self.currlineno = None, 0
24
25
26 def load(self, filename):
27 try:
28 self.clear()
29
30 cnt = 0
31 #Parse all lines
32 f = file(filename, "r")
33 for line in f.readlines():
34 cnt += 1
35
36 #Get command and tail
37 cmd = string.split(line)[0]
38 tail = string.join(string.split(line)[1:])
39
40 if cmd == "break":
41 self.parseBreak(tail)
42 elif cmd == "watch":
43 self.parseWatch(tail)
44 elif cmd == "int":
45 self.parseInt(tail)
46 elif cmd == "currpos":
47 self.parseCurrpos(tail)
48 else:
49 cnt -= 1
50 print "Unkown command", cmd
51 f.close()
52 return cnt
53
54 except IOError:
55 return None
56
57
58 def store(self, filename):
59 try:
60 f = file(filename, "w")
61
62 for b in self.breakpoints:
63 self.__writeBreak(f, b)
64
65 for w in self.watches:
66 self.__writeWatch(f, w)
67
68 for s in self.ints:
69 self.__writeInt(f, s)
70
71 if self.isCurrposSet():
72 self.__writeCurrpos(f)
73
74 f.close()
75 return True
76
77 except IOError:
78 return False
79
80
81
82 def parseBreak(self, tail):
83
84 tail = tail.strip()
85 rx = re.compile("^[\w/\._\-]+:\d+(\s+if\s+\S+.*)?$")
86
87 if not rx.search(tail):
88 print "Wrong breakpoint format:", tail
89 return
90
91 preif = string.split(tail, "if")[0].strip()
92 postif = string.join( string.split(tail, "if")[1:], "if").strip()
93
94 [file,lineno] = string.split(preif, ":")
95 lineno = int(lineno)
96
97 cond = None
98 if postif != "":
99 cond = postif
100
101 self.addBreak(file, lineno, cond)
102
103 def parseInt(self, tail):
104 tail.strip()
105 rx = re.compile("^[\w_\-]+\s+\d+$")
106
107 if not rx.search(tail):
108 print "Wrong size format:", tail
109 return
110
111 [name,val] = string.split(tail)
112 val = int(val)
113 self.addInt(name, val)
114
115 def parseWatch(self, tail):
116 self.addWatch(tail)
117
118 def parseCurrpos(self, tail):
119
120 tail = tail.strip()
121 rx = re.compile("^[\w/\._\-]+:\d+$")
122
123 if not rx.search(tail):
124 print "Wrong current position format:", tail
125 return
126
127 [file,lineno] = string.split(tail, ":")
128 lineno = int(lineno)
129
130 self.setCurrpos(file, lineno)
131
132
133 def __writeBreak(self, f, b):
134 if b["cond"] != None:
135 f.write("break %(file)s:%(lineno)d if %(cond)s\n" % b)
136 else:
137 f.write("break %(file)s:%(lineno)d\n" % b)
138
139 def __writeInt(self, f, s):
140 f.write("int %(name)s %(val)d\n" % s)
141
142 def __writeWatch(self, f, w):
143 f.write("watch %(expr)s\n" % w)
144
145 def __writeCurrpos(self, f):
146 f.write("currpos %s:%d\n" % (self.currfile, self.currlineno))
147
148
149 def addBreak(self, file, lineno, cond=None):
150 bp = {"file" : file, "lineno" : lineno, "cond" : cond}
151 if not bp in self.breakpoints:
152 self.breakpoints += [bp]
153
154 def addInt(self, name, val):
155 i = {"name": name, "val": val}
156 if not i in self.ints:
157 self.ints += [i]
158
159 def addWatch(self, expr):
160 w = {"expr" : expr.strip() }
161 if not w in self.watches:
162 self.watches += [w]
163
164 def setCurrpos(self, file, lineno):
165 self.currfile, self.currlineno = file, lineno
166
167 def isCurrposSet(self):
168 return self.currfile!=None
169
170
171 def findInt(self, name):
172 for i in self.ints:
173 if i["name"] == name:
174 return i["val"]
175 return None
176
177
178 def __str__(self):
179 return "breakpoints=" + str(self.breakpoints) + \
180 ", watches=" + str(self.watches) + \
181 ", ints=" + str(self.ints) + \
182 ", currpos=" + str((self.currfile, self.currlineno))
183
184
185