Skip to content
This repository
Browse code

Version 2 API and tests.

  • Loading branch information...
commit b614a84cc5dc2ff69da7ff155253f4b58b5e382c 1 parent 94ae76c
Mike Orr authored December 30, 2011
8  CHANGES.txt
... ...
@@ -1,6 +1,10 @@
1  
-dev   (unreleased)
  1
+2.0   (unreleased)
2 2
 ------------------
3  
-- Convert to git/Github/ReadTheDocs (from hg/Bitbucket).
  3
+- New version 2 API.
  4
+- Old version 1 API is supported for backward compatibility.
  5
+- Convert repository to Git (from Mercurial).
  6
+- Repository is now part of the Pylons project.
  7
+  (https://github.com/Pylons/SQLAHelper).
4 8
 
5 9
 1.0 (2011-12-25)
6 10
 ----------------
62  sqlahelper.py
@@ -3,33 +3,44 @@
3 3
 import sqlalchemy.orm as orm
4 4
 from zope.sqlalchemy import ZopeTransactionExtension
5 5
 
6  
-# Global variables initialized by ``reset()``.
7  
-_base = _session = _engines = _zte = None
  6
+# Import only version 1 API with "import *"
  7
+__all__ = ["add_engine", "get_base", "get_session", "get_engine"]
  8
+
  9
+# VERSION 2 API
  10
+
  11
+class AttributeContainer(object):
  12
+    def _clear(self):
  13
+        """Delete all instance attributes. For internal use only."""
  14
+        self.__dict__.clear()
  15
+
  16
+engines = AttributeContainer()
  17
+bases = AttributeContainer()
  18
+sessions = AttributeContainer()
  19
+
  20
+_zte = ZopeTransactionExtension()
  21
+
  22
+def set_default_engine(engine):
  23
+    engines.default = engine
  24
+    bases.default.metadata.bind = engine
  25
+    sessions.default.configure(bind=engine)
8 26
 
9 27
 def reset():
10  
-    """Delete all engines and restore the initial module state.
  28
+    """Restore the initial module state, deleting all modifications.
11 29
     
12 30
     This function is mainly for unit tests and debugging. It undoes all
13 31
     customizations and reverts to the initial module state.
14 32
     """
15  
-    global _base, _session, _engines, _zte
16  
-    _zte = ZopeTransactionExtension()
  33
+    engines._clear()
  34
+    bases._clear()
  35
+    sessions._clear()
  36
+    engines.default = None
  37
+    bases.default = declarative.declarative_base()
17 38
     sm = orm.sessionmaker(extension=[_zte])
18  
-    _base = declarative.declarative_base()
19  
-    _session = orm.scoped_session(sm)
20  
-    _engines = {}
  39
+    sessions.default = orm.scoped_session(sm)
21 40
 
22 41
 reset()
23 42
 
24  
-# PUBLIC API
25  
-
26  
-__all__ = [
27  
-    "add_engine", 
28  
-    "get_base",
29  
-    "get_session", 
30  
-    "get_engine",
31  
-    ]
32  
-    # "reset" is not included because it's not intended for normal use.
  43
+# VERSION 1 API
33 44
 
34 45
 def add_engine(engine, name="default"):
35 46
     """Add a SQLAlchemy engine to the engine repository.
@@ -42,10 +53,10 @@ def add_engine(engine, name="default"):
42 53
     metadata will be bound to it, and calling ``get_engine()`` without an
43 54
     argument will return it.
44 55
     """
45  
-    _engines[name] = engine
46 56
     if name == "default":
47  
-        _session.configure(bind=engine)
48  
-        _base.metadata.bind = engine
  57
+        set_default_engine(engine)
  58
+    else:
  59
+        setattr(engines, name, engine)
49 60
 
50 61
 def get_session():
51 62
     """Return the central SQLAlchemy contextual session.
@@ -64,7 +75,7 @@ def get_session():
64 75
 
65 76
         sqlahelper.get_session().configure(ext=[sqlahelper._zte, ...])
66 77
     """
67  
-    return _session
  78
+    return sessions.default
68 79
 
69 80
 def get_engine(name="default"):
70 81
     """Look up an engine by name in the engine repository and return it.
@@ -74,14 +85,14 @@ def get_engine(name="default"):
74 85
     Raise ``RuntimeError`` if no engine under that name has been configured.
75 86
     """
76 87
     try:
77  
-        return _engines[name]
78  
-    except KeyError:
  88
+        return getattr(engines, name)
  89
+    except AttributeError:
79 90
         raise RuntimeError("No engine '%s' was configured" % name)
80 91
 
81 92
 def get_base():
82 93
     """Return the central SQLAlchemy declarative base.
83 94
     """
84  
-    return _base
  95
+    return bases.default
85 96
 
86 97
 def set_base(base):
87 98
     """Set the central SQLAlchemy declarative base.
@@ -96,5 +107,4 @@ def set_base(base):
96 107
     ``set_base()`` early in the application's execution, before importing the
97 108
     third-party libraries.
98 109
     """
99  
-    global _base
100  
-    _base = base
  110
+    bases.default = base
6  tests.py
@@ -71,9 +71,9 @@ def test_multiple_engines_without_default(self):
71 71
         self.assertIs(sqlahelper.get_engine("db1"), db1)
72 72
         self.assertIs(sqlahelper.get_engine("db2"), db2)
73 73
         # There should be no default engine
74  
-        self.assertIs(sqlahelper.get_session().bind, None)
75  
-        self.assertIs(sqlahelper.get_base().metadata.bind, None)
76  
-        self.assertRaises(RuntimeError, sqlahelper.get_engine)
  74
+        self.assertIsNone(sqlahelper.get_session().bind)
  75
+        self.assertIsNone(sqlahelper.get_base().metadata.bind)
  76
+        self.assertIsNone(sqlahelper.get_engine())
77 77
 
78 78
 
79 79
 class TestDeclarativeBase(SQLAHelperTestCase):

0 notes on commit b614a84

Please sign in to comment.
Something went wrong with that request. Please try again.