From 9b49c13d4e6918b515a182eef705431281209ad3 Mon Sep 17 00:00:00 2001 From: Ben Schroeter Date: Tue, 13 Feb 2024 14:03:28 +1100 Subject: [PATCH] Black reapplied. #103 --- benchcab/benchcab.py | 14 +++++++------- benchcab/comparison.py | 9 +++------ benchcab/config.py | 5 +++++ benchcab/environment_modules.py | 4 ++++ benchcab/fluxsite.py | 4 +++- benchcab/main.py | 1 + benchcab/model.py | 1 + benchcab/utils/__init__.py | 6 ++++-- benchcab/utils/fs.py | 1 + benchcab/utils/repo.py | 11 +++++++++++ benchcab/utils/subprocess.py | 1 + tests/test_benchcab.py | 1 + tests/test_config.py | 2 +- 13 files changed, 43 insertions(+), 17 deletions(-) diff --git a/benchcab/benchcab.py b/benchcab/benchcab.py index dc9e401..76ce38f 100644 --- a/benchcab/benchcab.py +++ b/benchcab/benchcab.py @@ -26,7 +26,7 @@ from benchcab.utils import get_logger from benchcab.utils.fs import mkdir, next_path from benchcab.utils.pbs import render_job_script -from benchcab.utils.repo import SVNRepo, create_repo +from benchcab.utils.repo import create_repo from benchcab.utils.subprocess import SubprocessWrapper, SubprocessWrapperInterface from benchcab.workdir import setup_fluxsite_directory_tree @@ -50,6 +50,7 @@ def __init__( Path to the executable. validate_env : bool, optional Validate the environment, by default True + """ self.benchcab_exe_path = benchcab_exe_path self.validate_env = validate_env @@ -106,11 +107,10 @@ def _validate_environment(self, project: str, modules: list): paths = list(internal.MET_DIR.glob(f"{site_id}*")) if not paths: self.logger.error( - [ - f"Failed to infer met file for site id '{site_id}' in " - f"{internal.MET_DIR}." - ] + f"Failed to infer met file for site id '{site_id}' in " ) + self.logger.error(f"{internal.MET_DIR}.") + sys.exit(1) if len(paths) > 1: self.logger.error( @@ -262,7 +262,7 @@ def fluxsite_run_tasks(self, config_path: str): ncpus = config.get("pbs", {}).get( "ncpus", internal.FLUXSITE_DEFAULT_PBS["ncpus"] ) - run_tasks_in_parallel(tasks, n_processes=ncpus, verbose=verbose) + run_tasks_in_parallel(tasks, n_processes=ncpus) else: run_tasks(tasks) self.logger.info("Successfully ran fluxsite tasks") @@ -290,7 +290,7 @@ def fluxsite_bitwise_cmp(self, config_path: str): ncpus = config["fluxsite"]["pbs"]["ncpus"] except KeyError: ncpus = internal.FLUXSITE_DEFAULT_PBS["ncpus"] - run_comparisons_in_parallel(comparisons, n_processes=ncpus, verbose=verbose) + run_comparisons_in_parallel(comparisons, n_processes=ncpus) else: run_comparisons(comparisons) self.logger.info("Successfully ran comparison tasks") diff --git a/benchcab/comparison.py b/benchcab/comparison.py index 173ca02..495b9d7 100644 --- a/benchcab/comparison.py +++ b/benchcab/comparison.py @@ -32,6 +32,7 @@ def __init__( Files. task_name : str Name of the task. + """ self.files = files self.task_name = task_name @@ -57,12 +58,8 @@ def run(self) -> None: with output_file.open("w", encoding="utf-8") as file: file.write(exc.stdout) - self.logger.error( - [ - f"Failure: files {file_a.name} {file_b.name} differ. ", - f"Results of diff have been written to {output_file}", - ] - ) + self.logger.error(f"Failure: files {file_a.name} {file_b.name} differ. ") + self.logger.error(f"Results of diff have been written to {output_file}") sys.stdout.flush() diff --git a/benchcab/config.py b/benchcab/config.py index 25547d0..2cc5115 100644 --- a/benchcab/config.py +++ b/benchcab/config.py @@ -22,6 +22,7 @@ def __init__(self, validator: Validator): ---------- validator: cerberus.Validator A validation object that has been used and has the errors attribute. + """ # Nicely format the errors. errors = [f"{k} = {v}" for k, v in validator.errors.items()] @@ -51,6 +52,7 @@ def validate_config(config: dict) -> bool: ------ ConfigValidationError Raised when the configuration file fails validation. + """ # Load the schema schema = bu.load_package_data("config-schema.yml") @@ -81,6 +83,7 @@ def read_optional_key(config: dict): ---------- config : dict The configuration file with with/without optional keys + """ if "project" not in config: config["project"] = os.environ.get("PROJECT", None) @@ -118,6 +121,7 @@ def read_config_file(config_path: str) -> dict: ------- dict Configuration dict + """ # Load the configuration file. with Path.open(Path(config_path), "r", encoding="utf-8") as file: @@ -143,6 +147,7 @@ def read_config(config_path: str) -> dict: ------ ConfigValidationError Raised when the configuration file fails validation. + """ # Read configuration file config = read_config_file(config_path) diff --git a/benchcab/environment_modules.py b/benchcab/environment_modules.py index 70546a5..fe845e8 100644 --- a/benchcab/environment_modules.py +++ b/benchcab/environment_modules.py @@ -72,6 +72,7 @@ def module_is_avail(self, *args: str) -> bool: ------- bool True if available, False otherwise. + """ return module("is-avail", *args) @@ -82,6 +83,7 @@ def module_is_loaded(self, *args: str) -> bool: ------- bool True if loaded, False otherwise. + """ return module("is-loaded", *args) @@ -92,6 +94,7 @@ def module_load(self, *args: str) -> None: ------ EnvironmentModulesError Raised when module fails to load. + """ if not module("load", *args): raise EnvironmentModulesError("Failed to load modules: " + " ".join(args)) @@ -103,6 +106,7 @@ def module_unload(self, *args: str) -> None: ------ EnvironmentModulesError Raised when module fails to unload. + """ if not module("unload", *args): raise EnvironmentModulesError("Failed to unload modules: " + " ".join(args)) diff --git a/benchcab/fluxsite.py b/benchcab/fluxsite.py index bd3c41a..14ea70a 100644 --- a/benchcab/fluxsite.py +++ b/benchcab/fluxsite.py @@ -18,9 +18,9 @@ from benchcab import __version__, internal from benchcab.comparison import ComparisonTask from benchcab.model import Model +from benchcab.utils import get_logger from benchcab.utils.fs import chdir, mkdir from benchcab.utils.subprocess import SubprocessWrapper, SubprocessWrapperInterface -from benchcab.utils import get_logger # fmt: off # ====================================================== @@ -44,6 +44,7 @@ def deep_update(mapping: Dict[KeyType, Any], *updating_mappings: Dict[KeyType, A ------- Dict[KeyType, Any] Updated mapping. + """ updated_mapping = mapping.copy() for updating_mapping in updating_mappings: @@ -129,6 +130,7 @@ def __init__( Science configuration ID. sci_config : dict Science configuration. + """ self.model = model self.met_forcing_file = met_forcing_file diff --git a/benchcab/main.py b/benchcab/main.py index c935ebf..f7dc715 100644 --- a/benchcab/main.py +++ b/benchcab/main.py @@ -15,6 +15,7 @@ def parse_and_dispatch(parser): ---- parser : argparse.ArgumentParser Parser object. + """ args = vars(parser.parse_args(sys.argv[1:] if sys.argv[1:] else ["-h"])) diff --git a/benchcab/model.py b/benchcab/model.py index 5642eec..1bbf014 100644 --- a/benchcab/model.py +++ b/benchcab/model.py @@ -49,6 +49,7 @@ def __init__( Build script, by default None model_id : Optional[int], optional Model ID, by default None + """ self.repo = repo self.name = name if name else repo.get_branch_name() diff --git a/benchcab/utils/__init__.py b/benchcab/utils/__init__.py index 9493886..aa09428 100644 --- a/benchcab/utils/__init__.py +++ b/benchcab/utils/__init__.py @@ -5,14 +5,14 @@ """Top-level utilities.""" import json +import logging import os import pkgutil +import sys from importlib import resources from pathlib import Path import yaml -import logging -import sys # List of one-argument decoding functions. PACKAGE_DATA_DECODERS = dict(json=json.loads, yml=yaml.safe_load) @@ -25,6 +25,7 @@ def get_installed_root() -> Path: ------- Path Path to the installed root. + """ return Path(resources.files("benchcab")) @@ -36,6 +37,7 @@ def load_package_data(filename: str) -> dict: ---------- filename : str Filename of the file to load out of the data directory. + """ # Work out the encoding of requested file. ext = filename.split(".")[-1] diff --git a/benchcab/utils/fs.py b/benchcab/utils/fs.py index 78bc63c..2be838f 100644 --- a/benchcab/utils/fs.py +++ b/benchcab/utils/fs.py @@ -67,6 +67,7 @@ def mkdir(new_path: Path, **kwargs): Path to the directory to be created. **kwargs : dict, optional Additional options for `pathlib.Path.mkdir()` + """ get_logger().debug(f"Creating {new_path} directory") new_path.mkdir(**kwargs) diff --git a/benchcab/utils/repo.py b/benchcab/utils/repo.py index f7025be..2f89172 100644 --- a/benchcab/utils/repo.py +++ b/benchcab/utils/repo.py @@ -31,6 +31,7 @@ def get_revision(self) -> str: ------- str Human readable string describing the latest revision. + """ @abstractmethod @@ -41,6 +42,7 @@ def get_branch_name(self) -> str: ------- str Branch name of the source code. + """ @@ -51,6 +53,7 @@ class GitRepo(Repo): ---------- subprocess_handler: SubprocessWrapper Object for handling subprocess calls. + """ subprocess_handler = SubprocessWrapper() @@ -73,6 +76,7 @@ def __init__( commit: str, optional Commit hash (long). When specified the repository will reset to this commit when cloning. + """ self.url = url self.branch = branch @@ -103,6 +107,7 @@ def get_revision(self) -> str: ------- str Human readable string describing the latest revision. + """ repo = git.Repo(self.path) return f"commit {repo.head.commit.hexsha}" @@ -114,6 +119,7 @@ def get_branch_name(self) -> str: ------- str Branch name of the source code. + """ return self.branch @@ -125,6 +131,7 @@ class SVNRepo(Repo): ---------- subprocess_handler: SubprocessWrapper Object for handling subprocess calls. + """ subprocess_handler: SubprocessWrapperInterface = SubprocessWrapper() @@ -152,6 +159,7 @@ def __init__( revision: int, optional SVN revision number. When specified the branch will be set to this revision on checkout. + """ self.svn_root = svn_root self.branch_path = branch_path @@ -181,6 +189,7 @@ def get_revision(self) -> str: ------- str Human readable string describing the latest revision. + """ proc = self.subprocess_handler.run_cmd( f"svn info --show-item last-changed-revision {self.path}", @@ -195,6 +204,7 @@ def get_branch_name(self) -> str: ------- str Branch name of the source code. + """ return Path(self.branch_path).name @@ -218,6 +228,7 @@ def create_repo(spec: dict, path: Path) -> Repo: ------- Repo A subclass instance of `Repo`. + """ if "git" in spec: if "url" not in spec["git"]: diff --git a/benchcab/utils/subprocess.py b/benchcab/utils/subprocess.py index 7734141..8951c19 100644 --- a/benchcab/utils/subprocess.py +++ b/benchcab/utils/subprocess.py @@ -60,6 +60,7 @@ def run_cmd( ------- subprocess.CompletedProcess _description_ + """ # Use the logging level (10 = Debug) to determine verbosity. verbose = get_logger().getEffectiveLevel() == DEBUG_LEVEL diff --git a/tests/test_benchcab.py b/tests/test_benchcab.py index 5fdc370..12b5c69 100644 --- a/tests/test_benchcab.py +++ b/tests/test_benchcab.py @@ -1,4 +1,5 @@ """`pytest` tests for `benchcab.py`.""" + import re from contextlib import nullcontext as does_not_raise from unittest import mock diff --git a/tests/test_config.py b/tests/test_config.py index 1e98eeb..dcf306b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,6 +1,6 @@ """`pytest` tests for config.py.""" + import os -import re from contextlib import nullcontext as does_not_raise from pathlib import Path from pprint import pformat