some code beautifying: __waitForPrompt
[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.breakpoints = []
17 self.watches = []
18 self.ints = []
19
20
21 def load(self, filename):
22 try:
23 cnt = 0
24 #Parse all lines
25 f = file(filename, "r")
26 for line in f.readlines():
27 cnt += 1
28
29 #Get command and tail
30 cmd = string.split(line)[0]
31 tail = string.join(string.split(line)[1:])
32
33 if cmd == "break":
34 self.parseBreak(tail)
35 elif cmd == "watch":
36 self.parseWatch(tail)
37 elif cmd == "int":
38 self.parseInt(tail)
39 else:
40 cnt -= 1
41 print "Unkown command", cmd
42 f.close()
43 return cnt
44
45 except IOError:
46 return None
47
48
49 def store(self, filename):
50 try:
51 f = file(filename, "w")
52
53 for b in self.breakpoints:
54 self.__writeBreak(f, b)
55
56 for w in self.watches:
57 self.__writeWatch(f, w)
58
59 for s in self.ints:
60 self.__writeInt(f, s)
61
62 f.close()
63 return True
64
65 except IOError:
66 return False
67
68
69
70 def parseBreak(self, tail):
71
72 tail = tail.strip()
73 rx = re.compile("^[\w/\._\-]+:\d+(\s+if\s+\S+.*)?$")
74
75 if not rx.search(tail):
76 print "Wrong breakpoint format:", tail
77 return
78
79 preif = string.split(tail, "if")[0].strip()
80 postif = string.join( string.split(tail, "if")[1:], "if").strip()
81
82 [file,lineno] = string.split(preif, ":")
83 lineno = int(lineno)
84
85 cond = None
86 if postif != "":
87 cond = postif
88
89 self.addBreak(file, lineno, cond)
90
91 def parseInt(self, tail):
92 tail.strip()
93 rx = re.compile("^[\w_\-]+\s+\d+$")
94
95 if not rx.search(tail):
96 print "Wrong size format:", tail
97 return
98
99 [name,val] = string.split(tail)
100 val = int(val)
101 self.addInt(name, val)
102
103 def parseWatch(self, tail):
104 self.addWatch(tail)
105
106
107 def __writeBreak(self, f, b):
108 if b["cond"] != None:
109 f.write("break %(file)s:%(lineno)d if %(cond)s\n" % b)
110 else:
111 f.write("break %(file)s:%(lineno)d\n" % b)
112
113 def __writeInt(self, f, s):
114 f.write("int %(name)s %(val)d\n" % s)
115
116 def __writeWatch(self, f, w):
117 f.write("watch %(expr)s\n" % w)
118
119
120 def addBreak(self, file, lineno, cond=None):
121 bp = {"file" : file, "lineno" : lineno, "cond" : cond}
122 if not bp in self.breakpoints:
123 self.breakpoints += [bp]
124
125 def addInt(self, name, val):
126 i = {"name": name, "val": val}
127 if not i in self.ints:
128 self.ints += [i]
129
130 def addWatch(self, expr):
131 w = {"expr" : expr.strip() }
132 if not w in self.watches:
133 self.watches += [w]
134
135
136 def findInt(self, name):
137 for i in self.ints:
138 if i["name"] == name:
139 return i["val"]
140 return None
141
142
143 def __str__(self):
144 return "breakpoints=" + str(self.breakpoints) + \
145 ", watches=" + str(self.watches) + \
146 ", ints=" + str(self.ints)
147
148
149