fairical.scripts.utils¶
Tools for command-line applications.
Functions
|
Ensure parent directory exists and back-up copies. |
|
Save a dictionary into a CSV file with path checking and backup. |
|
Save a dictionary into a JSON file with path checking and backup. |
|
Validate a user defined metric for support. |
|
Validate one or more JSON files against a predefined data model. |
|
Validate one or more JSON files against a predefined data model. |
|
Validate a threshold setup. |
|
Click-option decorator that adds a |
- fairical.scripts.utils.prepare_and_backup(path)[source]¶
Ensure parent directory exists and back-up copies.
This function will check that the directory leading to a file path exists and will created it otherwise. It will also check if the file does not already exists, and back it up otherwise.
- fairical.scripts.utils.save_json_with_backup(path, data)[source]¶
Save a dictionary into a JSON file with path checking and backup.
This function will save a dictionary into a JSON file. It will check the existence of the directory leading to the file and create it if necessary. If the file already exists on the destination folder, it is backed-up before a new file is created with the new contents.
- fairical.scripts.utils.save_csv_with_backups(path, data)[source]¶
Save a dictionary into a CSV file with path checking and backup.
This function will save a dictionary into a CSV file. It will check the existence of the directory leading to the file and create it if necessary. If the file already exists on the destination folder, it is backed-up before a new file is created with the new contents.
- fairical.scripts.utils.validate_scores_json(ctx, param, value)[source]¶
Validate one or more JSON files against a predefined data model.
This function is intended to be used as a Click argument callback. It opens the given file path, parses its JSON content, and validates it against the library data model. If any validation error occurs, a click.BadParameter is raised to inform the user.
- Parameters:
- Return type:
list[tuple[Path,Scores] |tuple[tuple[Path,Scores],tuple[Path,Solutions]]]- Returns:
The parsed and validated JSON content as a list, where each element is either a a tuple mapping filename to scores, or a tuple containing two entries, the first mapping a filename to scores and a second (to be used for selecting a priori thresholds and systems) mapping a filename to pre-calculated solutions.
- Raises:
click.BadParameter – If the file cannot be read, contains invalid JSON, or fails data model validation.
- fairical.scripts.utils.validate_solutions_json(ctx, param, value)[source]¶
Validate one or more JSON files against a predefined data model.
This function is intended to be used as a Click argument callback. It opens the given file path, parses its JSON content, and validates it against the library data model. If any validation error occurs, a click.BadParameter is raised to inform the user.
- Parameters:
- Return type:
- Returns:
The parsed and validated JSON content as a dictionary, where keys represent the basename of files without extension.
- Raises:
click.BadParameter – If the file cannot be read, contains invalid JSON, or fails data model validation.
- fairical.scripts.utils.validate_metrics(ctx, param, value)[source]¶
Validate a user defined metric for support.
- Parameters:
- Return type:
- Returns:
The validated metrics.
- Raises:
click.BadParameter – If one or more of the provided metrics do not validate correctly.
- fairical.scripts.utils.validate_thresholds(ctx, param, value)[source]¶
Validate a threshold setup.
- Parameters:
- Return type:
- Returns:
The validated threshold, as our API likes it.
- Raises:
click.BadParameter – If the threshold cannot be validated.
- fairical.scripts.utils.verbosity_option(logger, short_name='v', name='verbose', dflt=0, **kwargs)[source]¶
Click-option decorator that adds a
-v/--verboseoption to a cli.This decorator adds a click option to your CLI to set the log-level on a provided
logging.Logger. You must specifically determine the logger that will be affected by this CLI option, via theloggeroption.@verbosity_option(logger=logger)
The verbosity option has the “count” type, and has a default value of 0. At each time you provide
-voptions on the command-line, this value is increased by one. For example, a CLI setting of-vvvwill set the value of this option to 3. This is the mapping between the value of this option (count of-vCLI options passed) and the log-level set at the provided logger:0 (no
-voption provided):logger.setLevel(logging.ERROR)1 (
-v):logger.setLevel(logging.WARNING)2 (
-vv):logger.setLevel(logging.INFO)3 (
-vvvor more):logger.setLevel(logging.DEBUG)
- Parameters:
logger (
Logger) – Thelogging.Loggerto be set.short_name (
str) – Short name of the option. If not set, then usevname (
str) – Long name of the option. If not set, then useverbose– this will also become the name of the contextual parameter for click.dlft – The default verbosity level to use (defaults to 0).
**kwargs (
Any) – Further keyword-arguments to be forwarded to the underlyingclick.option()
- Return type:
- Returns:
A callable, that follows the
click-framework policy for option decorators. Use it accordingly.