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

Lazy open output files, and always close them #314

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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