From 7150ca23d039cdebaa123bfc0e0535c4ac18bfea Mon Sep 17 00:00:00 2001
From: Conor Finn <conor.finn@stfc.ac.uk>
Date: Tue, 17 Dec 2019 09:19:48 +0000
Subject: [PATCH] RE #27187 Use save location in GUI functionality

---
 .../settings/settings_model.py                     | 14 +++++++-------
 .../settings/settings_presenter.py                 | 13 ++++++++-----
 .../tabs/calibration/model.py                      |  8 +++-----
 .../tabs/common/path_handling.py                   |  9 ++++++++-
 .../tabs/common/test/test_vanadium_corrections.py  | 10 ++++++----
 .../tabs/common/vanadium_corrections.py            |  4 ++--
 .../engineering_diffraction/tabs/focus/model.py    | 12 ++++++------
 .../tabs/focus/test/test_focus_model.py            |  2 +-
 8 files changed, 41 insertions(+), 31 deletions(-)

diff --git a/scripts/Engineering/gui/engineering_diffraction/settings/settings_model.py b/scripts/Engineering/gui/engineering_diffraction/settings/settings_model.py
index 8c197fcb4d6..b605a24cf98 100644
--- a/scripts/Engineering/gui/engineering_diffraction/settings/settings_model.py
+++ b/scripts/Engineering/gui/engineering_diffraction/settings/settings_model.py
@@ -8,12 +8,10 @@
 from __future__ import (absolute_import, division, print_function)
 
 from Engineering.gui.engineering_diffraction.settings.settings_helper import get_setting, set_setting
+from Engineering.gui.engineering_diffraction.tabs.common import path_handling
 
 
 class SettingsModel(object):
-    INTERFACES_SETTINGS_GROUP = "CustomInterfaces"
-    ENGINEERING_PREFIX = "EngineeringDiffraction2/"
-
     def get_settings_dict(self, keys):
         settings = {}
         for setting_name in keys:
@@ -24,8 +22,10 @@ class SettingsModel(object):
         for key in settings:
             self.set_setting(key, settings[key])
 
-    def get_setting(self, name):
-        return get_setting(self.INTERFACES_SETTINGS_GROUP, self.ENGINEERING_PREFIX, name)
+    @staticmethod
+    def get_setting(name):
+        return get_setting(path_handling.INTERFACES_SETTINGS_GROUP, path_handling.ENGINEERING_PREFIX, name)
 
-    def set_setting(self, name, value):
-        set_setting(self.INTERFACES_SETTINGS_GROUP, self.ENGINEERING_PREFIX, name, value)
\ No newline at end of file
+    @staticmethod
+    def set_setting(name, value):
+        set_setting(path_handling.INTERFACES_SETTINGS_GROUP, path_handling.ENGINEERING_PREFIX, name, value)
diff --git a/scripts/Engineering/gui/engineering_diffraction/settings/settings_presenter.py b/scripts/Engineering/gui/engineering_diffraction/settings/settings_presenter.py
index 380cfdef549..0b12d3fb1b2 100644
--- a/scripts/Engineering/gui/engineering_diffraction/settings/settings_presenter.py
+++ b/scripts/Engineering/gui/engineering_diffraction/settings/settings_presenter.py
@@ -70,8 +70,11 @@ class SettingsPresenter(object):
 
     @staticmethod
     def _validate_settings(settings):
-        all_keys = settings.keys() == SETTINGS_LIST
-        save_location = str(settings["save_location"])
-        save_valid = save_location is not "" and save_location is not None
-        recalc_valid = settings["recalc_vanadium"] is not None
-        return all_keys and save_valid and recalc_valid
+        try:
+            all_keys = settings.keys() == SETTINGS_LIST
+            save_location = str(settings["save_location"])
+            save_valid = save_location is not "" and save_location is not None
+            recalc_valid = settings["recalc_vanadium"] is not None
+            return all_keys and save_valid and recalc_valid
+        except KeyError:  # Settings contained invalid key.
+            return False
diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/model.py b/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/model.py
index 6616e236729..560ccd15ff3 100644
--- a/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/model.py
+++ b/scripts/Engineering/gui/engineering_diffraction/tabs/calibration/model.py
@@ -24,8 +24,6 @@ CURVES_WORKSPACE_NAME = "engggui_vanadium_curves"
 INTEGRATED_WORKSPACE_NAME = "engggui_vanadium_integration"
 CALIB_PARAMS_WORKSPACE_NAME = "engggui_calibration_banks_parameters"
 
