From 6eb466428600db57fa132ebfe28ffe0a0fe3f97c Mon Sep 17 00:00:00 2001 From: mirpedrol Date: Wed, 14 Dec 2022 10:41:10 +0100 Subject: [PATCH] add json.dump into a helper function --- nf_core/launch.py | 6 ++---- nf_core/lint_utils.py | 12 ++++++++++++ nf_core/modules/modules_json.py | 7 ++----- nf_core/schema.py | 6 ++---- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/nf_core/launch.py b/nf_core/launch.py index 0facca72dc..87150172f7 100644 --- a/nf_core/launch.py +++ b/nf_core/launch.py @@ -17,7 +17,7 @@ import nf_core.schema import nf_core.utils -from nf_core.lint_utils import run_prettier_on_file +from nf_core.lint_utils import dump_json_with_prettier log = logging.getLogger(__name__) @@ -701,9 +701,7 @@ def build_command(self): # Write the user selection to a file and run nextflow with that if self.use_params_file: - with open(self.params_out, "w") as fp: - json.dump(self.schema_obj.input_params, fp, indent=4) - run_prettier_on_file(self.params_out) + dump_json_with_prettier(self.params_out, self.schema_obj.input_params) self.nextflow_cmd += f' -params-file "{os.path.relpath(self.params_out)}"' # Call nextflow with a list of command line flags diff --git a/nf_core/lint_utils.py b/nf_core/lint_utils.py index f8ca563b00..c2fd75d375 100644 --- a/nf_core/lint_utils.py +++ b/nf_core/lint_utils.py @@ -1,3 +1,4 @@ +import json import logging import subprocess from pathlib import Path @@ -82,3 +83,14 @@ def run_prettier_on_file(file): "There was an error running the prettier pre-commit hook.\n" f"STDOUT: {e.stdout.decode()}\nSTDERR: {e.stderr.decode()}" ) + + +def dump_json_with_prettier(file_name, file_content): + """Dump a JSON file and run prettier on it. + Args: + file_name (Path | str): A file identifier as a string or pathlib.Path. + file_content (dict): Content to dump into the JSON file + """ + with open(file_name, "w") as fh: + json.dump(file_content, fh, indent=4) + run_prettier_on_file(file_name) diff --git a/nf_core/modules/modules_json.py b/nf_core/modules/modules_json.py index cda7c827dc..a1609e5fee 100644 --- a/nf_core/modules/modules_json.py +++ b/nf_core/modules/modules_json.py @@ -13,7 +13,7 @@ import nf_core.utils from nf_core.components.components_utils import get_components_to_install -from nf_core.lint_utils import run_prettier_on_file +from nf_core.lint_utils import dump_json_with_prettier from nf_core.modules.modules_repo import ( NF_CORE_MODULES_NAME, NF_CORE_MODULES_REMOTE, @@ -1023,10 +1023,7 @@ def dump(self): """ # Sort the modules.json self.modules_json["repos"] = nf_core.utils.sort_dictionary(self.modules_json["repos"]) - with open(self.modules_json_path, "w") as fh: - json.dump(self.modules_json, fh, indent=4) - fh.write("\n") - run_prettier_on_file(self.modules_json_path) + dump_json_with_prettier(self.modules_json_path, self.modules_json) def resolve_missing_installation(self, missing_installation, component_type): missing_but_in_mod_json = [ diff --git a/nf_core/schema.py b/nf_core/schema.py index 92cbac852d..04b17a9045 100644 --- a/nf_core/schema.py +++ b/nf_core/schema.py @@ -16,7 +16,7 @@ import nf_core.list import nf_core.utils -from nf_core.lint_utils import run_prettier_on_file +from nf_core.lint_utils import dump_json_with_prettier log = logging.getLogger(__name__) @@ -171,9 +171,7 @@ def save_schema(self, suppress_logging=False): num_params += sum(len(d.get("properties", {})) for d in self.schema.get("definitions", {}).values()) if not suppress_logging: log.info(f"Writing schema with {num_params} params: '{self.schema_filename}'") - with open(self.schema_filename, "w") as fh: - json.dump(self.schema, fh, indent=4) - run_prettier_on_file(self.schema_filename) + dump_json_with_prettier(self.schema_filename, self.schema) def load_input_params(self, params_path): """Load a given a path to a parameters file (JSON/YAML)