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