import logging
import os
import sys
from constants import DEFAULT_DATA_PATH
debug = logging.getLogger("settings").debug
error = logging.getLogger("settings").error
class Settings(dict):
def __init__(self, filename=None, autocreate=False):
dict.__init__(self)
self.filename = filename
self.autocreate = autocreate
if filename:
self.loadSettings()
def __getitem__(self, k):
try:
return dict.__getitem__(self, k)
except:
error("Could not locate setting %s", k)
return ""
def setFilename(self, filename):
self.filename = filename
def loadSettings(self):
if not self.filename:
error("Cannot loadSettings")
return
if not os.access(self.filename, os.R_OK) and self.autocreate:
self.createInitialSettings()
fp = None
try:
fp = open(self.filename, "r")
for line in fp:
line = line.strip()
if not line or line[0] == '#': continue
name, value = line.split(':', 1)
name = name.strip().lower()
value = value.strip()
dict.__setitem__(self, name, value)
fp.close()
debug("Loaded settings from: %s", self.filename)
except Exception, e:
error(e)
if fp: fp.close()
def saveSettings(self):
fp = None
try:
fp = open(self.filename, "w")
for k,v in self.items():
fp.write("%s: %s\n" % (k,v))
except Exception, e:
error("Could not saveSettings", e)
if fp: fp.close()
def createInitialSettings(self):
try:
fp = open(self.filename, "w")
except Exception, e:
error("Could not write file: %s - %s", self.filename, str(e))
sys.exit(1)
try:
data_path = os.path.join(os.environ['HOME'], DEFAULT_DATA_PATH)
except:
data_path = os.path.join(os.getcwd(), DEFAULT_DATA_PATH)
debug("Using %s as the data_path in %s", data_path, self.filename)
fp.write("data_path: %s\n" % data_path)
fp.close()
|