This repository has been archived on 2019-12-04. You can view files and clone it, but cannot push or open issues or pull requests.
inara_updater/utils.py

60 lines
1.6 KiB
Python
Raw Permalink Normal View History

2015-10-26 00:51:48 +00:00
from ConfigParser import ConfigParser
import os
import platform
2015-10-26 00:51:48 +00:00
def get_config_dir(make=False):
if platform.system() == 'Windows':
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
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.
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)
return settings
2015-10-26 00:51:48 +00:00
else:
try:
os.makedirs(get_config_dir())
except:
pass
return None
2015-10-26 00:51:48 +00:00
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
with the settings object as a parameter, then write the settings to the config
file.
"""
if settings is None:
settings = ConfigParser()
for section in ('ed_companion', 'inara', 'ships'):
if not settings.has_section(section):
settings.add_section(section)
config_func(settings)
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)