2015-10-26 00:51:48 +00:00
|
|
|
from ConfigParser import ConfigParser
|
|
|
|
import os
|
2016-01-21 18:45:45 +00:00
|
|
|
import platform
|
2015-10-26 00:51:48 +00:00
|
|
|
|
2016-01-23 00:32:46 +00:00
|
|
|
def get_config_dir(make=False):
|
2016-01-23 08:36:27 +00:00
|
|
|
if platform.system() == 'Windows':
|
2016-01-23 00:32:46 +00:00
|
|
|
config_suffix = os.path.join('AppData', 'Local', 'ed_tools')
|
|
|
|
else:
|
|
|
|
config_suffix = '.ed_tools'
|
|
|
|
|
|
|
|
return os.path.join(os.path.expanduser('~'), config_suffix)
|
2016-01-21 18:01:54 +00:00
|
|
|
|
2016-01-24 03:14:41 +00:00
|
|
|
def get_settings(use_gui=True, parent=None):
|
2015-10-26 00:51:48 +00:00
|
|
|
"""
|
|
|
|
Try to read the settings from file into ConfigParser object.
|
2016-01-28 16:52:09 +00:00
|
|
|
If the config file isn't found, return None.
|
2015-10-26 00:51:48 +00:00
|
|
|
"""
|
2016-01-21 18:01:54 +00:00
|
|
|
filename = os.path.join(get_config_dir(), 'settings.conf')
|
2015-10-26 00:51:48 +00:00
|
|
|
settings = ConfigParser()
|
|
|
|
|
|
|
|
if os.path.isfile(filename):
|
|
|
|
settings.read(filename)
|
2016-01-28 16:52:09 +00:00
|
|
|
return settings
|
2015-10-26 00:51:48 +00:00
|
|
|
else:
|
2016-01-23 00:32:46 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(get_config_dir())
|
|
|
|
except:
|
|
|
|
pass
|
2016-01-28 16:52:09 +00:00
|
|
|
return None
|
2015-10-26 00:51:48 +00:00
|
|
|
|
2016-01-28 16:52:09 +00:00
|
|
|
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.
|
|
|
|
"""
|
2016-01-24 03:14:41 +00:00
|
|
|
if settings is None:
|
|
|
|
settings = ConfigParser()
|
|
|
|
settings.add_section('ed_companion')
|
|
|
|
settings.add_section('inara')
|
2016-01-21 18:55:12 +00:00
|
|
|
|
2016-01-28 16:52:09 +00:00
|
|
|
config_func(settings)
|
|
|
|
|
2016-01-23 08:36:27 +00:00
|
|
|
with open(os.path.join(get_config_dir(), 'settings.conf'), 'wb') as f:
|
2016-01-21 18:45:45 +00:00
|
|
|
settings.write(f)
|
|
|
|
|
2016-01-23 08:36:27 +00:00
|
|
|
return settings
|