Strip trailing whitespaces.

Some other changes to make code look more pythonish. No important changes.
This commit is contained in:
Thibault Févry 2011-10-21 23:42:46 +00:00
parent 9d69eb2e24
commit 35ee1e6725
5 changed files with 131 additions and 134 deletions

View file

@ -12,54 +12,54 @@ import re
import itertools
import wesnoth_type_tools
reload (wesnoth_type_tools)
reload(wesnoth_type_tools)
from wesnoth_type_tools import strip_all_type
class NullPointerPrinter(object) :
class NullPointerPrinter(object):
"""Print NULL for null pointers"""
def __init__(self, val) :
def __init__(self, val):
pass
def to_string(self) :
def to_string(self):
return "NULL"
def display_hint(self) :
def display_hint(self):
return 'string'
def create_wesnoth_lookup_function(pretty_printers_dict) :
def create_wesnoth_lookup_function(pretty_printers_dict):
"""Closure for lookup function """
def wesnoth_lookup_function (val):
"Look-up and return a pretty-printer that can print val."
def wesnoth_lookup_function(val):
"Look-up and return a pretty-printer that can print val."
#If it is a null pointer or object return the null pointer printer
if (val.type.code == gdb.TYPE_CODE_PTR and long(val) == 0) or (val.address == 0):
return NullPointerPrinter(val)
# Get the type name.
# Get the type name.
type = strip_all_type(val)
# Get the type name.
# Get the type name.
typename = type.tag
if typename == None:
return None
# Iterate over local dictionary of types to determine
# if a printer is registered for that type. Return an
# instantiation of the printer if found.
for function in pretty_printers_dict :
if function.match (typename) :
return pretty_printers_dict[function] (val)
for function in pretty_printers_dict:
if function.match(typename):
return pretty_printers_dict[function](val)
# Cannot find a pretty printer. Return None.
return None
return wesnoth_lookup_function
def register (new_pretty_printers):
def register(new_pretty_printers):
"""register the regex and printers from the dictionary with gdb"""
#delete all previous wesnoth printers
@ -69,7 +69,6 @@ def register (new_pretty_printers):
remove_printers.append(a)
for a in remove_printers:
gdb.pretty_printers.remove(a)
#Add the new printers with the new dictionary
gdb.pretty_printers.append(create_wesnoth_lookup_function(new_pretty_printers))

View file

