update ov debian/overrides
[grml-etc.git] / etc / skel / .wmi / szs2.1 / clock.py
1 #!/usr/bin/python
2
3 # module that shows a clock
4 #
5 # TODO
6 #
7 # * finish support for 'workbeat'
8
9
10 from time import strftime,time,localtime,mktime
11 import szstoolbox
12
13 ready = False
14
15 def init():
16         """ the init thingy """
17         
18         global frmt, ready, acc, lst, bar
19         global wbdir, wbshow, wbstart, wbstop, wblabel, ew, sw, wboutside
20         bar = None
21         msg = szstoolbox.MSG()
22         cfg = szstoolbox.CFG('clock')
23         workbeat = cfg.read('workbeat')
24         if len(workbeat) > 0:
25                 wbdir = cfg.read('wbdirection')
26                 wboutside = cfg.read('wboutside')
27                 if wboutside not in ('yes','no'):
28                         wboutside = 'yes'
29                         msg.debug('illegal value for wboutside, using "yes"',2)
30                 wbshow = cfg.read('wbshow')
31                 if wbshow in ('bar','meter','both'):
32                         wblabel = cfg.read('wblabel')
33                 if wbshow in ('text','both'):
34                         ew = int(cfg.read('ew')) # EndWith
35                         sw = int(cfg.read('sw')) # StartWith            
36         else:
37                 wbdir == None
38                 wbshow == None
39         acc = int(szstoolbox.CFG('global').read('accuracy'))
40         frmt = str(cfg.read('format')).strip()
41         lst = frmt.split('%N')
42         ready = True
43
44         if wbdir not in ('up','down',None):
45                 wbdir = 'up'
46                 msg.debug('illegal workbeat direction, using "up"',2)
47         
48         if wbshow not in ('bar','meter','text','both',None):
49                 wbshow = 'both'
50                 msg.debug('illegal workbeat show option, using "both"',2)
51
52         if wbshow and wbdir:
53                 workbeat = workbeat.split(',')
54                 wbstart = workbeat[0].split(':')
55                 wbstop = workbeat[1].split(':')
56 def main():
57         """ the main function """
58
59         global frmt,workbeat,bar
60         if not ready:
61                 init()
62                 return ['init']
63
64         if len(lst) > 1:
65                 y = '@'+str(round((((time()+3600)%86400)/86.4),acc))
66                 y = y[:y.find('.')+acc+1]
67                 while len(y[y.find('.')+1:len(y)]) < acc: y=y+'0'
68                 frmt = lst[0]+y+lst[1]
69         
70         if wbshow <> None and wbdir <> None:
71                 tpl1 = list(localtime())
72                 tpl2 = list(localtime())
73                 tpl1[3] = int(wbstart[0])
74                 tpl1[4] = int(wbstart[1])
75                 tpl2[3] = int(wbstop[0])
76                 tpl2[4] = int(wbstop[1])
77                 start = mktime(tuple(tpl1))
78                 stop = mktime(tuple(tpl2))
79                 spanne = stop-start
80                 jetzt = time() - start
81                 
82 #               ### DEBUGSECTION ###
83 #               print '--------D-E-B-U-G--------'
84 #               print 'Start: '+str(start)#starttime
85 #               print 'Stop: '+str(stop) #endtime
86 #               print 'Spanne: '+str(spanne) #stop-start
87 #               print 'Jetzt: '+str(jetzt) #now
88 #               print 'wbshow: '+wbshow #showtype
89 #               print 'wbdir: '+wbdir #direction
90 #               print str(int(round(jetzt*100/spanne)))+'%'+wblabel #bar
91 #               print str(100-int(round(jetzt*100/spanne)))+'%'+wblabel #100-bar
92 #               print '--------D-E-B-U-G--------'
93 #               ### END OF DEBUG ###
94
95                 wb = jetzt * 100 / spanne
96                 wb = int(round(wb))
97                 if wboutside == 'yes':
98                         if wb > ew:
99                                 wb = ew
100                         elif wb < sw:
101                                 wb = sw
102                 else:
103                         if wb > ew or wb < sw:
104                                 wb = None
105
106                 tmp = frmt
107
108                 if wb <> None:
109                         if wbdir == 'down':
110                                 wb = 100 - wb           
111                         if wbshow in ('bar','meter'):
112                                 wbthing = None
113                                 if wb >= 0:
114                                         bar = str(wb)+'%'+wblabel
115                                 else:
116                                         bar = '0%'+wblabel
117                         elif wbshow == 'text':
118                                 tmp = frmt+' ('+str(wb)+'%%)'
119                                 bar = None
120                         else:
121                                 tmp = frmt+' ('+str(wb)+'%%)'
122                                 if wb >= 0:
123                                         bar = str(wb)+'%'+wblabel
124                                 else:
125                                         bar = '0%'+wblabel
126                 else:
127                         bar = None
128                         
129         if bar <> None:
130                 return [strftime(tmp),bar]
131         else:
132                 return [strftime(tmp)]
133
134 def debug():
135         print "============================="
136         print "frmt type is "+str(type(frmt))+" (should be 'str')"
137         print "bar type is "+str(type(bar))+" (should be 'str' or 'NoneType')"
138         print "outputted text: '"+strftime(frmt)+"'"
139         print "outputted bar: '"+str(bar)+"'"
140         print "Netbeat should be: @"+str(round((((time()+3600)%86400)/86.4),acc))
141         print "Actual time should be: "+strftime("%X")
142         print "Actual date should be: "+strftime("%x")
143         print "============================="
144         
145 if __name__ == '__main__':
146         """ only to debug... """
147
148         from sys import argv
149         from time import sleep
150
151         if "loop" in argv:
152                 while 1 == 1:
153                         main()
154                         debug()
155                         sleep(1)
156         else:
157                 main()
158                 debug()
159