diff options
author | Daniel Mueller <deso@posteo.net> | 2020-08-25 19:04:50 -0700 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2020-10-11 17:07:07 -0700 |
commit | 8cf63c6790192c30c81294e7a940d470bf061cbf (patch) | |
tree | 169ca792304f74a7b1548b5bad798b39649cd4c5 /src/tests/extension_var_test.py | |
parent | 330142e68cac0116babbf2fd64fc9ff0fde132c0 (diff) | |
download | nitrocli-8cf63c6790192c30c81294e7a940d470bf061cbf.tar.gz nitrocli-8cf63c6790192c30c81294e7a940d470bf061cbf.tar.bz2 |
Introduce support for user-provided extensions
This change introduces support for discovering and executing
user-provided extensions to the program. Extensions are useful for
allowing users to provide additional functionality on top of the
nitrocli proper. Implementation wise we stick to an approach similar to
git or cargo subcommands in nature: we search the directories listed in
the PATH environment variable for a file that starts with "nitrocli-",
followed by the extension name. This file is then executed. It is
assumed that the extension recognizes (or at least not prohibits) the
following arguments: --nitrocli (providing the path to the nitrocli
binary), --model (with the model passed to the main program), and
--verbosity (the verbosity level).
Diffstat (limited to 'src/tests/extension_var_test.py')
-rw-r--r-- | src/tests/extension_var_test.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/tests/extension_var_test.py b/src/tests/extension_var_test.py new file mode 100644 index 0000000..af7ec84 --- /dev/null +++ b/src/tests/extension_var_test.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2020 The Nitrocli Developers +# SPDX-License-Identifier: GPL-3.0-or-later + +from argparse import ( + ArgumentParser, +) +from enum import ( + Enum, +) +from os import ( + environ, +) +from sys import ( + argv, + exit, +) + + +class Action(Enum): + """An action to perform.""" + BINARY = "binary" + MODEL = "model" + NO_CACHE = "no-cache" + SERIAL_NUMBERS = "serial-numbers" + USB_PATH = "usb-path" + VERBOSITY = "verbosity" + + @classmethod + def all(cls): + """Return the list of all the enum members' values.""" + return [x.value for x in cls.__members__.values()] + + +def main(args): + """The extension's main function.""" + parser = ArgumentParser() + parser.add_argument(choices=Action.all(), dest="what") + parser.add_argument("--nitrocli", action="store", default=None) + parser.add_argument("--model", action="store", default=None) + # We deliberately store the argument to this option as a string + # because we can differentiate between None and a valid value, in + # order to verify that it really is supplied. + parser.add_argument("--verbosity", action="store", default=None) + + namespace = parser.parse_args(args[1:]) + # We create a "reverse" mapping from string to variant (e.g., model -> + # MODEL). + options = {v.value: k for k, v in Action.__members__.items()} + try: + var = options[namespace.what] + print(environ[f"NITROCLI_{var}"]) + except KeyError: + return 1 + + return 0 + + +if __name__ == "__main__": + exit(main(argv)) |