switch from popen2 to subprocess (fix deprecation warnings)
[uccvend-vendserver.git] / sql-edition / servers / Idler.py
index d11fe90..5b66455 100755 (executable)
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 
-import string, time, popen2, os
+import string, time, os
+from subprocess import Popen, PIPE
 from random import random
 from MessageKeeper import MessageKeeper
 
@@ -13,8 +14,8 @@ class Idler:
                self.v = v
 
        def next(self):
-               """Displays next stage of the idler"""
-               pass
+               """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"""
@@ -28,6 +29,32 @@ class Idler:
                """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):
                self.idle_state = 0
@@ -240,7 +267,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
@@ -253,10 +282,8 @@ class FortuneIdler(StringIdler):
                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()
+                       (lines, unused) = Popen((fortune,), close_fds=True, stdout=PIPE).communicate()
+                       text = string.join(lines)
                StringIdler.__init__(self, v, text,repeat=False)
 
        def affinity(self):
@@ -266,18 +293,17 @@ 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()
+                       (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):
+       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')
@@ -286,4 +312,4 @@ class FileIdler(StringIdler):
                StringIdler.__init__(self, v, text,repeat=repeat)
 
        def affinity(self):
-               return 
+               return self._affinity

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