1
0
mirror of https://github.com/gryf/fs-uae-wrapper.git synced 2026-01-08 06:44:19 +01:00

Refactoring wrapper

This commit is contained in:
2016-12-27 20:02:12 +01:00
parent c8b4207040
commit 36fe31c99e
2 changed files with 22 additions and 68 deletions

View File

@@ -11,51 +11,24 @@ from fs_uae_wrapper import utils
from fs_uae_wrapper import WRAPPER_KEY from fs_uae_wrapper import WRAPPER_KEY
def parse_option(string):
"""
Return parsed option as an key/value tuple, where key is an stripped
from dash option name and the value is an value stripped of whitespace.
"""
key = val = None
if '=' in string:
key, val = string.split('=', 1)
if key.startswith('--'):
key = key[2:].strip()
else:
key = key.strip()
val = val.strip()
elif string.startswith('--'):
key = string[2:].strip()
# parameters are always as strings - parse them when need it later
val = '1'
return key, val
def parse_args(): def parse_args():
""" """
Look out for config file and for config options which would be blindly Look out for config file and for config options which would be blindly
passed to fs-uae. passed to fs-uae.
""" """
fs_conf = None fs_conf = None
fs_uae_options = [] options = utils.CmdOption()
wrapper_options = {}
for parameter in sys.argv[1:]: for parameter in sys.argv[1:]:
key, val = parse_option(parameter) try:
if key is not None and val is not None: options.add(parameter)
if WRAPPER_KEY in key: except AttributeError:
wrapper_options[key] = val
else:
fs_uae_options.append(parameter)
else:
if os.path.exists(parameter): if os.path.exists(parameter):
fs_conf = parameter fs_conf = parameter
if fs_conf is None and os.path.exists('Config.fs-uae'): if fs_conf is None and os.path.exists('Config.fs-uae'):
fs_conf = 'Config.fs-uae' fs_conf = 'Config.fs-uae'
return fs_conf, fs_uae_options, wrapper_options return fs_conf, options
def usage(): def usage():
@@ -73,9 +46,9 @@ def usage():
def run(): def run():
"""run wrapper module""" """run wrapper module"""
config_file, fs_uae_options, wrapper_options = parse_args() config_file, cmd_options = parse_args()
if '--help' in fs_uae_options: if 'help' in cmd_options:
usage() usage()
sys.exit(0) sys.exit(0)
@@ -90,7 +63,7 @@ def run():
sys.stderr.write('Error: Configuration file have syntax issues\n') sys.stderr.write('Error: Configuration file have syntax issues\n')
sys.exit(2) sys.exit(2)
wrapper_module = wrapper_options.get(WRAPPER_KEY) wrapper_module = cmd_options.get(WRAPPER_KEY)
if not wrapper_module: if not wrapper_module:
wrapper_module = configuration.get(WRAPPER_KEY) wrapper_module = configuration.get(WRAPPER_KEY)
@@ -105,8 +78,7 @@ def run():
"exists.\n" % wrapper_module) "exists.\n" % wrapper_module)
sys.exit(3) sys.exit(3)
if not wrapper.run(config_file, fs_uae_options, wrapper_options, if not wrapper.run(config_file, cmd_options, configuration):
configuration):
sys.exit(4) sys.exit(4)

View File

