From 0aaee246568d0e3d3bf597d79420e010057f5657 Mon Sep 17 00:00:00 2001 From: Andreas Florath Date: Wed, 26 Dec 2012 22:54:09 +0100 Subject: [PATCH] Fixed UserPreferences basics --- rmtoo/lib/GenNonEmptyDict.py | 2 +- rmtoo/lib/UserPreferences.py | 27 +++++++++++++++++-- .../UnitTest/CoreTests/TestGenNonEmptyDict.py | 2 +- .../UnitTest/CoreTests/TestUserPreferences.py | 2 +- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/rmtoo/lib/GenNonEmptyDict.py b/rmtoo/lib/GenNonEmptyDict.py index bf491ed4..9e55eb8f 100644 --- a/rmtoo/lib/GenNonEmptyDict.py +++ b/rmtoo/lib/GenNonEmptyDict.py @@ -25,6 +25,6 @@ def insert(self, key, value): def __getitem__(self, key): '''Returns the key from the dict.''' if key not in self.__dict: - self.__dict[key] = self.__factory() + self.__dict[key] = self.__factory(key) return self.__dict[key] \ No newline at end of file diff --git a/rmtoo/lib/UserPreferences.py b/rmtoo/lib/UserPreferences.py index 551f392d..c2c28ad7 100644 --- a/rmtoo/lib/UserPreferences.py +++ b/rmtoo/lib/UserPreferences.py @@ -11,7 +11,25 @@ import os -class UserPreferences: +from rmtoo.lib.GenNonEmptyDict import GenNonEmptyDict + +class FileStorage(object): + '''Holds all the values which go in one file.''' + + def __init__(self, rel_filename): + '''Initializaes the FileStorage with the relative filename.''' + self.__rel_filename = rel_filename + self.__dict = {} + + def set_value(self, key, value): + '''Directly access the underlaying dict.''' + self.__dict[key] = value + + def get_value(self, key): + '''Directly access the underlaying dict.''' + return self.__dict[key] + +class UserPreferences(object): '''Global User Preferences handling. There are some global user preferences which must be stored system wide. This class with handle these things.''' @@ -33,6 +51,7 @@ def __init__(self, rmtoo_home_dir=None): '''Constructs a User Preferences object which can hold and store configuration values.''' self.__rmtoo_home_dir = self.__eval_rmtoo_home_dir(rmtoo_home_dir) + self.__file_storage = GenNonEmptyDict(FileStorage) def get_rmtoo_home_dir(self): '''Returns the used home directory.''' @@ -41,4 +60,8 @@ def get_rmtoo_home_dir(self): def set_value(self, filename, propname, value): '''Sets the property with the name 'propname' to the file 'filename' to the given value.''' - \ No newline at end of file + self.__file_storage[filename].set_value(propname, value) + + def get_value(self, filename, propname): + '''Gets the property from the file.''' + return self.__file_storage[filename].get_value(propname) \ No newline at end of file diff --git a/rmtoo/tests/UnitTest/CoreTests/TestGenNonEmptyDict.py b/rmtoo/tests/UnitTest/CoreTests/TestGenNonEmptyDict.py index ee1480dd..0c83fdcb 100644 --- a/rmtoo/tests/UnitTest/CoreTests/TestGenNonEmptyDict.py +++ b/rmtoo/tests/UnitTest/CoreTests/TestGenNonEmptyDict.py @@ -28,7 +28,7 @@ def test_int_only_existing(self): def test_int_not_existing(self): '''GenNonEmtpyDict: (int): not existing values''' - def string_was(): + def string_was(n): return "was" gned = GenNonEmptyDict(string_was) diff --git a/rmtoo/tests/UnitTest/CoreTests/TestUserPreferences.py b/rmtoo/tests/UnitTest/CoreTests/TestUserPreferences.py index 9e50d7af..4867ee89 100644 --- a/rmtoo/tests/UnitTest/CoreTests/TestUserPreferences.py +++ b/rmtoo/tests/UnitTest/CoreTests/TestUserPreferences.py @@ -30,6 +30,6 @@ def test_add_new_value(self): '''UserPreferences: add new value to preferences.''' mid = "ThisIsMyNotSoAutomatedGeneratedId" up = UserPreferences() - up.add_value("stats", "id", mid) + up.set_value("stats", "id", mid) assert(up.get_value("stats", "id") == mid) \ No newline at end of file