2013-03-17 17:20:21 +01:00
|
|
|
from sphinx.ext import autodoc
|
|
|
|
from sphinx.util.inspect import getargspec
|
|
|
|
from inspect import ArgSpec, getargspec, formatargspec
|
|
|
|
from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment
|
|
|
|
from itertools import count
|
|
|
|
|
|
|
|
|
|
|
|
class ThreadedDocumenter(autodoc.FunctionDocumenter):
|
|
|
|
'''Specialized documenter subclass for ThreadedSegment subclasses.'''
|
|
|
|
@classmethod
|
|
|
|
def can_document_member(cls, member, membername, isattr, parent):
|
|
|
|
return (isinstance(member, ThreadedSegment) or
|
|
|
|
super(ThreadedDocumenter, cls).can_document_member(member, membername, isattr, parent))
|
|
|
|
|
|
|
|
def format_args(self):
|
|
|
|
if isinstance(self.object, ThreadedSegment):
|
|
|
|
args = ['interval']
|
|
|
|
defaults = [getattr(self.object, 'interval', 1)]
|
|
|
|
methods = ['render', 'set_state']
|
|
|
|
if isinstance(self.object, KwThreadedSegment):
|
|
|
|
methods += ['key', 'render_one']
|
|
|
|
|
|
|
|
for method in methods:
|
|
|
|
if hasattr(self.object, method):
|
|
|
|
# Note: on <python-2.6 it may return simple tuple, not
|
|
|
|
# ArgSpec instance.
|
|
|
|
argspec = getargspec(getattr(self.object, method))
|
|
|
|
for i, arg in zip(count(-1, -1), reversed(argspec.args)):
|
|
|
|
if (arg == 'self' or
|
|
|
|
(arg == 'segment_info' and
|
2013-03-17 18:06:56 +01:00
|
|
|
getattr(self.object, 'powerline_requires_segment_info', None)) or
|
2013-03-17 17:20:21 +01:00
|
|
|
(method == 'render_one' and -i == len(argspec.args))):
|
|
|
|
continue
|
|
|
|
if argspec.defaults and len(argspec.defaults) >= -i:
|
|
|
|
default = argspec.defaults[i]
|
|
|
|
defaults.append(default)
|
|
|
|
args.append(arg)
|
|
|
|
else:
|
|
|
|
args.insert(0, arg)
|
|
|
|
argspec = ArgSpec(args=args, varargs=None, keywords=None, defaults=tuple(defaults))
|
|
|
|
else:
|
2013-03-17 18:06:56 +01:00
|
|
|
if hasattr(self.object, 'powerline_origin'):
|
|
|
|
obj = self.object.powerline_origin
|
|
|
|
else:
|
|
|
|
obj = self.object
|
|
|
|
|
|
|
|
argspec = getargspec(obj)
|
|
|
|
args = []
|
|
|
|
defaults = []
|
|
|
|
for i, arg in zip(count(-1, -1), reversed(argspec.args)):
|
|
|
|
if (arg == 'segment_info' and getattr(self.object, 'powerline_requires_segment_info', None)):
|
|
|
|
continue
|
|
|
|
if argspec.defaults and len(argspec.defaults) >= -i:
|
|
|
|
default = argspec.defaults[i]
|
|
|
|
defaults.append(default)
|
|
|
|
args.append(arg)
|
|
|
|
else:
|
|
|
|
args.insert(0, arg)
|
|
|
|
argspec = ArgSpec(args=args, varargs=argspec.varargs, keywords=argspec.keywords, defaults=tuple(defaults))
|
2013-03-17 17:20:21 +01:00
|
|
|
|
|
|
|
return formatargspec(*argspec).replace('\\', '\\\\')
|
|
|
|
|
|
|
|
|
|
|
|
def setup(app):
|
|
|
|
autodoc.setup(app)
|
|
|
|
app.add_autodocumenter(ThreadedDocumenter)
|