Commit ec408899 authored by Jim Fulton's avatar Jim Fulton

Added convenience methods ZODB.DB.open and ZEO.DB.open provide a

convenient way to open a connection to a database.  They open a
database and return a connection to it. When the connection is
closed, the database is closed as well.
parent 9851a6c0
......@@ -14,6 +14,11 @@ New Features
New Features
------------
- Convenience methods ZODB.DB.open and ZEO.DB.open provide a
convenient way to open a connection to a database. They open a
database and return a connection to it. When the connection is
closed, the database is closed as well.
- The zeopack script has gotten a number of improvements:
- Simplified command-line interface. (The old interface is still
......
......@@ -25,6 +25,15 @@ def DB(*args, **kw):
import ZEO.ClientStorage, ZODB
return ZODB.DB(ZEO.ClientStorage.ClientStorage(*args, **kw))
def open(*args, **kw):
db = DB(*args, **kw)
conn = db.open()
conn.onCloseCallback(db.close)
return conn
DB.open = open
del open
def server(path=None, blob_dir=None, storage_conf=None, zeo_conf=None,
port=None):
"""Convenience function to start a server for interactive exploration
......
......@@ -1151,6 +1151,30 @@ def delete_object_multiple_clients():
>>> cs.close()
"""
def open_convenience():
"""Often, we just want to open a single connection.
>>> addr, _ = start_server(path='data.fs')
>>> conn = ZEO.DB.open(addr)
>>> conn.root()
{}
>>> conn.root()['x'] = 1
>>> transaction.commit()
>>> conn.close()
Let's make sure the database was cloased when we closed the
connection, and that the data is there.
>>> db = ZEO.DB(addr)
>>> conn = db.open()
>>> conn.root()
{'x': 1}
>>> db.close()
"""
slow_test_classes = [
BlobAdaptedFileStorageTests, BlobWritableCacheTests,
DemoStorageTests, FileStorageTests, MappingStorageTests,
......
......@@ -329,6 +329,20 @@ def getTID(at, before):
return before
class Methods(object):
def __init__(self, name, ifunc, cfunc=None):
self.__name__ = name
self.im_func = ifunc
self.cm_func = cfunc
def __get__(self, inst, class_):
if inst is None:
if self.cm_func is None:
raise AttributeError("Only in instances", self.__name__)
return self.cm_func.__get__(class_, type(class_))
return self.im_func.__get__(inst, class_)
class DB(object):
"""The Object Database
-------------------
......@@ -755,6 +769,14 @@ class DB(object):
finally:
self._r()
def class_open(class_, *args, **kw):
db = class_(*args, **kw)
conn = db.open()
conn.onCloseCallback(db.close)
return conn
open = Methods('open', open, class_open)
def connectionDebugInfo(self):
result = []
t = time.time()
......
......@@ -146,8 +146,44 @@ def connectionDebugInfo():
"""
def passing_a_file_name_to_DB():
"""You can pass a file-storage file name to DB.
(Also note that we can access DB in ZODB.)
>>> db = ZODB.DB('data.fs')
>>> db.storage # doctest: +ELLIPSIS
<ZODB.FileStorage.FileStorage.FileStorage object at ...
>>> os.path.exists('data.fs')
True
>>> db.close()
"""
def open_convenience():
"""Often, we just want to open a single connection.
>>> conn = ZODB.DB.open('data.fs')
>>> conn.root()
{}
>>> conn.root()['x'] = 1
>>> transaction.commit()
>>> conn.close()
Let's make sure the database was cloased when we closed the
connection, and that the data is there.
>>> db = ZODB.DB('data.fs')
>>> conn = db.open()
>>> conn.root()
{'x': 1}
>>> db.close()
"""
def test_suite():
s = unittest.makeSuite(DBTests)
s.addTest(doctest.DocTestSuite())
s.addTest(doctest.DocTestSuite(
setUp=ZODB.tests.util.setUp, tearDown=ZODB.tests.util.tearDown,
))
return s
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