14 assert sys.version_info >= (3, 5)
 
  16 from presence import Presence
 
  18 def handleInterrupt(sig, frame):
 
  20     if os.getpid() == mainPid:
 
  21         logging.info( "Got stop signal." )
 
  23         for presence in presences:
 
  27     client = nio.AsyncClient(config['server']['url'], config['server']['user'])
 
  30         await client.login(config['server']['password'])
 
  33         for presence in presences:
 
  34             presence_tasks.append( asyncio.create_task(presence.run(client)) )
 
  36         done, pending = await asyncio.wait( presence_tasks,
 
  37                                             return_when = asyncio.FIRST_EXCEPTION )
 
  42             except Exception as e:
 
  43                 logging.error( "Error running task: %s" % str(e) )
 
  48 logging.basicConfig(format='[%(asctime)s] %(levelname)s: %(message)s',
 
  50                     datefmt='%m/%d/%Y %H:%M:%S')
 
  53 signal.signal(signal.SIGINT, handleInterrupt)
 
  57 if len(sys.argv) != 2:
 
  58     print("Usage: %s <config json>" % sys.argv[0])
 
  61 with open(sys.argv[1]) as configFile:
 
  62     config = json.load(configFile)
 
  66     for entry in os.listdir(config['paths']['fifodir']):
 
  67         fullpath = "%s/%s" % (config['paths']['fifodir'], entry)
 
  68         if stat.S_ISFIFO(os.stat(fullpath).st_mode):
 
  69             logging.info("Creating presence for %s..." % entry)
 
  70             presences.append( Presence(entry, fullpath) )
 
  71     if len(presences) == 0:
 
  72         logging.error("No fifos could be found!")
 
  76         logging.warning("Main loop exited!")
 
  77         logging.info("Restarting after grace period...")