@@ -64,32 +64,14 @@ class TestWrapper(TestCase):
self.assertRaises(SystemExit, wrapper.run) self.assertRaises(SystemExit, wrapper.run)
def test_parse_option(self):
# commandline origin
self.assertEqual(wrapper.parse_option('--fullscreen'),
('fullscreen', '1'))
self.assertEqual(wrapper.parse_option('--fade_out_duration=0'),
('fade_out_duration', '0'))
# pass the wrong parameter to fs-uae
self.assertEqual(wrapper.parse_option('-typo=true'), ('-typo', 'true'))
# pass the wrong parameter to fs-uae again
self.assertEqual(wrapper.parse_option('typo=true'), ('typo', 'true'))
# We have no idea what to do with this - might be a conf file
self.assertEqual(wrapper.parse_option('this-is-odd'), (None, None))
def test_parse_args(self): def test_parse_args(self):
# Looking for configuration file... first, we have nothing # Looking for configuration file... first, we have nothing
self.assertEqual(wrapper.parse_args(), (None, [], {})) self.assertEqual(wrapper.parse_args(), (None, {}))
# still no luck - nonexistent file # still no luck - nonexistent file
sys.argv.append('there-is-no-config.fs-uae') sys.argv.append('there-is-no-config.fs-uae')
self.assertEqual(wrapper.parse_args(), (None, [], {})) self.assertEqual(wrapper.parse_args(), (None, {}))
# lets make it # lets make it
os.chdir(self.dirname) os.chdir(self.dirname)
@@ -97,7 +79,7 @@ class TestWrapper(TestCase):
fobj.write('\n') fobj.write('\n')
self.assertEqual(wrapper.parse_args(), self.assertEqual(wrapper.parse_args(),
('there-is-no-config.fs-uae', [], {})) ('there-is-no-config.fs-uae', {}))
# remove argument, try to find default one # remove argument, try to find default one
sys.argv.pop() sys.argv.pop()
@@ -106,7 +88,7 @@ class TestWrapper(TestCase):
with open('Config.fs-uae', 'w') as fobj: with open('Config.fs-uae', 'w') as fobj:
fobj.write('\n') fobj.write('\n')
self.assertEqual(wrapper.parse_args(), ('Config.fs-uae', [], {})) self.assertEqual(wrapper.parse_args(), ('Config.fs-uae', {}))
# add --wrapper-foo and --wrapper-bar options # add --wrapper-foo and --wrapper-bar options
sys.argv.extend(['--wrapper=plain', '--wrapper_foo=1', sys.argv.extend(['--wrapper=plain', '--wrapper_foo=1',
@@ -118,12 +100,11 @@ class TestWrapper(TestCase):
with open('Config.fs-uae', 'w') as fobj: with open('Config.fs-uae', 'w') as fobj:
fobj.write('\n') fobj.write('\n')
conf, fsopts, wrapopts = wrapper.parse_args() conf, fsopts = wrapper.parse_args()
self.assertEqual(conf, 'Config.fs-uae') self.assertEqual(conf, 'Config.fs-uae')
self.assertListEqual(fsopts, []) self.assertDictEqual(fsopts, {'wrapper': 'plain',
self.assertDictEqual(wrapopts, {'wrapper': 'plain', 'wrapper_foo': '1',
'wrapper_foo': '1', 'wrapper_bar': 'false'})
'wrapper_bar': 'false'})
# mix wrapper* params in commandline and config # mix wrapper* params in commandline and config
sys.argv = ['fs-uae-wrapper', sys.argv = ['fs-uae-wrapper',
@@ -134,8 +115,9 @@ class TestWrapper(TestCase):
with open('Config.fs-uae', 'w') as fobj: with open('Config.fs-uae', 'w') as fobj:
fobj.write('[conf]\nwrapper = cd32\nwrapper_foo = /some/path\n') fobj.write('[conf]\nwrapper = cd32\nwrapper_foo = /some/path\n')
conf, fsopts, wrapopts = wrapper.parse_args() conf, fsopts = wrapper.parse_args()
self.assertEqual(conf, 'Config.fs-uae') self.assertEqual(conf, 'Config.fs-uae')
self.assertListEqual(fsopts, ['--fullscreen', '--fast_memory=4096']) self.assertDictEqual(fsopts, {'wrapper': 'plain',
self.assertDictEqual(wrapopts, {'wrapper': 'plain', 'wrapper_bar': 'false',
'wrapper_bar': 'false'}) 'fullscreen': '1',
'fast_memory': '4096'})