X-Git-Url: https://git.ucc.asn.au/?p=zanchey%2Fdispense2.git;a=blobdiff_plain;f=sql-edition%2Fservers%2FIdler.py;h=22285ded9a56d20c32976720efbb4ec8589725e7;hp=eedfe7335e2227982bc33276ff6ef03cd75e88b9;hb=b5c641ee2449ae97bb789cdd29e3b450973ae8bd;hpb=92192e8cce7e4aa9de005ef7e32b13eca1776a79 diff --git a/sql-edition/servers/Idler.py b/sql-edition/servers/Idler.py index eedfe73..22285de 100755 --- a/sql-edition/servers/Idler.py +++ b/sql-edition/servers/Idler.py @@ -6,20 +6,26 @@ 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): @@ -87,28 +93,37 @@ class TrainIdler(Idler): 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 + 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, reorder=0): @@ -122,7 +137,7 @@ class GrayIdler(Idler): self.reorder = reorder global orderings if not orderings: - orderings = OrderMaker().order() + orderings = OrderMaker() def next(self): output = self.do_next_state() @@ -194,7 +209,7 @@ class StringIdler(Idler): self.mk = MessageKeeper(v) self.text = self.clean_text(text) + " " - msg = [("",False, None),(self.text, repeat, 0.8)] + msg = [("",False, None),(self.text, repeat, IDLER_TEXT_SPEED)] self.mk.set_messages(msg) def clean_text(self, text): @@ -225,7 +240,9 @@ class ClockIdler(Idler): self.last = None def next(self): - output = time.strftime("%H:%M:%S") + 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 @@ -246,3 +263,29 @@ class FortuneIdler(StringIdler): 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