-
Notifications
You must be signed in to change notification settings - Fork 3
/
interval_event.py
114 lines (94 loc) · 3.86 KB
/
interval_event.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
from typing import Union, Type, List
from adb_settings import UserRotation
from adb_settings import Airplane
from adb_settings import KeyEvent
from telnet_connector import GsmProfile
from telnet_connector import NetworkDelay
from telnet_connector import NetworkStatus
import util
import config_reader as config
EVENT_TYPES = Union[Type[GsmProfile], Type[NetworkDelay],
Type[NetworkStatus], Type[Airplane],
Type[UserRotation], Type[KeyEvent]]
class IntervalEvent:
'''
`Interval Event` class contains the steps,
for each of which, an interval, an event and an event_type is assigned
e.g.
>>> [event_type: <enum 'NetworkStatus'>, step: 0, interval: 4, event: full]
where `event_type` represents one of the Event Types:
- GsmProfile,
- NetworkDelay,
- NetworkStatus,
- Airplane,
- UserRotation
`step` is a 0 indexed list, against which an `interval` in seconds
and an `event` of event_type is assigned.
'''
def __init__(self, step: int, interval: int,
event: str, event_type: EVENT_TYPES)->None:
self.step = step
self.interval = interval
self.event = event
self.event_type = event_type
def __str__(self) -> str:
# return "[event_type: {}, step: {}, interval: {}, event: {}]".format(
# self.event_type, self.step, self.interval, self.event)
return "{} {} {} {} {}".format(
util.return_current_time_in_logcat_style(),
self.event_type.__name__, self.step, self.interval, self.event)
__repr__ = __str__
def read_interval_event_from_file(file_address: str,
event_type:
EVENT_TYPES)->List[IntervalEvent]:
'''
imports event from file and returns `List[IntervalEvent]`
'''
util.check_file_directory_exists(file_address, True)
lines: list = open(file_address).read().split('\n')
step: int = 0
event_type_name = None
events: List[IntervalEvent] = []
total_event_duration: int = 0
for line in lines:
in_values = line.split(',')
if len(in_values) is 3:
try:
event_value = int(in_values[0])
event_interval = int(in_values[1])
event_type_name = str(in_values[2])
except ValueError:
print('Caught Error! Please check value of: ' + in_values)
if in_values[2] == 'GsmProfile':
i_event = IntervalEvent(
step, event_interval, GsmProfile(event_value).name,
GsmProfile)
elif in_values[2] == 'NetworkDelay':
i_event = IntervalEvent(
step, event_interval, NetworkDelay(event_value).name,
NetworkDelay)
elif in_values[2] == 'NetworkStatus':
i_event = IntervalEvent(
step, event_interval, NetworkStatus(event_value).name,
NetworkStatus)
elif in_values[2] == 'UserRotation':
i_event = IntervalEvent(
step, event_interval, UserRotation(event_value).name,
UserRotation)
else:
raise ValueError(
"incorrect format of Event type: " + in_values[2])
events.append(i_event)
total_event_duration += event_interval
if total_event_duration > config.DURATION:
print("total event interval duration from file (" +
str(total_event_duration)
+ ") can not be larger than " + str(config.DURATION))
raise ValueError()
print("successfully imported from file. Type: " +
event_type_name + "; total duration=" + str(total_event_duration))
return events
def main():
pass
if __name__ == '__main__':
main()