mirror of
https://github.com/gryf/fs-uae-wrapper.git
synced 2025-12-19 12:28:12 +01:00
Utils refactoring.
Added safe guards for get_config function, introduced CmdOption class for convenient accessing options as a dict keys with option to make them as a list of commandline options suitable for passing to fs-uae.
This commit is contained in:
@@ -25,6 +25,36 @@ ARCHIVERS = {'.tar': ['tar', 'xf'],
|
|||||||
'.lzx': ['unlzx']}
|
'.lzx': ['unlzx']}
|
||||||
|
|
||||||
|
|
||||||
|
class CmdOption(dict):
|
||||||
|
"""
|
||||||
|
Holder class for commandline switches.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def add(self, option):
|
||||||
|
"""parse and add option to the dictionary"""
|
||||||
|
if not option.startswith('--'):
|
||||||
|
raise AttributeError("Cannot add option `%s' to the dictionary" %
|
||||||
|
option)
|
||||||
|
if '=' in option:
|
||||||
|
key, val = option.split('=', 1)
|
||||||
|
key = key[2:].strip()
|
||||||
|
self[key] = val.strip()
|
||||||
|
else:
|
||||||
|
key = option[2:].strip()
|
||||||
|
# parameters are always as options - parse them when need it later
|
||||||
|
self[key] = '1'
|
||||||
|
|
||||||
|
def list(self):
|
||||||
|
"""Return list of options as it was passed through the commandline"""
|
||||||
|
ret_list = []
|
||||||
|
for key, val in self.items():
|
||||||
|
if val != '1':
|
||||||
|
ret_list.append('--%(k)s=%(v)s' % {'k': key, 'v': val})
|
||||||
|
else:
|
||||||
|
ret_list.append('--%(k)s' % {'k': key})
|
||||||
|
return ret_list
|
||||||
|
|
||||||
|
|
||||||
def get_config_options(conf):
|
def get_config_options(conf):
|
||||||
"""Read config file and return options as a dict"""
|
"""Read config file and return options as a dict"""
|
||||||
parser = configparser.SafeConfigParser()
|
parser = configparser.SafeConfigParser()
|
||||||
@@ -79,32 +109,16 @@ def merge_wrapper_options(configuration, wrapper_options):
|
|||||||
return options
|
return options
|
||||||
|
|
||||||
|
|
||||||
def options_to_dict(commandline):
|
|
||||||
"""
|
|
||||||
"Parse" commandline switches and return them as dictionary
|
|
||||||
"""
|
|
||||||
options = {}
|
|
||||||
for option in commandline:
|
|
||||||
if option.startswith('--'):
|
|
||||||
if '=' in option:
|
|
||||||
key, val = option[2:].split('=')
|
|
||||||
options[key.strip()] = val.strip()
|
|
||||||
else:
|
|
||||||
options[option[2:].strip()] = '1'
|
|
||||||
|
|
||||||
return options
|
|
||||||
|
|
||||||
|
|
||||||
def merge_all_options(configuration, commandline):
|
def merge_all_options(configuration, commandline):
|
||||||
"""
|
"""
|
||||||
Merge dictionaries with wrapper options into one. Commandline options
|
Merge dictionaries with wrapper options into one. Commandline options
|
||||||
have precedence.
|
have precedence.
|
||||||
"""
|
"""
|
||||||
options = {}
|
options = {}
|
||||||
for key, val in configuration:
|
for key, val in configuration.items():
|
||||||
options[key] = val
|
options[key] = val
|
||||||
|
|
||||||
options.update(options_to_dict(commandline))
|
options.update(commandline)
|
||||||
|
|
||||||
return options
|
return options
|
||||||
|
|
||||||
@@ -170,20 +184,25 @@ def get_config(conf_file):
|
|||||||
for path, conf_dir in paths:
|
for path, conf_dir in paths:
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
config = get_config_options(path)
|
config = get_config_options(path)
|
||||||
config.update(get_config_options(conf_file))
|
if config is None:
|
||||||
|
continue
|
||||||
|
conf = get_config_options(conf_file) or {}
|
||||||
|
config.update(conf)
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
conf_dir = None
|
conf_dir = None
|
||||||
config = get_config_options(conf_file)
|
config = get_config_options(conf_file) or {}
|
||||||
|
|
||||||
if 'base_dir' in config:
|
if 'base_dir' in config:
|
||||||
base_dir = interpolate_variables(config['base_dir'], conf_file)
|
base_dir = interpolate_variables(config['base_dir'], conf_file)
|
||||||
host = os.path.join(base_dir, 'Configurations/Host.fs-uae')
|
host = os.path.join(base_dir, 'Configurations/Host.fs-uae')
|
||||||
|
|
||||||
if os.path.exists(host):
|
if os.path.exists(host):
|
||||||
config.update(get_config_options(host))
|
host_conf = get_config_options(host) or {}
|
||||||
|
config.update(host_conf)
|
||||||
# overwrite host options again via provided custom/relative conf
|
# overwrite host options again via provided custom/relative conf
|
||||||
config.update(get_config_options(conf_file))
|
conf = get_config_options(conf_file) or {}
|
||||||
|
config.update(conf)
|
||||||
elif conf_dir:
|
elif conf_dir:
|
||||||
config['_base_dir'] = conf_dir
|
config['_base_dir'] = conf_dir
|
||||||
|
|
||||||
|
|||||||
@@ -66,3 +66,36 @@ class TestUtils(TestCase):
|
|||||||
fobj.write("[conf]\nwrapper = \n")
|
fobj.write("[conf]\nwrapper = \n")
|
||||||
conf = utils.get_config_options(self.fname)
|
conf = utils.get_config_options(self.fname)
|
||||||
self.assertDictEqual(conf, {'wrapper': ''})
|
self.assertDictEqual(conf, {'wrapper': ''})
|
||||||
|
|
||||||
|
|
||||||
|
class TestCmdOptions(TestCase):
|
||||||
|
|
||||||
|
def test_add(self):
|
||||||
|
|
||||||
|
cmd = utils.CmdOption()
|
||||||
|
|
||||||
|
# commandline origin
|
||||||
|
cmd.add('--fullscreen')
|
||||||
|
self.assertEqual(cmd['fullscreen'], '1')
|
||||||
|
|
||||||
|
cmd.add('--fade_out_duration=0')
|
||||||
|
self.assertEqual(cmd['fade_out_duration'], '0')
|
||||||
|
|
||||||
|
# pass the wrong parameter to fs-uae
|
||||||
|
self.assertRaises(AttributeError, cmd.add, '-typo=0')
|
||||||
|
|
||||||
|
# pass the wrong parameter to fs-uae again
|
||||||
|
self.assertRaises(AttributeError, cmd.add, 'typo=true')
|
||||||
|
|
||||||
|
# We have no idea what to do with this - might be a conf file
|
||||||
|
self.assertRaises(AttributeError, cmd.add, 'this-is-odd')
|
||||||
|
|
||||||
|
def test_list(self):
|
||||||
|
|
||||||
|
cmd = utils.CmdOption()
|
||||||
|
cmd.add('--fullscreen')
|
||||||
|
cmd.add('--fast_memory=4096')
|
||||||
|
|
||||||
|
self.assertDictEqual(cmd, {'fullscreen': '1', 'fast_memory': '4096'})
|
||||||
|
self.assertListEqual(cmd.list(),
|
||||||
|
['--fullscreen', '--fast_memory=4096'])
|
||||||
|
|||||||
Reference in New Issue
Block a user