1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import inspect
17
18
20 """
21 GameInspect is a class that can inspect and modify object trees.
22
23 The top most object must be a dictionary
24 """
25
27 """
28 Init function of the GameInspect class.
29
30 @param object_root: The root dictionary of the tree
31 """
32 self.root = object_root
33
35 """
36 Takes the objectname string and tries to find the object that it is
37 representing and returns that object.
38
39 Example: battle.enemy_list[1].sprite._images[1]
40
41 @param objectname: The string that represents the object's path.
42 @return: Returns the object requested
43 @raise Exception: Throws an Exception with the string being the
44 path error.
45 """
46 last = "empt"
47 obj = "empt"
48 last_token = ""
49
50
51 object_tokens = objectname.split(".")
52
53
54
55 try:
56 obj = self.root[object_tokens[0]]
57 last = obj
58 last_token = object_tokens[0]
59
60 except KeyError:
61 raise Exception("%s is not registered with the game engine" %
62 object_tokens[0])
63
64
65 for token in object_tokens[1:]:
66
67
68 dict_token = token.split('[')
69 try:
70 last = obj
71 obj = getattr(obj, dict_token[0])
72 last_token = dict_token[0]
73
74 except:
75 raise Exception("Error finding member element: %s" % token)
76
77
78 for d_token in dict_token[1:]:
79 if d_token[-1] == "]":
80 d_token = d_token[:-1]
81
82 try:
83 key = int(d_token)
84 except:
85 key = d_token
86
87 try:
88 last = obj
89 obj = obj[key]
90 last_token = key
91 except:
92 raise Exception("Unable to find %s" % key)
93
94 else:
95 raise Exception("Invalid Syntax, expected ] at end of %s" %
96 d_token)
97
98 return obj, last, last_token
99
100 - def set_eval(self, objectname, statement):
101 """
102 Sets the object referenced by objectname to a value returned by
103 passing the string stored in the val parameter to an eval statement.
104
105 @param objectname: A string representation of the location
106 of the object being inspected in relation
107 to the game engine registered object.
108 @param statement: A string to be evaluated and set to the object.
109 """
110 try:
111 obj, last, last_token = self.drilldown_object(objectname)
112 except Exception, detail:
113 return str(detail)
114
115 try:
116 setattr(last, last_token, eval(str(statement)))
117 except Exception, detail:
118 return str(detail)
119
120 - def set_str(self, objectname, val):
121 """
122 Sets the object referenced by objectname to a string passed into the
123 val parameter.
124
125 @param objectname: A string representation of the location
126 of the object being inspected in relation
127 to the game engine registered object.
128 @param val: A string to be set as the value of the object.
129 """
130 try:
131 obj, last, last_token = self.drilldown_object(objectname)
132 except Exception, detail:
133 return str(detail)
134
135 setattr(last, last_token, val)
136
137 - def set_int(self, objectname, val):
138 """
139 Sets the object referenced by objectname to an integer passed into the
140 val parameter. It may be a string that holds the int as it will be
141 type casted.
142
143 @param objectname: A string representation of the location
144 of the object being inspected in relation
145 to the game engine registered object.
146 @param val: An int/string containing an int to be set as
147 the value of the object.
148 """
149 try:
150 obj, last, last_token = self.drilldown_object(objectname)
151 except Exception, detail:
152 return str(detail)
153
154 try:
155 setattr(last, last_token, int(val))
156 except:
157 return str(detail)
158
160 """
161 Displays information about the object path it is passed
162
163 @param objectname: A string representation of the location
164 of the object being inspected in relation
165 to the game engine registered object.
166 """
167 try:
168 obj, last, last_token = self.drilldown_object(objectname)
169
170 except Exception, detail:
171 return str(detail)
172
173 classname = obj.__class__.__name__
174
175
176 if hasattr(obj, "__dict__"):
177 attribute_list = "Attributes:"
178 attributes = obj.__dict__
179 for attribute_key in attributes.keys():
180 attribute_list = "%s\n\t%s:%s" % (attribute_list,
181 attribute_key, str(attributes[attribute_key]))
182
183
184 method_list = inspect.getmembers(obj, inspect.ismethod)
185 if method_list != []:
186
187
188
189 attribute_list = "%s\n\nMethods:" % attribute_list
190 for method in method_list:
191 attribute_list = "%s\n\t%s" % (attribute_list, method[0])
192
193
194 args, vargs, kwargs, local = inspect.getargspec(method[1])
195
196
197 attribute_list = "%s\n\t\tArgs: %s" % \
198 (attribute_list, ",".join(args))
199
200
201 if vargs:
202 attribute_list = "%s\n\t\tVArgs: %s" % \
203 (attribute_list, ",".join(vargs))
204
205
206 if kwargs:
207 attribute_list = "%s\n\t\tKWArgs: %s" % \
208 (attribute_list, ",".join(kwargs))
209
210
211 elif hasattr(obj, "keys"):
212 attribute_list = "Dictionary Items:"
213
214 for d_obj in obj.keys():
215 attribute_list = "%s\n\t%s:%s" % (attribute_list, d_obj,
216 str(obj[d_obj]))
217
218
219 elif type(obj).__name__ == 'list':
220 i = 0
221 attribute_list = "List Items:"
222 for item in obj:
223 attribute_list = "%s\n\t%d:%s" % (attribute_list, i, str(item))
224 i = i + 1
225
226
227
228 else:
229 attribute_list = str(obj)
230
231 return "Class: %s\n%s" % (classname, attribute_list)
232