From 0c55d6bb7753795718ed83797bcf6d02b852233a Mon Sep 17 00:00:00 2001
From: Chandrasekhar Ramakrishnan <chandrasekhar.ramakrishnan@id.ethz.ch>
Date: Thu, 2 Mar 2017 17:39:35 +0100
Subject: [PATCH]  SSDM-4670: Implementing writing configs.

---
 src/python/OBis/obis/dm/config.py      | 40 ++++++++++++++++++++++
 src/python/OBis/obis/dm/config_test.py | 47 ++++++++++++++++++++------
 2 files changed, 76 insertions(+), 11 deletions(-)

diff --git a/src/python/OBis/obis/dm/config.py b/src/python/OBis/obis/dm/config.py
index 96ee469af84..3da8b7cc242 100644
--- a/src/python/OBis/obis/dm/config.py
+++ b/src/python/OBis/obis/dm/config.py
@@ -75,6 +75,12 @@ class ConfigEnv(object):
     def add_param(self, param):
         self.params[param.name] = param
 
+    def location_at_path(self, location_path):
+        location = self.locations
+        for path in location_path:
+            location = location[path]
+        return location
+
 
 def default_location_resolver(location):
     """Given a location, return a path"""
@@ -129,6 +135,30 @@ class ConfigResolver(object):
                     result[name] = val
         return result
 
+    def set_value_for_parameter(self, name, value, loc):
+        """Set the value for the parameter
+        :param name: Name of the parameter
+        :param loc: Either 'local' or 'global'
+        :return:
+        """
+        param = self.env.params[name]
+        location_config_dict = self.set_cache_value_for_parameter(param, value, loc)
+        if loc != 'global':
+            if param.private:
+                location_path = [loc, 'private']
+            else:
+                location_path = [loc, 'public']
+        else:
+            location_path = [loc]
+        location = self.env.location_at_path(location_path)
+        location_dir_path = self.location_resolver(location)
+        if not os.path.exists(location_dir_path):
+            print("Create {}".format(location_dir_path))
+            os.makedirs(location_dir_path)
+        config_path = os.path.join(location_dir_path, 'config.json')
+        with open(config_path, "w") as f:
+            json.dump(location_config_dict, f)
+
     def value_for_parameter(self, param, loc):
         config = self.location_cache[loc]
         if loc != 'global':
@@ -137,3 +167,13 @@ class ConfigResolver(object):
             else:
                 config = config['public']
         return config.get(param.name)
+
+    def set_cache_value_for_parameter(self, param, value, loc):
+        config = self.location_cache[loc]
+        if loc != 'global':
+            if param.private:
+                config = config['private']
+            else:
+                config = config['public']
+        config[param.name] = value
+        return config
diff --git a/src/python/OBis/obis/dm/config_test.py b/src/python/OBis/obis/dm/config_test.py
index c5cdd2981b4..81c9fd210a7 100644
--- a/src/python/OBis/obis/dm/config_test.py
+++ b/src/python/OBis/obis/dm/config_test.py
@@ -10,6 +10,7 @@ Copyright (c) 2017 Chandrasekhar Ramakrishnan. All rights reserved.
 """
 import json
 import os
+import shutil
 
 from . import config
 
@@ -19,24 +20,48 @@ def test_config_location_resolver():
     assert config.default_location_resolver(loc) == os.path.join(os.path.expanduser("~"), '.obis')
 
 
-def dummy_user_config_path():
+def user_config_test_data_path():
     return os.path.join(os.path.dirname(__file__), '..', 'test-data', 'user_config')
 
 
-def dummy_location_resolver(location):
-    if location.root == 'user_home':
-        return os.path.join(dummy_user_config_path(), location.basename)
-    else:
-        return config.default_location_resolver(location)
+def copy_user_config_test_data(tmpdir):
+    config_test_data_src = user_config_test_data_path()
+    config_test_data_dst = str(tmpdir.join(os.path.basename(config_test_data_src)))
+    shutil.copytree(config_test_data_src, config_test_data_dst)
+    return config_test_data_src, config_test_data_dst
 
 
-def test_read_config():
-    resolver = config.ConfigResolver(location_resolver=dummy_location_resolver)
+def location_resolver_for_test(tmpdir):
+    def resolver(location):
+        if location.root == 'user_home':
+            return os.path.join(str(tmpdir), 'user_config', location.basename)
+        else:
+            return config.default_location_resolver(location)
+
+    return resolver
+
+
+def test_read_config(tmpdir):
+    copy_user_config_test_data(tmpdir)
+    resolver = config.ConfigResolver(location_resolver=location_resolver_for_test(tmpdir))
+    config_dict = resolver.config_dict()
+    assert config_dict is not None
+    with open(os.path.join(user_config_test_data_path(), ".obis", "config.json")) as f:
+        expected_dict = json.load(f)
+    assert config_dict['user'] == expected_dict['user']
+
+
+def test_write_config(tmpdir):
+    copy_user_config_test_data(tmpdir)
+    resolver = config.ConfigResolver(location_resolver=location_resolver_for_test(tmpdir))
     config_dict = resolver.config_dict()
     assert config_dict is not None
-    print(config_dict)
-    with open(os.path.join(dummy_user_config_path(), ".obis", "config.json")) as f:
+    with open(os.path.join(user_config_test_data_path(), ".obis", "config.json")) as f:
         expected_dict = json.load(f)
+    assert config_dict['openbis_url'] == expected_dict['openbis_url']
     assert config_dict['user'] == expected_dict['user']
 
-# TODO Override the location resolver and test that we can write values
+    resolver.set_value_for_parameter('user', 'new_user', 'global')
+    config_dict = resolver.config_dict()
+    assert config_dict['openbis_url'] == expected_dict['openbis_url']
+    assert config_dict['user'] == 'new_user'
-- 
GitLab