Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non-interactive mode to build-schema module #416

Merged
merged 9 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Code contributions to the release:
- Auto-fill tax_id and host_disease based on organism fields [#407](https://github.com/BU-ISCIII/relecov-tools/pull/407)
- Implement Pull Request Template [#410](https://github.com/BU-ISCIII/relecov-tools/pull/410)
- Now pipeline-manager splits by organism-template first [#412](https://github.com/BU-ISCIII/relecov-tools/pull/412)
- Implement non-interactive execution of build-schema module [#416](https://github.com/BU-ISCIII/relecov-tools/pull/416)

#### Fixes

Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,13 @@ Options:
-v, --draft_version TEXT Version of the JSON schema specification to be
used. Example: '2020-12'. See: https://json-
schema.org/specification-links
--version Specifies the version of the metadata template to generate.
-d, --diff BOOLEAN Prints a changelog/diff between the base and
--version TEXT Specifies the version of the metadata template to
generate.
-p, --project TEXT Specify the project you want to generate the schema
and template for.
--non-interactive BOOLEAN executes the module by assigning the default interactive
parameters
-d, --diff BOOLEAN Prints a changelog/diff between the base and
incoming versions of the schema. Required for the generation
of the JSON schema.
-o, --out_dir PATH Path to save output file/s
Expand Down
23 changes: 21 additions & 2 deletions relecov_tools/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,15 +710,34 @@ def pipeline_manager(input, templates_root, output, config, folder_names):
@click.option(
"-p", "--project", help="Specficy the project to build the metadata template."
)
@click.option(
"--non-interactive",
is_flag=True,
help="Run the script without user interaction, using default values.",
)
@click.option("-o", "--out_dir", type=click.Path(), help="Path to save output file/s")
def build_schema(
input_file, schema_base, draft_version, diff, out_dir, version, project
input_file,
schema_base,
draft_version,
diff,
out_dir,
version,
project,
non_interactive,
):
"""Generates and updates JSON Schema files from Excel-based database definitions."""
# Build new schema
try:
schema_update = relecov_tools.build_schema.SchemaBuilder(
input_file, schema_base, draft_version, diff, out_dir, version, project
input_file,
schema_base,
draft_version,
diff,
out_dir,
version,
project,
non_interactive,
)

# Build new schema
Expand Down
43 changes: 31 additions & 12 deletions relecov_tools/build_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ def __init__(
out_dir=None,
version=None,
project=None,
non_interactive=False,
):
"""
Initialize the SchemaBuilder class. This class generates a JSON Schema file based on the provided draft version.
It reads the database definition from an Excel file and allows customization of the schema generation process.
"""
self.excel_file_path = excel_file_path
self.non_interactive = non_interactive
# Validate input data
if not self.excel_file_path or not os.path.isfile(self.excel_file_path):
log.error("A valid Excel file path must be provided.")
Expand Down Expand Up @@ -111,11 +113,19 @@ def __init__(
self.show_diff = True

# Validate json schema draft version
self.draft_version = (
relecov_tools.assets.schema_utils.jsonschema_draft.check_valid_version(
draft_version
if not draft_version:
if self.non_interactive:
self.draft_version = "2020-12"
else:
self.draft_version = relecov_tools.assets.schema_utils.jsonschema_draft.check_valid_version(
draft_version
)
else:
self.draft_version = (
relecov_tools.assets.schema_utils.jsonschema_draft.check_valid_version(
draft_version
)
)
)

# Validate base schema
if base_schema_path is not None:
Expand Down Expand Up @@ -267,9 +277,11 @@ def validate_database_definition(self, json_data):
stderr.print(f"\t- Log errors saved to:\n\t{error_file_path}")

# Ask user whether to continue or stop execution
if not relecov_tools.utils.prompt_yn_question(
if self.non_interactive or relecov_tools.utils.prompt_yn_question(
"Errors found in database values. Do you want to continue? (Y/N)"
):
pass
else:
return log_errors
else:
stderr.print("[green]\t- Database validation passed")
Expand Down Expand Up @@ -608,8 +620,12 @@ def get_schema_diff(self, base_schema, new_schema):
def print_save_schema_diff(self, diff_lines=None):
# Set user's choices
choices = ["Print to standard output (stdout)", "Save to file", "Both"]
diff_output_choice = relecov_tools.utils.prompt_selection(
"How would you like to print the diff between schemes?:", choices
diff_output_choice = (
"Save to file"
if self.non_interactive
else relecov_tools.utils.prompt_selection(
"How would you like to print the diff between schemes?:", choices
)
)
if diff_output_choice in ["Print to standard output (stdout)", "Both"]:
for line in diff_lines:
Expand Down Expand Up @@ -671,8 +687,12 @@ def create_metadatalab_excel(self, json_schema):
try:
# Retrieve existing files in the output directory
output_files = os.listdir(self.output_folder)
notes_control_input = input(
"\033[93mEnter a note about changes made to the schema: \033[0m"
notes_control_input = (
"Auto-generated update"
if self.non_interactive
else input(
"\033[93mEnter a note about changes made to the schema: \033[0m"
)
)
# Identify existing template files
template_files = [
Expand Down Expand Up @@ -1175,10 +1195,9 @@ def handle_build_schema(self):
self.save_new_schema(new_schema_json)

# Create metadata lab template
promp_answ = relecov_tools.utils.prompt_yn_question(
if self.non_interactive or relecov_tools.utils.prompt_yn_question(
"Do you want to create a metadata lab file?:"
)
if promp_answ:
):
self.create_metadatalab_excel(new_schema_json)

# Return new schema
Expand Down
Loading