home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.6)
-
- from __future__ import with_statement
- from contextlib import closing
- import sys
- import socket
- import wx
- import logging
- import threading
- from traceback import print_exc
-
- def log(msg):
- pass
-
-
- class SingleInstanceApp(wx.App):
-
- def __init__(self, appname, *args, **kws):
- appname = '%s-%s' % (appname, wx.GetUserId())
- mgr = InstanceChecker(appname, 'localhost', InstanceChecker.default_port)
- self.instance_checker = mgr
-
- try:
- should_quit = self._check_and_raise_other()
- if should_quit:
- log('instance already running. quitting!')
- self._do_quit()
- except Exception:
- print_exc()
-
- wx.App.__init__(self, *args, **kws)
-
-
- def start_server(self):
- if self.instance_checker.isServerRunning():
- return None
- port_taken = self.instance_checker.startServer()
- if port_taken:
- if self._check_and_raise_other():
- self._do_quit()
-
-
-
-
- def _do_quit(self):
- sys.exit(0)
-
-
- def _check_and_raise_other(self):
- another = self.instance_checker.isAnotherRunning()
- log('another instance running: %r' % another)
- if another:
- sent_raise = self.instance_checker.sendRaisePreviousFrameCommand()
- log('sent raise command: %r' % sent_raise)
-
- if another and sent_raise:
- return True
-
-
- def StopSingleInstanceServer(self):
- return self.instance_checker.stopServer()
-
-
- def SetTopWindow(self, w):
- wx.App.SetTopWindow(self, w)
- self.instance_checker.setFrame(w)
- self.start_server()
-
-
- def MainLoop(self, *args, **kws):
- if not hasattr(self, 'instance_checker'):
- raise AssertionError('must call SetTopWindow on this app first')
- hasattr(self, 'instance_checker')
-
- try:
- wx.App.MainLoop(self, *args, **kws)
- finally:
- self.instance_checker.stopServer()
-
-
-
- SERVER_NOT_STARTED = 0
- SERVER_STARTED = 1
- SERVER_PORT_TAKEN = 2
-
- class ServerThread(threading.Thread):
- backlog = 5
-
- def __init__(self, host, port, function, timeout = None, cv = None):
- threading.Thread.__init__(self, name = self.__class__.__name__ + '-' + host + ':' + str(port))
- self.host = host
- self.port = port
- self.function = function
- self.die = False
- if not timeout:
- pass
- self.timeout = 1
- self.cv = cv
- self.status = SERVER_NOT_STARTED
-
-
- def _notify_status(self, status):
- pass
-
-
- def run(self):
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-
- try:
- s.bind((self.host, self.port))
- except socket.error:
- e = None
- self._notify_status(SERVER_PORT_TAKEN)
- return None
-
- self._notify_status(SERVER_STARTED)
- s.listen(self.backlog)
- s.settimeout(self.timeout)
- while not self.die:
-
- try:
- (client, address) = s.accept()
- if self.die:
- break
-
-
- try:
- log('accepted a single instance ping, sending "ok"')
- client.sendall('ok')
- finally:
- client.close()
-
-
- try:
- self.function()
- except Exception:
- print_exc()
-
- continue
- except socket.timeout:
- e = None
- if self.die:
- log('singleinstance s.accept()')
- break
-
- self.die
-
-
- None<EXCEPTION MATCH>socket.timeout
-
- try:
- s.close()
- except:
- pass
-
-
-
- def isRunning(self):
- return not (self.die)
-
-
- def stop(self):
- self.die = True
-
-
-
- def poke_client_port(host, port):
-
- try:
- log('connecting to (%r, %r)' % (host, port))
-
- try:
- s = _[1]
- s.connect((host, port))
- data = s.recv(512)
- log('received bytes from other digsby process: %r' % (data,))
- if data != 'ok':
- return False
- finally:
- pass
-
- except Exception:
- print_exc()
- return False
-
- return True
-
-
- class InstanceChecker(object):
- default_port = 8791
-
- def __init__(self, name, host, port, frame = None, func = None):
- self.name = name
- self.frame = frame
- self.port = port
- self.host = host
- self.logger = logging.getLogger('')
- if not func:
-
- self.func = lambda : wx.CallAfter(self._InstanceChecker__raiseFrame)
-
- self.s_checker = wx.SingleInstanceChecker(self.name)
-
-
- def startServer(self):
- self.logger.info('Server stuff')
- self._quit_cv = threading.Condition()
- self.server = ServerThread(self.host, self.port, self.func, cv = self._quit_cv)
-
- try:
- self._quit_cv.__enter__()
-
- try:
- self.server.start()
- while self.server.status == SERVER_NOT_STARTED:
- self._quit_cv.wait()
- continue
- self._quit_cv.__exit__
- if self.server.status == SERVER_PORT_TAKEN:
- self.logger.info('instance checker port was already taken, quitting')
- return True
- finally:
- pass
-
- except Exception:
- e = None
- self.logger.error("Couldn't start single instance checker server because: %r", e)
- raise e
-
-
-
- def sendRaisePreviousFrameCommand(self):
- self.logger.info('Poking IPC loopback connection')
- return poke_client_port(self.host, self.port)
-
-
- def isServerRunning(self):
- if hasattr(self, 'server'):
- pass
- return self.server.isRunning()
-
-
- def stopServer(self):
- if hasattr(self, 's_checker'):
- del self.s_checker
-
- if hasattr(self, 'server') and self.server.isRunning():
- self.server.stop()
- return True
- self.logger.warning("Tried to stop a server that wasn't running")
- return False
-
-
- def setFrame(self, f):
- self.frame = f
-
-
- def setFunc(self, func):
- self.func = func
-
-
- def __raiseFrame(self):
- if self.frame is None:
- log('wxApp.SetTopWindow was not called, cannot raise frame')
- return None
- self.frame.Show(True)
- self.frame.Iconize(False)
- self.frame.Raise()
- if hasattr(self.frame, 'ComeBackFromAutoHide'):
- self.frame.ComeBackFromAutoHide()
-
-
-
- def isAnotherRunning(self):
- return self.s_checker.IsAnotherRunning()
-
-
- if __name__ == '__main__':
- app = SingleInstanceApp(0)
- f = wx.Frame(None, -1, 'This app only runs once!')
- f.Show(True)
- app.SetTopWindow(f)
- app.MainLoop()
-
-