From 5a6a5b479391d63d8c8e68bb03c8fb0e8fb525b4 Mon Sep 17 00:00:00 2001 From: Mark Tearle Date: Sat, 13 Dec 2014 15:58:29 +0800 Subject: [PATCH] Tidy up Idler initialisation and affinity setting --- VendServer/Idler.py | 47 ++++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/VendServer/Idler.py b/VendServer/Idler.py index 3a8aced..4a32a2e 100755 --- a/VendServer/Idler.py +++ b/VendServer/Idler.py @@ -10,8 +10,12 @@ orderings = None IDLER_TEXT_SPEED=1.8 class Idler: - def __init__(self, v): + def __init__(self, v, affinity=None): self.v = v + if affinity: + self._affinity = affinity + else: + self._affinity = 1 def next(self): """Displays next stage of the idler. Returns time to the next step""" @@ -27,11 +31,12 @@ class Idler: def affinity(self): """How much we want this idler to be the next one chosen""" - return 1 + return self._affinity class GreetingIdler(Idler): def __init__(self, v, secs_to_greeting = None): - self.v = v + affinity = 0 + Idler.__init__(self, v, affinity = affinity) self.secs_to_greeting = secs_to_greeting self.message_displayed = False @@ -52,13 +57,10 @@ class GreetingIdler(Idler): def finished(self): return self.message_displayed - def affinity(self): - return 0 - class TrainIdler(Idler): def __init__(self, v): + Idler.__init__(self, v) self.idle_state = 0 - self.v = v def put_shark(self, s, l): if self.s[l] == ' ': @@ -154,11 +156,11 @@ class OrderMaker: class GrayIdler(Idler): def __init__(self, v, one=None, zero=None, reorder=0): + Idler.__init__(self, v) self.bits = 8 self.size = 1 << self.bits self.i = 0 self.grayCode = 0 - self.v = v self.one = one self.zero = zero self.reorder = reorder @@ -231,8 +233,8 @@ class GrayIdler(Idler): class StringIdler(Idler): - def __init__(self, v, text="Hello Cruel World! ",repeat=True): - self.v = v + def __init__(self, v, text="Hello Cruel World! ",repeat=True, affinity=None): + Idler.__init__(self, v, affinity=affinity) self.mk = MessageKeeper(v) self.text = self.clean_text(text) + " " @@ -263,7 +265,8 @@ class StringIdler(Idler): class ClockIdler(Idler): def __init__(self, v): - self.v = v + affinity = 3 + Idler.__init__(self, v, affinity = affinity) self.last = None def next(self): @@ -274,42 +277,30 @@ class ClockIdler(Idler): self.v.display(" %8.8s " % (output)) self.last = output - def affinity(self): - return 3 - class FortuneIdler(StringIdler): - def __init__(self, v): + def __init__(self, v, affinity = 30): 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 = lines.replace('\n', ' ').replace('\r', '') - StringIdler.__init__(self, v, text,repeat=False) + StringIdler.__init__(self, v, text,repeat=False, affinity=affinity) - def affinity(self): - return 20 class PipeIdler(StringIdler): - def __init__(self, v, command, args): + def __init__(self, v, command, args, affinity = 5): 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 = lines.replace('\n', ' ').replace('\r', '') - StringIdler.__init__(self, v, text,repeat=False) - - def affinity(self): - return 20 + StringIdler.__init__(self, v, text,repeat=False, affinity=affinity) 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 + StringIdler.__init__(self, v, text,repeat=False, affinity=affinity) -- 2.20.1