summaryrefslogtreecommitdiffstats
path: root/docs/source/powerline_autodoc.py
blob: eba42edb7895ced034a643b37ce7dc925567f601 (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
56
57
58
59
60
61
62
63
64
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)

import os

from inspect import formatargspec

from sphinx.ext import autodoc

from powerline.lint.inspect import getconfigargspec
from powerline.segments import Segment
from powerline.lib.unicode import unicode


def formatvalue(val):
	if type(val) is str:
		return '="' + unicode(val, 'utf-8').replace('"', '\\"').replace('\\', '\\\\') + '"'
	else:
		return '=' + repr(val)


class ThreadedDocumenter(autodoc.FunctionDocumenter):
	'''Specialized documenter subclass for ThreadedSegment subclasses.'''
	@classmethod
	def can_document_member(cls, member, membername, isattr, parent):
		return (isinstance(member, Segment) or
			super(ThreadedDocumenter, cls).can_document_member(member, membername, isattr, parent))

	def format_args(self):
		argspec = getconfigargspec(self.object)
		return formatargspec(*argspec, formatvalue=formatvalue).replace('\\', '\\\\')


class Repr(object):
	def __init__(self, repr_contents):
		self.repr_contents = repr_contents

	def __repr__(self):
		return '<{0}>'.format(self.repr_contents)


class EnvironDocumenter(autodoc.AttributeDocumenter):
	@classmethod
	def can_document_member(cls, member, membername, isattr, parent):
		if type(member) is dict and member.get('environ') is os.environ:
			return True
		else:
			return False

	def import_object(self, *args, **kwargs):
		ret = super(EnvironDocumenter, self).import_object(*args, **kwargs)
		if not ret:
			return ret
		self.object = self.object.copy()
		if 'home' in self.object:
			self.object.update(home=Repr('home directory'))
		self.object.update(environ=Repr('environ dictionary'))
		return True


def setup(app):
	autodoc.setup(app)
	app.add_autodocumenter(ThreadedDocumenter)
	app.add_autodocumenter(EnvironDocumenter)