This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
get_help.py
292 lines (182 loc) · 6.64 KB
/
get_help.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python3
# coding: utf-8
import importlib
import inspect
from pathlib import Path
import textwrap
import logging
logger = logging.getLogger(__name__)
class multi_line_string():
def __init__(self, s=' ', columns=65):
self._string = []
self.string = s
self.columns=columns
def __str__(self):
return self.string
@property
def string(self):
return '\n'.join(self._string)
@string.setter
def string(self, s):
self._string.append(s)
@property
def string_list(self):
return self._string
@property
def wrapped_string(self):
return '\n'.join(textwrap.wrap(' '.join(self._string), self.columns)).lstrip()
def get_modules(root='./plugins/'):
'''get a list of modules contained within the path specified
Args:
root(str): path to search for modules
Returns:
list of str: list modules found within the root'''
module_list = []
p = Path(root).resolve()
for i in p.glob('*'):
if i.is_dir() and i.name[0] not in ('_', '.'):
module_list.append(i.name)
return module_list
def get_module_docs(module):
'''return only user-facing docstrings that contain "%U"
Args:
module: python module
Returns:
string containing docstrings
'''
mls = multi_line_string()
members = inspect.getmembers(module)
for member in members:
if inspect.isfunction(member[1]):
# skip entries that don't have a docstring
if not member[1].__doc__:
continue
# skip docstrings functions not tagged with '%U' as last characters
if member[1].__doc__.endswith('%U'):
mls.string = f'FUNCTION: {member[0]}'
mls.string = member[1].__doc__.replace('%U', '')
mls.string = '_'*75
mls.string = ' '
else:
continue
return mls.string
def get_layouts(module):
'''get layout names provided by a plugin
Args:
module: python module
Returns:
string containing layout names'''
layout_ignore = ['os', 'dir_path']
mls = multi_line_string()
try:
my_dir = dir(getattr(module, 'layout'))
except AttributeError:
my_dir =[f'NO LAYOUTS FOUND IN {module.__name__}']
mls.string = 'LAYOUTS AVAILABLE:'
for item in my_dir:
if not item.startswith('__') and not item in layout_ignore:
mls.string = f' {item}'
return mls.string
def get_data_keys(module):
'''return data keys provided by plugin
Args:
module: python module
Returns:
string containing data keys provided by a plugin's update_function '''
mls = multi_line_string()
try:
data = module.constants.data
except AttributeError:
data = {'no keys available': None}
mls.string = f'\nDATA KEYS AVAILABLE FOR USE IN LAYOUTS PROVIDED BY {module.__name__}:'
for k in data.keys():
mls.string = f' {k}'
return mls.string
def get_sample_config(module):
'''return sample configuration
Args:
module: python module
Returns:
string containing sample configuration if it exists'''
mls = multi_line_string()
sample_config = None
try:
sample_config = module.constants.sample_config
except AttributeError:
sample_config = f'no sample configuration provided in {module.__name__}.constants'
mls.string = f'\nSAMPLE CONFIGURATION FOR {module.__name__}'
mls.string = sample_config
return mls.string
def get_doc_string(module, function):
'''return a docstring for a function from within a module
Args:
module: python module
function(str): string of function contained in module
Returns:
string containing docstring of for module.function'''
try:
f = getattr(module, function)
except AttributeError as e:
return e
return f.__doc__
def get_help(module=None, print_help=True, plugin_path='./plugins'):
'''display information for a plugin module including:
* Functions available
* Layouts defined
* data keys returned by update_function()
Args:
module(`str`): "plugin_name" or "plugin_name.function" or None for a list of plugins
when a function is provided, the function is executed'''
plugin_path = Path(plugin_path)
mls = multi_line_string()
plugin_list = []
if not module:
mls.string = 'get plugin information and user-facing functions:'
mls.string = 'Usage: --plugin_info PLUGIN_NAME|PLUGIN_NAME.FUNCTION'
mls.string = 'PLUGINS AVAILABLE:'
for i in get_modules(plugin_path):
mls.string = f' {i}'
if print_help:
print(mls.string)
return mls.string
my_module = module.split('.')
logging.debug(f'gathering information for: {module}')
# my_module = module.split('.')
# logging.debug(f'my_module: {my_module}')
try:
plugin_name = f'{".".join(plugin_path.parts)}.{my_module[0]}.{my_module[0]}'
logging.debug(f'attempting to import: {plugin_name}')
imported = importlib.import_module(plugin_name)
except ImportError as e:
logging.warning(f'error importing {plugin_name}: {e}')
mls.string = f'error importing {plugin_name}: {str(e)}'
imported = False
# try:
# logging.debug(f'importing module: {my_module[0]}')
# imported = importlib.import_module(f'{plugin_path.name}.{my_module[0]}.{my_module[0]}')
# except Exception as e:
# mls.string = f'error gathering information for module: {str(e)}'
# logging.debug(mls.string)
# imported = None
try:
version = imported.constants.version
except AttributeError:
version = 'no version provided'
# try:
# version = imported.constants.version
# except AttributeError:
# version = 'no version provided'
if len(my_module) == 1 and imported:
plugin_list.append(my_module)
mls.string = f'PLUGIN: {my_module[0]} v:{version}\n'
mls.string = get_module_docs(imported)
mls.string = get_sample_config(imported)
mls.string = get_layouts(imported)
mls.string = get_data_keys(imported)
elif len(my_module) > 1:
mls.string = get_doc_string(imported, my_module[1])
else:
pass
if print_help:
print(mls.string)
return mls.string