Fix the bug where leaving the program open makes us always fail due to being logged out.
Also, some major refactoring to make the gui code cleaner.
This commit is contained in:
parent
4fdd7ba705
commit
0c2626ee5b
4 changed files with 191 additions and 134 deletions
80
utils.py
80
utils.py
|
@ -1,7 +1,5 @@
|
|||
from ConfigParser import ConfigParser
|
||||
import os
|
||||
import Tkinter as tk
|
||||
import tkSimpleDialog, tkMessageBox
|
||||
import platform
|
||||
|
||||
def get_config_dir(make=False):
|
||||
|
@ -15,91 +13,35 @@ def get_config_dir(make=False):
|
|||
def get_settings(use_gui=True, parent=None):
|
||||
"""
|
||||
Try to read the settings from file into ConfigParser object.
|
||||
If the config file isn't found, initialize it.
|
||||
If the config file isn't found, return None.
|
||||
"""
|
||||
filename = os.path.join(get_config_dir(), 'settings.conf')
|
||||
settings = ConfigParser()
|
||||
|
||||
if os.path.isfile(filename):
|
||||
settings.read(filename)
|
||||
return settings
|
||||
else:
|
||||
try:
|
||||
os.makedirs(get_config_dir())
|
||||
except:
|
||||
pass
|
||||
return None
|
||||
|
||||
settings = update_settings(use_gui, parent)
|
||||
|
||||
return settings
|
||||
|
||||
|
||||
def update_settings(gui=True, parent=None, settings=None):
|
||||
def update_settings(config_func, settings=None):
|
||||
"""
|
||||
This function will initialize settings if it is None, call the passed function
|
||||
with the settings object as a parameter, then write the settings to the config
|
||||
file.
|
||||
"""
|
||||
if settings is None:
|
||||
settings = ConfigParser()
|
||||
settings.add_section('ed_companion')
|
||||
settings.add_section('inara')
|
||||
if gui:
|
||||
dialog = ConfigDialog(parent, settings)
|
||||
else:
|
||||
_settings_prompt_cli(settings)
|
||||
print "To change these settings later, edit " + filename
|
||||
|
||||
config_func(settings)
|
||||
|
||||
with open(os.path.join(get_config_dir(), 'settings.conf'), 'wb') as f:
|
||||
settings.write(f)
|
||||
|
||||
return settings
|
||||
|
||||
|
||||
def _settings_prompt_cli(settings):
|
||||
settings.set('ed_companion', 'username', raw_input("Elite Username (email address): "))
|
||||
settings.set('ed_companion', 'password', raw_input("Elite Password: "))
|
||||
settings.set('inara', 'username', raw_input("Inara Username: "))
|
||||
settings.set('inara', 'password', raw_input("Inara Password: "))
|
||||
|
||||
|
||||
class ConfigDialog(tkSimpleDialog.Dialog):
|
||||
def __init__(self, parent, settings, title="Authentication Data"):
|
||||
self.settings = settings
|
||||
self.entries = []
|
||||
self.data = []
|
||||
tkSimpleDialog.Dialog.__init__(self, parent, title)
|
||||
|
||||
def body(self, parent):
|
||||
i = 0
|
||||
values = []
|
||||
|
||||
for section, value in (('ed_companion', 'username'),
|
||||
('ed_companion', 'password'),
|
||||
('inara', 'username'),
|
||||
('inara', 'password')):
|
||||
if self.settings.has_option(section, value):
|
||||
values.append(self.settings.get(section, value))
|
||||
else:
|
||||
values.append("")
|
||||
|
||||
for field in ("Elite Username (email address):",
|
||||
"Elite Password:",
|
||||
"Inara Username:",
|
||||
"Inara Password:"):
|
||||
label = tk.Label(parent, text=field)
|
||||
label.grid(row=i, column=0, sticky=tk.W)
|
||||
entry = tk.Entry(parent, width=30)
|
||||
entry.insert(0, values[i])
|
||||
entry.grid(row=i, column=1, sticky=tk.E)
|
||||
self.entries.append(entry)
|
||||
i += 1
|
||||
return self.entries[0]
|
||||
|
||||
def validate(self):
|
||||
for entry in self.entries:
|
||||
if entry.get().strip() == "":
|
||||
tkMessageBox.showwarning("Missing Data",
|
||||
"You must provide a value for every field.")
|
||||
return False
|
||||
return True
|
||||
|
||||
def apply(self):
|
||||
self.settings.set('ed_companion', 'username', self.entries[0].get().strip())
|
||||
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())
|
||||
|
|
Reference in a new issue