22285ded9a56d20c32976720efbb4ec8589725e7
1 #!/usr/bin/env python
3 import string, time, popen2, os
4 from random import random
5 from MessageKeeper import MessageKeeper
7 orderings = None
9 IDLER_TEXT_SPEED=1.8
11 class Idler:
12         def __init__(self, v):
13                 self.v = v
15         def next(self):
16                 """Displays next stage of the idler"""
17                 pass
19         def reset(self):
20                 """Resets the idler to a known intial state"""
21                 pass
23         def finished(self):
24                 """Returns True if the idler is considered finished"""
25                 return False
27         def affinity(self):
28                 """How much we want this idler to be the next one chosen"""
29                 return 1
31 class TrainIdler(Idler):
32         def __init__(self, v):
33                 self.idle_state = 0
34                 self.v = v
36         def put_shark(self, s, l):
37                 if self.s[l] == ' ':
38                         self.s[l] = s
39                 elif self.s[l] == 'X':
40                         self.s[l] = '*'
41                 else:
42                         self.s[l] = 'X'
44         def next(self):
45                 # does the next stage of a dance
46                 self.s = [' ']*10
47                 shark1 = self.idle_state % 18
48                 if shark1 < 9:
49                         self.put_shark('^', shark1)
50                 else:
51                         self.put_shark('^', 18-shark1)
53                 shark2 = ((self.idle_state+4) % 36)/2
54                 if shark2 < 9:
55                         self.put_shark('<', shark2)
56                 else:
57                         self.put_shark('<', 18-shark2)
59                 shark3 = ((self.idle_state+7) % 54)/3
60                 if shark3 < 9:
61                         self.put_shark('>', 9-shark3)
62                 else:
63                         self.put_shark('>', 9-(18-shark3))
65                 train1 = ((self.idle_state%(18*36)))
66                 train1_start = 122
67                 if train1 > train1_start and train1 < train1_start+(10*2):
68                         for i in range(5):
69                                 ptr = i+train1-train1_start-5
70                                 if ptr >= 0 and ptr < 10: self.s[ptr] = '#'
72                 train2 = ((self.idle_state%(18*36)))
73                 train2_start = 400
74                 if train2 > train2_start and train2 < train2_start+(10*2):
75                         for i in range(5):
76                                 ptr = i+train2-train2_start-5
77                                 if ptr >= 0 and ptr < 10: self.s[9-ptr] = '#'
79                 train3 = ((self.idle_state%(18*36)))
80                 train3_start = 230
81                 if train3 > train3_start and train3 < train3_start+(10*2):
82                         for i in range(10):
83                                 ptr = i+train3-train3_start-10
84                                 if ptr >= 0 and ptr < 10: self.s[ptr] = '-'
86                 self.v.display(string.join(self.s, ''))
87                 self.idle_state += 1
88                 self.idle_state %= 18*36*54
90         def reset(self):
91                 self.idle_state = 0
93 class OrderMaker:
94         def __init__(self, n=8):
95                 self.n = n
96                 self.make_factorials(n)
98         def make_factorials(self, n):
99                 self.factorial = []
100                 a = 1
101                 for i in range(1,n+1):
102                         self.factorial.append(a)
103                         a *= i
105         def order(self, index):
106                 used = []
107                 for i in range(0,self.n):
108                         used.append(i)
109                 i = self.n-1
110                 j = 0
111                 res = []
112                 while i >= 0:
113                         a = index/self.factorial[i]
114                         index %= self.factorial[i]
115                         res.append(a+1)
116                         i -= 1
117                         j += 1
118                 for i in range(0,self.n):
119                         tmp = used[res[i]-1]
120                         for j in range(res[i],self.n):
121                                 used[j-1] = used[j]
122                         res[i] = tmp
123                 return res
125         def __getitem__(self, i):
126                 return self.order(i)
128 class GrayIdler(Idler):
129         def __init__(self, v, one=None, zero=None, reorder=0):
130                 self.bits = 8
131                 self.size = 1 << self.bits
132                 self.i = 0
133                 self.grayCode = 0
134                 self.v = v
135                 self.one = one
136                 self.zero = zero
137                 self.reorder = reorder
138                 global orderings
139                 if not orderings:
140                         orderings = OrderMaker()
142         def next(self):
143                 output = self.do_next_state()
144                 # does the next stage of a dance
145                 if self.zero:
146                         output = string.replace(output, "0", self.zero)
147                 if self.one:
148                         output = string.replace(output, "1", self.one)
149                 if self.reorder:
150                         global orderings
151                         newoutput = ""
152                         for i in range(0,8):
153                                 newoutput += output[orderings[self.reorder][i]]
154                         output = newoutput
155                 self.v.display(" %8.8s " % (output))
156                 self.i = (self.i + 1) % self.size
158         def do_next_state(self):
159                 self.grayCode = self.i ^ (self.i >> 1)
160                 output = self.dec2bin(self.grayCode)
162                 return "0"*(self.bits-len(output))+output
165         def dec2bin(self,num):
166             """Convert long/integer number to binary string.
168             E.g. dec2bin(12) ==> '1100'.
170             from http://starship.python.net/~gherman/playground/decbingray/decbingray.py"""
172             assert num >= 0, "Decimal number must be >= 0!"
174             # Gracefully handle degenerate case.
175             # (Not really needed, but anyway.)
176             if num == 0:
177                 return '0'
179             # Find highest value bit.
180             val, j = 1L, 1L
181             while val < num:
182                 val, j = val*2L, j+1L
184             # Convert.
185             bin = ''
186             i = j - 1
187             while i + 1L:
188                 k = pow(2L, i)
189                 if num >= k:
190                     bin = bin + '1'
191                     num = num - k
192                 else:
193                     if len(bin) > 0:
194                         bin = bin + '0'
195                 i = i - 1L
197             return bin
199         def reset(self):
200                 self.i = 0
201                 self.grayCode = 0
202                 if self.reorder:
203                         self.reorder = int(random()*40319)+1
206 class StringIdler(Idler):
207         def __init__(self, v, text="Hello Cruel World!  ",repeat=True):
208                 self.v = v
209                 self.mk = MessageKeeper(v)
210                 self.text = self.clean_text(text) + "          "
212                 msg = [("",False, None),(self.text, repeat, IDLER_TEXT_SPEED)]
213                 self.mk.set_messages(msg)
215         def clean_text(self, text):
216                 # nothing like a bit of good clean text :)
217                 valid = string.digits \
218                         + string.letters \
219                         + string.punctuation \
220                         + " "
221                 # uppercase it
222                 text = string.upper(text)
223                 clean = ""
224                 for char in text:
225                         if char in valid:
226                                 clean = clean + char
227                         else:
228                                 clean = clean + " "
229                 return clean
231         def next(self):
232                 self.mk.update_display()
234         def finished(self):
235                 return self.mk.done()
237 class ClockIdler(Idler):
238         def __init__(self, v):
239                 self.v = v
240                 self.last = None
242         def next(self):
243                 colonchar = ':'
244                 if int(time.time()*2) & 1: colonchar = ' '
245                 output = time.strftime("%%H%c%%M%c%%S"%(colonchar,colonchar))
246                 if output != self.last:
247                         self.v.display(" %8.8s " % (output))
248                         self.last = output
250         def affinity(self):
251                 return 3
253 class FortuneIdler(StringIdler):
254         def __init__(self, v):
255                 fortune = "/usr/games/fortune"
256                 text = "I broke my wookie...."
257                 if os.access(fortune,os.F_OK|os.X_OK):
258                         (stdout, stdin) = popen2.popen2(fortune)
260                         stdout.close()
261                         stdin.close()
262                 StringIdler.__init__(self, v, text,repeat=False)
264         def affinity(self):
265                 return 20
267 class PipeIdler(StringIdler):
268         def __init__(self, v, command, args):
269                 text = "I ate my cookie...."
270                 if os.access(command,os.F_OK|os.X_OK):
271                         (stdout, stdin) = popen2.popen2(command+' '+args)
273                         stdout.close()
274                         stdin.close()
275                 StringIdler.__init__(self, v, text,repeat=False)
277         def affinity(self):
278                 return 20
280 class FileIdler(StringIdler):
281         def __init__(self, v, thefile=None, repeat=False):
282                 text = "I broke my wookie...."
284                 if file and os.access(thefile,os.F_OK|os.R_OK):
285                         f = file(thefile,'r')