Package fortuneengine :: Module GameInspect
[hide private]
[frames] | no frames]

Source Code for Module fortuneengine.GameInspect

  1  #    FortuneEngine is free software: you can redistribute it and/or modify 
  2  #    it under the terms of the GNU General Public License as published by 
  3  #    the Free Software Foundation, either version 3 of the License, or 
  4  #    (at your option) any later version. 
  5  # 
  6  #    FortuneEngine is distributed in the hope that it will be useful, 
  7  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
  8  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  9  #    GNU General Public License for more details. 
 10  # 
 11  #    You should have received a copy of the GNU General Public License 
 12  #    along with the FortuneEngine.  If not, see <http://www.gnu.org/licenses/>. 
 13  # 
 14  #    Author: Justin Lewis  <jlew.blackout@gmail.com> 
 15   
 16  import inspect 
 17   
 18   
19 -class GameInspect(object):
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
26 - def __init__(self, object_root):
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
34 - def drilldown_object(self, objectname):
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 # Objects are separated by the period (".") symbol 51 object_tokens = objectname.split(".") 52 53 # Check if the first part of the name is registered with the 54 # game engine as that is our starting point 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 # Handles dot notation for sub modules by looping through the tokens 65 for token in object_tokens[1:]: 66 67 # Splits the dictionary/list token ("[") 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 # Handles dictionaries 78 for d_token in dict_token[1:]: 79 if d_token[-1] == "]": 80 d_token = d_token[:-1] 81 # Try list notation first then try dictionary notation 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
159 - def inspect_object(self, objectname):
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 # If it has the __dict__ attribute, it is an object we can inspect 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 # Inspect the object for all its methods 184 method_list = inspect.getmembers(obj, inspect.ismethod) 185 if method_list != []: 186 187 # Loop through the methods in the object and print them 188 # to the console 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 # Inspect the arguments to the current method 194 args, vargs, kwargs, local = inspect.getargspec(method[1]) 195 196 # Display function arguments 197 attribute_list = "%s\n\t\tArgs: %s" % \ 198 (attribute_list, ",".join(args)) 199 200 # Display * and ** arguments if they were found 201 if vargs: 202 attribute_list = "%s\n\t\tVArgs: %s" % \ 203 (attribute_list, ",".join(vargs)) 204 205 # Display KW Arguments if they were found 206 if kwargs: 207 attribute_list = "%s\n\t\tKWArgs: %s" % \ 208 (attribute_list, ",".join(kwargs)) 209 210 # If dictionary, show keys 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 # If list, iterate over the list and show its values 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 # We don't know what it is, so just display string representation 227 # of the object in question 228 else: 229 attribute_list = str(obj) 230 231 return "Class: %s\n%s" % (classname, attribute_list)
232