aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSix <unknown>2010-05-01 23:06:55 -0400
committerSix <unknown>2010-05-01 23:06:55 -0400
commite1cd280724290c8354f510922f8b13d3896d0e89 (patch)
tree226ef26f38a87333e4d01a0e1c959b8fb9a8af11
parent8780fb51777b9042cdfd12a3625bbdc871621820 (diff)
downloaddodai-macsupport-e1cd280724290c8354f510922f8b13d3896d0e89.tar.bz2
dodai-macsupport-e1cd280724290c8354f510922f8b13d3896d0e89.tar.xz
dodai-macsupport-e1cd280724290c8354f510922f8b13d3896d0e89.zip
Removed dodai.config.db because it is replaced with dodai.config.databases. Removed dodai.config.file because it is replaced with dodai.config.files
-rw-r--r--dodai/config/db/__init__.py179
-rw-r--r--dodai/config/db/sa.py67
-rw-r--r--dodai/config/file.py116
-rw-r--r--test/test_config/test_db.py106
-rw-r--r--test/test_config/test_file.py101
5 files changed, 0 insertions, 569 deletions
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 @@
1# Copyright (C) 2010 Leonard Thomas
2#
3# This file is part of Dodai.
4#
5# Dodai is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# Dodai is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with Dodai. If not, see <http://www.gnu.org/licenses/>.
17
18class ConfigDb(object):
19
20 def __init__(self):
21 self.connections = {}
22 self._handlers = {}
23 from dodai.config.db.sa import Sa
24 self.register_handler('sa', Sa)
25
26 def register_handler(self, name, obj):
27 self._handlers[name] = [obj, None]
28
29 def add_config(self, config_parser=None):
30 if config_parser:
31 if hasattr(config_parser, 'sections') and \
32 hasattr(config_parser, 'options'):
33 config_obj = ConfigDbFile(config_parser)
34 self._add_connections(config_obj)
35 else:
36 raise NotConfigParserObject()
37
38 def _add_connections(self, config_obj):
39 connections = config_obj()
40 for name, obj in connections.items():
41 self.connections[name] = obj
42
43 def load(self, name):
44 if name in self.connections:
45 connection = self.connections[name]
46 if connection.db_obj:
47 return connection.db_obj
48 else:
49 handler = self._load_handler(connection.handler)
50 db_obj = handler.load(connection)
51 self.connections[name].db_obj = db_obj
52 return db_obj
53
54 def _load_handler(self, name):
55 if name in self._handlers:
56 handler = self._handlers[name]
57 cls = handler[0]
58 obj = handler[1]
59 if not obj:
60 obj = cls()
61 self._handlers[name] = [cls, obj]
62 return obj
63 raise UnknownHandlerException(name)
64
65
66
67class ConfigDbFile(object):
68
69 OPTIONS_REQUIRED = [
70 ['protocol', 'hostname', 'port', 'username', 'password','database'],
71 ['protocol', 'filename']
72 ]
73 OPTIONS_EXTRA = ['protocol_extra', 'handler']
74 DEFAULT_HANDLER = 'sa'
75
76 def __init__(self, config_parser):
77 self.parser = config_parser
78 self._options = self._all_options()
79 self.connections = {}
80
81 def __call__(self):
82 if not self.connections:
83 for section in self.parser.sections():
84 if self._is_valid(section):
85 obj = self._build_connection(section)
86 self.connections[obj.name] = obj
87 return self.connections
88
89 def _all_options(self):
90 out = []
91 for option_group in self.OPTIONS_REQUIRED:
92 for option in option_group:
93 if option not in out:
94 out.append(option)
95 for option in self.OPTIONS_EXTRA:
96 if option not in out:
97 out.append(option)
98 return out
99
100 def _is_valid(self, section):
101 for option_group in self.OPTIONS_REQUIRED:
102 total = len(option_group)
103 count = 0
104 for option in option_group:
105 if option in self.parser.options(section):
106 value = self.parser.get(section, option)
107 if value:
108 count += 1
109 if count >= total:
110 return True
111 return False
112
113 def _build_connection(self, section):
114 obj = ConfigDbConnection()
115 for option in self._options:
116 obj.name = section
117 if self.parser.has_option(section, option):
118 value = self.parser.get(section, option)
119 setattr(obj, option, value)
120 if not hasattr(obj, 'handler') or not obj.handler:
121 obj.handler = self.DEFAULT_HANDLER
122 return obj
123
124
125class BaseConfigDb(object):
126
127 PROTOCOLS = ['postgresql', 'mysql', 'sqlite', 'mssql', 'oracle']
128
129 def _clean(self, obj):
130 obj.protocol = self._clean_protocol(obj.protocol)
131 if hasattr(obj, 'port'):
132 obj.port = self._clean_port(obj.port)
133
134 def _clean_protocol(self, data):
135 data = data.lower()
136 if data in ('postgres', 'postgre'):
137 data = 'postgresql'
138 if data not in self.PROTOCOLS:
139 raise InvalidProtocolException(data)
140 else:
141 return data
142
143 def _clean_port(self, data):
144 try:
145 data = int(data)
146 except ValueError:
147 data = None
148 except TypeError:
149 data = None
150 if data:
151 if data <1 or data > 65535:
152 raise InvalidPortException(data)
153 return data
154
155
156class ConfigDbConnection(object):
157
158 def __init__(self):
159 self.db_obj = None
160
161
162class NotConfigParserObject(Exception):
163 pass
164
165
166class InvalidProtocolException(Exception):
167 pass
168
169
170class InvalidPortException(Exception):
171 pass
172
173
174class UnknownHandlerException(Exception):
175 pass
176
177
178class UnknownConnectionException(Exception):
179 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 @@
1# Copyright (C) 2010 Leonard Thomas
2#
3# This file is part of Dodai.
4#
5# Dodai is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# Dodai is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with Dodai. If not, see <http://www.gnu.org/licenses/>.
17
18from dodai.config.db import BaseConfigDb
19from dodai.db import Db
20
21class Sa(BaseConfigDb):
22
23
24 def load(self, obj):
25 from sqlalchemy.orm import sessionmaker
26 self._clean(obj)
27 db = Db()
28 db.engine = self._build_engine(obj)
29 Session = sessionmaker(bind=db.engine)
30 db.session = Session()
31 db.name = obj.name
32 db.protocol = obj.protocol
33 if hasattr(obj, 'schema'):
34 if obj.schema:
35 db.schema = obj.schema
36 if hasattr(obj, 'filename') and obj.filename:
37 db.filename = obj.filename
38 else:
39 db.hostname = obj.hostname
40 if hasattr(obj, 'port') and obj.port:
41 db.port = obj.port
42 db.database = obj.database
43 return db
44
45 def _build_connection_string(self, obj):
46 out = []
47 out.append('{db.protocol}')
48 if hasattr(obj, 'protocol_extra') and obj.protocol_extra:
49 out.append('+{db.protocol_extra}')
50 out.append('://')
51 if hasattr(obj, 'filename') and obj.filename:
52 out.append('{db.filename}')
53 else:
54 out.append('{db.username}:{db.password}@')
55 out.append('{db.hostname}')
56 if hasattr(obj, 'port') and obj.port:
57 out.append(':{db.port}')
58 out.append('/{db.database}')
59 out = ''.join(out)
60 out = out.format(db=obj)
61 return out
62
63 def _build_engine(self, obj):
64 from sqlalchemy import create_engine
65 connection_string = self._build_connection_string(obj)
66 db_obj = create_engine(connection_string)
67 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 @@
1# Copyright (C) 2010 Leonard Thomas
2#
3# This file is part of Dodai.
4#
5# Dodai is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# Dodai is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with Dodai. If not, see <http://www.gnu.org/licenses/>.
17
18
19import os
20import ConfigParser
21
22class ConfigFile(object):
23
24 def __init__(self):
25 self._files = []
26 self._parser = None
27 self.files_loaded = []
28 self._dir = None
29
30 def set_directory(self, path):
31 """ Sets the direcory where files will be looked for
32 raises: InvalidDirectoryException or DirectoryDoesNotExistException
33 """
34 if os.path.isdir(path):
35 self._dir = path
36 else:
37 if os.path.isfile(path):
38 raise InvalidDirectoryException(path)
39 else:
40 raise DirectoryDoesNotExistException(path)
41
42 def get_directory(self):
43 """ Returns the directory where files will be looked for
44 """
45 return self._dir
46
47 def add_file(self, path):
48 """ Adds a full file path with the given path (list or string)
49 raises: FileDoesNotExistException
50 """
51 if isinstance(path, list):
52 for file_ in path:
53 self._add_file(file_)
54 else:
55 if path not in self._files:
56 self._add_file(path)
57
58 def _add_file(self, path):
59 """ Adds the given file path file to the object if the filepath
60 doesn't already exist
61 """
62 if os.path.isfile(path):
63 if path not in self._files:
64 self._files.append(path)
65 else:
66 raise FileDoesNotExistException(path)
67
68 def get_files(self):
69 """ Returns a list of files that were added to this object
70 """
71 return self._files
72
73 def parser(self):
74 """ Returns a ConfigParser.ConfigParser object with files loaded
75 raises: NoFilesToLoadException
76 """
77 self._reset_parser()
78 if not self._parser:
79 if not self._files:
80 raise NoFilesToLoadException()
81 self._parser = ConfigParser.ConfigParser()
82 self.files_loaded = self._parser.read(self._files)
83 return self._parser
84
85 def load(self, name):
86 """ Takes the given name and merges it with the object's directory
87 then adds the path to the object
88 """
89 if not self._dir:
90 raise DirectoryNotSetException()
91 else:
92 path = os.path.join(self._dir, name)
93 self.add_file(path)
94
95 def _reset_parser(self):
96 """ Resets the _parser property if the files_loaded does not equal
97 the files assigned to this object
98 """
99 if self._parser:
100 if self.files_loaded != self._files:
101 self._parser = None
102
103class NoFilesToLoadException(Exception):
104 pass
105
106class DirectoryNotSetException(Exception):
107 pass
108
109class InvalidDirectoryException(Exception):
110 pass
111
112class DirectoryDoesNotExistException(Exception):
113 pass
114
115class FileDoesNotExistException(Exception):
116 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 @@
1# Copyright (C) 2010 Leonard Thomas
2#
3# This file is part of Dodai.
4#
5# Dodai is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# Dodai is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with Dodai. If not, see <http://www.gnu.org/licenses/>.
17
18import unittest
19import os
20from dodai.config.db import ConfigDb
21from dodai.config.db import ConfigDbFile
22from dodai.config.db import BaseConfigDb
23from dodai.config.db import NotConfigParserObject
24from dodai.config.db import InvalidProtocolException
25from dodai.config.db import InvalidPortException
26from dodai.config.db import UnknownHandlerException
27from dodai.config.db import UnknownConnectionException
28from dodai.config.db.sa import Sa
29from dodai.config.file import ConfigFile
30from dodai.db import Db
31
32class TestConfigDb(unittest.TestCase):
33
34
35 def setUp(self):
36 self.config_db = ConfigDb()
37 config = ConfigFile()
38 config.set_directory(os.path.dirname(os.path.abspath(__file__)))
39 config.load('config.cfg')
40 self.parser = config.parser()
41
42 def test_setup(self):
43 obj = self.config_db._handlers['sa'][0]
44 self.assertTrue(obj == Sa)
45
46 def test_register_handler(self):
47 self.config_db.register_handler('foo', Exception)
48 self.assertTrue('foo' in self.config_db._handlers.keys())
49
50 def test_add_config_one(self):
51 self.config_db.add_config(config_parser=self.parser)
52 self.assertTrue('test_db' in self.config_db.connections.keys())
53
54 def test_add_config_two(self):
55 self.failUnlessRaises(NotConfigParserObject, self.config_db.add_config,
56 config_parser='blah')
57
58 def test_load_one(self):
59 self.config_db.add_config(config_parser=self.parser)
60 obj = self.config_db.load('test_db')
61 self.assertTrue(isinstance(obj, Db))
62
63 def test_load_two(self):
64 self.config_db.add_config(config_parser=self.parser)
65 obj = self.config_db.load('test_db')
66 obj = self.config_db.load('test_db')
67 self.assertTrue(isinstance(obj, Db))
68
69 def test_load_handler(self):
70 self.failUnlessRaises(UnknownHandlerException,
71 self.config_db._load_handler, 'test')
72
73 def test_clean_protocol_one(self):
74 self.config_db.add_config(config_parser=self.parser)
75 obj = self.config_db.load('test_db_two')
76 self.assertTrue(isinstance(obj, Db))
77
78 def test_clean_protocol_one(self):
79 self.config_db.add_config(config_parser=self.parser)
80 obj = self.config_db.load('test_db_two')
81 self.assertTrue(isinstance(obj, Db))
82
83 def test_clean_protocol_two(self):
84 self.config_db.add_config(config_parser=self.parser)
85 self.failUnlessRaises(InvalidProtocolException, self.config_db.load,
86 'test_db_three')
87
88 def test_clean_port_one(self):
89 obj = BaseConfigDb()
90 data = obj._clean_port('ad')
91 self.assertTrue(data == None)
92
93 def test_clean_port_two(self):
94 obj = BaseConfigDb()
95 data = obj._clean_port(None)
96 self.assertTrue(data == None)
97
98 def test_clean_port_three(self):
99 obj = BaseConfigDb()
100 self.failUnlessRaises(InvalidPortException, obj._clean_port, 66666)
101
102
103 def test_file_load_one(self):
104 self.config_db.add_config(config_parser=self.parser)
105 obj = self.config_db.load('test_db_six')
106 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 @@
1# Copyright (C) 2010 Leonard Thomas
2#
3# This file is part of Dodai.
4#
5# Dodai is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# Dodai is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with Dodai. If not, see <http://www.gnu.org/licenses/>.
17
18import unittest
19import os
20from dodai.config.file import ConfigFile
21from dodai.config.file import DirectoryDoesNotExistException
22from dodai.config.file import DirectoryNotSetException
23from dodai.config.file import InvalidDirectoryException
24from dodai.config.file import FileDoesNotExistException
25from dodai.config.file import NoFilesToLoadException
26import ConfigParser
27
28class TestConfigFile(unittest.TestCase):
29
30 def setUp(self):
31 self.obj = ConfigFile()
32 self.path = os.path.dirname(__file__)
33 self.filename = 'config'
34 self.filepath = os.path.join(self.path, self.filename)
35 next_file = os.path.join(self.path, 'config.cfg')
36 self.paths = [self.filepath, next_file]
37
38 def test_file_add(self):
39 self.obj.add_file(self.filepath)
40 paths = [self.filepath]
41 files = self.obj.get_files()
42 self.assertEqual(files, paths)
43
44 def test_double_file_add(self):
45 self.obj.add_file(self.filepath)
46 self.obj.add_file(self.filepath)
47 paths = [self.filepath]
48 files = self.obj.get_files()
49 self.assertEqual(files, paths)
50
51 def test_file_does_not_exist(self):
52 path = os.path.join(self.path, 'foo')
53 self.failUnlessRaises(FileDoesNotExistException,
54 self.obj.add_file, path)
55
56 def test_multiple_file_add(self):
57 self.obj.add_file(self.paths)
58 files = self.obj.get_files()
59 self.assertEqual(files, self.paths)
60
61 def test_empty_parser(self):
62 self.failUnlessRaises(Exception, self.obj.parser)
63
64 def test_parser(self):
65 self.obj.add_file(self.filepath)
66 parser = self.obj.parser()
67 self.assertTrue(isinstance(parser, ConfigParser.ConfigParser))
68
69 def test_parser_error(self):
70 self.failUnlessRaises(NoFilesToLoadException, self.obj.parser)
71
72 def test_set_directory(self):
73 self.obj.set_directory(self.path)
74 dir = self.obj.get_directory()
75 self.assertEqual(dir, self.path)
76
77 def test_invalid_directory(self):
78 self.failUnlessRaises(InvalidDirectoryException,
79 self.obj.set_directory, self.filepath)
80
81 def test_directory_does_not_exist(self):
82 path = os.path.join(self.path, 'nowayjose')
83 self.failUnlessRaises(DirectoryDoesNotExistException,
84 self.obj.set_directory, path)
85
86 def test_load(self):
87 self.obj.set_directory(self.path)
88 self.obj.load(self.filename)
89 check = [self.filepath]
90 self.assertEqual(check, self.obj.get_files())
91
92 def test_no_directory_set(self):
93 self.failUnlessRaises(DirectoryNotSetException,
94 self.obj.load, self.filename)
95
96 def test_reset_parser(self):
97 self.obj.add_file(self.filepath)
98 self.obj.parser()
99 self.obj.add_file(self.paths)
100 self.obj.parser()
101 self.assertEqual(self.obj.files_loaded, self.obj.get_files())