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

48 lines
1.2 KiB
Python
Raw 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 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')
config_func(settings)
with open(os.path.join(get_config_dir(), 'settings.conf'), 'wb') as f:
settings.write(f)
return settings