summaryrefslogtreecommitdiff
path: root/misc/pylib/robofab/tools/objectDumper.py
blob: 29110ca531b9a627cccf462b0ac2bb08e9b9d639 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""Simple and ugly way to print some attributes and properties of an object to stdout.
FontLab doesn't have an object browser and sometimes we do need to look inside"""

from pprint import pprint

def classname(object, modname):
    """Get a class name and qualify it with a module name if necessary."""
    name = object.__name__
    if object.__module__ != modname:
        name = object.__module__ + '.' + name
    return name

def _objectDumper(object, indent=0, private=False):
	"""Collect a dict with the contents of the __dict__ as a quick means of peeking inside
	an instance. Some RoboFab locations do not support PyBrowser and still need debugging."""
	data = {}
	data['__class__'] = "%s at %d"%(classname(object.__class__, object.__module__), id(object))
	for k in object.__class__.__dict__.keys():
		if private and k[0] == "_":
			continue
		x = object.__class__.__dict__[k]
		if hasattr(x, "fget"):	#other means of recognising a property?
			try:
				try:
					value = _objectDumper(x.fget(self), 1)
				except:
					value = x.fget(self)
				data[k] = "[property, %s] %s"%(type(x.fget(self)).__name__, value)
			except:
				data[k] = "[property] (Error getting property value)"
	for k in object.__dict__.keys():
		if private and k[0] == "_":
			continue
		try:	
			data[k] = "[attribute, %s] %s"%(type(object.__dict__[k]).__name__, `object.__dict__[k]`)
		except:
			data[k] = "[attribute] (Error getting attribute value)"
	return data

def flattenDict(dict, indent=0):
	t = []
	k = dict.keys()
	k.sort()
	print
	print '---RoboFab Object Dump---'
	for key in k:
		value = dict[key]
		t.append(indent*"\t"+"%s: %s"%(key, value))
	t.append('')
	return "\r".join(t)

def dumpObject(object, private=False):
	print pprint(_objectDumper(object, private=private))