Skip to content

Commit

Permalink
Fixed UserPreferences basics
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Florath committed Dec 26, 2012
1 parent 4a09fcc commit 0aaee24
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion rmtoo/lib/GenNonEmptyDict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

27 changes: 25 additions & 2 deletions rmtoo/lib/UserPreferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'''
Expand All @@ -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.'''
Expand All @@ -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.'''

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)
2 changes: 1 addition & 1 deletion rmtoo/tests/UnitTest/CoreTests/TestGenNonEmptyDict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion rmtoo/tests/UnitTest/CoreTests/TestUserPreferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 0aaee24

Please sign in to comment.