cc-4105: Added exceptions classes. Created config wrapper class
This commit is contained in:
parent
45385dffce
commit
76cac68fe7
|
@ -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]
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,14 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
class BadSongFile(Exception):
|
class BadSongFile(Exception):
|
||||||
def __init__(self, path):
|
def __init__(self, path): self.path = 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):
|
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
|
||||||
|
|
Loading…
Reference in New Issue