From 349b558d09b1297c699fd82557c924b088e14c0b Mon Sep 17 00:00:00 2001
From: Thomas Kluyver <thomas@kluyver.me.uk>
Date: Thu, 9 Jul 2020 16:32:20 +0100
Subject: [PATCH] Use os.path functions instead of string formatting

---
 xfel_calibrate/calibrate.py | 29 ++++++++++++++---------------
 xfel_calibrate/settings.py  |  2 +-
 2 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/xfel_calibrate/calibrate.py b/xfel_calibrate/calibrate.py
index 52470a648..030a8063b 100755
--- a/xfel_calibrate/calibrate.py
+++ b/xfel_calibrate/calibrate.py
@@ -163,7 +163,7 @@ def extract_title_author_version(nb):
     # In case of a standard installation a version is stored
     # in the _version.py file
     try:
-        git_dir = '{}/../.git'.format(os.path.dirname(__file__))
+        git_dir = os.path.join(PKG_DIR, '..', '.git')
         FNULL = open(os.devnull, 'w')
         version = check_output(['git',
                                 '--git-dir={}'.format(git_dir),
@@ -276,7 +276,7 @@ def balance_sequences(in_folder, run, sequences, sequences_per_node,
     import glob
     import numpy as np
     if sequences[0] == -1:
-        path = "{}/r{:04d}/*{}-S*.h5".format(in_folder, run, path_inset)
+        path = os.path.join(in_folder, f"r{run:04d}", f"*{path_inset}-S*.h5")
         sequence_files = glob.glob(path)
         seq_nums = set()
         for sf in sequence_files:
@@ -319,11 +319,9 @@ def make_extended_parser() -> argparse.ArgumentParser:
         # The information is extracted from the first markdown cell of
         # the notebook.
         for caltype, notebook in det_notebooks.items():
-            nbpath = os.path.abspath(
-                "{}/{}".format(os.path.dirname(__file__), notebook["notebook"]))
-            with open(nbpath, "r") as f:
-                nb = nbformat.read(f, as_version=4)
-                msg += make_epilog(nb, caltype=caltype)
+            nbpath = os.path.join(PKG_DIR, notebook["notebook"])
+            nb = nbformat.read(nbpath, as_version=4)
+            msg += make_epilog(nb, caltype=caltype)
 
         return make_initial_parser(epilog=msg)
     elif len(sys.argv) <= 3:
@@ -528,7 +526,7 @@ def create_finalize_script(fmt_args, temp_path, job_list):
                     ''')
 
     fmt_args['joblist'] = job_list
-    f_name = "{}/finalize.sh".format(temp_path)
+    f_name = os.path.join(temp_path, "finalize.sh")
     with open(f_name, "w") as finfile:
         finfile.write(textwrap.dedent(tmpl.render(**fmt_args)))
 
@@ -546,7 +544,7 @@ def save_executed_command(run_tmp_path, version):
     :parm version: git version of the pycalibration package
     """
 
-    f_name = "{}/run_calibrate.sh".format(run_tmp_path)
+    f_name = os.path.join(run_tmp_path, "run_calibrate.sh")
     with open(f_name, "w") as finfile:
         finfile.write(f'# pycalibration version: {version}\n')
         finfile.write(' '.join(sys.argv))
@@ -638,7 +636,7 @@ def concurrent_run(temp_path, nb, nbname, args, cparm=None, cval=None,
     new_name = "{}__{}__{}.ipynb".format(
         os.path.basename(base_name), cparm, suffix)
 
-    nbpath = "{}/{}".format(temp_path, new_name)
+    nbpath = os.path.join(temp_path, new_name)
     with open(nbpath, "w") as f:
         f.write(nbconvert.exporters.export(
             nbconvert.NotebookExporter, new_nb)[0])
@@ -653,14 +651,14 @@ def concurrent_run(temp_path, nb, nbname, args, cparm=None, cval=None,
         srun_base = get_launcher_command(args, temp_path, dep_jids)
         print(" ".join(srun_base))
 
-    srun_base += [os.path.abspath("{}/bin/slurm_calibrate.sh".format(os.path.dirname(__file__))),  # path to helper sh
+    srun_base += [os.path.join(PKG_DIR, "bin", "slurm_calibrate.sh"),  # path to helper sh
                   os.path.abspath(nbpath),  # path to notebook
                   python_path,  # path to python
                   ipython_path,  # path to ipython
                   jupyter_path,  # path to jupyter
                   ipcluster_path,  # path to ipcluster
                   # python activate path
-                  activate_path if activate_path!="" else "{}/bin/activate.sh".format(os.path.dirname(__file__)),
+                  activate_path if activate_path!="" else os.path.join(PKG_DIR, "bin", "activate.sh"),
                   args.get("cluster_profile", "NO_CLUSTER"),
                   '"{}"'.format(base_name.upper()),
                   '"{}"'.format(args["detector"].upper()),
@@ -807,7 +805,7 @@ def run():
         cluster_profile = "slurm_prof_{}".format(run_uuid)
 
     # create a temporary output directory to work in
-    run_tmp_path = "{}/slurm_out_{}_{}_{}".format(temp_path, detector, caltype, run_uuid)
+    run_tmp_path = os.path.join(temp_path, f"slurm_out_{detector}_{caltype}_{run_uuid}")
     os.makedirs(run_tmp_path)
 
     # Write all input parameters to rst file to be included to final report
@@ -816,8 +814,9 @@ def run():
     save_executed_command(run_tmp_path, version)
 
     # wait on all jobs to run and then finalize the run by creating a report from the notebooks
-    out_path = "{}/{}/{}/{}".format(report_path, detector.upper(),
-                                    caltype.upper(), datetime.now().isoformat())
+    out_path = os.path.join(
+        report_path, detector.upper(), caltype.upper(), datetime.now().isoformat()
+    )
     if try_report_to_output:
         if "out_folder" in args:
             out_path = os.path.abspath(args["out_folder"])
diff --git a/xfel_calibrate/settings.py b/xfel_calibrate/settings.py
index fdf56f40c..cbe4c21b6 100644
--- a/xfel_calibrate/settings.py
+++ b/xfel_calibrate/settings.py
@@ -1,7 +1,7 @@
 import os
 
 # path into which temporary files from each run are placed
-temp_path = "{}/temp/".format(os.getcwd())
+temp_path = os.path.abspath("temp/")
 
 # Path to use for calling Python. If the environment is correctly set, simply the command
 python_path = "python"
-- 
GitLab