-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
883 additions
and
207 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
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,103 @@ | ||
Command line interface | ||
====================== | ||
|
||
The :code:`rzb` command | ||
----------------------- | ||
|
||
Razorback provides some command line tools under the :code:`rzb` command. | ||
Typing the command shows the help message with the list of available commands : | ||
|
||
.. code-block:: bash | ||
$ rzb | ||
Usage: rzb [OPTIONS] COMMAND [ARGS]... | ||
The razorback command line interface. | ||
Options: | ||
-h, --help Show this message and exit. | ||
Commands: | ||
path Manipulate data path. | ||
version Razorback installed version. | ||
Getting the version number with :code:`rzb version` | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
To get the number of the installed version : | ||
|
||
.. code-block:: bash | ||
$ rzb version | ||
razorback 0.4.0 | ||
Manipulating the data path with :code:`rzb path` | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
:code:`rzb path` provides several subcommands for manipulating the data path. | ||
Just typing :code:`rzb path` performs no action but shows the help and the list of available commands : | ||
|
||
.. code-block:: bash | ||
$ rzb path | ||
Usage: rzb path [OPTIONS] COMMAND [ARGS]... | ||
Manipulate data path. | ||
Data path is either global or local. If the local path is not available, | ||
the global path is used instead. | ||
The path commands depend on the current directory where they are executed. | ||
Options: | ||
-c, --create Create the local path if missing. | ||
-h, --help Show this message and exit. | ||
Commands: | ||
base Current base data path. | ||
metronix Current path for Metronix calibration files. | ||
Without options, :code:`rzb path base` and :code:`rzb path metronix` just show the path where data, like calibration files, will be searched. This path depends on the working directory. | ||
The option :code:`--create` will create the corresponding local path. | ||
Extending the command line interface | ||
------------------------------------ | ||
You can add commands to the :code:`rzb` command line interface by using `Click <https://click.palletsprojects.com/>`_ and `setuptools entry points <https://setuptools.readthedocs.io/en/latest/userguide/entry_point.html>`_. | ||
Let us look at an example. | ||
We have a simple `Click <https://click.palletsprojects.com/>`_ program in our package: | ||
.. code-block:: python | ||
# mypkg/rzb_cli.py | ||
import click | ||
@click.command('say-hello') | ||
def cli(): | ||
click.echo('Hello world !') | ||
We also have a `setup.py` for installing our package. | ||
To extend the :code:`rzb` command, we need to informs the `setup()` function in the following way: | ||
.. code-block:: python | ||
# setup.py | ||
setup( | ||
# ... | ||
entry_points={ | ||
'rzb.commands': [ | ||
'say-hello=mypkg.rzb_cli:cli', | ||
] | ||
}, | ||
) | ||
Once `mypkg` is installed (:code:`python setup.py install` or :code:`pip install .`), the :code:`rzb` command can now expose our new subcommand: | ||
.. code-block:: bash | ||
$ rzb say-hello | ||
Hello world ! |
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,80 @@ | ||
|
||
import pathlib | ||
import os | ||
from pkg_resources import iter_entry_points | ||
import click | ||
import razorback as rzb | ||
|
||
|
||
class PluginGroup(click.Group): | ||
|
||
def __init__(self, *args, **kwds): | ||
self.extra_commands = { | ||
e.name: e.load() for e in iter_entry_points('rzb.commands') | ||
} | ||
super().__init__(*args, **kwds) | ||
|
||
def list_commands(self, ctx): | ||
return sorted(super().list_commands(ctx) + list(self.extra_commands)) | ||
|
||
def get_command(self, ctx, name): | ||
return self.extra_commands.get(name) or super().get_command(ctx, name) | ||
|
||
|
||
@click.group(cls=PluginGroup, context_settings={'help_option_names': ('-h', '--help')}) | ||
def cli(): | ||
""" The razorback command line interface. | ||
""" | ||
pass | ||
|
||
|
||
@cli.command() | ||
def version(): | ||
""" Razorback installed version. | ||
""" | ||
click.echo(f"razorback{'+' if rzb.plus else ''} {rzb.__version__}") | ||
|
||
|
||
@cli.group() | ||
@click.option('-c', '--create', is_flag=True, | ||
help="Create the local path if missing." | ||
) | ||
@click.pass_context | ||
def path(ctx, create): | ||
""" Manipulate data path. | ||
Data path is either global or local. | ||
If the local path is not available, the global path is used instead. | ||
The path commands depend on the current directory where they are executed. | ||
""" | ||
ctx.obj = {'create': create} | ||
|
||
|
||
def path_action(ctx, target): | ||
if not ctx.obj['create']: | ||
click.echo(rzb.data.data_path(target)) | ||
else: | ||
p = rzb.data.local_data_path(target) | ||
if p.exists(): | ||
click.echo(f"{p} already exists") | ||
else: | ||
p.mkdir(parents=True) | ||
click.echo(f"{p} created") | ||
|
||
|
||
@path.command('base') | ||
@click.pass_context | ||
def path_base(ctx): | ||
""" Current base data path. | ||
""" | ||
path_action(ctx, '') | ||
|
||
|
||
@path.command('metronix') | ||
@click.pass_context | ||
def path_metronix(ctx): | ||
""" Current path for Metronix calibration files. | ||
""" | ||
path_action(ctx, rzb.calibrations.METRONIX_DATA_PATH) |
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
Oops, something went wrong.