]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/PyCrust/wxd/decorator.py
6765268f5bca3e1e76ff86b0925ab28c319a0edb
1 """Decorator utility for documentation and shell scripting."""
3 __author__
= "Patrick K. O'Brien <pobrien@orbtech.com>"
5 __revision__
= "$Revision$"[11:-2]
16 def decorate(real
, decoration
):
17 """Decorate real module with docstrings from decoration module."""
18 realdict
= real
.__dict
__
19 for item
in decoration
.__dict
__.values():
20 if inspect
.isclass(item
):
21 decorateClass(item
, realdict
)
22 elif inspect
.isfunction(item
):
23 decorateFunction(item
, realdict
)
25 def decorateClass(item
, realdict
):
26 classname
= item
.__name
__
27 if not classname
.startswith('wx'):
28 classname
= 'wx' + classname
30 wxclass
= realdict
[classname
]
35 wxclass
.__doc
__ = item
.__doc
__
36 # Get attributes from only the item's local dictionary!
37 for attrname
, attr
in item
.__dict
__.items():
38 # If the attribute has a docstring, and the wx class has a
40 if hasattr(attr
, '__doc__') and hasattr(wxclass
, attrname
):
41 if inspect
.isfunction(attr
):
42 # Class methods are functions.
43 doc
= getdoc(attr
, drop
=True)
44 # Is getattr() okay, or do we want to only look in
45 # the wxclass.__dict__ and wxclassPtr.__dict__?
46 wxattr
= getattr(wxclass
, attrname
)
47 # Our class code may be defined incorrectly, and
48 # the wxattr may not actually be a class method,
49 # but that's okay because the following attempt
57 def decorateFunction(item
, realdict
):
58 funcname
= item
.__name
__
59 if funcname
in realdict
.keys():
60 func
= realdict
[funcname
]
61 doc
= getdoc(item
, drop
=False)
63 # Built-in functions have a read-only docstring. :-(
69 def getdoc(attr
, drop
=False):
70 """Return a docstring for attr, which should be a method."""
73 doc
= inspect
.getdoc(attr
).strip()
75 # tip is a string with name(argspec), like: "SetLabel(label)"
77 argspec
= apply(inspect
.formatargspec
, inspect
.getargspec(attr
))
78 # The first parameter to a method is a reference to an instance,
79 # usually coded as "self", and is usually passed automatically by
80 # Python and therefore we want to drop it.
81 temp
= argspec
.split(',')
82 if len(temp
) == 1: # No other arguments.
84 elif drop
: # Drop the first argument.
85 argspec
= '(' + ','.join(temp
[1:]).lstrip()
87 argspec
= ','.join(temp
).lstrip()
89 firstline
= doc
.split('\n')[0].lstrip()
91 doc
= '%s\n\n%s' % (tip
, doc
)