X-Git-Url: https://git.ucc.asn.au/?p=uccvend-vendserver.git;a=blobdiff_plain;f=sql-edition%2Fservers%2FIdler.py;h=e56f8d755d0bab59c251c2a05231f3fd72a1920b;hp=4efb56fe5b45226e96685e64eaaa12bddcf979c7;hb=5769169657fae81bc68c88c9fa19313acbb9e83f;hpb=c945258a9f4c89bf8f19108ed0ee2f477d5a9493 diff --git a/sql-edition/servers/Idler.py b/sql-edition/servers/Idler.py index 4efb56f..e56f8d7 100755 --- a/sql-edition/servers/Idler.py +++ b/sql-edition/servers/Idler.py @@ -1,16 +1,32 @@ #!/usr/bin/env python -import string +import string, time, popen2, os +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""" pass 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 TrainIdler(Idler): def __init__(self, v): @@ -74,8 +90,34 @@ class TrainIdler(Idler): def reset(self): self.idle_state = 0 +class OrderMaker: + def __init__(self, n=8): + self.n = n + self.a = [] + self.u = [] + self.s = [] + for x in range(0,n): + self.u.append(False) + self.go() + + def go(self): + from copy import deepcopy + if len(self.s) == self.n: + self.a.append(deepcopy(self.s)) + else: + for x in range(0,self.n): + if self.u[x]: continue + self.s.append(x) + self.u[x] = True + self.go() + self.u[x] = False + self.s.pop() + + def order(self): + return self.a + 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 @@ -83,6 +125,10 @@ class GrayIdler(Idler): self.v = v self.one = one self.zero = zero + self.reorder = reorder + global orderings + if not orderings: + orderings = OrderMaker().order() def next(self): output = self.do_next_state() @@ -91,6 +137,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 @@ -138,3 +190,91 @@ class GrayIdler(Idler): 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): + output = time.strftime("%H:%M:%S") + 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): + (stdout, stdin) = popen2.popen2(fortune) + text = string.join(stdout.readlines()) + stdout.close() + stdin.close() + 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): + (stdout, stdin) = popen2.popen2(command+' '+args) + text = string.join(stdout.readlines()) + stdout.close() + stdin.close() + StringIdler.__init__(self, v, text,repeat=False) + + def affinity(self): + return 20 + +class FileIdler(StringIdler): + def __init__(self, v, thefile=None, repeat=False): + text = "I broke my wookie...." + + 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 8