def __init__(self, rmtoo_home_dir=None): '''Constructs a User Preferences object which can hold and store configuration values.''' def __create_new_FileStorage(rel_filename): return FileStorage(rel_filename, self.__rmtoo_home_dir) self.__rmtoo_home_dir = self.__eval_rmtoo_home_dir(rmtoo_home_dir) self.__file_storage = GenNonEmptyDict(__create_new_FileStorage)
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.''' __default_rmtoo_home_dir = "~/.rmtoo" '''This is the default location of the rmtoo home dir.''' @staticmethod def __eval_rmtoo_home_dir(rmtoo_home_dir): '''Evaluates the rmtoo home dir. If given (and nor None), the provided directory is used. If not given, the default is used.''' if rmtoo_home_dir == None: return os.path.expanduser("~/.rmtoo") else: return rmtoo_home_dir def __init__(self, rmtoo_home_dir=None): '''Constructs a User Preferences object which can hold and store configuration values.''' def __create_new_FileStorage(rel_filename): return FileStorage(rel_filename, self.__rmtoo_home_dir) self.__rmtoo_home_dir = self.__eval_rmtoo_home_dir(rmtoo_home_dir) self.__file_storage = GenNonEmptyDict(__create_new_FileStorage) def read(self): '''Reads in all the files in the home directory and puts them into internal memory.''' for f in os.listdir(self.__rmtoo_home_dir): fs = FileStorage(f, self.__rmtoo_home_dir) fs.read() self.__file_storage.insert(f, fs) def get_rmtoo_home_dir(self): '''Returns the used home directory.''' return self.__rmtoo_home_dir 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) def write(self): '''Write the whole set of files to the directory.''' for _, f in self.__file_storage.iteritems(): f.write()
def test_int_not_existing(self): '''GenNonEmtpyDict: (int): not existing values''' def string_was(n): return "was" gned = GenNonEmptyDict(string_was) self.assertEqual("was", gned[7])
def test_int_only_existing(self): '''GenNonEmtpyDict: (int): Only exisiting values.''' gned = GenNonEmptyDict(int) gned.insert(1, "one") gned.insert(2, "two") gned.insert(3, "three") gned.insert(77, "seventyseven") self.assertEqual("one", gned[1]) self.assertEqual("seventyseven", gned[77])