some minor speedups using caches and more selective updating
[enigma2.git] / lib / python / Components / Converter / EventTime.py
1 from Converter import Converter
2 from Poll import Poll
3 from time import time
4
5 class EventTime(Poll, Converter, object):
6         STARTTIME = 0
7         ENDTIME = 1
8         REMAINING = 2
9         PROGRESS = 3
10         DURATION = 4
11
12         def __init__(self, type):
13                 Converter.__init__(self, type)
14                 Poll.__init__(self)
15                 if type == "EndTime":
16                         self.type = self.ENDTIME
17                 elif type == "Remaining":
18                         self.type = self.REMAINING
19                         self.poll_interval = 60*1000
20                         self.poll_enabled = True
21                 elif type == "StartTime":
22                         self.type = self.STARTTIME
23                 elif type == "Duration":
24                         self.type = self.DURATION
25                 elif type == "Progress":
26                         self.type = self.PROGRESS
27                         self.poll_interval = 30*1000
28                         self.poll_enabled = True
29                 else:
30                         raise str("'%s' is not <StartTime|EndTime|Remaining|Duration> for EventTime converter" % type)
31
32         def getTime(self):
33                 if self.cache is None or self.cache[0] is None:
34                         self.cache = (self.__getTime(), self.cache and self.cache[1])
35                 return self.cache[0]
36         
37         def __getTime(self):    
38                 assert self.type != self.PROGRESS
39
40                 event = self.source.event
41                 if event is None:
42                         return None
43                         
44                 if self.type == self.STARTTIME:
45                         return event.getBeginTime()
46                 elif self.type == self.ENDTIME:
47                         return event.getBeginTime() + event.getDuration()
48                 elif self.type == self.DURATION:
49                         return event.getDuration()
50                 elif self.type == self.REMAINING:
51                         now = int(time())
52                         start_time = event.getBeginTime()
53                         duration = event.getDuration()
54                         end_time = start_time + duration
55                         if start_time <= now <= end_time:
56                                 return (duration, end_time - now)
57                         else:
58                                 return (duration, None)
59
60         def getValue(self):
61                 if self.cache is None or self.cache[1] is None:
62                         self.cache = (self.cache and self.cache[0], self.__getValue())
63                 return self.cache[1]
64
65         def __getValue(self):
66                 assert self.type == self.PROGRESS
67
68                 event = self.source.event
69                 if event is None:
70                         return None
71
72                 now = int(time())
73                 start_time = event.getBeginTime()
74                 duration = event.getDuration()
75                 if start_time <= now <= (start_time + duration) and duration > 0:
76                         return (now - start_time) * 1000 / duration
77                 else:
78                         return None
79
80         time = property(getTime)
81         value = property(getValue)
82         range = 1000