This repository has been archived by the owner on Dec 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
docs.py
142 lines (110 loc) · 4.38 KB
/
docs.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from os.path import exists, join, abspath, sep, splitext, dirname
from json import dumps
from provy.core import Role
import os
import fnmatch
import inspect
class RoleDoc(object):
def __init__(self, role, name, module, docs):
self.role = role
self.name = name
self.module = module
self.fullname = "%s.%s" % (module, name)
self.docs = docs
self.methods = []
self.parse_methods(role)
def parse_methods(self, role):
for name, member in inspect.getmembers(role):
if not inspect.ismethod(member) or name.startswith('_'):
continue
if not member.__module__ == role.__module__:
continue
if not member.__doc__:
print "Warning: Method %s of role %s does not have docstring." % (name, role.__name__)
self.add_method(NameDoc(name, member.__doc__))
def add_method(self, method_doc):
self.methods.append(method_doc)
def to_dict(self):
obj = {
'__name__': self.name,
'__fullName__': self.fullname,
'__module__': self.module,
'__doc__': self.docs and self.docs.strip() or None,
'__methods__': []
}
for method in self.methods:
obj['__methods__'].append(method.to_dict())
return obj
class NameDoc(object):
def __init__(self, name, doc):
self.name = name
self.doc = doc
def to_dict(self):
return {
'__name__': self.name,
'__doc__': self.doc and self.doc.strip() or None
}
def main():
path = "/tmp/docs.json"
source_path = join(os.curdir, 'provy', 'more')
if not exists(dirname(path)):
os.makedirs(dirname(path))
root_namespace = 'provy.more'
roles_to_document = {
'Role': RoleDoc(Role, 'Role', 'provy.core.roles', Role.__doc__)
}
for root, dirs, files in os.walk(source_path):
for file_name in files:
if file_name == "__init__.py":
continue
if not fnmatch.fnmatch(file_name, '*.py'):
continue
module_path = '%s.%s.%s' % (root_namespace,
get_namespace_for(root),
splitext(file_name)[0])
module = import_module(module_path)
for name, member in inspect.getmembers(module):
if not inspect.isclass(member) or not issubclass(member, Role):
continue
if member.__module__ != module_path:
continue
if not member.__doc__:
print "Warning: Role %s.%s does not have docstring." % (member.__module__, name)
roles_to_document[module_path] = RoleDoc(member,
name,
member.__module__,
member.__doc__)
tree = {}
for full_name, role_doc in roles_to_document.iteritems():
role = role_doc.role
name = role_doc.name
current = tree
module = __import__(role.__module__)
for part in role.__module__.split('.'):
if hasattr(module, part):
module = getattr(module, part)
if not part in current:
if not module.__doc__:
print "Warning: Module %s does not have docstring." % module.__name__
current[part] = {
'__name__': module.__name__,
'__doc__': module.__doc__ and module.__doc__.strip() or None
}
if part == role.__module__.split('.')[-1]:
current[part][role_doc.name] = role_doc.to_dict()
current = current[part]
contents = dumps(tree, sort_keys=True, separators=(',', ':'))
with open(path, 'w') as f:
f.write(contents)
def import_module(module_path):
module = __import__(module_path)
return reduce(getattr, module_path.split('.')[1:], module)
def get_namespace_for(directory):
source_path = abspath(join(os.curdir, 'provy', 'more'))
diff = abspath(directory).replace(source_path, '')
namespace = '.'.join([module for module in diff.split(sep) if module])
return namespace
if __name__ == '__main__':
main()