Skip to content

Commit

Permalink
Merge pull request #11 from DOED-DAAD/dev
Browse files Browse the repository at this point in the history
updated cli error handling
  • Loading branch information
mattheww95 authored Oct 23, 2024
2 parents e2a95b6 + 0e20963 commit 4a1586e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.pytest**
.ruff_cache
__pycache__
.vscode
.vscode
errors.txt
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ requires-python = ">=3.8"
license = "MIT"
keywords = []
authors = [
{ name = "Matthew Wells", email = "[email protected]" },
{ name = "Matthew Wells", email = "[email protected]" },
]
classifiers = [
"Development Status :: 4 - Beta",
Expand All @@ -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"
Expand Down
21 changes: 3 additions & 18 deletions src/mikrokondo_tools/__main__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
# SPDX-FileCopyrightText: 2024-present Matthew Wells <[email protected]>
# SPDX-FileCopyrightText: 2024-present Matthew Wells <[email protected]>
#
# 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())
19 changes: 18 additions & 1 deletion src/mikrokondo_tools/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

2 changes: 1 addition & 1 deletion src/mikrokondo_tools/cli/samplesheet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
ngs_data.create_sample_sheet()
17 changes: 12 additions & 5 deletions src/mikrokondo_tools/samplesheet/samplesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class DuplicateFilesException(Exception):
class MissingSchemaException(Exception):
pass

class NoFilesFoundException(Exception):
pass

@dataclass
class SampleRow:
sample: str
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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())
Expand All @@ -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

0 comments on commit 4a1586e

Please sign in to comment.