+ if not hasattr(self, "_debug"): # only set if not already initialized
+ self._debug = False
+ if not hasattr(self, "_singleInstance"): # only set if not already initialized
+ self._singleInstance = True
+
+ # if _singleInstance is TRUE only allow one single instance of app to run.
+ # When user tries to run a second instance of the app, abort startup,
+ # But if user also specifies files to open in command line, send message to running app to open those files
+ if self._singleInstance:
+ # create shared memory temporary file
+ if wx.Platform == '__WXMSW__':
+ tfile = tempfile.TemporaryFile(prefix="ag", suffix="tmp")
+ fno = tfile.fileno()
+ self._sharedMemory = mmap.mmap(fno, 1024, "shared_memory")
+ else:
+ tfile = file(os.path.join(tempfile.gettempdir(), tempfile.gettempprefix() + getpass.getuser() + "AGSharedMemory"), 'w+b')
+ tfile.write("*")
+ tfile.seek(1024)
+ tfile.write(" ")
+ tfile.flush()
+ fno = tfile.fileno()
+ self._sharedMemory = mmap.mmap(fno, 1024)
+
+ self._singleInstanceChecker = wx.SingleInstanceChecker(self.GetAppName() + '-' + wx.GetUserId())
+ if self._singleInstanceChecker.IsAnotherRunning():
+ # have running single instance open file arguments
+ foundArgs = False
+ args = sys.argv[1:]
+ for arg in args:
+ if arg[0] != '/' and arg[0] != '-':
+ foundArgs = True
+ break
+
+ if foundArgs:
+ data = pickle.dumps(args)
+ while 1:
+ self._sharedMemory.seek(0)
+ marker = self._sharedMemory.read_byte()
+ if marker == '\0' or marker == '*': # available buffer
+ self._sharedMemory.seek(0)
+ self._sharedMemory.write_byte('-') # set writing marker
+ self._sharedMemory.write(data) # write files we tried to open to shared memory
+ self._sharedMemory.seek(0)
+ self._sharedMemory.write_byte('+') # set finished writing marker
+ self._sharedMemory.flush()
+ break
+ else:
+ time.sleep(1) # give enough time for buffer to be available
+
+ return False
+ else:
+ self._timer = wx.PyTimer(self.DoBackgroundListenAndLoad)
+ self._timer.Start(250)
+