-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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).
- Loading branch information
Showing
11 changed files
with
422 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,3 +72,6 @@ version = "0.1" | |
|
||
[dev-dependencies.regex] | ||
version = "1" | ||
|
||
[dev-dependencies.tempfile] | ||
version = "3.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python | ||
|
||
# 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.""" | ||
NITROCLI = "nitrocli" | ||
MODEL = "model" | ||
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:]) | ||
if namespace.what == Action.NITROCLI.value: | ||
print(environ["NITROCLI_BINARY"]) | ||
elif namespace.what == Action.MODEL.value: | ||
print(environ["NITROCLI_MODEL"]) | ||
elif namespace.what == Action.VERBOSITY.value: | ||
print(environ["NITROCLI_VERBOSITY"]) | ||
else: | ||
return 1 | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
exit(main(argv)) |
Oops, something went wrong.