@ -21,7 +21,7 @@ set print static-members off
"""
__doc__="""
__doc__ = """
python reload(wesnoth_gdb) #Interactively reload wesnoth_gdb
python wesnoth.gdb.help() #Help message
python print wesnoth_gdb.__doc__ #Help message
@ -37,9 +37,9 @@ import sys, gdb
def help():
print __doc__
#Force a reload, which is handy if you are interactively editting
#Force a reload, which is handy if you are interactively editting
if 'register_wesnoth_pretty_printers' in sys.modules:
reload (register_wesnoth_pretty_printers)
reload(register_wesnoth_pretty_printers)
else:
import register_wesnoth_pretty_printers
@ -53,7 +53,7 @@ pretty_printers_dict = {}
pretty_printers_dict = wesnoth_pretty_printers.add_printers(pretty_printers_dict)
register_wesnoth_pretty_printers.register(pretty_printers_dict)
#options
#get/set the default
@ -61,5 +61,3 @@ def get_levels_of_recursion():
return wesnoth_pretty_printers.RecursionManager.get_level()
def set_levels_of_recursion(num):
return wesnoth_pretty_printers.RecursionManager.set_level(num)

View file

@ -1,5 +1,5 @@
# Assorted Pretty printers for wesnoth data structures in gdb
# Assorted Pretty printers for wesnoth data structures in gdb
import gdb
import re
@ -7,14 +7,14 @@ import itertools
import wesnoth_type_tools
reload (wesnoth_type_tools)
reload(wesnoth_type_tools)
from wesnoth_type_tools import strip_all_type, dereference_if_possible
class RecursionManager(object):
"""Keeps track of the levels of recrusion and whether expansion should happen or not """
default=2
curr=0
default = 2
curr = 0
# @classmethod
# def __init__(cls, val):
@ -24,78 +24,79 @@ class RecursionManager(object):
@classmethod
def get_level(cls):
return cls.default
@classmethod
def set_level(cls, val):
cls.curr=0;
if val >= 0 :
cls.curr = 0
if val >= 0:
cls.default = val
return cls.default
@classmethod
def reset(cls):
cls.curr=0;
cls.curr = 0
return cls.default
@classmethod
def should_display(cls) :
def should_display(cls):
return cls.curr <= cls.default
@classmethod
def inc(cls):
cls.curr = cls.curr + 1
cls.curr += 1
return cls.should_display()
@classmethod
def dec(cls):
if cls.curr > 0 :
cls.curr = cls.curr - 1
if cls.curr > 0:
cls.curr -= 1
return cls.should_display()
#Printer for n_interned::t_interned
class T_InternedPrinter(object) :
#Printer for n_interned::t_interned
class T_InternedPrinter(object):
"""Print a t_interned_token<T>"""
def __init__(self, val) :
def __init__(self, val):
self.val = val
def to_string(self) :
def to_string(self):
#Returns either a string or an other convertible to string
return self.val['iter_']['first']['_M_dataplus']['_M_p']
def display_hint(self) :
def display_hint(self):
#one of 'string' 'array' 'map'
return 'string'
#Printer for n_token::t_token
class T_TokenPrinter(object) :
#Printer for n_token::t_token
class T_TokenPrinter(object):
"""Print a t_token"""
def __init__(self, val) :
def __init__(self, val):
self.val = val
def to_string(self) :
def to_string(self):
# Get the type.
type = self.val.type
# Get the type name.
# Get the type name.
type = strip_all_type(self.val)
#Return the underlying base class
baseclass = type.fields()[0].type
return self.val.cast(baseclass)
def display_hint(self) :
def display_hint(self):
#one of 'string' 'array' 'map'
return 'string'
class TstringPrinter(object) :
class TstringPrinter(object):
"""Print a t_string"""
def __init__(self, val) :
def __init__(self, val):
self.val = val
def to_string(self) :
# Get the type name.
def to_string(self):
# Get the type name.
type = strip_all_type(self.val)
#Dereference pointers
@ -103,79 +104,79 @@ class TstringPrinter(object) :
#Return the underlying base class
baseclass = type.fields()[0].type
shared = lval.cast(baseclass)['val_']
if shared == 0 :
if shared == 0:
return 'NULL'
#Print the untranslated string or an error
try :
try:
ret = shared.dereference()['val']['value_']['iter_']['first']
except RuntimeError, e:
return "wesnoth_gdb error invalid tstring"
return ret
def display_hint(self) :
def display_hint(self):
#one of 'string' 'array' 'map'
return 'string'
class AttributeValuePrinter(object) :
class AttributeValuePrinter(object):
"""Print an attribute_value"""
def __init__(self, val) :
def __init__(self, val):
self.val = val
def to_string(self) :
def to_string(self):
return 'attribute_value'
def children(self):
# Get the type.
# Get the type.
self.type = strip_all_type(self.val)
try:
#Dereference pointers
lval = dereference_if_possible(self.val)
attr = lval
attr = lval
attr_type = attr['type_']
class Atype:
EMPTY = 0
EMPTY = 0
BOOL = 1
INT = 2
DOUBLE = 3
TSTRING = 4
TOKEN = 5
if attr_type == Atype.EMPTY:
yield "EMPTY",""
yield "EMPTY", ""
elif attr_type == Atype.BOOL:
yield 'bool', 'true' if attr['bool_value_'] else 'false'
elif attr_type == Atype.INT :
yield 'int','int ' + ('%s' % attr['int_value_'])
elif attr_type == Atype.DOUBLE :
yield 'double','double ' + ('%s' % attr['double_value_'])
elif attr_type == Atype.TOKEN :
yield 'token','token ' + ('%s' % attr['token_value_'])
elif attr_type == Atype.TSTRING :
yield 't_string','t_string ' + ('%s' % attr['t_string_value_'])
else :
yield 'unknown',"unknown type code = " +('%d' % attr_type)
elif attr_type == Atype.INT:
yield 'int', 'int ' + ('%s' % attr['int_value_'])
elif attr_type == Atype.DOUBLE:
yield 'double', 'double ' + ('%s' % attr['double_value_'])
elif attr_type == Atype.TOKEN:
yield 'token', 'token ' + ('%s' % attr['token_value_'])
elif attr_type == Atype.TSTRING:
yield 't_string', 't_string ' + ('%s' % attr['t_string_value_'])
else:
yield 'unknown', "unknown type code = " + ('%d' % attr_type)
except RuntimeError, e:
yield 'error',"wesnoth_gdb error invalid %s" % self.val.type
yield 'error', "wesnoth_gdb error invalid %s" % self.val.type
def display_hint(self) :
def display_hint(self):
#one of 'string' 'array' 'map'
return 'string'
#This will be brittle as it depends on the underlying boost implementation, but its better than nothing
#With much help from http://stackoverflow.com/questions/2804641/pretty-printing-boostunordered-map-on-gdb
class BoostUnorderedMapPrinter(object) :
class BoostUnorderedMapPrinter(object):
""" Print a boost unordered map"""
class _iterator:
def __init__ (self, val):
def __init__(self, val):
self.buckets = val['table_']['buckets_']
if self.buckets != 0:
t_key = val.type.template_argument(0)
@ -192,15 +193,15 @@ class BoostUnorderedMapPrinter(object) :
return self
def next(self):
if self.buckets == 0 or not RecursionManager.should_display() :
if self.buckets == 0 or not RecursionManager.should_display():
raise StopIteration
while not self.node:
self.current_bucket = self.current_bucket + 1
self.current_bucket += 1
if self.current_bucket >= self.bucket_count:
raise StopIteration
self.node = self.buckets[self.current_bucket]['next_']
iterator = self.node.cast(self.node_pointer).cast(self.base_pointer).cast(self.pair_pointer).dereference()
iterator = self.node.cast(self.node_pointer).cast(self.base_pointer).cast(self.pair_pointer).dereference()
self.node = self.node['next_']
return ('%s' % iterator['first']), iterator['second']
@ -208,20 +209,20 @@ class BoostUnorderedMapPrinter(object) :
def __init__(self, val):
self.val = val
self.buckets = val['table_']['buckets_']
self.descended = False;
self.descended = False
def __del__(self) :
if self.descended :
RecursionManager.dec()
def __del__(self):
if self.descended:
RecursionManager.dec()
def children(self):
self.descended = True;
RecursionManager.inc()
self.descended = True
RecursionManager.inc()
return self._iterator(self.val)
def to_string(self):
ret = "boost::unordered_map"
if self.buckets == 0 :
if self.buckets == 0:
ret += " EMPTY"
return ret
@ -230,7 +231,7 @@ class BoostUnorderedMapPrinter(object) :
class BoostUnorderedMapIteratorPrinter(object):
def __init__ (self, val):
def __init__(self, val):
pair = val.type.template_argument(0).template_argument(0)
t_key = pair.template_argument(0)
t_val = pair.template_argument(1)
@ -243,7 +244,7 @@ class BoostUnorderedMapIteratorPrinter(object):
def to_string(self):
if not self.node:
return 'NULL'
iterator = self.node.cast(self.node_pointer).cast(self.base_pointer).cast(self.pair_pointer).dereference()
iterator = self.node.cast(self.node_pointer).cast(self.base_pointer).cast(self.pair_pointer).dereference()
return iterator['second']
def display_hint(self):
@ -251,50 +252,49 @@ class BoostUnorderedMapIteratorPrinter(object):
class ConfigPrinter(object) :
class ConfigPrinter(object):
"""Print a config"""
def __init__(self, val) :
def __init__(self, val):
self.val = val
# Get the type name.
# Get the type name.
self.type = strip_all_type(self.val)
#Dereference pointers
self.lval = dereference_if_possible(self.val)
def to_string(self) :
def to_string(self):
return "config"
def children(self) :
if RecursionManager.should_display() :
def children(self):
if RecursionManager.should_display():
#yield "invalid", self.val['invalid']
yield "values", self.lval['values']
yield "children", self.lval['children']
RecursionManager.inc()
RecursionManager.inc()
try:
yield "ordered_children", self.lval['ordered_children']
except RuntimeError, e:
RecursionManager.dec()
RecursionManager.dec()
else:
RecursionManager.dec()
RecursionManager.dec()
else :
else:
pass
def display_hint(self) :
def display_hint(self):
#one of 'string' 'array' 'map'
return 'string'
# register the pretty-printers
def add_printers(pretty_printers_dict) :
pretty_printers_dict[re.compile ('^n_interned::t_interned_token.*$')] = T_InternedPrinter
pretty_printers_dict[re.compile ('^n_token::t_token$')] = T_TokenPrinter
pretty_printers_dict[re.compile ('^t_string$')] = TstringPrinter
pretty_printers_dict[re.compile ('^config::attribute_value$')] = AttributeValuePrinter
pretty_printers_dict[re.compile ('^boost::unordered_map.*$')] = BoostUnorderedMapPrinter
pretty_printers_dict[re.compile ('^boost::unordered_detail::hash_iterator\<std::allocator\<std::pair\<.*$')] = BoostUnorderedMapIteratorPrinter
pretty_printers_dict[re.compile ('^config$')] = ConfigPrinter
return pretty_printers_dict
def add_printers(pretty_printers_dict):
pretty_printers_dict[re.compile('^n_interned::t_interned_token.*$')] = T_InternedPrinter
pretty_printers_dict[re.compile('^n_token::t_token$')] = T_TokenPrinter
pretty_printers_dict[re.compile('^t_string$')] = TstringPrinter
pretty_printers_dict[re.compile('^config::attribute_value$')] = AttributeValuePrinter
pretty_printers_dict[re.compile('^boost::unordered_map.*$')] = BoostUnorderedMapPrinter
pretty_printers_dict[re.compile('^boost::unordered_detail::hash_iterator\<std::allocator\<std::pair\<.*$')] = BoostUnorderedMapIteratorPrinter
pretty_printers_dict[re.compile('^config$')] = ConfigPrinter
return pretty_printers_dict

View file

@ -6,15 +6,15 @@ def strip_all_type(val):
"Strip the typename of all qualifiers and typedefs"
# Get the type.
type = val.type.unqualified()
# If it points to a reference, get the reference.
if (type.code == gdb.TYPE_CODE_REF) or (type.code == gdb.TYPE_CODE_PTR):
if type.code == gdb.TYPE_CODE_REF or type.code == gdb.TYPE_CODE_PTR:
try: type = type.target().unqualified()
except TypeError: type = val.type.unqualified()
# Get the unqualified type, stripped of typedefs.
type = type.strip_typedefs()
return type
def dereference_if_possible(val):
@ -22,9 +22,9 @@ def dereference_if_possible(val):
# Get the type.
type = val.type.unqualified()
# If it points to a reference, get the reference.
if (type.code == gdb.TYPE_CODE_PTR):
if type.code == gdb.TYPE_CODE_PTR:
return val.dereference()
return val

View file

@ -109,9 +109,9 @@ if __name__ == "__main__":
# strip
data = re.sub(" \*(?: |)", "", data)
#annotation
data = re.sub(r'@(?:begin|end|allow|remove)\{(?:parent|tag|link|global|type|key)\}(?:\{.*\})',"",data)
data = re.sub(r'@(?:begin|end|allow|remove)\{(?:parent|tag|link|global|type|key)\}(?:\{.*\})', "", data)
return data
def get_value(data, key):
@ -268,7 +268,7 @@ if __name__ == "__main__":
def create_widget_overview_table(data):
"""Creates a table for all available widgets."""
regex = re_record_start
regex += re_variable # widget type
regex += re_field_separator
@ -369,7 +369,7 @@ if __name__ == "__main__":
result += "|}"
return result
def validate_table(table):
"""Validates a table.
@ -392,7 +392,7 @@ if __name__ == "__main__":
result += "in:\n%s\n\n" % (table)
return result
else:
return None
return None
def create_table(table):
"""Wrapper for creating tables."""
@ -465,7 +465,7 @@ if __name__ == "__main__":
nest_level += 1
i = i.lstrip("@begin{description}{")
type += [i[:i.find("}")]]
i = i.strip(type[nest_level-1] + "}")
i = i.strip(type[nest_level - 1] + "}")
# We also handle end of blocks.
elif i.startswith("@end") and i != "@end{description}":