diff options
author | Six <unknown> | 2010-05-20 20:37:56 -0400 |
---|---|---|
committer | Six <unknown> | 2010-05-20 20:37:56 -0400 |
commit | 099133ba43547600768b401699cb5b2f4c1f229b (patch) | |
tree | 3da613fc747468ac7f698da9ec4570a22b265adf | |
parent | f005d4af39cb2c870b009b8b564748a348b80b40 (diff) | |
download | dodai-macsupport-099133ba43547600768b401699cb5b2f4c1f229b.tar.bz2 dodai-macsupport-099133ba43547600768b401699cb5b2f4c1f229b.tar.xz dodai-macsupport-099133ba43547600768b401699cb5b2f4c1f229b.zip |
changed examples for next release
-rw-r--r-- | .hgignore | 1 | ||||
-rw-r--r-- | dodai/config/__init__.py | 41 | ||||
-rw-r--r-- | dodai/config/databases/sa.py | 3 | ||||
-rw-r--r-- | examples/config/config.cfg | 4 | ||||
-rw-r--r-- | examples/example_01.py | 36 | ||||
-rw-r--r-- | examples/example_02.py | 15 | ||||
-rw-r--r-- | examples/example_03.py | 13 | ||||
-rw-r--r-- | test/test_config/test_init.py | 3 |
8 files changed, 64 insertions, 52 deletions
@@ -6,3 +6,4 @@ bin | |||
6 | include | 6 | include |
7 | lib | 7 | lib |
8 | .coverage | 8 | .coverage |
9 | examples/config/example.log | ||
diff --git a/dodai/config/__init__.py b/dodai/config/__init__.py index 6c3011c..e849f8e 100644 --- a/dodai/config/__init__.py +++ b/dodai/config/__init__.py | |||
@@ -25,25 +25,36 @@ class Config(object): | |||
25 | setattr(obj, key, val) | 25 | setattr(obj, key, val) |
26 | return obj | 26 | return obj |
27 | 27 | ||
28 | def __getattr__(self, key): | 28 | @property |
29 | 29 | def files(self): | |
30 | if 'files' == key: | 30 | if not hasattr(self, '_files'): |
31 | from dodai.config.files import ConfigFiles | 31 | from dodai.config.files import ConfigFiles |
32 | from dodai.tools.odict import OrderedDict | 32 | from dodai.tools.odict import OrderedDict |
33 | from dodai.tools import Section | 33 | from dodai.tools import Section |
34 | from dodai.tools.himo import String2Himo | 34 | from dodai.tools.himo import String2Himo |
35 | from ConfigParser import ConfigParser | ||
35 | s2h = String2Himo() | 36 | s2h = String2Himo() |
36 | self.files = ConfigFiles(OrderedDict, Section, s2h) | 37 | self._files = ConfigFiles(OrderedDict, Section, s2h) |
37 | return self.files | 38 | self._files.register_parser_object(ConfigParser) |
38 | elif 'options' == key: | 39 | return self._files |
40 | |||
41 | @property | ||
42 | def options(self): | ||
43 | if not hasattr(self, '_options'): | ||
39 | from dodai.config.option import ConfigOption | 44 | from dodai.config.option import ConfigOption |
40 | self.options = ConfigOption() | 45 | self._options = ConfigOption() |
41 | return self.options | 46 | return self._options |
42 | elif 'logs' == key: | 47 | |
48 | @property | ||
49 | def logs(self): | ||
50 | if not hasattr(self, '_logs'): | ||
43 | from dodai.config.log import ConfigLog | 51 | from dodai.config.log import ConfigLog |
44 | self.logs = ConfigLog() | 52 | self._logs = ConfigLog() |
45 | return self.logs | 53 | return self._logs |
46 | elif 'databases' == key: | 54 | |
55 | @property | ||
56 | def databases(self): | ||
57 | if not hasattr(self, '_databases'): | ||
47 | # Wire up the sqlalchemy objects to use in the sa object | 58 | # Wire up the sqlalchemy objects to use in the sa object |
48 | # which will be used as the default database handler | 59 | # which will be used as the default database handler |
49 | from dodai.config.databases import ConfigDatabases | 60 | from dodai.config.databases import ConfigDatabases |
@@ -52,10 +63,8 @@ class Config(object): | |||
52 | from sqlalchemy import create_engine | 63 | from sqlalchemy import create_engine |
53 | from dodai.db import Db | 64 | from dodai.db import Db |
54 | sa = Sa(create_engine, sessionmaker, Db) | 65 | sa = Sa(create_engine, sessionmaker, Db) |
55 | self.databases = ConfigDatabases(sa, 'sa') | 66 | self._databases = ConfigDatabases(sa, 'sa') |
56 | return self.databases | 67 | return self._databases |
57 | else: | ||
58 | raise KeyError(key) | ||
59 | 68 | ||
60 | def set(self, key, val): | 69 | def set(self, key, val): |
61 | if not hasattr(self, 'vars'): | 70 | if not hasattr(self, 'vars'): |
diff --git a/dodai/config/databases/sa.py b/dodai/config/databases/sa.py index 21999c2..eaf35ea 100644 --- a/dodai/config/databases/sa.py +++ b/dodai/config/databases/sa.py | |||
@@ -15,7 +15,6 @@ | |||
15 | # You should have received a copy of the GNU General Public License | 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/>. | 16 | # along with Dodai. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | |||
19 | class Sa(object): | 18 | class Sa(object): |
20 | """Callable object that will wire up sqlalchemy engine and | 19 | """Callable object that will wire up sqlalchemy engine and |
21 | session objects | 20 | session objects |
@@ -74,7 +73,7 @@ class Sa(object): | |||
74 | out.append('+{section[protocol_extra]}') | 73 | out.append('+{section[protocol_extra]}') |
75 | out.append('://') | 74 | out.append('://') |
76 | if section.has_key('filename') and section['filename']: | 75 | if section.has_key('filename') and section['filename']: |
77 | out.append('{section[filename]}') | 76 | out.append('/{section[filename]}') |
78 | else: | 77 | else: |
79 | out.append('{section[username]}:{section[password]}@') | 78 | out.append('{section[username]}:{section[password]}@') |
80 | out.append('{section[hostname]}') | 79 | out.append('{section[hostname]}') |
diff --git a/examples/config/config.cfg b/examples/config/config.cfg index 36b0414..a99c4bd 100644 --- a/examples/config/config.cfg +++ b/examples/config/config.cfg | |||
@@ -27,3 +27,7 @@ username=test | |||
27 | password=test | 27 | password=test |
28 | port=12345 | 28 | port=12345 |
29 | database=testing | 29 | database=testing |
30 | |||
31 | [test_file_db] | ||
32 | protocol=sqlite | ||
33 | filename=test_file_db.sqlite | ||
diff --git a/examples/example_01.py b/examples/example_01.py index 7915e5f..d77c163 100644 --- a/examples/example_01.py +++ b/examples/example_01.py | |||
@@ -35,35 +35,29 @@ def main(config): | |||
35 | print config.__dict__ | 35 | print config.__dict__ |
36 | 36 | ||
37 | 37 | ||
38 | |||
39 | if __name__ == "__main__": | 38 | if __name__ == "__main__": |
40 | 39 | ||
41 | config = Config() | 40 | config = Config() |
42 | config.options().add_quiet() | 41 | config.options.add_quiet() |
43 | config.options().add_log_level() | 42 | config.options.add_log_level() |
44 | config.options().parser.add_option('-c', '--crazy', dest='crazy', | 43 | config.options.parser.add_option('-c', '--crazy', dest='crazy', |
45 | default=False, help="Crazy mode") | 44 | default=False, help="Crazy mode") |
46 | config.set('crazy', config.options().get_options().crazy) | 45 | config.set('crazy', config.options.get_options().crazy) |
47 | config.set('verbose', config.options().get_options().verbose) | 46 | config.set('verbose', config.options.get_options().verbose) |
48 | 47 | ||
49 | 48 | ||
50 | path = p.join(p.dirname(p.abspath(__file__)), 'logs') | 49 | log_directory = p.join(p.dirname(p.abspath(__file__)), 'config') |
51 | config.logs().set_directory(path) | 50 | config.logs.set_directory(log_directory) |
52 | config.logs().set_log_level(config.options().get_options().log_level) | 51 | config.logs.set_log_level(config.options.get_options().log_level) |
53 | log = config.logs().load(__file__) | 52 | log = config.logs.load('test_log') |
54 | config.logs().attach_file_handler(log, 'example.log') | 53 | config.logs.attach_file_handler(log, 'example.log') |
55 | config.logs().attach_screen_handler(log, 'critical') | 54 | config.logs.attach_screen_handler(log, 'critical') |
56 | config.set('log', log) | 55 | config.set('log', log) |
57 | 56 | ||
58 | 57 | ||
59 | path = p.join(p.dirname(p.abspath(__file__)), 'config') | 58 | path = p.join(p.dirname(p.abspath(__file__)), 'config') |
60 | config.files().set_directory(path) | 59 | filename = p.join(path, 'config.cfg') |
61 | config.files().load('config.cfg') | 60 | config.files.add(filename) |
62 | foo = config.files().parser().get('title', 'foo') | 61 | sections = config.files.load() |
63 | config.set('foo', foo) | 62 | config.set('foo', sections['title']['foo']) |
64 | |||
65 | |||
66 | |||
67 | |||
68 | |||
69 | main(config()) | 63 | main(config()) |
diff --git a/examples/example_02.py b/examples/example_02.py index 2844121..28ee179 100644 --- a/examples/example_02.py +++ b/examples/example_02.py | |||
@@ -43,15 +43,18 @@ from dodai.config import Config | |||
43 | 43 | ||
44 | def main(config): | 44 | def main(config): |
45 | print config.db.__dict__ | 45 | print config.db.__dict__ |
46 | print config.file_db.__dict__ | ||
46 | 47 | ||
47 | 48 | ||
48 | if __name__ == "__main__": | 49 | if __name__ == "__main__": |
49 | 50 | ||
50 | config = Config() | 51 | # path to config file |
51 | path = p.join(p.dirname(p.abspath(__file__)), 'config') | 52 | path = p.join(p.dirname(p.abspath(__file__)), 'config') |
52 | config.files().set_directory(path) | 53 | filename = p.join(path, 'config.cfg') |
53 | config.files().load('config.cfg') | 54 | |
54 | config.dbs().add_config(config_parser=config.files().parser()) | 55 | config = Config() |
55 | db = config.dbs().load('test_db') | 56 | config.files.add(filename) |
56 | config.set('db', db) | 57 | config.databases.add(config.files.load()) |
58 | config.set('db', config.databases.load('test_db')) | ||
59 | config.set('file_db', config.databases.load('test_file_db')) | ||
57 | main(config()) | 60 | main(config()) |
diff --git a/examples/example_03.py b/examples/example_03.py index b94c39e..da84eb8 100644 --- a/examples/example_03.py +++ b/examples/example_03.py | |||
@@ -38,7 +38,7 @@ On ubuntu you can do: | |||
38 | """ | 38 | """ |
39 | 39 | ||
40 | from dodai import Configure | 40 | from dodai import Configure |
41 | 41 | from dodai.tools import home_directory | |
42 | 42 | ||
43 | def main(config): | 43 | def main(config): |
44 | print config.db.engine | 44 | print config.db.engine |
@@ -47,8 +47,13 @@ def main(config): | |||
47 | 47 | ||
48 | if __name__ == "__main__": | 48 | if __name__ == "__main__": |
49 | 49 | ||
50 | config = Configure('test') | 50 | |
51 | db = config.dbs().load('test_db') | 51 | # path to config file |
52 | path = p.join(p.dirname(p.abspath(__file__)), 'config') | ||
53 | filename = p.join(path, 'config.cfg') | ||
54 | |||
55 | config = Configure('test', filename) | ||
56 | db = config.databases.load('test_db') | ||
52 | config.set('db', db) | 57 | config.set('db', db) |
53 | config.set('home_directory', config.home_directory) | 58 | config.set('home_directory', home_directory()) |
54 | main(config()) | 59 | main(config()) |
diff --git a/test/test_config/test_init.py b/test/test_config/test_init.py index 3246af2..7e7e873 100644 --- a/test/test_config/test_init.py +++ b/test/test_config/test_init.py | |||
@@ -44,9 +44,6 @@ class TestConfig(unittest.TestCase): | |||
44 | def test_databases(self): | 44 | def test_databases(self): |
45 | self.assertTrue(isinstance(self.config.databases, ConfigDatabases)) | 45 | self.assertTrue(isinstance(self.config.databases, ConfigDatabases)) |
46 | 46 | ||
47 | def test_getattr_error(self): | ||
48 | self.failUnlessRaises(KeyError, self.config.__getattr__, 'foo') | ||
49 | |||
50 | def test_set_one(self): | 47 | def test_set_one(self): |
51 | self.config.set('foo', 'bar') | 48 | self.config.set('foo', 'bar') |
52 | self.assertTrue('foo' in self.config.vars.keys()) | 49 | self.assertTrue('foo' in self.config.vars.keys()) |