Commit 6f263f07 authored by Georges Dubus's avatar Georges Dubus

Made RelStorage an extra dependency, along with psycopg2.

Moved the relstorage resolvers in another file to avoid import
problems.
parent 28ae352e
...@@ -12,7 +12,7 @@ except: ...@@ -12,7 +12,7 @@ except:
README = '' README = ''
CHANGES = '' CHANGES = ''
requires = ['ZODB3', 'RelStorage',] requires = ['ZODB3',]
tests_require = requires + ['mock'] tests_require = requires + ['mock']
setup(name='zodburi', setup(name='zodburi',
...@@ -40,6 +40,9 @@ setup(name='zodburi', ...@@ -40,6 +40,9 @@ setup(name='zodburi',
file = zodburi.resolvers:file_storage_resolver file = zodburi.resolvers:file_storage_resolver
zconfig = zodburi.resolvers:zconfig_resolver zconfig = zodburi.resolvers:zconfig_resolver
memory = zodburi.resolvers:mapping_storage_resolver memory = zodburi.resolvers:mapping_storage_resolver
postgres = zodburi.resolvers:postgresql_resolver postgres = zodburi.resolvers_relstorage:postgresql_resolver [postgres]
""" """,
extras_require={
'postgres': ['RelStorage', 'psycopg2'],
},
) )
...@@ -10,9 +10,6 @@ from ZODB.DemoStorage import DemoStorage ...@@ -10,9 +10,6 @@ from ZODB.DemoStorage import DemoStorage
from ZODB.MappingStorage import MappingStorage from ZODB.MappingStorage import MappingStorage
from ZODB.blob import BlobStorage from ZODB.blob import BlobStorage
import ZConfig import ZConfig
from relstorage.adapters.postgresql import PostgreSQLAdapter
from relstorage.options import Options
from relstorage.storage import RelStorage
from zodburi.datatypes import convert_bytesize from zodburi.datatypes import convert_bytesize
from zodburi.datatypes import convert_int from zodburi.datatypes import convert_int
...@@ -202,70 +199,7 @@ class ZConfigURIResolver(object): ...@@ -202,70 +199,7 @@ class ZConfigURIResolver(object):
return factory.open, dbkw return factory.open, dbkw
# Not a real resolver, but we use interpret_kwargs
class PostgreSQLAdapterHelper(Resolver):
_int_args = ('connect_timeout', )
_string_args = ('ssl_mode', )
def __call__(self, parsed_uri, kw):
dsn_args = [
('dbname', parsed_uri.path[1:]),
('user', parsed_uri.username),
('password', parsed_uri.password),
('host', parsed_uri.hostname),
('port', parsed_uri.port)
]
kw, unused = self.interpret_kwargs(kw)
dsn_args.extend(kw.items())
dsn = ' '.join("%s='%s'"%arg for arg in dsn_args)
def factory(options):
return PostgreSQLAdapter(dsn=dsn, options=options)
return factory, unused
# The relstorage support is inspired by django-zodb.
# Oracle and mysql should be easily implementable from here
class RelStorageURIResolver(Resolver):
_int_args = ('poll_interval', 'cache_local_mb', 'commit_lock_timeout',
'commit_lock_id', 'read_only', 'shared_blob_dir',
'keep_history', 'pack_gc', 'pack_dry_run', 'strict_tpc',
'create', 'demostorage',)
_string_args = ('name', 'blob_dir', 'replica_conf', 'cache_module_name',
'cache_prefix', 'cache_delta_size_limit')
_bytesize_args = ('blob_cache_size', 'blob_cache_size_check',
'blob_cache_chunk_size')
_float_args = ('replica_timeout', 'pack_batch_timeout', 'pack_duty_cycle',
'pack_max_delay')
_tuple_args = ('cache_servers',)
def __init__(self, adapter_helper):
self.adapter_helper = adapter_helper
def __call__(self, uri):
uri = uri.replace('postgres://', 'http://', 1)
parsed_uri = urlparse.urlsplit(uri)
kw = dict(cgi.parse_qsl(parsed_uri.query))
adapter_factory, kw = self.adapter_helper(parsed_uri, kw)
kw, unused = self.interpret_kwargs(kw)
demostorage = kw.pop('demostorage', False)
options = Options(**kw)
def factory():
adapter = adapter_factory(options)
storage = RelStorage(adapter=adapter, options=options)
if demostorage:
storage = DemoStorage(base=storage)
return storage
return factory, unused
client_storage_resolver = ClientStorageURIResolver() client_storage_resolver = ClientStorageURIResolver()
file_storage_resolver = FileStorageURIResolver() file_storage_resolver = FileStorageURIResolver()
zconfig_resolver = ZConfigURIResolver() zconfig_resolver = ZConfigURIResolver()
mapping_storage_resolver = MappingStorageURIResolver() mapping_storage_resolver = MappingStorageURIResolver()
postgresql_resolver = RelStorageURIResolver(PostgreSQLAdapterHelper())
\ No newline at end of file
import cgi
import urlparse
from ZODB.DemoStorage import DemoStorage
from relstorage.adapters.postgresql import PostgreSQLAdapter
from relstorage.options import Options
from relstorage.storage import RelStorage
from .resolvers import Resolver
# Not a real resolver, but we use interpret_kwargs
class PostgreSQLAdapterHelper(Resolver):
_int_args = ('connect_timeout', )
_string_args = ('ssl_mode', )
def __call__(self, parsed_uri, kw):
dsn_args = [
('dbname', parsed_uri.path[1:]),
('user', parsed_uri.username),
('password', parsed_uri.password),
('host', parsed_uri.hostname),
('port', parsed_uri.port)
]
kw, unused = self.interpret_kwargs(kw)
dsn_args.extend(kw.items())
dsn = ' '.join("%s='%s'"%arg for arg in dsn_args)
def factory(options):
return PostgreSQLAdapter(dsn=dsn, options=options)
return factory, unused
# The relstorage support is inspired by django-zodb.
# Oracle and mysql should be easily implementable from here
class RelStorageURIResolver(Resolver):
_int_args = ('poll_interval', 'cache_local_mb', 'commit_lock_timeout',
'commit_lock_id', 'read_only', 'shared_blob_dir',
'keep_history', 'pack_gc', 'pack_dry_run', 'strict_tpc',
'create', 'demostorage',)
_string_args = ('name', 'blob_dir', 'replica_conf', 'cache_module_name',
'cache_prefix', 'cache_delta_size_limit')
_bytesize_args = ('blob_cache_size', 'blob_cache_size_check',
'blob_cache_chunk_size')
_float_args = ('replica_timeout', 'pack_batch_timeout', 'pack_duty_cycle',
'pack_max_delay')
_tuple_args = ('cache_servers',)
def __init__(self, adapter_helper):
self.adapter_helper = adapter_helper
def __call__(self, uri):
uri = uri.replace('postgres://', 'http://', 1)
parsed_uri = urlparse.urlsplit(uri)
kw = dict(cgi.parse_qsl(parsed_uri.query))
adapter_factory, kw = self.adapter_helper(parsed_uri, kw)
kw, unused = self.interpret_kwargs(kw)
demostorage = kw.pop('demostorage', False)
options = Options(**kw)
def factory():
adapter = adapter_factory(options)
storage = RelStorage(adapter=adapter, options=options)
if demostorage:
storage = DemoStorage(base=storage)
return storage
return factory, unused
postgresql_resolver = RelStorageURIResolver(PostgreSQLAdapterHelper())
\ No newline at end of file
...@@ -415,11 +415,11 @@ class TestMappingStorageURIResolver(Base, unittest.TestCase): ...@@ -415,11 +415,11 @@ class TestMappingStorageURIResolver(Base, unittest.TestCase):
class TestPostgreSQLURIResolver(unittest.TestCase): class TestPostgreSQLURIResolver(unittest.TestCase):
def _getTargetClass(self): def _getTargetClass(self):
from zodburi.resolvers import RelStorageURIResolver from zodburi.resolvers_relstorage import RelStorageURIResolver
return RelStorageURIResolver return RelStorageURIResolver
def _makeOne(self): def _makeOne(self):
from zodburi.resolvers import PostgreSQLAdapterHelper from zodburi.resolvers_relstorage import PostgreSQLAdapterHelper
klass = self._getTargetClass() klass = self._getTargetClass()
return klass(PostgreSQLAdapterHelper()) return klass(PostgreSQLAdapterHelper())
...@@ -445,8 +445,8 @@ class TestPostgreSQLURIResolver(unittest.TestCase): ...@@ -445,8 +445,8 @@ class TestPostgreSQLURIResolver(unittest.TestCase):
kwargs = f({'read_only':'false'}) kwargs = f({'read_only':'false'})
self.assertEqual(kwargs[0], {'read_only':0}) self.assertEqual(kwargs[0], {'read_only':0})
@mock.patch('zodburi.resolvers.PostgreSQLAdapter') @mock.patch('zodburi.resolvers_relstorage.PostgreSQLAdapter')
@mock.patch('zodburi.resolvers.RelStorage') @mock.patch('zodburi.resolvers_relstorage.RelStorage')
def test_call(self, RelStorage, PostgreSQLAdapter): def test_call(self, RelStorage, PostgreSQLAdapter):
from relstorage.options import Options from relstorage.options import Options
resolver = self._makeOne() resolver = self._makeOne()
...@@ -459,8 +459,8 @@ class TestPostgreSQLURIResolver(unittest.TestCase): ...@@ -459,8 +459,8 @@ class TestPostgreSQLURIResolver(unittest.TestCase):
options=expected_options) options=expected_options)
RelStorage.assert_called_once_with(adapter=PostgreSQLAdapter(), options=expected_options) RelStorage.assert_called_once_with(adapter=PostgreSQLAdapter(), options=expected_options)
@mock.patch('zodburi.resolvers.PostgreSQLAdapter') @mock.patch('zodburi.resolvers_relstorage.PostgreSQLAdapter')
@mock.patch('zodburi.resolvers.RelStorage') @mock.patch('zodburi.resolvers_relstorage.RelStorage')
def test_call_adapter_options(self, RelStorage, PostgreSQLAdapter): def test_call_adapter_options(self, RelStorage, PostgreSQLAdapter):
from relstorage.options import Options from relstorage.options import Options
resolver = self._makeOne() resolver = self._makeOne()
...@@ -475,8 +475,8 @@ class TestPostgreSQLURIResolver(unittest.TestCase): ...@@ -475,8 +475,8 @@ class TestPostgreSQLURIResolver(unittest.TestCase):
RelStorage.assert_called_once_with(adapter=PostgreSQLAdapter(), options=expected_options) RelStorage.assert_called_once_with(adapter=PostgreSQLAdapter(), options=expected_options)
@mock.patch('zodburi.resolvers.PostgreSQLAdapter') @mock.patch('zodburi.resolvers_relstorage.PostgreSQLAdapter')
@mock.patch('zodburi.resolvers.RelStorage') @mock.patch('zodburi.resolvers_relstorage.RelStorage')
def test_invoke_factory_demostorage(self, RelStorage, PostgreSQLAdapter): def test_invoke_factory_demostorage(self, RelStorage, PostgreSQLAdapter):
from ZODB.DemoStorage import DemoStorage from ZODB.DemoStorage import DemoStorage
resolver = self._makeOne() resolver = self._makeOne()
...@@ -499,13 +499,13 @@ class TestEntryPoints(unittest.TestCase): ...@@ -499,13 +499,13 @@ class TestEntryPoints(unittest.TestCase):
def test_it(self): def test_it(self):
from pkg_resources import load_entry_point from pkg_resources import load_entry_point
from zodburi import resolvers from zodburi import resolvers, resolvers_relstorage
expected = [ expected = [
('memory', resolvers.MappingStorageURIResolver), ('memory', resolvers.MappingStorageURIResolver),
('zeo', resolvers.ClientStorageURIResolver), ('zeo', resolvers.ClientStorageURIResolver),
('file', resolvers.FileStorageURIResolver), ('file', resolvers.FileStorageURIResolver),
('zconfig', resolvers.ZConfigURIResolver), ('zconfig', resolvers.ZConfigURIResolver),
('postgres', resolvers.RelStorageURIResolver), ('postgres', resolvers_relstorage.RelStorageURIResolver),
] ]
for name, cls in expected: for name, cls in expected:
target = load_entry_point('zodburi', 'zodburi.resolvers', name) target = load_entry_point('zodburi', 'zodburi.resolvers', name)
......
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