3 import string, time, popen2, os
4 from random import random
5 from MessageKeeper import MessageKeeper
12 def __init__(self, v):
16 """Displays next stage of the idler"""
20 """Resets the idler to a known intial state"""
24 """Returns True if the idler is considered finished"""
28 """How much we want this idler to be the next one chosen"""
31 class TrainIdler(Idler):
32 def __init__(self, v):
36 def put_shark(self, s, l):
39 elif self.s[l] == 'X':
45 # does the next stage of a dance
47 shark1 = self.idle_state % 18
49 self.put_shark('^', shark1)
51 self.put_shark('^', 18-shark1)
53 shark2 = ((self.idle_state+4) % 36)/2
55 self.put_shark('<', shark2)
57 self.put_shark('<', 18-shark2)
59 shark3 = ((self.idle_state+7) % 54)/3
61 self.put_shark('>', 9-shark3)
63 self.put_shark('>', 9-(18-shark3))
65 train1 = ((self.idle_state%(18*36)))
67 if train1 > train1_start and train1 < train1_start+(10*2):
69 ptr = i+train1-train1_start-5
70 if ptr >= 0 and ptr < 10: self.s[ptr] = '#'
72 train2 = ((self.idle_state%(18*36)))
74 if train2 > train2_start and train2 < train2_start+(10*2):
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)))
81 if train3 > train3_start and train3 < train3_start+(10*2):
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, ''))
88 self.idle_state %= 18*36*54
94 def __init__(self, n=8):
96 self.make_factorials(n)
98 def make_factorials(self, n):
101 for i in range(1,n+1):
102 self.factorial.append(a)
105 def order(self, index):
107 for i in range(0,self.n):
113 a = index/self.factorial[i]
114 index %= self.factorial[i]
118 for i in range(0,self.n):
120 for j in range(res[i],self.n):
125 def __getitem__(self, i):
128 class GrayIdler(Idler):
129 def __init__(self, v, one=None, zero=None, reorder=0):
131 self.size = 1 << self.bits
137 self.reorder = reorder
140 orderings = OrderMaker()
143 output = self.do_next_state()
144 # does the next stage of a dance
146 output = string.replace(output, "0", self.zero)
148 output = string.replace(output, "1", self.one)
153 newoutput += output[orderings[self.reorder][i]]
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.)
179 # Find highest value bit.
182 val, j = val*2L, j+1L
203 self.reorder = int(random()*40319)+1
206 class StringIdler(Idler):
207 def __init__(self, v, text="Hello Cruel World! ",repeat=True):
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 \
219 + string.punctuation \
222 text = string.upper(text)
232 self.mk.update_display()
235 return self.mk.done()
237 class ClockIdler(Idler):
238 def __init__(self, v):
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))
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)
259 text = string.join(stdout.readlines())
262 StringIdler.__init__(self, v, text,repeat=False)
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)
272 text = string.join(stdout.readlines())
275 StringIdler.__init__(self, v, text,repeat=False)
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')
286 text = string.join(f.readlines())
288 StringIdler.__init__(self, v, text,repeat=repeat)