From 34653e4738af2d97204ed75caa4c7f5df7e742bd Mon Sep 17 00:00:00 2001 From: Andreas Balogh Date: Sat, 6 May 2017 19:41:29 +0200 Subject: [PATCH] pycache added --- .gitignore | 2 + src/zcm.py | 194 ++++++++++++++++++++++++++--------------------------- 2 files changed, 99 insertions(+), 97 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e1ef61 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/var/ +__pycache__ diff --git a/src/zcm.py b/src/zcm.py index a61f2dd..4ffe95c 100644 --- a/src/zcm.py +++ b/src/zcm.py @@ -1,97 +1,97 @@ -#!/usr/bin/python3.5 -# encoding: utf8 - -# Copyright (c) 2016 Andreas -# See LICENSE for details. - -""" ZODB context manager """ - -# from BTrees.IOBTree import IOBTree -# from BTrees.OOBTree import OOBTree -# from persistent import Persistent -# from persistent.list import PersistentList as PList -# from persistent.mapping import PersistentMapping as PDict - -from ZEO.ClientStorage import ClientStorage -from ZODB import DB -from ZODB.FileStorage import FileStorage - - -class ZDatabase(): - """ Provides a ZODB database context manager """ - - def __init__(self, uri, **kwargs): - self.storage = create_storage(uri) - self.db = DB(self.storage, **kwargs) - - def __enter__(self): - return self.db - - def __exit__(self, exc_type, exc_value, traceback): - self.db.close() - return False - - -class ZConnection(): - """ Provides a ZODB connection with auto-abort (default). - Provides a tuple of connection and root object: - with ZConnection(db) as (cx, root): - root.one = "ok" - ZConnection implements a connection context manager. - Transaction context managers in contrast do auto-commit: - a) with db.transaction() as connection, or - b) with cx.transaction_manager as transaction, or - 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 - """ - def __init__(self, db, auto_commit=False, transaction_manager=None): - self.db = db - self.auto_commit = auto_commit - self.transaction_manager = transaction_manager - self.cx = None - - def __enter__(self): - if self.transaction_manager: - self.cx = self.db.open(self.transaction_manager) - else: - self.cx = self.db.open() - return self.cx, self.cx.root() - - def __exit__(self, exc_type, exc_value, traceback): - if self.auto_commit: - self.cx.transaction_manager.commit() - self.cx.close() - return False - - -def create_storage(uri): - """ supported URIs - file://e:/workspaces/zeo/bots.fs - zeo://localhost:8001 - e:/workspaces/zeo/bots.fs - @see https://en.wikipedia.org/wiki/Uniform_Resource_Identifier - """ - if uri.startswith("file://"): - storage = FileStorage(uri[7:]) - elif uri.startswith("zeo://"): - addr, port = uri[6:].split(":") - # addr_ = addr.encode("ASCII") - storage = ClientStorage((addr, int(port))) - else: - storage = FileStorage(uri) - return storage - - -def database(uri): - """ convenience function for single thread, return one connection from the pool """ - storage = create_storage(uri) - db = DB(storage) - return db - - -def connection(db): - """ Convenience function for multi thread, returns - connection, transaction manager and root - """ - cx = db.open() - return cx, cx.root() +#!/usr/bin/python3.5 +# encoding: utf8 + +# Copyright (c) 2016 Andreas +# See LICENSE for details. + +""" ZODB context manager """ + +# from BTrees.IOBTree import IOBTree +# from BTrees.OOBTree import OOBTree +# from persistent import Persistent +# from persistent.list import PersistentList as PList +# from persistent.mapping import PersistentMapping as PDict + +from ZEO.ClientStorage import ClientStorage +from ZODB import DB +from ZODB.FileStorage import FileStorage + + +class ZDatabase(): + """ Provides a ZODB database context manager """ + + def __init__(self, uri, **kwargs): + self.storage = create_storage(uri) + self.db = DB(self.storage, **kwargs) + + def __enter__(self): + return self.db + + def __exit__(self, exc_type, exc_value, traceback): + self.db.close() + return False + + +class ZConnection(): + """ Provides a ZODB connection with auto-abort (default). + Provides a tuple of connection and root object: + with ZConnection(db) as (cx, root): + root.one = "ok" + ZConnection implements a connection context manager. + Transaction context managers in contrast do auto-commit: + a) with db.transaction() as connection, or + b) with cx.transaction_manager as transaction, or + 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 + """ + def __init__(self, db, auto_commit=False, transaction_manager=None): + self.db = db + self.auto_commit = auto_commit + self.transaction_manager = transaction_manager + self.cx = None + + def __enter__(self): + if self.transaction_manager: + self.cx = self.db.open(self.transaction_manager) + else: + self.cx = self.db.open() + return self.cx, self.cx.root() + + def __exit__(self, exc_type, exc_value, traceback): + if self.auto_commit: + self.cx.transaction_manager.commit() + self.cx.close() + return False + + +def create_storage(uri): + """ supported URIs + file://e:/workspaces/zeo/bots.fs + zeo://localhost:8001 + e:/workspaces/zeo/bots.fs + @see https://en.wikipedia.org/wiki/Uniform_Resource_Identifier + """ + if uri.startswith("file://"): + storage = FileStorage(uri[7:]) + elif uri.startswith("zeo://"): + addr, port = uri[6:].split(":") + # addr_ = addr.encode("ASCII") + storage = ClientStorage((addr, int(port))) + else: + storage = FileStorage(uri) + return storage + + +def database(uri): + """ convenience function for single thread, return one connection from the pool """ + storage = create_storage(uri) + db = DB(storage) + return db + + +def connection(db): + """ Convenience function for multi thread, returns + connection, transaction manager and root + """ + cx = db.open() + return cx, cx.root()