From 967230c8aaf7da5f51d284cfc0918662bff733e9 Mon Sep 17 00:00:00 2001
From: karnem <mikhail.karnevskiy@desy.de>
Date: Thu, 13 Jun 2019 14:31:00 +0200
Subject: [PATCH] Use os tools to work with file path

---
 cal_tools/cal_tools/tools.py | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/cal_tools/cal_tools/tools.py b/cal_tools/cal_tools/tools.py
index 7a7ca69be..320ab0897 100644
--- a/cal_tools/cal_tools/tools.py
+++ b/cal_tools/cal_tools/tools.py
@@ -62,34 +62,35 @@ def prepare_plots(run_path, threshold=1000000):
     Convert svg file to pdf or png to be used for latex
 
     Conversion of svg to vector graphics pdf is performed using svglib3.
-    This procedure is CPU consuming. In order to speeup process
+    This procedure is CPU consuming. In order to speed up process
     large svg files are converted to png format.
 
-    Link in rst files to converted image files are corrected respectively.
+    The links in the rst files are adapted accordingly to the
+    converted image files.
 
     :param run_path: Run path of the slurm job
-    :param threshold: Max svg file size to be converted to pdf
+    :param threshold: Max svg file size (in bytes) to be converted to pdf
     """
+    print('Convert svg to pdf and png')
     run_path = os.path.abspath(run_path)
     files = glob.glob('{}/*_files/*svg'.format(run_path))
 
     for f_path in files:
-        print(f_path, f_path[len(run_path):])
-        dir_name, f_name = f_path[len(run_path) + 1:].split('/')
+        dir_name = os.path.dirname(f_path)
+        f_name = os.path.basename(f_path)
 
         if (os.stat(f_path)).st_size < threshold:
             check_call(["svg2pdf", "{}".format(f_path)], shell=False)
-            new_f_name = f_name[:-4] + '.pdf'
+            new_f_name = os.path.splitext(f_name)[0] + '.pdf'
         else:
             check_call(["convert", "{}".format(f_path),
                         "{}.png".format(f_path[:-4])], shell=False)
-            new_f_name = f_name[:-4] + '.png'
+            new_f_name = os.path.splitext(f_name)[0] + '.png'
 
-        print('{} converted to {}'.format(f_name, new_f_name))
         check_call(["sed",
                     "-i",
                     "s/{}/{}/g".format(f_name, new_f_name),
-                    "{}/{}.rst".format(run_path, dir_name[:-6])],
+                    "{}.rst".format(dir_name[:-6])],
                    shell=False)
 
 
-- 
GitLab