Advertisement
Guest User

Untitled

a guest
Aug 5th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. import sys
  2. import subprocess
  3. import os
  4. import select
  5. import time
  6.  
  7. class Communicator(object):
  8.     ON_POSIX = 'posix' in sys.builtin_module_names
  9.     WINDOWS = False
  10.     try:
  11.         select.poll()
  12.         from interruptingcow import timeout
  13.     except:
  14.         WINDOWS = True
  15.         pass
  16.  
  17.     def __init__(self, bot_name, command):
  18.         self.name = bot_name
  19.         self.process = subprocess.Popen(command, stdout=subprocess.PIPE,
  20.                                         stdin=subprocess.PIPE, bufsize=1,
  21.                                         close_fds=Communicator.ON_POSIX,
  22.                                         cwd="bots/"+name+"/")
  23.         if not Communicator.WINDOWS:
  24.             self.pollin = select.poll()
  25.             self.pollin.register(self.process.stdin, select.POLLOUT)
  26.  
  27.     def get_response(self):
  28.         if Communicator.WINDOWS:
  29.             while True:
  30.                 message = self.process.stdout.readline()
  31.                 if __debug__:
  32.                     print "got message:"+message
  33.                 return message
  34.  
  35.         if __debug__: print "waiting for response from " + self.name
  36.         try:
  37.             with Communicator.timeout(self.time_limit, exception=RuntimeError):
  38.                 response = self.process.stdout.readline()
  39.                 if __debug__: print "got response from " + \
  40.                                     self.name + " : " + response.rstrip()
  41.                 return response
  42.         except RuntimeError:
  43.             if __debug__: print "gave up on " + self.name
  44.             raise RuntimeError(self.name+
  45.                                " didn't produce a response within one second")
  46.  
  47.     def send_message(self, message):
  48.         if __debug__: print "send message to " + self.name + " : " + message
  49.         if Communicator.WINDOWS:
  50.             self.process.stdin.write(message+"\n")
  51.             self.process.stdin.flush()
  52.             return
  53.         try:
  54.             with Communicator.timeout(self.time_limit, exception=RuntimeError):
  55.                 while not self.pollin.poll(0):
  56.                     time.sleep(0.1)
  57.                 self.process.stdin.write(message+"\n")
  58.                 if __debug__: print "sent message to " + self.name
  59.         except RuntimeError:
  60.             if __debug__: print "gave up on " + self.name
  61.             raise RuntimeError(self.name+
  62.                                " didn't accept a message within one second")
  63.  
  64. def read_players():
  65.     communicators = []
  66.     for dir in os.listdir(r"bots/"):
  67.         try:
  68.             file = open("bots/"+dir+"/command.txt", 'r')
  69.         except IOError:
  70.             continue
  71.         command = file.read()
  72.         file.close()
  73.         communicators.append(Communicator(dir,command))
  74.     return communicators
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement