~allenap/storm/blocked-references

« back to all changes in this revision

Viewing changes to storm/references.py

  • Committer: Gavin Panella
  • Date: 2011-05-04 14:44:18 UTC
  • Revision ID: gavin@gromper.net-20110504144418-y946anf33vyf80g2
Make it possible to block Reference gets that hit the database.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# You should have received a copy of the GNU Lesser General Public License
19
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
20
#
 
21
 
 
22
from contextlib import contextmanager
21
23
import weakref
22
24
 
23
25
from storm.exceptions import (
103
105
    C{MyGuy.other_guy_id} or C{OtherGuy.my_guy_id} accordingly.
104
106
    """
105
107
 
 
108
    _blocked = False
 
109
 
 
110
    @classmethod
 
111
    @contextmanager
 
112
    def blocked(cls):
 
113
        old_blocked = cls._blocked
 
114
        try:
 
115
            cls._blocked = True
 
116
            yield
 
117
        finally:
 
118
            cls._blocked = old_blocked
 
119
 
 
120
    @classmethod
 
121
    @contextmanager
 
122
    def unblocked(cls):
 
123
        old_blocked = cls._blocked
 
124
        try:
 
125
            cls._blocked = False
 
126
            yield
 
127
        finally:
 
128
            cls._blocked = old_blocked
 
129
 
106
130
    # Must initialize _relation later because we don't want to resolve
107
131
    # string references at definition time, since classes refered to might
108
132
    # not be available yet.  Notice that this attribute is "public" to the
141
165
        if local is None:
142
166
            return self
143
167
 
 
168
        if self._blocked:
 
169
            # XXX: We probably want to block on the remote store (which might
 
170
            # be different).
 
171
            with Store.of(local).blocked():
 
172
                return self.get(local)
 
173
        else:
 
174
            return self.get(local)
 
175
 
 
176
    def get(self, local):
144
177
        remote = self._relation.get_remote(local)
145
178
        if remote is not None:
146
179
            return remote