From e1cd280724290c8354f510922f8b13d3896d0e89 Mon Sep 17 00:00:00 2001 From: Six Date: Sat, 1 May 2010 23:06:55 -0400 Subject: Removed dodai.config.db because it is replaced with dodai.config.databases. Removed dodai.config.file because it is replaced with dodai.config.files --- dodai/config/db/__init__.py | 179 ------------------------------------------ dodai/config/db/sa.py | 67 ---------------- dodai/config/file.py | 116 --------------------------- test/test_config/test_db.py | 106 ------------------------- test/test_config/test_file.py | 101 ------------------------ 5 files changed, 569 deletions(-) delete mode 100644 dodai/config/db/__init__.py delete mode 100644 dodai/config/db/sa.py delete mode 100644 dodai/config/file.py delete mode 100644 test/test_config/test_db.py delete mode 100644 test/test_config/test_file.py diff --git a/dodai/config/db/__init__.py b/dodai/config/db/__init__.py deleted file mode 100644 index fa510ac..0000000 --- a/dodai/config/db/__init__.py +++ /dev/null @@ -1,179 +0,0 @@ -# Copyright (C) 2010 Leonard Thomas -# -# This file is part of Dodai. -# -# Dodai is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Dodai is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Dodai. If not, see . - -class ConfigDb(object): - - def __init__(self): - self.connections = {} - self._handlers = {} - from dodai.config.db.sa import Sa - self.register_handler('sa', Sa) - - def register_handler(self, name, obj): - self._handlers[name] = [obj, None] - - def add_config(self, config_parser=None): - if config_parser: - if hasattr(config_parser, 'sections') and \ - hasattr(config_parser, 'options'): - config_obj = ConfigDbFile(config_parser) - self._add_connections(config_obj) - else: - raise NotConfigParserObject() - - def _add_connections(self, config_obj): - connections = config_obj() - for name, obj in connections.items(): - self.connections[name] = obj - - def load(self, name): - if name in self.connections: - connection = self.connections[name] - if connection.db_obj: - return connection.db_obj - else: - handler = self._load_handler(connection.handler) - db_obj = handler.load(connection) - self.connections[name].db_obj = db_obj - return db_obj - - def _load_handler(self, name): - if name in self._handlers: - handler = self._handlers[name] - cls = handler[0] - obj = handler[1] - if not obj: - obj = cls() - self._handlers[name] = [cls, obj] - return obj - raise UnknownHandlerException(name) - - - -class ConfigDbFile(object): - - OPTIONS_REQUIRED = [ - ['protocol', 'hostname', 'port', 'username', 'password','database'], - ['protocol', 'filename'] - ] - OPTIONS_EXTRA = ['protocol_extra', 'handler'] - DEFAULT_HANDLER = 'sa' - - def __init__(self, config_parser): - self.parser = config_parser - self._options = self._all_options() - self.connections = {} - - def __call__(self): - if not self.connections: - for section in self.parser.sections(): - if self._is_valid(section): - obj = self._build_connection(section) - self.connections[obj.name] = obj - return self.connections - - def _all_options(self): - out = [] - for option_group in self.OPTIONS_REQUIRED: - for option in option_group: - if option not in out: - out.append(option) - for option in self.OPTIONS_EXTRA: - if option not in out: - out.append(option) - return out - - def _is_valid(self, section): - for option_group in self.OPTIONS_REQUIRED: - total = len(option_group) - count = 0 - for option in option_group: - if option in self.parser.options(section): - value = self.parser.get(section, option) - if value: - count += 1 - if count >= total: - return True - return False - - def _build_connection(self, section): - obj = ConfigDbConnection() - for option in self._options: - obj.name = section - if self.parser.has_option(section, option): - value = self.parser.get(section, option) - setattr(obj, option, value) - if not hasattr(obj, 'handler') or not obj.handler: - obj.handler = self.DEFAULT_HANDLER - return obj - - -class BaseConfigDb(object): - - PROTOCOLS = ['postgresql', 'mysql', 'sqlite', 'mssql', 'oracle'] - - def _clean(self, obj): - obj.protocol = self._clean_protocol(obj.protocol) - if hasattr(obj, 'port'): - obj.port = self._clean_port(obj.port) - - def _clean_protocol(self, data): - data = data.lower() - if data in ('postgres', 'postgre'): - data = 'postgresql' - if data not in self.PROTOCOLS: - raise InvalidProtocolException(data) - else: - return data - - def _clean_port(self, data): - try: - data = int(data) - except ValueError: - data = None - except TypeError: - data = None - if data: - if data <1 or data > 65535: - raise InvalidPortException(data) - return data - - -class ConfigDbConnection(object): - - def __init__(self): - self.db_obj = None - - -class NotConfigParserObject(Exception): - pass - - -class InvalidProtocolException(Exception): - pass - - -class InvalidPortException(Exception): - pass - - -class UnknownHandlerException(Exception): - pass - - -class UnknownConnectionException(Exception): - pass diff --git a/dodai/config/db/sa.py b/dodai/config/db/sa.py deleted file mode 100644 index 7654b44..0000000 --- a/dodai/config/db/sa.py +++ /dev/null @@ -1,67 +0,0 @@ -# Copyright (C) 2010 Leonard Thomas -# -# This file is part of Dodai. -# -# Dodai is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Dodai is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Dodai. If not, see . - -from dodai.config.db import BaseConfigDb -from dodai.db import Db - -class Sa(BaseConfigDb): - - - def load(self, obj): - from sqlalchemy.orm import sessionmaker - self._clean(obj) - db = Db() - db.engine = self._build_engine(obj) - Session = sessionmaker(bind=db.engine) - db.session = Session() - db.name = obj.name - db.protocol = obj.protocol - if hasattr(obj, 'schema'): - if obj.schema: - db.schema = obj.schema - if hasattr(obj, 'filename') and obj.filename: - db.filename = obj.filename - else: - db.hostname = obj.hostname - if hasattr(obj, 'port') and obj.port: - db.port = obj.port - db.database = obj.database - return db - - def _build_connection_string(self, obj): - out = [] - out.append('{db.protocol}') - if hasattr(obj, 'protocol_extra') and obj.protocol_extra: - out.append('+{db.protocol_extra}') - out.append('://') - if hasattr(obj, 'filename') and obj.filename: - out.append('{db.filename}') - else: - out.append('{db.username}:{db.password}@') - out.append('{db.hostname}') - if hasattr(obj, 'port') and obj.port: - out.append(':{db.port}') - out.append('/{db.database}') - out = ''.join(out) - out = out.format(db=obj) - return out - - def _build_engine(self, obj): - from sqlalchemy import create_engine - connection_string = self._build_connection_string(obj) - db_obj = create_engine(connection_string) - return db_obj diff --git a/dodai/config/file.py b/dodai/config/file.py deleted file mode 100644 index 5cab6f8..0000000 --- a/dodai/config/file.py +++ /dev/null @@ -1,116 +0,0 @@ -# Copyright (C) 2010 Leonard Thomas -# -# This file is part of Dodai. -# -# Dodai is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Dodai is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Dodai. If not, see . - - -import os -import ConfigParser - -class ConfigFile(object): - - def __init__(self): - self._files = [] - self._parser = None - self.files_loaded = [] - self._dir = None - - def set_directory(self, path): - """ Sets the direcory where files will be looked for - raises: InvalidDirectoryException or DirectoryDoesNotExistException - """ - if os.path.isdir(path): - self._dir = path - else: - if os.path.isfile(path): - raise InvalidDirectoryException(path) - else: - raise DirectoryDoesNotExistException(path) - - def get_directory(self): - """ Returns the directory where files will be looked for - """ - return self._dir - - def add_file(self, path): - """ Adds a full file path with the given path (list or string) - raises: FileDoesNotExistException - """ - if isinstance(path, list): - for file_ in path: - self._add_file(file_) - else: - if path not in self._files: - self._add_file(path) - - def _add_file(self, path): - """ Adds the given file path file to the object if the filepath - doesn't already exist - """ - if os.path.isfile(path): - if path not in self._files: - self._files.append(path) - else: - raise FileDoesNotExistException(path) - - def get_files(self): - """ Returns a list of files that were added to this object - """ - return self._files - - def parser(self): - """ Returns a ConfigParser.ConfigParser object with files loaded - raises: NoFilesToLoadException - """ - self._reset_parser() - if not self._parser: - if not self._files: - raise NoFilesToLoadException() - self._parser = ConfigParser.ConfigParser() - self.files_loaded = self._parser.read(self._files) - return self._parser - - def load(self, name): - """ Takes the given name and merges it with the object's directory - then adds the path to the object - """ - if not self._dir: - raise DirectoryNotSetException() - else: - path = os.path.join(self._dir, name) - self.add_file(path) - - def _reset_parser(self): - """ Resets the _parser property if the files_loaded does not equal - the files assigned to this object - """ - if self._parser: - if self.files_loaded != self._files: - self._parser = None - -class NoFilesToLoadException(Exception): - pass - -class DirectoryNotSetException(Exception): - pass - -class InvalidDirectoryException(Exception): - pass - -class DirectoryDoesNotExistException(Exception): - pass - -class FileDoesNotExistException(Exception): - pass diff --git a/test/test_config/test_db.py b/test/test_config/test_db.py deleted file mode 100644 index ed46eae..0000000 --- a/test/test_config/test_db.py +++ /dev/null @@ -1,106 +0,0 @@ -# Copyright (C) 2010 Leonard Thomas -# -# This file is part of Dodai. -# -# Dodai is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Dodai is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Dodai. If not, see . - -import unittest -import os -from dodai.config.db import ConfigDb -from dodai.config.db import ConfigDbFile -from dodai.config.db import BaseConfigDb -from dodai.config.db import NotConfigParserObject -from dodai.config.db import InvalidProtocolException -from dodai.config.db import InvalidPortException -from dodai.config.db import UnknownHandlerException -from dodai.config.db import UnknownConnectionException -from dodai.config.db.sa import Sa -from dodai.config.file import ConfigFile -from dodai.db import Db - -class TestConfigDb(unittest.TestCase): - - - def setUp(self): - self.config_db = ConfigDb() - config = ConfigFile() - config.set_directory(os.path.dirname(os.path.abspath(__file__))) - config.load('config.cfg') - self.parser = config.parser() - - def test_setup(self): - obj = self.config_db._handlers['sa'][0] - self.assertTrue(obj == Sa) - - def test_register_handler(self): - self.config_db.register_handler('foo', Exception) - self.assertTrue('foo' in self.config_db._handlers.keys()) - - def test_add_config_one(self): - self.config_db.add_config(config_parser=self.parser) - self.assertTrue('test_db' in self.config_db.connections.keys()) - - def test_add_config_two(self): - self.failUnlessRaises(NotConfigParserObject, self.config_db.add_config, - config_parser='blah') - - def test_load_one(self): - self.config_db.add_config(config_parser=self.parser) - obj = self.config_db.load('test_db') - self.assertTrue(isinstance(obj, Db)) - - def test_load_two(self): - self.config_db.add_config(config_parser=self.parser) - obj = self.config_db.load('test_db') - obj = self.config_db.load('test_db') - self.assertTrue(isinstance(obj, Db)) - - def test_load_handler(self): - self.failUnlessRaises(UnknownHandlerException, - self.config_db._load_handler, 'test') - - def test_clean_protocol_one(self): - self.config_db.add_config(config_parser=self.parser) - obj = self.config_db.load('test_db_two') - self.assertTrue(isinstance(obj, Db)) - - def test_clean_protocol_one(self): - self.config_db.add_config(config_parser=self.parser) - obj = self.config_db.load('test_db_two') - self.assertTrue(isinstance(obj, Db)) - - def test_clean_protocol_two(self): - self.config_db.add_config(config_parser=self.parser) - self.failUnlessRaises(InvalidProtocolException, self.config_db.load, - 'test_db_three') - - def test_clean_port_one(self): - obj = BaseConfigDb() - data = obj._clean_port('ad') - self.assertTrue(data == None) - - def test_clean_port_two(self): - obj = BaseConfigDb() - data = obj._clean_port(None) - self.assertTrue(data == None) - - def test_clean_port_three(self): - obj = BaseConfigDb() - self.failUnlessRaises(InvalidPortException, obj._clean_port, 66666) - - - def test_file_load_one(self): - self.config_db.add_config(config_parser=self.parser) - obj = self.config_db.load('test_db_six') - self.assertTrue(isinstance(obj, Db)) diff --git a/test/test_config/test_file.py b/test/test_config/test_file.py deleted file mode 100644 index 1ed014e..0000000 --- a/test/test_config/test_file.py +++ /dev/null @@ -1,101 +0,0 @@ -# Copyright (C) 2010 Leonard Thomas -# -# This file is part of Dodai. -# -# Dodai is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Dodai is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Dodai. If not, see . - -import unittest -import os -from dodai.config.file import ConfigFile -from dodai.config.file import DirectoryDoesNotExistException -from dodai.config.file import DirectoryNotSetException -from dodai.config.file import InvalidDirectoryException -from dodai.config.file import FileDoesNotExistException -from dodai.config.file import NoFilesToLoadException -import ConfigParser - -class TestConfigFile(unittest.TestCase): - - def setUp(self): - self.obj = ConfigFile() - self.path = os.path.dirname(__file__) - self.filename = 'config' - self.filepath = os.path.join(self.path, self.filename) - next_file = os.path.join(self.path, 'config.cfg') - self.paths = [self.filepath, next_file] - - def test_file_add(self): - self.obj.add_file(self.filepath) - paths = [self.filepath] - files = self.obj.get_files() - self.assertEqual(files, paths) - - def test_double_file_add(self): - self.obj.add_file(self.filepath) - self.obj.add_file(self.filepath) - paths = [self.filepath] - files = self.obj.get_files() - self.assertEqual(files, paths) - - def test_file_does_not_exist(self): - path = os.path.join(self.path, 'foo') - self.failUnlessRaises(FileDoesNotExistException, - self.obj.add_file, path) - - def test_multiple_file_add(self): - self.obj.add_file(self.paths) - files = self.obj.get_files() - self.assertEqual(files, self.paths) - - def test_empty_parser(self): - self.failUnlessRaises(Exception, self.obj.parser) - - def test_parser(self): - self.obj.add_file(self.filepath) - parser = self.obj.parser() - self.assertTrue(isinstance(parser, ConfigParser.ConfigParser)) - - def test_parser_error(self): - self.failUnlessRaises(NoFilesToLoadException, self.obj.parser) - - def test_set_directory(self): - self.obj.set_directory(self.path) - dir = self.obj.get_directory() - self.assertEqual(dir, self.path) - - def test_invalid_directory(self): - self.failUnlessRaises(InvalidDirectoryException, - self.obj.set_directory, self.filepath) - - def test_directory_does_not_exist(self): - path = os.path.join(self.path, 'nowayjose') - self.failUnlessRaises(DirectoryDoesNotExistException, - self.obj.set_directory, path) - - def test_load(self): - self.obj.set_directory(self.path) - self.obj.load(self.filename) - check = [self.filepath] - self.assertEqual(check, self.obj.get_files()) - - def test_no_directory_set(self): - self.failUnlessRaises(DirectoryNotSetException, - self.obj.load, self.filename) - - def test_reset_parser(self): - self.obj.add_file(self.filepath) - self.obj.parser() - self.obj.add_file(self.paths) - self.obj.parser() - self.assertEqual(self.obj.files_loaded, self.obj.get_files()) -- cgit v1.2.3