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