Replace os with pathlib where paths are used (#1984)

This commit is contained in:
Ayaz Salikhov
2023-08-26 17:41:40 +04:00
committed by GitHub
parent 6aded4bc1d
commit 0d8b4e4987
3 changed files with 12 additions and 15 deletions

View File

@@ -4,6 +4,7 @@
import os import os
import stat import stat
import subprocess import subprocess
from pathlib import Path
from jupyter_core.paths import jupyter_data_dir from jupyter_core.paths import jupyter_data_dir
@@ -24,15 +25,14 @@ distinguished_name = req_distinguished_name
[req_distinguished_name] [req_distinguished_name]
""" """
if "GEN_CERT" in os.environ: if "GEN_CERT" in os.environ:
dir_name = jupyter_data_dir() dir_name = Path(jupyter_data_dir())
pem_file = os.path.join(dir_name, "notebook.pem") dir_name.mkdir(parents=True, exist_ok=True)
os.makedirs(dir_name, exist_ok=True) pem_file = dir_name / "notebook.pem"
# Generate an openssl.cnf file to set the distinguished name # Generate an openssl.cnf file to set the distinguished name
cnf_file = os.path.join(os.getenv("CONDA_DIR", "/usr/lib"), "ssl", "openssl.cnf") cnf_file = Path(os.getenv("CONDA_DIR", "/usr/lib")) / "ssl/openssl.cnf"
if not os.path.isfile(cnf_file): if not cnf_file.exists():
with open(cnf_file, "w") as fh: cnf_file.write_text(OPENSSL_CONFIG)
fh.write(OPENSSL_CONFIG)
# Generate a certificate if one doesn't exist on disk # Generate a certificate if one doesn't exist on disk
subprocess.check_call( subprocess.check_call(
@@ -50,8 +50,8 @@ if "GEN_CERT" in os.environ:
] ]
) )
# Restrict access to the file # Restrict access to the file
os.chmod(pem_file, stat.S_IRUSR | stat.S_IWUSR) pem_file.chmod(stat.S_IRUSR | stat.S_IWUSR)
c.ServerApp.certfile = pem_file c.ServerApp.certfile = str(pem_file)
# Change default umask for all subprocesses of the Server if set in the environment # Change default umask for all subprocesses of the Server if set in the environment
if "NB_UMASK" in os.environ: if "NB_UMASK" in os.environ:

View File

@@ -1,8 +1,6 @@
# Matplotlib: Create a simple plot example. # Matplotlib: Create a simple plot example.
# Refs: https://matplotlib.org/stable/gallery/lines_bars_and_markers/simple_plot.html # Refs: https://matplotlib.org/stable/gallery/lines_bars_and_markers/simple_plot.html
import os
# Optional test with [Matplotlib Jupyter Integration](https://github.com/matplotlib/ipympl) # Optional test with [Matplotlib Jupyter Integration](https://github.com/matplotlib/ipympl)
# %matplotlib widget # %matplotlib widget
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
@@ -21,7 +19,8 @@ ax.set(
title="About as simple as it gets, folks", title="About as simple as it gets, folks",
) )
ax.grid() ax.grid()
# Note that the test can be run headless by checking if an image is produced # Note that the test can be run headless by checking if an image is produced
file_path = os.path.join("/tmp", "test.png") file_path = "/tmp/test.png"
fig.savefig(file_path) fig.savefig(file_path)
print(f"File {file_path} saved") print(f"File {file_path} saved")

View File

@@ -1,6 +1,4 @@
# Matplotlib: Test tex fonts # Matplotlib: Test tex fonts
import os
import matplotlib import matplotlib
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
@@ -22,6 +20,6 @@ y = [1, 2]
ax.plot(x, y, label="a label") ax.plot(x, y, label="a label")
ax.legend(fontsize=15) ax.legend(fontsize=15)
file_path = os.path.join("/tmp", "test_fonts.png") file_path = "/tmp/test_fonts.png"
fig.savefig(file_path) fig.savefig(file_path)
print(f"File {file_path} saved") print(f"File {file_path} saved")