diff --git a/python_apps/media-monitor2/media/monitor/config.py b/python_apps/media-monitor2/media/monitor/config.py new file mode 100644 index 000000000..c57870439 --- /dev/null +++ b/python_apps/media-monitor2/media/monitor/config.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +import os +from configobj import ConfigObj +import copy + +from media.monitor.log import Loggable +from media.monitor.exceptions import NoConfigFile, ConfigAccessViolation + +class MMConfig(Loggable): + def __init__(self, path): + if not os.path.exists(path): + self.logger.error("Configuration file does not exist. Path: '%s'" % path) + raise NoConfigFile(path) + self.cfg = ConfigObj(path) + + def __getitem__(self, key): + """ + We always return a copy of the config item to prevent callers from doing any modifications + through the returned objects methods + """ + return copy.deepcopy(self.cfg[key]) + + def __setitem__(self, key, value): + """ + We use this method not to allow anybody to mess around with config file + any settings made should be done through MMConfig's instance methods + """ + raise ConfigAccessViolation(key) + + def save(self): self.cfg.write() + + # Remove this after debugging... + def haxxor_set(self, key, value): self.cfg[key] = value + def haxxor_get(self, key): return self.cfg[key] + + diff --git a/python_apps/media-monitor2/media/monitor/exceptions.py b/python_apps/media-monitor2/media/monitor/exceptions.py index 0b5db6200..263f0763c 100644 --- a/python_apps/media-monitor2/media/monitor/exceptions.py +++ b/python_apps/media-monitor2/media/monitor/exceptions.py @@ -1,7 +1,14 @@ # -*- coding: utf-8 -*- class BadSongFile(Exception): - def __init__(self, path): - self.path = path + def __init__(self, path): self.path = path + def __str__(self): return "Can't read %s" % self.path + +class NoConfigFile(Exception): + def __init__(self, path): self.path = path def __str__(self): - return "Can't read %s" % self.path + return "Path '%s' for config file does not exit" % self.path + +class ConfigAccessViolation(Exception): + def __init__(self,key): self.key = key + def __str__(self): return "You must not access key '%s' directly" % self.key