My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Changes
# 20120128 migrate to new core4 with 'process_status' and 'status_type'

__all__ = ['PyMetaInfo', ]
__doc__ = '''Generate meta description for python module. Based on lib/pydoc.py'''

import types
import pkgutil
import inspect
from pydoc import locate, isdata, visiblename

from pyflowctrl import core4 as core

INPUT_STREAM_SCHEMA = {
"module": {
"type": "string",
"title": "Module name",
"required": "true",
}
}

OUTPUT_STREAM_SCHEMA = {
"name": {
"type": "string",
"title": "Module or class name",
"required": "true",
},
"description": {
"type": "string",
"title": "Module or class description",
},
"object_type": {
"type": "string",
"title": "Module or class type",
"required": "true",
},
"based_on": {
"type": "array",
"title": "list of bassed-on classes",
"items": {
"type": "string",
"title": "Based-on class",
}
},
"classes": {
"type": "array",
"title": "List of classes in module",
"items": {
"type": "object",
"title": "Class of module",
"properties:": {
"name": {
"type": "string",
"title": "name of class",
"required": "true",
},
"path": {
"type": "string",
"title": "path for import class",
"required": "true",
},
"based_on": {
"type": "array",
"title": "Based-on classes",
"required": "true",
"items": {
"type": "string",
"title": "Based-on class",
"required": "false",
}
}
}
}
},
"packages": {
"type": "array",
"title": "list of module packages",
"items": {
"type": "string",
"title": "Package",
}
},
"submodules": {
"type": "array",
"title": "list of submodules",
"items": {
"type": "string",
"title": "Submodule",
}
},
"funcs": {
"type": "array",
"title": "list of functions",
"items": {
"type": "string",
"title": "function name",
}
},
"data": { # TODO define it
"type": "any",
"title": "list of parameters",
},
}


class PyMetaInfo(core.Process):
''' Extact metadata information about module/class '''

__PROCESS__ = {
'name': 'PyMetaInfo',
'description': 'Extact metadata information about module/class. Customized for pyflowctrl.',
'author': 'Andrey Usov <http://devel.ownport.net>',
'url': 'http://pyflowctrl.googlecode.com/hg/pyflowctrl/core4/processes/pymetainfo2.py',
'io': {
'input': {'type': core.stream_type.input, 'schema': INPUT_STREAM_SCHEMA,},
'output': {'type': core.stream_type.output, 'schema': OUTPUT_STREAM_SCHEMA,},
},
}

def get_elements(self, obj, element_type):
''' get list of object's elements by type'''
try:
all = obj.__all__
except AttributeError:
all = None
elements = []
for key, value in inspect.getmembers(obj, element_type):
if visiblename(key, all, value):
elements.append((key, value))
return elements

def get_classes(self, obj):
''' return list of classes '''
classes = []
for c in self.get_elements(obj, inspect.isclass):
name = c[0]
path = '.'.join((c[1].__module__,c[1].__name__))
based_on = []
for c in c[1].__bases__:
based_on.append('.'.join((c.__module__, c.__name__)))
classes.append((name, path, based_on))
return classes

def get_funcs(self, obj):
''' return list of functions, filtered'''
funcs = []
for f in self.get_elements(obj, inspect.isroutine):
if isinstance(f[1], types.MethodType):
f_name = f[0]
f_desc = inspect.getdoc(f[1]) or inspect.getcomments(obj)
funcs.append((f_name, f_desc))
return funcs

def get_data(self, obj):
''' return data for object. filtered'''
data = []
for e in self.get_elements(obj, isdata):
if e[0] not in ['__dict__', '__weakref__']:
data.append(e)
return data

def get_modpkgs(self, obj):
''' return list of module's packages '''
modpkgs = []
modpkgs_names = set()
if hasattr(obj, '__path__'):
for importer, modname, ispkg in pkgutil.iter_modules(obj.__path__):
modpkgs_names.add(modname)
if ispkg:
modpkgs.append(modname + ' (package)')
else:
modpkgs.append(modname)
modpkgs.sort()
return modpkgs

def remove_empty_meta(self, metadata):
''' remove empty data '''
cleared_meta = {}
for k in metadata:
if metadata[k]:
cleared_meta[k] = metadata[k]
return cleared_meta

def get_metadata(self, obj):
''' get dictionary of object's metadata '''

return self.remove_empty_meta(metadata)

def main(self):
while True:
try:
packet = self.io['input'].get()
except core.EmptyStream:
yield core.process_status.waiting
continue

obj = locate(packet.module)
# remove input fields
del packet.module

if obj == None:
yield core.process_status.processing
continue

packet.name = obj.__name__
packet.object_type = type(obj)
packet.description = inspect.getdoc(obj) or inspect.getcomments(obj)
packet.classes = self.get_classes(obj)
packet.funcs = self.get_funcs(obj)
packet.data = self.get_data(obj)
packet.module_packages = self.get_modpkgs(obj)
packet.submodules = self.get_elements(obj, inspect.ismodule)

packet.based_on = []
if hasattr(obj, '__bases__'):
for c in list(obj.__bases__):
packet.based_on.append('.'.join((c.__module__, c.__name__)))

self.io['output'].put(packet)
yield core.process_status.processing




Change log

5566ed22b3cb by Andrey Usov <ownport> on Feb 2, 2012   Diff
added support of stream schema
Go to: 
Project members, sign in to write a code review

Older revisions

0684dc5ed0f6 by Andrey Usov <ownport> on Jan 30, 2012   Diff
remove comments
b117d433dc07 by Andrey Usov <ownport> on Jan 30, 2012   Diff
migration to new definition of stream
scheme
75188a3da290 by Andrey Usov <ownport> on Jan 24, 2012   Diff
pyflowctrl/core4/processes/pymetainfo2
.py: metainfo "url" corrected
All revisions of this file

File info

Size: 7005 bytes, 227 lines
Powered by Google Project Hosting