home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / singleinstance.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  6.9 KB  |  241 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. from contextlib import closing
  6. import sys
  7. import socket
  8. import wx
  9. import logging
  10. import threading
  11. from traceback import print_exc
  12.  
  13. def log(msg):
  14.     pass
  15.  
  16.  
  17. class SingleInstanceApp(wx.App):
  18.     
  19.     def __init__(self, appname, *args, **kws):
  20.         appname = '%s-%s' % (appname, wx.GetUserId())
  21.         mgr = InstanceChecker(appname, 'localhost', InstanceChecker.default_port)
  22.         self.instance_checker = mgr
  23.         
  24.         try:
  25.             another = mgr.isAnotherRunning()
  26.             log('another instance running: %r' % another)
  27.             if another:
  28.                 sent_raise = mgr.sendRaisePreviousFrameCommand()
  29.                 log('sent raise command: %r' % sent_raise)
  30.             
  31.             if another and sent_raise:
  32.                 log('instance already running. quitting!')
  33.                 sys.exit(0)
  34.         except Exception:
  35.             print_exc()
  36.  
  37.         wx.App.__init__(self, *args, **kws)
  38.  
  39.     
  40.     def StopSingleInstanceServer(self):
  41.         return self.instance_checker.stopServer()
  42.  
  43.     
  44.     def SetTopWindow(self, w):
  45.         wx.App.SetTopWindow(self, w)
  46.         instcheck = self.instance_checker
  47.         instcheck.setFrame(w)
  48.         if not instcheck.isServerRunning():
  49.             instcheck.startServer()
  50.         
  51.  
  52.     
  53.     def MainLoop(self, *args, **kws):
  54.         if not hasattr(self, 'instance_checker'):
  55.             raise AssertionError('must call SetTopWindow on this app first')
  56.         
  57.         
  58.         try:
  59.             wx.App.MainLoop(self, *args, **kws)
  60.         finally:
  61.             self.instance_checker.stopServer()
  62.  
  63.  
  64.  
  65.  
  66. class ServerThread(threading.Thread):
  67.     backlog = 5
  68.     
  69.     def __init__(self, host, port, function, timeout = None):
  70.         threading.Thread.__init__(self, name = self.__class__.__name__ + '-' + host + ':' + str(port))
  71.         self.host = host
  72.         self.port = port
  73.         self.function = function
  74.         self.die = False
  75.         if not timeout:
  76.             pass
  77.         self.timeout = 0.2
  78.  
  79.     
  80.     def run(self):
  81.         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  82.         
  83.         try:
  84.             s.bind((self.host, self.port))
  85.         except socket.error:
  86.             e = None
  87.             if e.args[0] == 10049:
  88.                 pass
  89.             
  90.             raise 
  91.  
  92.         s.listen(self.backlog)
  93.         s.settimeout(self.timeout)
  94.         while not self.die:
  95.             
  96.             try:
  97.                 (client, address) = s.accept()
  98.                 if self.die:
  99.                     break
  100.                 
  101.                 
  102.                 try:
  103.                     log('accepted a single instance ping, sending "ok"')
  104.                     client.sendall('ok')
  105.                 finally:
  106.                     client.close()
  107.  
  108.                 
  109.                 try:
  110.                     self.function()
  111.                 except Exception:
  112.                     print_exc()
  113.  
  114.             continue
  115.             except socket.timeout:
  116.                 e = None
  117.                 if self.die:
  118.                     log('singleinstance s.accept()')
  119.                     break
  120.                 
  121.                 self.die
  122.             
  123.  
  124.             None<EXCEPTION MATCH>socket.timeout
  125.         
  126.         try:
  127.             s.close()
  128.         except:
  129.             pass
  130.  
  131.  
  132.     
  133.     def isRunning(self):
  134.         return not (self.die)
  135.  
  136.     
  137.     def stop(self):
  138.         self.die = True
  139.  
  140.  
  141.  
  142. def poke_client_port(host, port):
  143.     
  144.     try:
  145.         log('connecting to (%r, %r)' % (host, port))
  146.         
  147.         try:
  148.             s = _[2]
  149.             s.connect((host, port))
  150.             data = s.recv(512)
  151.             log('received bytes from other digsby process: %r' % (data,))
  152.             if data != 'ok':
  153.                 return False
  154.         finally:
  155.             pass
  156.  
  157.     except Exception:
  158.         print_exc()
  159.         return False
  160.  
  161.     return True
  162.  
  163.  
  164. class InstanceChecker(object):
  165.     default_port = 8791
  166.     
  167.     def __init__(self, name, host, port, frame = None, func = None):
  168.         self.name = name
  169.         self.frame = frame
  170.         self.port = port
  171.         self.host = host
  172.         self.logger = logging.getLogger('')
  173.         if not func:
  174.             
  175.             self.func = lambda : wx.CallAfter(self._InstanceChecker__raiseFrame)
  176.         
  177.         self.s_checker = wx.SingleInstanceChecker(self.name)
  178.  
  179.     
  180.     def startServer(self):
  181.         self.logger.info('Server stuff')
  182.         self.server = ServerThread(self.host, self.port, self.func)
  183.         
  184.         try:
  185.             self.server.start()
  186.         except Exception:
  187.             e = None
  188.             log.error("Couldn't start single instance checker server because: %r", e)
  189.             raise e
  190.  
  191.  
  192.     
  193.     def sendRaisePreviousFrameCommand(self):
  194.         self.logger.info('Poking IPC loopback connection')
  195.         return poke_client_port(self.host, self.port)
  196.  
  197.     
  198.     def isServerRunning(self):
  199.         if hasattr(self, 'server'):
  200.             pass
  201.         return self.server.isRunning()
  202.  
  203.     
  204.     def stopServer(self):
  205.         if hasattr(self, 's_checker'):
  206.             del self.s_checker
  207.         
  208.         if self.server.isRunning():
  209.             self.server.stop()
  210.             return True
  211.         else:
  212.             self.logger.warning("Tried to stop a server that wasn't running")
  213.             return False
  214.  
  215.     
  216.     def setFrame(self, f):
  217.         self.frame = f
  218.  
  219.     
  220.     def setFunc(self, func):
  221.         self.func = func
  222.  
  223.     
  224.     def __raiseFrame(self):
  225.         self.frame.Show(True)
  226.         self.frame.Iconize(False)
  227.         self.frame.Raise()
  228.  
  229.     
  230.     def isAnotherRunning(self):
  231.         return self.s_checker.IsAnotherRunning()
  232.  
  233.  
  234. if __name__ == '__main__':
  235.     app = SingleInstanceApp(0)
  236.     f = wx.Frame(None, -1, 'This app only runs once!')
  237.     f.Show(True)
  238.     app.SetTopWindow(f)
  239.     app.MainLoop()
  240.  
  241.