-
Notifications
You must be signed in to change notification settings - Fork 1
/
star_namer.py
executable file
·80 lines (67 loc) · 2.08 KB
/
star_namer.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
#!/usr/bin/python3
# Replace star imports with something more useful
# Usage: ./star_namer.py <module_file> <script_filename>
# Requires pylint: https://www.pylint.org/#install
import os
import sys
import inspect
import universe
from sd.common import error, eprint
from sd.columns import indenter, auto_cols
from sd.easy_args import easy_parse
def main():
if len(sys.argv) < 3:
print("Usage: ./star_namer.py <module_file> <script_filenames...> --options...")
sys.exit(1)
args = [\
["exclude", "", list],
"Exclude any function found in these module names",
["local", '', bool],
"Don't list any functions outside of file",
["actual", '', bool, False],
"Print the actual module each function is found in",
]
positionals = [\
["module"],
"Module name to search through functions",
["scripts", '', list],
"Python scripts to scan through",
]
#Load the args:
args = easy_parse(args, positionals)
filenames = args.scripts
for name in filenames:
if not os.path.exists(name):
error(name, "does not exist")
mymod = universe.load_mod(args.module)
modname = universe.get_modname(mymod)
modvars = universe.get_members(args.module)
print("Found defined variables in module", modname+':')
out = [['Name:', 'Module:', 'Function:']]
for name, func in modvars.items():
out.append([name, universe.get_modname(inspect.getmodule(func)), func])
auto_cols(out)
print("\n")
for filename in filenames:
functions = universe.scrape_wildcard(filename, modvars)
if len(filenames) > 1:
print('\n')
eprint(filename+':', '\n', v=2)
if functions:
out = dict()
for name, func in functions.items():
mod = universe.get_modname(inspect.getmodule(func))
if mod in args.exclude:
continue
if args.local and mod != modname:
continue
mod = mod if args.actual else modname
out.setdefault(mod, []).append(name)
for mod, funcs in out.items():
header = 'from ' + mod + ' import '
for line in indenter(', '.join(funcs), header=header, wrap=80):
print(line.rstrip(','))
else:
print("<no functions found>")
if __name__ == "__main__":
main()