Skip to content

Commit

Permalink
Lazy open output files, and always close them (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtmckee authored Jul 8, 2024
1 parent 6061b3e commit 09620a6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 6 additions & 2 deletions readme_renderer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def main(cli_args: Optional[List[str]] = None) -> None:
help="README format (inferred from input file name or package)")
parser.add_argument('input', help="Input README file or package name")
parser.add_argument('-o', '--output', help="Output file (default: stdout)",
type=argparse.FileType('w'), default='-')
default='-')
args = parser.parse_args(cli_args)

content_format = args.format
Expand Down Expand Up @@ -55,7 +55,11 @@ def main(cli_args: Optional[List[str]] = None) -> None:
"`rst`, or `txt`)")
if rendered is None:
sys.exit(1)
print(rendered, file=args.output)
if args.output == "-":
print(rendered, file=sys.stdout)
else:
with open(args.output, "w") as fp:
print(rendered, file=fp)


if __name__ == '__main__':
Expand Down
5 changes: 4 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ deps =
pytest
pytest-cov
pytest-icdiff
setenv =
# Display up to 20 frames in backtraces when showing ResourceWarnings.
PYTHONTRACEMALLOC=20
commands =
pytest --strict-markers --cov {posargs}
pytest -Wall --strict-markers --cov {posargs}
extras = md

[testenv:mypy]
Expand Down

0 comments on commit 09620a6

Please sign in to comment.