From 8b44f4ed522c4153644c191fc3347fa530dcaf7f Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Mon, 3 Dec 2012 12:26:51 +0800 Subject: [PATCH] Configuration file can be stored on system --- config-dist.json | 8 +-- lib/config.py | 210 +++++++++++++++++++++++++++++------------------------- lib/exceptions.py | 11 +++ 3 files changed, 128 insertions(+), 101 deletions(-) diff --git a/config-dist.json b/config-dist.json index a821a5a..b4b3738 100644 --- a/config-dist.json +++ b/config-dist.json @@ -3,16 +3,16 @@ // A comment in this file MUST be preceded by white spaces or nothing // { - // Directories to work with. + // Directories to work with. // All 3 of them must exist. - // www and storage CANNOT point to the same directory + // www and storage CANNOT point to the same directory and must be writeable. "dirs": { // The web directory you are going to access Moodle from. A symlink will be created here. "www": "/var/www", // The directory where the instances will be stored "storage": "/var/www/moodles", - // A directory used by MDK to store different kind of things such as backups and cached repositories - "moodle": "/home/fred/.moodle" + // A directory used by MDK to store different kind of things such as backups and cached repositories. + "moodle": "/home/fred/.moodle-sdk" }, // List of remotes to work with diff --git a/lib/config.py b/lib/config.py index 3da0e84..ed0e48f 100644 --- a/lib/config.py +++ b/lib/config.py @@ -23,106 +23,122 @@ http://github.com/FMCorz/mdk """ import json, re, os +from tools import debug +from exceptions import ConfigFileCouldNotBeLoaded, ConfigFileNotFound + class Conf(object): - filename = 'config.json' - data = None - path = None - configfile = None - - def __init__(self, path, filename = None): - self.path = path - if filename != None: - self.filename = filename - self.configfile = os.path.join(self.path, self.filename) - self.load() - - def add(self, name, value): - """Add a new config to the config file""" - if self.get(name) != None: - raise Exception('Setting already declared') - self.set(name, value) - - def get(self, name = None): - """Return a setting or None if not found""" - if name == None: - return self.data - name = unicode(name).split('.') - data = self.data - for n in name: - try: - data = data[n] - except: - data = None - break - return data - - def load(self, fn = None): - """Loads the configuration from the config file""" - if fn == None: - fn = self.configfile - try: - lines = '' - f = open(fn, 'r') - for l in f: - if re.match(r'^\s*//', l): continue - lines += l - self.data = {} - if len(lines) > 0: - self.data = json.loads(lines) - f.close() - except: - raise Exception('Could not load config file %s' % fn) - - def remove(self, name): - """Remove a setting""" - name = unicode(name).split('.') - count = len(name) - data = self.data - for i in range(count): - n = name[i] - if i == count -1: - try: - del data[n] - except: - pass - break - else: - try: - data = data[n] - except: - break - self.save() - - def save(self): - """Save the settings to the config file""" - try: - f = open(self.configfile, 'w') - json.dump(self.data, f, indent = 4) - f.close() - except Exception as e: - print e - raise Exception('Could not save to config file %s' % self.configfile) - - def set(self, name, value): - """Set a new setting""" - value = unicode(value) - name = unicode(name).split('.') - count = len(name) - data = self.data - for i in range(count): - n = name[i] - if i == count -1: - data[n] = value - break - else: - try: - data = data[n] - except: - data[n] = {} - data = data[n] - self.save() + directories = ['~/.moodle-sdk/', '/etc/moodle-sdk/'] + filename = 'config.json' + data = None + configfile = None + + def __init__(self, path, filename=None): + """Creates the configuration object""" + self.directories.append(path) + if filename != None: + self.filename = filename + self.configfile = self.resolve() + self.load() + + def add(self, name, value): + """Add a new config to the config file""" + if self.get(name) != None: + raise Exception('Setting already declared') + self.set(name, value) + + def get(self, name = None): + """Return a setting or None if not found""" + if name == None: + return self.data + name = unicode(name).split('.') + data = self.data + for n in name: + try: + data = data[n] + except: + data = None + break + return data + + def load(self, fn = None): + """Loads the configuration from the config file""" + if fn == None: + fn = self.configfile + try: + lines = '' + f = open(fn, 'r') + for l in f: + if re.match(r'^\s*//', l): continue + lines += l + self.data = {} + if len(lines) > 0: + self.data = json.loads(lines) + f.close() + except: + raise ConfigFileCouldNotBeLoaded('Could not load config file %s' % fn) + + def remove(self, name): + """Remove a setting""" + name = unicode(name).split('.') + count = len(name) + data = self.data + for i in range(count): + n = name[i] + if i == count -1: + try: + del data[n] + except: + pass + break + else: + try: + data = data[n] + except: + break + self.save() + + def resolve(self): + """Resolve the path to the configuration file""" + path = None + for directory in self.directories: + candidate = os.path.expanduser(os.path.join(directory, self.filename)) + if os.path.isfile(candidate): + path = candidate + break + if path == None: + raise ConfigFileNotFound('Could not find config file') + return path + + def save(self): + """Save the settings to the config file""" + try: + f = open(self.configfile, 'w') + json.dump(self.data, f, indent = 4) + f.close() + except Exception as e: + print e + raise ConfigFileCouldNotBeSaved('Could not save to config file %s' % self.configfile) + + def set(self, name, value): + """Set a new setting""" + value = unicode(value) + name = unicode(name).split('.') + count = len(name) + data = self.data + for i in range(count): + n = name[i] + if i == count -1: + data[n] = value + break + else: + try: + data = data[n] + except: + data[n] = {} + data = data[n] + self.save() path = os.path.join(os.path.dirname(__file__), '..') C = Conf(path) diff --git a/lib/exceptions.py b/lib/exceptions.py index 143c5d9..22a502b 100644 --- a/lib/exceptions.py +++ b/lib/exceptions.py @@ -22,11 +22,22 @@ along with this program. If not, see . http://github.com/FMCorz/mdk """ + class BackupDirectoryExistsException(Exception): pass + class BackupDBExistsException(Exception): pass + class BackupDBEngineNotSupported(Exception): pass + + +class ConfigFileNotFound(Exception): + pass + + +class ConfigFileCouldNotBeLoaded(Exception): + pass -- 2.11.0