Skip to content

Commit

Permalink
Use a named tuple to better organise the data
Browse files Browse the repository at this point in the history
  • Loading branch information
muffato authored and ens-bwalts committed Jul 27, 2021
1 parent a370a19 commit 9d2b33b
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions wrappers/python3/wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# limitations under the License.


import collections
import sys

import eHive
Expand Down Expand Up @@ -43,17 +44,18 @@ def do_build():

## And here we select the mode

WrapperMode = collections.namedtuple('WrapperMode', ['function', 'args'])
available_modes = {
'version' : (do_version, []),
'build' : (do_build, []),
'check_exists' : (do_check_exists, ['module_name']),
'run' : (do_run, ['module_name', 'fd_in', 'fd_out', 'debug'])
'version' : WrapperMode(do_version, []),
'build' : WrapperMode(do_build, []),
'check_exists' : WrapperMode(do_check_exists, ['module_name']),
'run' : WrapperMode(do_run, ['module_name', 'fd_in', 'fd_out', 'debug'])
}

def usage(msg):
error = "Command-line error: " + msg + "\nUsage: \n"
for (mode, (_, args)) in available_modes.items():
error += "\t" + " ".join([sys.argv[0], mode] + args) + "\n"
for (mode, impl) in available_modes.items():
error += "\t" + " ".join([sys.argv[0], mode] + impl.args) + "\n"
print(error, file=sys.stderr)
sys.exit(1)

Expand All @@ -63,11 +65,11 @@ if len(sys.argv) == 1:
mode = sys.argv[1]
if mode not in available_modes:
usage('Unknown mode "{0}"'.format(mode))
impl = available_modes[mode]

if len(sys.argv)-2 < len(available_modes[mode][1]):
usage('Not enough arguments for mode "' + mode + '". Expecting: ' + ' '.join(available_modes[mode][1]))
if len(sys.argv)-2 > len(available_modes[mode][1]):
usage('Too many arguments for mode "' + mode + '". Expecting: ' + (' '.join(available_modes[mode][1]) if available_modes[mode][1] else '(none)'))

available_modes[mode][0]()
if len(sys.argv)-2 < len(impl.args):
usage('Not enough arguments for mode "' + mode + '". Expecting: ' + ' '.join(impl.args))
if len(sys.argv)-2 > len(impl.args):
usage('Too many arguments for mode "' + mode + '". Expecting: ' + (' '.join(impl.args) if impl.args else '(none)'))
impl.function()

0 comments on commit 9d2b33b

Please sign in to comment.