From 79dcc9a23813b5f480e0aadafe4b391b5443e497 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Sat, 30 Jan 2016 19:00:05 -0500 Subject: [PATCH] Add 'ship name' to stats, and make it possible to tag ships with names. --- actions.py | 1 + gui.py | 66 ++++++++++++++++++++++++++++++++++++++++++------------ utils.py | 22 +++++++++++++----- 3 files changed, 70 insertions(+), 19 deletions(-) diff --git a/actions.py b/actions.py index 58a30e5..0f66bfd 100644 --- a/actions.py +++ b/actions.py @@ -21,4 +21,5 @@ def update_inara(inara_session): 'location': data['lastSystem']['name'], 'credits': data['commander']['credits'], 'assets': assets, + 'ship_id': data['ship']['id'], } diff --git a/gui.py b/gui.py index 93ea1a1..c4d5ee6 100644 --- a/gui.py +++ b/gui.py @@ -6,6 +6,7 @@ import utils class UpdateWindow(object): def __init__(self, parent, settings): + self.ship_id = None self.parent = parent if settings is not None: self.settings = settings @@ -17,18 +18,20 @@ class UpdateWindow(object): self.message = tk.StringVar() self.message.set("Click Update to update!") message_label = tk.Label(self.frame, textvariable=self.message) - message_label.grid(columnspan=2, padx=20, pady=20) + message_label.grid(columnspan=3, padx=20, pady=20) self.info = InfoFrame(self.frame) - self.info.grid(columnspan=2) + self.info.grid(columnspan=3) - self.update_button = tk.Button(self.frame, text="Update", height=2, width=4, - command=self._update_inara) + self.update_button = tk.Button(self.frame, text="Update", command=self._update_inara) self.update_button.grid(row=2, column=0, pady=10) - config_button = tk.Button(self.frame, text="Config", height=1, width=2, - command=self._update_settings) - config_button.grid(row=2, column=1, sticky=tk.E+tk.S, padx=5, pady=5) + self.ship_button = tk.Button(self.frame, text="Name Ship", command=self._update_ship_dialog) + self.ship_button.grid(row=2, column=1, sticky=tk.E+tk.S, padx=5, pady=5) + self.ship_button['state'] = tk.DISABLED + + config_button = tk.Button(self.frame, text="Config", command=self._update_settings) + config_button.grid(row=2, column=2, sticky=tk.E+tk.S, padx=5, pady=5) self._try_login() @@ -37,17 +40,24 @@ class UpdateWindow(object): self.parent.update() try: data = actions.update_inara(self.session) - self.info.update_info(data) - self.message.set("Update successful! (Last update: %s)" % - datetime.now().isoformat(' ')[:16]) except: if second_try: self.message.set("Error updating! Double-check your config,\nor try again later.") + return else: # We don't use self._try_login() here because we don't want to disable the update button in this case. self.session = actions.do_logins(self.settings) self._update_inara(True) + self.ship_id = data['ship_id'] + ship_name = 'Unknown' + if self.settings.has_option('ships', str(self.ship_id)): + ship_name = self.settings.get('ships', str(self.ship_id)) + self.info.update_info(data, ship_name) + self.message.set("Update successful! (Last update: %s)" % + datetime.now().isoformat(' ')[:16]) + self.ship_button['state'] = tk.NORMAL # Once we have a current ship ID, we can use the ship button. + def _update_settings(self): self.settings = utils.update_settings(self._render_config_dialog, self.settings) self._try_login() @@ -63,18 +73,23 @@ class UpdateWindow(object): def _render_config_dialog(self, settings): dialog = ConfigDialog(self.frame, settings) + def _update_ship_dialog(self): + dialog = ShipDialog(self.frame, self.settings, self.ship_id) + class InfoFrame(tk.Frame): def __init__(self, parent, *args, **kwargs): tk.Frame.__init__(self, parent, *args, **kwargs) self.cmdr = self._add_row(0, "CMDR:") - self.system = self._add_row(1, "Location:") - self.credits = self._add_row(2, "Credit Balance:") - self.assets = self._add_row(3, "Current Assets:") + self.ship = self._add_row(1, "Current Ship:") + self.system = self._add_row(2, "Location:") + self.credits = self._add_row(3, "Credit Balance:") + self.assets = self._add_row(4, "Current Assets:") - def update_info(self, data): + def update_info(self, data, ship_name): self.cmdr.set(data['cmdr']) + self.ship.set(ship_name) self.system.set(data['location']) self.credits.set(str(data['credits'])) self.assets.set(str(data['assets'])) @@ -134,3 +149,26 @@ class ConfigDialog(tkSimpleDialog.Dialog): self.settings.set('ed_companion', 'password', self.entries[1].get().strip()) self.settings.set('inara', 'username', self.entries[2].get().strip()) self.settings.set('inara', 'password', self.entries[3].get().strip()) + + + +class ShipDialog(tkSimpleDialog.Dialog): + def __init__(self, parent, settings, ship_id, title="Ship Name"): + self.settings = settings + self.ship_id = ship_id + tkSimpleDialog.Dialog.__init__(self, parent, title) + + def body(self, parent): + ship_name = '' + if self.settings.has_option('ships', str(self.ship_id)): + ship_name = self.settings.get('ships', str(self.ship_id)) + + label = tk.Label(parent, text="Enter your ship's name. This must match what is entered in Inara.") + label.pack() + self.entry = tk.Entry(parent, width=30) + self.entry.insert(0, ship_name) + self.entry.pack() + return self.entry + + def apply(self): + utils.settings_update_ship(self.settings, self.ship_id, self.entry.get().strip()) diff --git a/utils.py b/utils.py index d127479..53ad4c0 100644 --- a/utils.py +++ b/utils.py @@ -28,6 +28,14 @@ def get_settings(use_gui=True, parent=None): pass return None +def settings_update_ship(settings, ship_id, ship_name): + # We include this check for backwards-compatibility. + if not settings.has_section('ships'): + settings.add_section('ships') + + settings.set('ships', str(ship_id), ship_name) + write_settings(settings) + def update_settings(config_func, settings=None): """ This function will initialize settings if it is None, call the passed function @@ -36,12 +44,16 @@ def update_settings(config_func, settings=None): """ if settings is None: settings = ConfigParser() - settings.add_section('ed_companion') - settings.add_section('inara') + + for section in ('ed_companion', 'inara', 'ships'): + if not settings.has_section(section): + settings.add_section(section) config_func(settings) - - with open(os.path.join(get_config_dir(), 'settings.conf'), 'wb') as f: - settings.write(f) + write_settings(settings) return settings + +def write_settings(settings): + with open(os.path.join(get_config_dir(), 'settings.conf'), 'wb') as f: + settings.write(f)