From 0d8b4e498766fb451e3ba08f9ade9da344db5d83 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Sat, 26 Aug 2023 17:41:40 +0400 Subject: [PATCH] Replace os with pathlib where paths are used (#1984) --- images/base-notebook/jupyter_server_config.py | 18 +++++++++--------- .../data/matplotlib/matplotlib_1.py | 5 ++--- .../data/matplotlib/matplotlib_fonts_1.py | 4 +--- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/images/base-notebook/jupyter_server_config.py b/images/base-notebook/jupyter_server_config.py index c4c501478b..5c13308ac0 100644 --- a/images/base-notebook/jupyter_server_config.py +++ b/images/base-notebook/jupyter_server_config.py @@ -4,6 +4,7 @@ import os import stat import subprocess +from pathlib import Path from jupyter_core.paths import jupyter_data_dir @@ -24,15 +25,14 @@ [req_distinguished_name] """ if "GEN_CERT" in os.environ: - dir_name = jupyter_data_dir() - pem_file = os.path.join(dir_name, "notebook.pem") - os.makedirs(dir_name, exist_ok=True) + dir_name = Path(jupyter_data_dir()) + dir_name.mkdir(parents=True, exist_ok=True) + pem_file = dir_name / "notebook.pem" # Generate an openssl.cnf file to set the distinguished name - cnf_file = os.path.join(os.getenv("CONDA_DIR", "/usr/lib"), "ssl", "openssl.cnf") - if not os.path.isfile(cnf_file): - with open(cnf_file, "w") as fh: - fh.write(OPENSSL_CONFIG) + cnf_file = Path(os.getenv("CONDA_DIR", "/usr/lib")) / "ssl/openssl.cnf" + if not cnf_file.exists(): + cnf_file.write_text(OPENSSL_CONFIG) # Generate a certificate if one doesn't exist on disk subprocess.check_call( @@ -50,8 +50,8 @@ ] ) # Restrict access to the file - os.chmod(pem_file, stat.S_IRUSR | stat.S_IWUSR) - c.ServerApp.certfile = pem_file + pem_file.chmod(stat.S_IRUSR | stat.S_IWUSR) + c.ServerApp.certfile = str(pem_file) # Change default umask for all subprocesses of the Server if set in the environment if "NB_UMASK" in os.environ: diff --git a/tests/scipy-notebook/data/matplotlib/matplotlib_1.py b/tests/scipy-notebook/data/matplotlib/matplotlib_1.py index e1a0add681..8ccf369efd 100644 --- a/tests/scipy-notebook/data/matplotlib/matplotlib_1.py +++ b/tests/scipy-notebook/data/matplotlib/matplotlib_1.py @@ -1,8 +1,6 @@ # Matplotlib: Create a simple plot example. # 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) # %matplotlib widget import matplotlib.pyplot as plt @@ -21,7 +19,8 @@ title="About as simple as it gets, folks", ) ax.grid() + # 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) print(f"File {file_path} saved") diff --git a/tests/scipy-notebook/data/matplotlib/matplotlib_fonts_1.py b/tests/scipy-notebook/data/matplotlib/matplotlib_fonts_1.py index 951a3d87c4..8944f2b8ff 100644 --- a/tests/scipy-notebook/data/matplotlib/matplotlib_fonts_1.py +++ b/tests/scipy-notebook/data/matplotlib/matplotlib_fonts_1.py @@ -1,6 +1,4 @@ # Matplotlib: Test tex fonts -import os - import matplotlib import matplotlib.pyplot as plt @@ -22,6 +20,6 @@ ax.plot(x, y, label="a label") ax.legend(fontsize=15) -file_path = os.path.join("/tmp", "test_fonts.png") +file_path = "/tmp/test_fonts.png" fig.savefig(file_path) print(f"File {file_path} saved")