forked from jeremydhoon/tfutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.py
101 lines (88 loc) · 2.77 KB
/
publish.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
#!/usr/bin/env python
"""
publish.py -- create a problem set from a Python module.
"""
import inspect
import re
import sys
DOCTEST_MOD = """
if __name__ == "__main__":
import doctest
doctest.testmod()"""
MAIN_FUNCTION = """
if __name__ == "__main__":
import sys
sys.exit(main(sys.argv))"""
class CodeObject(object):
def __init__(self, sIn, sOut, iLine):
self.sIn = sIn
self.sOut = sOut
self.iLine = iLine
@classmethod
def from_module(cls, sKey, mod):
if sKey == mod.__name__:
sOut = "import %s" % sKey
else:
sOut = "import %s as %s" % (mod.__name__, sKey)
return CodeObject("",sOut + "\n", inspect.getsourcelines(mod)[1])
def build_strip_re(sDoc):
return re.compile(r'\s*""".*"""', re.DOTALL)
def strip_function(fxn,sSrc=None):
sDoc = fxn.__doc__
if sDoc is None:
return
reDoc = build_strip_re(sDoc)
if sSrc is None:
sSrc = inspect.getsource(fxn)
listS = reDoc.split(sSrc, 1)
return ('%s\n\t"""%s"""\n\traise NotImplementedError\n'
% (listS[0],sDoc)).expandtabs(4)
def is_builtin(o):
try:
inspect.getfile(o)
except TypeError:
return True
return False
def get_objects(mod):
listCo = []
for sName,o in inspect.getmembers(mod):
if sName == "__doc__" and o is not None:
listCo.append(CodeObject(o,'"""%s"""\n' % o,-2))
continue
if is_builtin(o) or hasattr(o,"ignore"):
continue
if inspect.ismodule(o):
listCo.append(CodeObject.from_module(sName,o))
continue
listSrcLines,iLine = inspect.getsourcelines(o)
sSrc = "".join(listSrcLines)
if inspect.isfunction(o):
if o.__doc__ is not None and not hasattr(o, "is_support"):
sSrcOut = strip_function(o,sSrc)
else:
sSrcOut = sSrc
listCo.append(CodeObject(sSrc, sSrcOut, iLine))
else:
listCo.append(CodeObject(sSrc, sSrc, iLine))
return listCo
def dump_code_objects(listCo, fAddMain):
listCo.sort(lambda a,b: a.iLine - b.iLine)
listS = ["#!/usr/bin/env python\n\n"]
for co in listCo:
listS.append(co.sOut)
listS.append("\n")
if fAddMain:
listS.extend(("\n", MAIN_FUNCTION, "\n"))
return "".join(listS)
def clean_module(mod):
return dump_code_objects(get_objects(mod), True)
def main(argv):
import loadconfig
sConfDir = loadconfig.get_config_dir()
sys.path.append(sConfDir)
dictConf = loadconfig.load_config_file(sConfDir)
mod = __import__(dictConf["workmodule"].rsplit('.',1)[0]) #import dtree
print dump_code_objects(get_objects(mod), True)
if __name__ == "__main__":
import sys
sys.exit(main(sys.argv))