Skip to content

Commit

Permalink
Respond to review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TedBrookings committed Jul 27, 2023
1 parent 195b4b3 commit d8ab719
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
13 changes: 9 additions & 4 deletions fgpyo/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,11 @@ def write_lines(path: Path, lines_to_write: Iterable[Any]) -> None:


@contextmanager
def redirect_dev_null(
file_num: int = sys.stderr.fileno(),
) -> Generator[None, None, None]:
def redirect_to_dev_null(file_num: int) -> Generator[None, None, None]:
"""A context manager that redirects output of file handle to /dev/null
Args:
file_num: number of filehandle to redirect. Uses stderr by default
file_num: number of filehandle to redirect.
"""
# open /dev/null for writing
f_devnull = os.open(os.devnull, os.O_RDWR)
Expand All @@ -221,3 +219,10 @@ def redirect_dev_null(
# restore file descriptor and close devnull
os.dup2(save_stderr, file_num)
os.close(f_devnull)


@contextmanager
def suppress_stderr() -> Generator[None, None, None]:
"""A context manager that redirects output of stderr to /dev/null"""
with redirect_to_dev_null(file_num=sys.stderr.fileno()):
yield
2 changes: 1 addition & 1 deletion fgpyo/sam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def _pysam_open(
kwargs["check_sq"] = False

# Open it alignment file, suppressing stderr in case index files are older than SAM file
with fgpyo.io.redirect_dev_null():
with fgpyo.io.suppress_stderr():
alignment_file = pysam.AlignmentFile(path, **kwargs)
# now restore stderr and return the alignment file
return alignment_file
Expand Down
2 changes: 1 addition & 1 deletion fgpyo/vcf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def reader(path: VcfPath) -> Generator[VcfReader, None, None]:
path: the path to a VCF, or an open file handle
"""
if isinstance(path, (str, Path, TextIO)):
with fgpyo.io.redirect_dev_null():
with fgpyo.io.suppress_stderr():
# to avoid spamming log about index older than vcf, redirect stderr to /dev/null: only
# when first opening the file
_reader = VariantFile(path, mode="r") # type: ignore
Expand Down

0 comments on commit d8ab719

Please sign in to comment.