Use context manager instead of plain open (#1211)

Context manager with open closes the files after usage.

When the object is already a pathlib.Path i used read_text
write_text functions

In some cases pathlib.Path.open were used in context manager,
it is basically the same as builtin open.

Thanks, Konstantin Baikov!
This commit is contained in:
Konstantin Baikov 2021-03-11 17:15:49 +01:00 committed by GitHub
commit 8e18c79431
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 93 additions and 63 deletions

View file

@ -141,9 +141,11 @@ def publish_subcommand(publish):
if show_files:
if os.path.exists("metadata.json"):
print("=== metadata.json ===\n")
print(open("metadata.json").read())
with open("metadata.json") as fp:
print(fp.read())
print("\n==== Dockerfile ====\n")
print(open("Dockerfile").read())
with open("Dockerfile") as fp:
print(fp.read())
print("\n====================\n")
image_id = f"gcr.io/{project}/{name}"

View file

@ -171,9 +171,11 @@ def temporary_heroku_directory(
os.chdir(tmp.name)
if metadata_content:
open("metadata.json", "w").write(json.dumps(metadata_content, indent=2))
with open("metadata.json", "w") as fp:
fp.write(json.dumps(metadata_content, indent=2))
open("runtime.txt", "w").write("python-3.8.7")
with open("runtime.txt", "w") as fp:
fp.write("python-3.8.7")
if branch:
install = [
@ -182,11 +184,11 @@ def temporary_heroku_directory(
else:
install = ["datasette"] + list(install)
open("requirements.txt", "w").write("\n".join(install))
with open("requirements.txt", "w") as fp:
fp.write("\n".join(install))
os.mkdir("bin")
open("bin/post_compile", "w").write(
"datasette inspect --inspect-file inspect-data.json"
)
with open("bin/post_compile", "w") as fp:
fp.write("datasette inspect --inspect-file inspect-data.json")
extras = []
if template_dir:
@ -218,7 +220,8 @@ def temporary_heroku_directory(
procfile_cmd = "web: datasette serve --host 0.0.0.0 {quoted_files} --cors --port $PORT --inspect-file inspect-data.json {extras}".format(
quoted_files=quoted_files, extras=" ".join(extras)
)
open("Procfile", "w").write(procfile_cmd)
with open("Procfile", "w") as fp:
fp.write(procfile_cmd)
for path, filename in zip(file_paths, file_names):
link_or_copy(path, os.path.join(tmp.name, filename))