From 7f2cf3a28636938a5efebbd40333759f220a351d Mon Sep 17 00:00:00 2001 From: Jonathan Harris Date: Tue, 6 Sep 2016 12:26:59 +0100 Subject: [PATCH] Rationalise startup sequence --- EDMarketConnector.py | 37 +++++++++++++++++-------------------- L10n/en.template | 2 +- eddn.py | 3 ++- monitor.py | 16 +++++++++------- prefs.py | 3 --- 5 files changed, 29 insertions(+), 32 deletions(-) diff --git a/EDMarketConnector.py b/EDMarketConnector.py index b647addf..231d121a 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -122,10 +122,7 @@ class AppWindow: self.theme_button.grid(row=row, columnspan=2, sticky=tk.NSEW) theme.register_alternate((self.button, self.theme_button), {'row':row, 'columnspan':2, 'sticky':tk.NSEW}) self.status.grid(columnspan=2, sticky=tk.EW) - theme.button_bind(self.theme_button, self.getandsend) - self.w.bind('', self.getandsend) - self.w.bind('', self.getandsend) for child in frame.winfo_children(): child.grid_configure(padx=5, pady=(platform=='win32' and 1 or 3)) @@ -240,21 +237,16 @@ class AppWindow: theme.register_highlight(self.station) theme.apply(self.w) - # Special handling for overrideredict - self.w.bind("", self.onmap) + self.w.bind("", self.onmap) # Special handling for overrideredict + self.w.bind('', self.getandsend) + self.w.bind('', self.getandsend) + self.w.bind_all('<>', self.getandsend) # Hotkey monitoring + self.w.bind_all('<>', self.journal_event) # Journal monitoring + self.w.bind_all('<>', self.onexit) # Updater # Load updater after UI creation (for WinSparkle) import update self.updater = update.Updater(self.w) - self.w.bind_all('<>', self.onexit) # user-generated - - # Install hotkey monitoring - self.w.bind_all('<>', self.getandsend) # user-generated - hotkeymgr.register(self.w, config.getint('hotkey_code'), config.getint('hotkey_mods')) - - # Install log monitoring - self.w.bind_all('<>', self.journal_event) # user-generated - monitor.start(self.w) # First run if not config.get('username') or not config.get('password'): @@ -309,15 +301,20 @@ class AppWindow: if __debug__: print_exc() self.status['text'] = unicode(e) - # Try to obtain exclusive lock on journal cache, even if we don't need it yet - try: - eddn.load() - except Exception as e: - self.status['text'] = unicode(e) - if not getattr(sys, 'frozen', False): self.updater.checkForUpdates() # Sparkle / WinSparkle does this automatically for packaged apps + # Try to obtain exclusive lock on journal cache, even if we don't need it yet + if not eddn.load(): + self.status['text'] = 'Error: Is another copy of this app already running?' # Shouldn't happen - don't bother localizing + + # (Re-)install hotkey monitoring + hotkeymgr.register(self.w, config.getint('hotkey_code'), config.getint('hotkey_mods')) + + # (Re-)install log monitoring + if not monitor.start(self.w): + self.status['text'] = 'Error: Check %s' % _('E:D journal file location') # Location of the new Journal file in E:D 2.2 + self.cooldown() # callback after verification code diff --git a/L10n/en.template b/L10n/en.template index 5a60690d..e725146a 100644 --- a/L10n/en.template +++ b/L10n/en.template @@ -109,7 +109,7 @@ /* Empire rank. [stats.py] */ "Duke" = "Duke"; -/* Location of the new Journal file in E:D 2.2. [prefs.py] */ +/* Location of the new Journal file in E:D 2.2. [EDMarketConnector.py] */ "E:D journal file location" = "E:D journal file location"; /* Empire rank. [stats.py] */ diff --git a/eddn.py b/eddn.py index 21d29caa..41ec1fda 100644 --- a/eddn.py +++ b/eddn.py @@ -54,7 +54,8 @@ class _EDDN: if self.replayfile: self.replayfile.close() self.replayfile = None - raise Exception("Error: Is another copy of this app already running?") # Shouldn't happen - don't bother localizing + return False + return True def flush(self): self.replayfile.seek(0, SEEK_SET) diff --git a/monitor.py b/monitor.py index bb62a33f..4b106706 100644 --- a/monitor.py +++ b/monitor.py @@ -96,6 +96,15 @@ class EDLogs(FileSystemEventHandler): self.stop() self.currentdir = logdir + # Latest pre-existing logfile - e.g. if E:D is already running. Assumes logs sort alphabetically. + # Do this before setting up the observer in case the journal directory has gone away + try: + logfiles = sorted([x for x in listdir(self.currentdir) if x.startswith('Journal.')]) + self.logfile = logfiles and join(self.currentdir, logfiles[-1]) or None + except: + self.logfile = None + return False + # Set up a watchog observer. This is low overhead so is left running irrespective of whether monitoring is desired. # File system events are unreliable/non-existent over network drives on Linux. # We can't easily tell whether a path points to a network drive, so assume @@ -110,13 +119,6 @@ class EDLogs(FileSystemEventHandler): if not self.observed and not polling: self.observed = self.observer.schedule(self, self.currentdir) - # Latest pre-existing logfile - e.g. if E:D is already running. Assumes logs sort alphabetically. - try: - logfiles = sorted([x for x in listdir(self.currentdir) if x.startswith('Journal.')]) - self.logfile = logfiles and join(self.currentdir, logfiles[-1]) or None - except: - self.logfile = None - if __debug__: print '%s "%s"' % (polling and 'Polling' or 'Monitoring', self.currentdir) print 'Start logfile "%s"' % self.logfile diff --git a/prefs.py b/prefs.py index 6beef4f8..75c83789 100644 --- a/prefs.py +++ b/prefs.py @@ -489,9 +489,6 @@ class PreferencesDialog(tk.Toplevel): self.callback() def _destroy(self): - # Re-enable hotkey and log monitoring before exit - hotkeymgr.register(self.parent, config.getint('hotkey_code'), config.getint('hotkey_mods')) - monitor.start(self.parent) self.parent.wm_attributes('-topmost', config.getint('always_ontop') and 1 or 0) self.destroy()