Commit f889b874 authored by Chris Rossi's avatar Chris Rossi

Fix backwards compatability with zconfig--may specify a database or a storage.

parent 21c2c799
...@@ -3,6 +3,7 @@ import cgi ...@@ -3,6 +3,7 @@ import cgi
from cStringIO import StringIO from cStringIO import StringIO
import urlparse import urlparse
from ZODB.config import ZODBDatabase
from ZEO.ClientStorage import ClientStorage from ZEO.ClientStorage import ClientStorage
from ZODB.FileStorage.FileStorage import FileStorage from ZODB.FileStorage.FileStorage import FileStorage
from ZODB.DemoStorage import DemoStorage from ZODB.DemoStorage import DemoStorage
...@@ -158,6 +159,7 @@ class ZConfigURIResolver(object): ...@@ -158,6 +159,7 @@ class ZConfigURIResolver(object):
<schema> <schema>
<import package="ZODB"/> <import package="ZODB"/>
<multisection type="ZODB.storage" attribute="storages" /> <multisection type="ZODB.storage" attribute="storages" />
<multisection type="ZODB.database" attribute="databases" />
</schema> </schema>
""" """
...@@ -169,16 +171,30 @@ class ZConfigURIResolver(object): ...@@ -169,16 +171,30 @@ class ZConfigURIResolver(object):
schema_xml = self.schema_xml_template schema_xml = self.schema_xml_template
schema = ZConfig.loadSchemaFile(StringIO(schema_xml)) schema = ZConfig.loadSchemaFile(StringIO(schema_xml))
config, handler = ZConfig.loadConfig(schema, path) config, handler = ZConfig.loadConfig(schema, path)
for factory in config.storages: for config_item in config.databases + config.storages:
if not frag: if not frag:
# use the first defined in the file # use the first defined in the file
break break
elif frag == factory.name: elif frag == config_item.name:
# match found # match found
break break
else: else:
raise KeyError("No storage named %s found" % frag) raise KeyError("No storage or database named %s found" % frag)
return factory.open, dict(cgi.parse_qsl(query))
if isinstance(config_item, ZODBDatabase):
config = config_item.config
factory = config.storage
dbkw = {
'connection_cache_size': config.cache_size,
'connection_pool_size': config.pool_size,
}
if config.database_name:
dbkw['database_name'] = config.database_name
else:
factory = config_item
dbkw = dict(cgi.parse_qsl(query))
return factory.open, dbkw
client_storage_resolver = ClientStorageURIResolver() client_storage_resolver = ClientStorageURIResolver()
......
...@@ -265,7 +265,7 @@ class TestZConfigURIResolver(unittest.TestCase): ...@@ -265,7 +265,7 @@ class TestZConfigURIResolver(unittest.TestCase):
def tearDown(self): def tearDown(self):
self.tmp.close() self.tmp.close()
def test_named_database(self): def test_named_storage(self):
self.tmp.write(""" self.tmp.write("""
<demostorage foo> <demostorage foo>
</demostorage> </demostorage>
...@@ -280,7 +280,7 @@ class TestZConfigURIResolver(unittest.TestCase): ...@@ -280,7 +280,7 @@ class TestZConfigURIResolver(unittest.TestCase):
from ZODB.MappingStorage import MappingStorage from ZODB.MappingStorage import MappingStorage
self.assertTrue(isinstance(storage, MappingStorage), storage) self.assertTrue(isinstance(storage, MappingStorage), storage)
def test_anonymous_database(self): def test_anonymous_storage(self):
self.tmp.write(""" self.tmp.write("""
<mappingstorage> <mappingstorage>
</mappingstorage> </mappingstorage>
...@@ -294,6 +294,7 @@ class TestZConfigURIResolver(unittest.TestCase): ...@@ -294,6 +294,7 @@ class TestZConfigURIResolver(unittest.TestCase):
storage = factory() storage = factory()
from ZODB.MappingStorage import MappingStorage from ZODB.MappingStorage import MappingStorage
self.assertTrue(isinstance(storage, MappingStorage)) self.assertTrue(isinstance(storage, MappingStorage))
self.assertEqual(dbkw, {})
def test_query_string_args(self): def test_query_string_args(self):
self.tmp.write(""" self.tmp.write("""
...@@ -308,7 +309,7 @@ class TestZConfigURIResolver(unittest.TestCase): ...@@ -308,7 +309,7 @@ class TestZConfigURIResolver(unittest.TestCase):
factory, dbkw = resolver('zconfig://%s?foo=bar' % self.tmp.name) factory, dbkw = resolver('zconfig://%s?foo=bar' % self.tmp.name)
self.assertEqual(dbkw, {'foo': 'bar'}) self.assertEqual(dbkw, {'foo': 'bar'})
def test_database_not_found(self): def test_storage_not_found(self):
self.tmp.write(""" self.tmp.write("""
<mappingstorage x> <mappingstorage x>
</mappingstorage> </mappingstorage>
...@@ -317,6 +318,44 @@ class TestZConfigURIResolver(unittest.TestCase): ...@@ -317,6 +318,44 @@ class TestZConfigURIResolver(unittest.TestCase):
resolver = self._makeOne() resolver = self._makeOne()
self.assertRaises(KeyError, resolver, 'zconfig://%s#y' % self.tmp.name) self.assertRaises(KeyError, resolver, 'zconfig://%s#y' % self.tmp.name)
def test_anonymous_database(self):
self.tmp.write("""
<zodb>
<mappingstorage>
</mappingstorage>
</zodb>
""")
self.tmp.flush()
resolver = self._makeOne()
factory, dbkw = resolver('zconfig://%s' % self.tmp.name)
storage = factory()
from ZODB.MappingStorage import MappingStorage
self.assertTrue(isinstance(storage, MappingStorage))
self.assertEqual(dbkw, {
'connection_cache_size': 5000,
'connection_pool_size': 7})
def test_named_database(self):
self.tmp.write("""
<zodb x>
<mappingstorage>
</mappingstorage>
database-name foo
cache-size 20000
pool-size 5
</zodb>
""")
self.tmp.flush()
resolver = self._makeOne()
factory, dbkw = resolver('zconfig://%s#x' % self.tmp.name)
storage = factory()
from ZODB.MappingStorage import MappingStorage
self.assertTrue(isinstance(storage, MappingStorage))
self.assertEqual(dbkw, {
'connection_cache_size': 20000,
'connection_pool_size': 5,
'database_name': 'foo'})
class TestMappingStorageURIResolver(Base, unittest.TestCase): class TestMappingStorageURIResolver(Base, unittest.TestCase):
def _getTargetClass(self): def _getTargetClass(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment