-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTesting_api.py
More file actions
166 lines (139 loc) · 4.1 KB
/
Copy pathTesting_api.py
File metadata and controls
166 lines (139 loc) · 4.1 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
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
import scheduler
import datetime
import time
import logging
# TODO: move to a debug util
def DebugON(name):
logger = logging.getLogger(name)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
ch.setFormatter(formatter)
logger.addHandler(ch)
DebugON("secheduler")
import code
import readline
import atexit
import os
Channels = {
"Clear Ch.1": { "num": 110, "desc":"5 min event, no audio" },
"Clear Ch.2": { "num": 120, "desc":"5 min event, no audio" },
"Scrambled Ch.1": { "num": 130, "desc":"5 min event, no audio" },
"Scrambled Ch.2": { "num": 140, "desc":"5 min event, no audio" },
}
class ActionList(object):
def __init__(self):
self.my_scheduler = scheduler.Scheduler()
self.action_list = []
self.receipt_list = []
self.timeout = 100
def AddAction(self, action):
self.action_list.append(action)
def setStartTime(self, time):
self.start_time = time
def DoActions(self):
#todo order action by time.
for act in self.action_list:
foo_task = scheduler.Task(act.name ,
self.start_time + datetime.timedelta(seconds=act.time_offset) ,
lambda x: None,
scheduler.RunOnce(act.Run))
self.receipt_list.append( self.my_scheduler.schedule_task(foo_task) )
self.my_scheduler.start()
print "started"
#self.my_scheduler.join()
#print "after join"
def isFinished(self):
return not self.my_scheduler.isAlive()
def __str__(self):
ret = ""
for a in self.action_list:
ret += "%s -%s - %s - %s\n" % (a.name, a.params ,self.start_time + datetime.timedelta(seconds=a.time_offset), a.done)
return ret
Actions = ActionList()
class Action(object):
loop = 0
def __init__(self, name, func, params, offset=None):
self.name = name
self.params = params
self.time_offset = offset
self.func = func
self.done = False
def Run(self):
stat = self.func(*self.params)
self.done = True
self.ret_val = stat
def __unicode__(self):
print self.name
class Anchor(object):
def __init__(self, name):
self.name = name
class AnchorList(list):
pass
Anchors = AnchorList()
class PVR_api(object):
@staticmethod
def StartLive(channel):
print channel
print datetime.datetime.now()
def BookEvent(self, channel, offset=None, time=None):
print channel, offset
print datetime.datetime.now()
PVR = PVR_api()
class Testing_api(object):
current_time = 0
current_test = ""
def Init(self, name):
#set current test name
self.current_test = name
def Do(self, action, params, time):
self.current_time += time
Actions.AddAction( Action(action.__name__, action, params, self.current_time) )
def DoList(self, action, params, time):
self.current_time += time
Actions.AddAction( Action(action.__name__, action, params, self.current_time) )
def DoAt(self, action, params, time):
self.current_time += time
Actions.AddAction( Action(action.__name__, action, params, time) )
def getNextRoundTime(self, num=5):
curr = datetime.datetime.now()
x = curr.minute % int(num)
if x > 0:
ad = num - x
else:
ad = num
return curr.replace(second=0, microsecond=001000) + datetime.timedelta(minutes=ad)
def Run(self):
print "Running:\n %s" % (self.current_test,)
Actions.setStartTime( self.getNextRoundTime() )
print Actions
Actions.DoActions()
TEST = Testing_api()
Test_List = ["Test_01","Test_02","Test_03",]
Command_List = ["stat", "quit"]
def completer(text, state):
l = []
for i in Test_List+Command_List:
if text in i:
l.append(i)
return l[int(state)]
def Test_01():
TEST.Init("Test 01 - Start live based on time")
TEST.Do( PVR.StartLive, ("Clear Ch.1", ), 5 )
TEST.Do( PVR.StartLive, ("Clear Ch.2", ), 15 )
TEST.DoAt( PVR.BookEvent, ("Clear Ch.4", 2) , 20 )
con = code.InteractiveConsole()
readline.parse_and_bind("tab: complete")
readline.set_completer(completer)
Test_01()
TEST.Run()
fini = False
while not Actions.isFinished():
input = con.raw_input()
if input in Test_List:
exec(input)
TEST.Run()
if input == "stat":
print Actions
if input == "quit":
break