-CALIBRATION_DIR = path.join(path_handling.OUT_FILES_ROOT_DIR, "Calibration", "")
-
 NORTH_BANK_TEMPLATE_FILE = "template_ENGINX_241391_236516_North_bank.prm"
 SOUTH_BANK_TEMPLATE_FILE = "template_ENGINX_241391_236516_South_bank.prm"
 
@@ -64,10 +62,10 @@ class CalibrationModel(object):
             params_table.append([i, difc[i], 0.0, tzero[i]])
         self.update_calibration_params_table(params_table)
 
-        self.create_output_files(CALIBRATION_DIR, difc, tzero, sample_path, vanadium_path,
-                                 instrument)
+        calib_dir = path.join(path_handling.get_output_path(), "Calibration", "")
+        self.create_output_files(calib_dir, difc, tzero, sample_path, vanadium_path, instrument)
         if rb_num:
-            user_calib_dir = path.join(path_handling.OUT_FILES_ROOT_DIR, "User", rb_num,
+            user_calib_dir = path.join(path_handling.get_output_path(), "User", rb_num,
                                        "Calibration", "")
             self.create_output_files(user_calib_dir, difc, tzero, sample_path, vanadium_path,
                                      instrument)
diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/common/path_handling.py b/scripts/Engineering/gui/engineering_diffraction/tabs/common/path_handling.py
index 35da5482bfb..76dfa0d6379 100644
--- a/scripts/Engineering/gui/engineering_diffraction/tabs/common/path_handling.py
+++ b/scripts/Engineering/gui/engineering_diffraction/tabs/common/path_handling.py
@@ -8,9 +8,16 @@
 from __future__ import (absolute_import, division, print_function)
 
 from os import path
+from Engineering.gui.engineering_diffraction.settings.settings_helper import get_setting
 
-OUT_FILES_ROOT_DIR = path.join(path.expanduser("~"), "Engineering_Mantid")
+INTERFACES_SETTINGS_GROUP = "CustomInterfaces"
+ENGINEERING_PREFIX = "EngineeringDiffraction2/"
 
 
 def get_run_number_from_path(run_path, instrument):
     return path.splitext(path.basename(run_path))[0].replace(instrument, '').lstrip('0')
+
+
+def get_output_path():
+    location = get_setting(INTERFACES_SETTINGS_GROUP, ENGINEERING_PREFIX, "save_location")
+    return location if location is not None else ""
diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/common/test/test_vanadium_corrections.py b/scripts/Engineering/gui/engineering_diffraction/tabs/common/test/test_vanadium_corrections.py
index 9fa95031234..9468b3183a8 100644
--- a/scripts/Engineering/gui/engineering_diffraction/tabs/common/test/test_vanadium_corrections.py
+++ b/scripts/Engineering/gui/engineering_diffraction/tabs/common/test/test_vanadium_corrections.py
@@ -59,17 +59,19 @@ class VanadiumCorrectionsTest(unittest.TestCase):
         self.assertEqual(0, van_correction.call_count)
         self.assertEqual(0, save.call_count)
 
+    @patch(dir_path + ".vanadium_corrections.path_handling.get_output_path")
     @patch(dir_path + ".vanadium_corrections.makedirs")
-    def test_file_path_generation(self, makedirs):
+    def test_file_path_generation(self, makedirs, out_path):
+        out_path.return_value = path.join(path.expanduser("~"), "Test_Directory")
         vanadium_run_number = "1234"
-        engineering_path = path.join(path.expanduser("~"), "Engineering_Mantid")
+        engineering_path = path.join(path.expanduser("~"), "Test_Directory")
         if path.exists(engineering_path):
             rmtree(engineering_path)
         output = vanadium_corrections._generate_saved_workspace_file_paths(vanadium_run_number)
         self.assertEqual(output,
-                         (path.join(path.expanduser("~"), "Engineering_Mantid", "Vanadium_Runs",
+                         (path.join(path.expanduser("~"), "Test_Directory", "Vanadium_Runs",
                                     "1234_precalculated_vanadium_run_integration.nxs"),
-                          path.join(path.expanduser("~"), "Engineering_Mantid", "Vanadium_Runs",
+                          path.join(path.expanduser("~"), "Test_Directory", "Vanadium_Runs",
                                     "1234_precalculated_vanadium_run_bank_curves.nxs")))
         self.assertEqual(1, makedirs.call_count)
 
diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/common/vanadium_corrections.py b/scripts/Engineering/gui/engineering_diffraction/tabs/common/vanadium_corrections.py
index 2f79c91c499..5e8a6f61974 100644
--- a/scripts/Engineering/gui/engineering_diffraction/tabs/common/vanadium_corrections.py
+++ b/scripts/Engineering/gui/engineering_diffraction/tabs/common/vanadium_corrections.py
@@ -113,10 +113,10 @@ def _generate_saved_workspace_file_paths(vanadium_number, rb_num=""):
     integrated_filename = vanadium_number + SAVED_FILE_INTEG_SUFFIX
     curves_filename = vanadium_number + SAVED_FILE_CURVE_SUFFIX
     if rb_num:
-        vanadium_dir = path.join(path_handling.OUT_FILES_ROOT_DIR, "User", rb_num,
+        vanadium_dir = path.join(path_handling.get_output_path(), "User", rb_num,
                                  VANADIUM_DIRECTORY_NAME)
     else:
-        vanadium_dir = path.join(path_handling.OUT_FILES_ROOT_DIR, VANADIUM_DIRECTORY_NAME)
+        vanadium_dir = path.join(path_handling.get_output_path(), VANADIUM_DIRECTORY_NAME)
     if not path.exists(vanadium_dir):
         makedirs(vanadium_dir)
     return path.join(vanadium_dir, integrated_filename), path.join(vanadium_dir, curves_filename)
diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/model.py b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/model.py
index 9b67f260aba..504802a0e95 100644
--- a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/model.py
+++ b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/model.py
@@ -104,36 +104,36 @@ class FocusModel(object):
     def _save_focused_output_files_as_gss(self, instrument, sample_path, bank, sample_workspace,
                                           rb_num):
         gss_output_path = path.join(
-            path_handling.OUT_FILES_ROOT_DIR, "Focus",
+            path_handling.get_output_path(), "Focus",
             self._generate_output_file_name(instrument, sample_path, bank, ".gss"))
         SaveGSS(InputWorkspace=sample_workspace, Filename=gss_output_path)
         if rb_num is not None:
             gss_output_path = path.join(
-                path_handling.OUT_FILES_ROOT_DIR, "User", rb_num, "Focus",
+                path_handling.get_output_path(), "User", rb_num, "Focus",
                 self._generate_output_file_name(instrument, sample_path, bank, ".gss"))
             SaveGSS(InputWorkspace=sample_workspace, Filename=gss_output_path)
 
     def _save_focused_output_files_as_nexus(self, instrument, sample_path, bank, sample_workspace,
                                             rb_num):
         nexus_output_path = path.join(
-            path_handling.OUT_FILES_ROOT_DIR, "Focus",
+            path_handling.get_output_path(), "Focus",
             self._generate_output_file_name(instrument, sample_path, bank, ".nxs"))
         SaveNexus(InputWorkspace=sample_workspace, Filename=nexus_output_path)
         if rb_num is not None:
             nexus_output_path = path.join(
-                path_handling.OUT_FILES_ROOT_DIR, "User", rb_num, "Focus",
+                path_handling.get_output_path(), "User", rb_num, "Focus",
                 self._generate_output_file_name(instrument, sample_path, bank, ".nxs"))
             SaveNexus(InputWorkspace=sample_workspace, Filename=nexus_output_path)
 
     def _save_focused_output_files_as_xye(self, instrument, sample_path, bank, sample_workspace,
                                           rb_num):
         xye_output_path = path.join(
-            path_handling.OUT_FILES_ROOT_DIR, "Focus",
+            path_handling.get_output_path(), "Focus",
             self._generate_output_file_name(instrument, sample_path, bank, ".dat"))
         SaveFocusedXYE(InputWorkspace=sample_workspace, Filename=xye_output_path, SplitFiles=False)
         if rb_num is not None:
             xye_output_path = path.join(
-                path_handling.OUT_FILES_ROOT_DIR, "User", rb_num, "Focus",
+                path_handling.get_output_path(), "User", rb_num, "Focus",
                 self._generate_output_file_name(instrument, sample_path, bank, ".dat"))
             SaveFocusedXYE(InputWorkspace=sample_workspace,
                            Filename=xye_output_path,
diff --git a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_focus_model.py b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_focus_model.py
index 53b8456052a..dde10ad71e5 100644
--- a/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_focus_model.py
+++ b/scripts/Engineering/gui/engineering_diffraction/tabs/focus/test/test_focus_model.py
@@ -83,7 +83,7 @@ class FocusModelTest(unittest.TestCase):
     @patch(file_path + ".SaveNexus")
     def test_save_output_files_with_no_RB_number(self, nexus, gss, xye):
         mocked_workspace = "mocked-workspace"
-        output_file = path.join(path_handling.OUT_FILES_ROOT_DIR, "Focus",
+        output_file = path.join(path_handling.get_output_path(), "Focus",
                                 "ENGINX_123_bank_North.nxs")
 
         self.model._save_output("ENGINX", "Path/To/ENGINX000123.whatever", "North",
-- 
GitLab