forked from jeremydhoon/tfutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtftask.py
More file actions
105 lines (92 loc) · 2.52 KB
/
tftask.py
File metadata and controls
105 lines (92 loc) · 2.52 KB
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
#!/usr/bin/env python
"""
tftask.py -- programmtic utilities for designating problem set tasks.
"""
import StringIO
import sys
import eventlog
def _capture_stdout(fxn,*args,**kwargs):
fileOldStdout = sys.stdout
sys.stdout = StringIO.StringIO()
try:
oRet = fxn(*args, **kwargs)
sOut = sys.stdout.getvalue()
sys.stdout.close()
finally:
sys.stdout = fileOldStdout
return sOut,oRet
class BaseTask(object):
_IS_TASK = True
def get_name(self):
return None
def get_description(self):
return None
def dependencies(self):
return None
def get_type(self):
return None
def get_priority(self):
return 0
def get_extra_data(self):
return None
def validate(self, oOut):
return None
def task(self):
raise TypeError("%s has no task." % self.__class__.__name__)
def run(self):
fValidation = True
tb = None
sConsole = None
oOut = None
try:
sConsole,oOut = _capture_stdout(self.task)
fValidation = self.validate(oOut)
except KeyboardInterrupt:
raise
except:
import traceback
fValidation = False
tb = traceback.format_exc()
if fValidation is not False:
eventlog.task_success(self.get_name(), 0)
else:
eventlog.task_failure(self.get_name(), 0, tb)
return {"console": sConsole, "result": oOut, "valid": fValidation,
"tb": tb}
class GraphTask(BaseTask):
def get_type(self):
return "graph"
def validate(self, listPair):
for tpl in listPair:
if len(tpl) < 2:
return False
return True
class ChartTask(BaseTask):
def get_type(self):
return "chart"
class MultipleChartTask(BaseTask):
def __init__(self, num_charts):
self.num_charts = num_charts
def get_extra_data(self):
return self.num_charts
def get_type(self):
return "multiple_chart"
def list_tasks(mod):
if isinstance(mod,basestring):
mod = globals()[mod]
listTask = []
for sName in dir(mod):
o = getattr(mod,sName)
if isinstance(o,type) and hasattr(o,"_IS_TASK"):
listTask.append(o())
return listTask
def main(sModName="__main__"):
try:
import json
except:
import simplejson as json
mod = __import__(sModName)
listTask = list_tasks(mod)
for tk in listTask:
print json.dumps(tk.run(), indent=4)
return 0