pycache added
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
/var/
|
||||||
|
__pycache__
|
||||||
194
src/zcm.py
194
src/zcm.py
@@ -1,97 +1,97 @@
|
|||||||
#!/usr/bin/python3.5
|
#!/usr/bin/python3.5
|
||||||
# encoding: utf8
|
# encoding: utf8
|
||||||
|
|
||||||
# Copyright (c) 2016 Andreas
|
# Copyright (c) 2016 Andreas
|
||||||
# See LICENSE for details.
|
# See LICENSE for details.
|
||||||
|
|
||||||
""" ZODB context manager """
|
""" ZODB context manager """
|
||||||
|
|
||||||
# from BTrees.IOBTree import IOBTree
|
# from BTrees.IOBTree import IOBTree
|
||||||
# from BTrees.OOBTree import OOBTree
|
# from BTrees.OOBTree import OOBTree
|
||||||
# from persistent import Persistent
|
# from persistent import Persistent
|
||||||
# from persistent.list import PersistentList as PList
|
# from persistent.list import PersistentList as PList
|
||||||
# from persistent.mapping import PersistentMapping as PDict
|
# from persistent.mapping import PersistentMapping as PDict
|
||||||
|
|
||||||
from ZEO.ClientStorage import ClientStorage
|
from ZEO.ClientStorage import ClientStorage
|
||||||
from ZODB import DB
|
from ZODB import DB
|
||||||
from ZODB.FileStorage import FileStorage
|
from ZODB.FileStorage import FileStorage
|
||||||
|
|
||||||
|
|
||||||
class ZDatabase():
|
class ZDatabase():
|
||||||
""" Provides a ZODB database context manager """
|
""" Provides a ZODB database context manager """
|
||||||
|
|
||||||
def __init__(self, uri, **kwargs):
|
def __init__(self, uri, **kwargs):
|
||||||
self.storage = create_storage(uri)
|
self.storage = create_storage(uri)
|
||||||
self.db = DB(self.storage, **kwargs)
|
self.db = DB(self.storage, **kwargs)
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
return self.db
|
return self.db
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_value, traceback):
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
self.db.close()
|
self.db.close()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
class ZConnection():
|
class ZConnection():
|
||||||
""" Provides a ZODB connection with auto-abort (default).
|
""" Provides a ZODB connection with auto-abort (default).
|
||||||
Provides a tuple of connection and root object:
|
Provides a tuple of connection and root object:
|
||||||
with ZConnection(db) as (cx, root):
|
with ZConnection(db) as (cx, root):
|
||||||
root.one = "ok"
|
root.one = "ok"
|
||||||
ZConnection implements a connection context manager.
|
ZConnection implements a connection context manager.
|
||||||
Transaction context managers in contrast do auto-commit:
|
Transaction context managers in contrast do auto-commit:
|
||||||
a) with db.transaction() as connection, or
|
a) with db.transaction() as connection, or
|
||||||
b) with cx.transaction_manager as transaction, or
|
b) with cx.transaction_manager as transaction, or
|
||||||
c) with transaction.manager as transaction (for the thread-local transaction manager)
|
c) with transaction.manager as transaction (for the thread-local transaction manager)
|
||||||
See also http://www.zodb.org/en/latest/guide/transactions-and-threading.html
|
See also http://www.zodb.org/en/latest/guide/transactions-and-threading.html
|
||||||
"""
|
"""
|
||||||
def __init__(self, db, auto_commit=False, transaction_manager=None):
|
def __init__(self, db, auto_commit=False, transaction_manager=None):
|
||||||
self.db = db
|
self.db = db
|
||||||
self.auto_commit = auto_commit
|
self.auto_commit = auto_commit
|
||||||
self.transaction_manager = transaction_manager
|
self.transaction_manager = transaction_manager
|
||||||
self.cx = None
|
self.cx = None
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
if self.transaction_manager:
|
if self.transaction_manager:
|
||||||
self.cx = self.db.open(self.transaction_manager)
|
self.cx = self.db.open(self.transaction_manager)
|
||||||
else:
|
else:
|
||||||
self.cx = self.db.open()
|
self.cx = self.db.open()
|
||||||
return self.cx, self.cx.root()
|
return self.cx, self.cx.root()
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_value, traceback):
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
if self.auto_commit:
|
if self.auto_commit:
|
||||||
self.cx.transaction_manager.commit()
|
self.cx.transaction_manager.commit()
|
||||||
self.cx.close()
|
self.cx.close()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def create_storage(uri):
|
def create_storage(uri):
|
||||||
""" supported URIs
|
""" supported URIs
|
||||||
file://e:/workspaces/zeo/bots.fs
|
file://e:/workspaces/zeo/bots.fs
|
||||||
zeo://localhost:8001
|
zeo://localhost:8001
|
||||||
e:/workspaces/zeo/bots.fs
|
e:/workspaces/zeo/bots.fs
|
||||||
@see https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
|
@see https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
|
||||||
"""
|
"""
|
||||||
if uri.startswith("file://"):
|
if uri.startswith("file://"):
|
||||||
storage = FileStorage(uri[7:])
|
storage = FileStorage(uri[7:])
|
||||||
elif uri.startswith("zeo://"):
|
elif uri.startswith("zeo://"):
|
||||||
addr, port = uri[6:].split(":")
|
addr, port = uri[6:].split(":")
|
||||||
# addr_ = addr.encode("ASCII")
|
# addr_ = addr.encode("ASCII")
|
||||||
storage = ClientStorage((addr, int(port)))
|
storage = ClientStorage((addr, int(port)))
|
||||||
else:
|
else:
|
||||||
storage = FileStorage(uri)
|
storage = FileStorage(uri)
|
||||||
return storage
|
return storage
|
||||||
|
|
||||||
|
|
||||||
def database(uri):
|
def database(uri):
|
||||||
""" convenience function for single thread, return one connection from the pool """
|
""" convenience function for single thread, return one connection from the pool """
|
||||||
storage = create_storage(uri)
|
storage = create_storage(uri)
|
||||||
db = DB(storage)
|
db = DB(storage)
|
||||||
return db
|
return db
|
||||||
|
|
||||||
|
|
||||||
def connection(db):
|
def connection(db):
|
||||||
""" Convenience function for multi thread, returns
|
""" Convenience function for multi thread, returns
|
||||||
connection, transaction manager and root
|
connection, transaction manager and root
|
||||||
"""
|
"""
|
||||||
cx = db.open()
|
cx = db.open()
|
||||||
return cx, cx.root()
|
return cx, cx.root()
|
||||||
|
|||||||
Reference in New Issue
Block a user