Add LICENSE
[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 self.command = None
25
26
27 def load(self, filename):
28 try:
29 self.clear()
30
31 cnt = 0
32 #Parse all lines
33 f = file(filename, "r")
34 for line in f.readlines():
35 cnt += 1
36
37 #Get command and tail
38 cmd = string.split(line)[0]
39 tail = string.join(string.split(line)[1:])
40
41 if cmd == "break":
42 self.parseBreak(tail)
43 elif cmd == "watch":
44 self.parseWatch(tail)
45 elif cmd == "int":
46 self.parseInt(tail)
47 elif cmd == "currpos":
48 self.parseCurrpos(tail)
49 elif cmd == "cmd":
50 self.parseCommand(tail)
51 else:
52 cnt -= 1
53 print "Unkown command", cmd
54 f.close()
55 return cnt
56
57 except IOError:
58 return None
59
60
61 def store(self, filename):
62 try:
63 f = file(filename, "w")
64
65 for b in self.breakpoints:
66 self.__writeBreak(f, b)
67
68 for w in self.watches:
69 self.__writeWatch(f, w)
70
71 for s in self.ints:
72 self.__writeInt(f, s)
73
74 if self.isCurrposSet():
75 self.__writeCurrpos(f)
76
77 if self.getCommand() != None:
78 self.__writeCommand(f)
79
80 f.close()
81 return True
82
83 except IOError:
84 return False
85
86
87
88 def parseBreak(self, tail):
89
90 tail = tail.strip()
91 rx = re.compile("^[\w/\._\-]+:\d+(\s+if\s+\S+.*)?$")
92
93 if not rx.search(tail):
94 print "Wrong breakpoint format:", tail
95 return
96
97 preif = string.split(tail, " if ")[0].strip()
98 postif = string.join( string.split(tail, " if ")[1:], " if ").strip()
99
100 [file,lineno] = string.split(preif, ":")
101 lineno = int(lineno)
102
103 cond = None
104 if postif != "":
105 cond = postif
106
107 self.addBreak(file, lineno, cond)
108
109 def parseInt(self, tail):
110 tail.strip()
111 rx = re.compile("^[\w_\-]+\s+\d+$")
112
113 if not rx.search(tail):
114 print "Wrong size format:", tail
115 return
116
117 [name,val] = string.split(tail)
118 val = int(val)
119 self.addInt(name, val)
120
121 def parseWatch(self, tail):
122 self.addWatch(tail)
123
124 def parseCurrpos(self, tail):
125
126 tail = tail.strip()
127 rx = re.compile("^[\w/\._\-]+:\d+$")
128
129 if not rx.search(tail):
130 print "Wrong current position format:", tail
131 return
132
133 [file,lineno] = string.split(tail, ":")
134 lineno = int(lineno)
135
136 self.setCurrpos(file, lineno)
137
138 def parseCommand(self, tail):
139 self.command = tail
140
141
142 def __writeBreak(self, f, b):
143 if b["cond"] != None:
144 f.write("break %(file)s:%(lineno)d if %(cond)s\n" % b)
145 else:
146 f.write("break %(file)s:%(lineno)d\n" % b)
147
148 def __writeInt(self, f, s):
149 f.write("int %(name)s %(val)d\n" % s)
150
151 def __writeWatch(self, f, w):
152 f.write("watch %(expr)s\n" % w)
153
154 def __writeCurrpos(self, f):
155 f.write("currpos %s:%d\n" % (self.currfile, self.currlineno))
156
157 def __writeCommand(self, f):
158 f.write("cmd %s\n" % self.command)
159
160
161 def addBreak(self, file, lineno, cond=None):
162 bp = {"file" : file, "lineno" : lineno, "cond" : cond}
163 if not bp in self.breakpoints:
164 self.breakpoints += [bp]
165
166 def addInt(self, name, val):
167 i = {"name": name, "val": val}
168 if not i in self.ints:
169 self.ints += [i]
170
171 def addWatch(self, expr):
172 w = {"expr" : expr.strip() }
173 if not w in self.watches:
174 self.watches += [w]
175
176 def setCurrpos(self, file, lineno):
177 self.currfile, self.currlineno = file, lineno
178
179 def isCurrposSet(self):
180 return self.currfile!=None
181
182
183 def delCurrpos(self):
184 self.currfile = None
185
186 def setCommand(self,cmd):
187 self.command = cmd
188
189 def getCommand(self):
190 return self.command
191
192 def findInt(self, name):
193 for i in self.ints:
194 if i["name"] == name:
195 return i["val"]
196 return None
197
198
199 def __str__(self):
200 return "breakpoints=" + str(self.breakpoints) + \
201 ", watches=" + str(self.watches) + \
202 ", ints=" + str(self.ints) + \
203 ", currpos=" + str((self.currfile, self.currlineno))
204
205
206