switch from popen2 to subprocess (fix deprecation warnings)
[uccvend-vendserver.git] / sql-edition / servers / Idler.py
index ecf3e36..5b66455 100755 (executable)
@@ -1,13 +1,59 @@
 #!/usr/bin/env python
 
-import string
+import string, time, os
+from subprocess import Popen, PIPE
+from random import random
+from MessageKeeper import MessageKeeper
+
+orderings = None
+
+IDLER_TEXT_SPEED=1.8
 
 class Idler:
        def __init__(self, v):
                self.v = v
 
        def next(self):
+               """Displays next stage of the idler. Returns time to the next step"""
+               return 1
+
+       def reset(self):
+               """Resets the idler to a known intial state"""
                pass
+       
+       def finished(self):
+               """Returns True if the idler is considered finished"""
+               return False
+
+       def affinity(self):
+               """How much we want this idler to be the next one chosen"""
+               return 1
+
+class GreetingIdler(Idler):
+       def __init__(self, v, secs_to_greeting = None):
+               self.v = v
+               self.secs_to_greeting = secs_to_greeting
+               self.message_displayed = False
+
+       def next(self):
+               if not self.secs_to_greeting is None:
+                       x = self.secs_to_greeting
+                       self.secs_to_greeting = None
+                       return x
+
+               self.v.display('UCC SNACKS')
+               self.message_displayed = True
+               return 5
+
+       def reset(self):
+               self.message_displayed = False
+               self.secs_to_greeting = None
+
+       def finished(self):
+               return self.message_displayed
+
+       def affinity(self):
+               return 0
 
 class TrainIdler(Idler):
        def __init__(self, v):
@@ -68,8 +114,46 @@ class TrainIdler(Idler):
                self.idle_state += 1
                self.idle_state %= 18*36*54
 
+       def reset(self):
+               self.idle_state = 0
+
+class OrderMaker:
+       def __init__(self, n=8):
+               self.n = n
+               self.make_factorials(n)
+       
+       def make_factorials(self, n):
+               self.factorial = []
+               a = 1
+               for i in range(1,n+1):
+                       self.factorial.append(a)
+                       a *= i
+
+       def order(self, index):
+               used = []
+               for i in range(0,self.n):
+                       used.append(i)
+               i = self.n-1
+               j = 0
+               res = []
+               while i >= 0:
+                       a = index/self.factorial[i]
+                       index %= self.factorial[i]
+                       res.append(a+1)
+                       i -= 1
+                       j += 1
+               for i in range(0,self.n):
+                       tmp = used[res[i]-1]
+                       for j in range(res[i],self.n):
+                               used[j-1] = used[j]
+                       res[i] = tmp
+               return res
+
+       def __getitem__(self, i):
+               return self.order(i)
+
 class GrayIdler(Idler):
-       def __init__(self, v, one=None, zero=None):
+       def __init__(self, v, one=None, zero=None, reorder=0):
                self.bits = 8
                self.size = 1 << self.bits
                self.i = 0
@@ -77,6 +161,10 @@ class GrayIdler(Idler):
                self.v = v
                self.one = one
                self.zero = zero
+               self.reorder = reorder
+               global orderings
+               if not orderings:
+                       orderings = OrderMaker()
 
        def next(self):
                output = self.do_next_state()
@@ -85,6 +173,12 @@ class GrayIdler(Idler):
                        output = string.replace(output, "0", self.zero)
                if self.one:
                        output = string.replace(output, "1", self.one)
+               if self.reorder:
+                       global orderings
+                       newoutput = ""
+                       for i in range(0,8):
+                               newoutput += output[orderings[self.reorder][i]]
+                       output = newoutput
                self.v.display(" %8.8s " % (output))
                self.i = (self.i + 1) % self.size
 
@@ -129,5 +223,93 @@ class GrayIdler(Idler):
 
            return bin
 
+       def reset(self):
+               self.i = 0
+               self.grayCode = 0
+               if self.reorder:
+                       self.reorder = int(random()*40319)+1
+
+
+class StringIdler(Idler):
+       def __init__(self, v, text="Hello Cruel World!  ",repeat=True):
+               self.v = v
+               self.mk = MessageKeeper(v)
+               self.text = self.clean_text(text) + "          "
+               
+               msg = [("",False, None),(self.text, repeat, IDLER_TEXT_SPEED)]
+               self.mk.set_messages(msg)
+
+       def clean_text(self, text):
+               # nothing like a bit of good clean text :)
+               valid = string.digits \
+                       + string.letters \
+                       + string.punctuation \
+                       + " "
+               # uppercase it
+               text = string.upper(text)
+               clean = ""
+               for char in text:
+                       if char in valid:
+                               clean = clean + char
+                       else:
+                               clean = clean + " "
+               return clean
+
+       def next(self):
+               self.mk.update_display()
+
+       def finished(self):     
+               return self.mk.done()
+
+class ClockIdler(Idler):
+       def __init__(self, v):
+               self.v = v
+               self.last = None
+
+       def next(self):
+               colonchar = ':'
+               if int(time.time()*2) & 1: colonchar = ' '
+               output = time.strftime("%%H%c%%M%c%%S"%(colonchar,colonchar))
+               if output != self.last:
+                       self.v.display(" %8.8s " % (output))
+                       self.last = output
+
+       def affinity(self):
+               return 3 
+
+class FortuneIdler(StringIdler):
+       def __init__(self, v):
+               fortune = "/usr/games/fortune"
+               text = "I broke my wookie...."
+               if os.access(fortune,os.F_OK|os.X_OK):
+                       (lines, unused) = Popen((fortune,), close_fds=True, stdout=PIPE).communicate()
+                       text = string.join(lines)
+               StringIdler.__init__(self, v, text,repeat=False)
+
+       def affinity(self):
+               return 20
+
+class PipeIdler(StringIdler):
+       def __init__(self, v, command, args):
+               text = "I ate my cookie...."
+               if os.access(command,os.F_OK|os.X_OK):
+                       (lines, unused) = Popen([command,] + args.split(), close_fds=True, stdout=PIPE).communicate()
+                       text = string.join(lines)
+               StringIdler.__init__(self, v, text,repeat=False)
+
+       def affinity(self):
+               return 20
+
+class FileIdler(StringIdler):
+       def __init__(self, v, thefile=None, repeat=False, affinity=8):
+               text = "I broke my wookie...."
+               self._affinity = affinity
 
+               if file and os.access(thefile,os.F_OK|os.R_OK):
+                       f = file(thefile,'r')
+                       text = string.join(f.readlines())
+                       f.close()
+               StringIdler.__init__(self, v, text,repeat=repeat)
 
+       def affinity(self):
+               return self._affinity

UCC git Repository :: git.ucc.asn.au