From 0e20963d9a947c99d55f562494dcd798d4a42ac2 Mon Sep 17 00:00:00 2001 From: Matthew Wells Date: Wed, 23 Oct 2024 11:10:15 -0500 Subject: [PATCH] updated cli error handling --- .gitignore | 3 ++- pyproject.toml | 4 ++-- src/mikrokondo_tools/__main__.py | 21 +++---------------- src/mikrokondo_tools/cli/__init__.py | 19 ++++++++++++++++- .../cli/samplesheet/__init__.py | 2 +- .../samplesheet/samplesheet.py | 17 ++++++++++----- 6 files changed, 38 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index f996fde..7f49961 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .pytest** .ruff_cache __pycache__ -.vscode \ No newline at end of file +.vscode +errors.txt \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 2da0a08..cec1acc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ requires-python = ">=3.8" license = "MIT" keywords = [] authors = [ - { name = "Matthew Wells", email = "mattwells9@shaw.ca" }, + { name = "Matthew Wells", email = "mattheww9514@gmail.com" }, ] classifiers = [ "Development Status :: 4 - Beta", @@ -36,7 +36,7 @@ Issues = "https://github.com/unknown/mikrokondo-tools/issues" Source = "https://github.com/unknown/mikrokondo-tools" [project.scripts] -mikrokondo-tools = "mikrokondo_tools.cli:mikrokondo_tools" +mikrokondo-tools = "mikrokondo_tools.cli:safe_entry_point" [tool.hatch.version] path = "src/mikrokondo_tools/__about__.py" diff --git a/src/mikrokondo_tools/__main__.py b/src/mikrokondo_tools/__main__.py index ec49d5a..89cddfc 100755 --- a/src/mikrokondo_tools/__main__.py +++ b/src/mikrokondo_tools/__main__.py @@ -1,25 +1,10 @@ -# SPDX-FileCopyrightText: 2024-present Matthew Wells +# SPDX-FileCopyrightText: 2024-present Matthew Wells # # SPDX-License-Identifier: MIT import sys -import traceback -import mikrokondo_tools.utils as u + if __name__ == "__main__": #from mikrokondo_tools.cli import mikrokondo_tools from mikrokondo_tools.cli import main - logger = u.get_logger(__name__) - - try: - main() - except Exception as e: - errors_out = "errors.txt" - logger.warning("Error encountered appending traceback to %s", errors_out) - with open(errors_out, 'r') as output: - output.write(traceback.format_exc()) - error_number = e.errno if hasattr(e, "errno") else -1 - SystemExit(error_number) - else: - logger.info("Program finished.") - - sys.exit(0) + sys.exit(main()) diff --git a/src/mikrokondo_tools/cli/__init__.py b/src/mikrokondo_tools/cli/__init__.py index 2e7c433..e0fa3b0 100755 --- a/src/mikrokondo_tools/cli/__init__.py +++ b/src/mikrokondo_tools/cli/__init__.py @@ -3,6 +3,9 @@ # SPDX-License-Identifier: MIT import click +import traceback +import mikrokondo_tools.utils as u + from mikrokondo_tools.__about__ import __version__ from mikrokondo_tools.cli.download import download from mikrokondo_tools.cli.samplesheet import samplesheet @@ -17,6 +20,20 @@ def mikrokondo_tools(): mikrokondo_tools.add_command(samplesheet) +def safe_entry_point(): + logger = u.get_logger(__name__) + try: + mikrokondo_tools(prog_name='mikrokondo-tools') + except Exception as e: + errors_out = "errors.txt" + logger.warning("Error encountered appending traceback to %s for debugging.", errors_out) + with open(errors_out, 'a') as output: + output.write(traceback.format_exc()) + error_number = e.errno if hasattr(e, "errno") else -1 + SystemExit(error_number) + else: + logger.info("Program finished.") def main(): - return mikrokondo_tools(prog_name='mikrokondo-tools') + return safe_entry_point + diff --git a/src/mikrokondo_tools/cli/samplesheet/__init__.py b/src/mikrokondo_tools/cli/samplesheet/__init__.py index 29d8128..2e55dd9 100644 --- a/src/mikrokondo_tools/cli/samplesheet/__init__.py +++ b/src/mikrokondo_tools/cli/samplesheet/__init__.py @@ -21,4 +21,4 @@ def samplesheet(output_sheet, read_1, read_2, input_directory, schema_input): data = ss.get_samples(p.Path(input_directory)) ngs_data = ss.NGSData(data[0], data[1], read_1, read_2, output_sheet, schema_input) - return ngs_data.create_sample_sheet() \ No newline at end of file + ngs_data.create_sample_sheet() \ No newline at end of file diff --git a/src/mikrokondo_tools/samplesheet/samplesheet.py b/src/mikrokondo_tools/samplesheet/samplesheet.py index 740b6fb..15567ee 100644 --- a/src/mikrokondo_tools/samplesheet/samplesheet.py +++ b/src/mikrokondo_tools/samplesheet/samplesheet.py @@ -30,6 +30,9 @@ class DuplicateFilesException(Exception): class MissingSchemaException(Exception): pass +class NoFilesFoundException(Exception): + pass + @dataclass class SampleRow: sample: str @@ -189,10 +192,11 @@ def organize_data(self) -> t.Dict[str, t.List[SampleRow]]: pe_reads, se_reads, assemblies = self.get_ngs_data() sample_sheet: t.Dict[str, t.List[SampleRow]] = dict() - for k, v in pe_reads.items(): - sample_sheet[k] = [] - for idx in range(len(v[0])): - sample_sheet[k].append(SampleRow(sample=k, fastq_1=v[0][idx], fastq_2=v[1][idx])) + if pe_reads: + for k, v in pe_reads.items(): + sample_sheet[k] = [] + for idx in range(len(v[0])): + sample_sheet[k].append(SampleRow(sample=k, fastq_1=v[0][idx], fastq_2=v[1][idx])) if se_reads: self.update_sample_sheet_se(sample_sheet, se_reads.items(), SampleRow.longreads_key()) if assemblies: @@ -294,7 +298,7 @@ def get_samples(directory: p.Path) -> t.Tuple[t.List[p.Path], t.List[p.Path]]: try: sfx = file.suffixes[-2] # get second last file extension except IndexError: - logger.error("File: %s is inappropriately no other extension is present besides %s", file, sfx) + logger.error("File: %s is inappropriately named no other extension is present besides %s", file, sfx) sys.exit(-1) if sfx in __FASTQ_EXTENSIONS__: reads.append(file.absolute()) @@ -303,5 +307,8 @@ def get_samples(directory: p.Path) -> t.Tuple[t.List[p.Path], t.List[p.Path]]: else: logger.warning("Miscellaneous file present in sample directory: %s", file) + if not reads and not fastas: + logger.error("No files found in: %s", directory) + raise NoFilesFoundException return reads, fastas