forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfreezer.py
More file actions
56 lines (42 loc) · 2.21 KB
/
freezer.py
File metadata and controls
56 lines (42 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright 2009-2017 Ram Rachum.
# This program is distributed under the MIT license.
import abc
from python_toolbox import context_management
from python_toolbox import misc_tools
from python_toolbox import caching
from .delegatee_context_manager import DelegateeContextManager
class Freezer(context_management.DelegatingContextManager):
'''
A freezer is used as a context manager to "freeze" and "thaw" an object.
Different kinds of objects have different concepts of "freezing" and
"thawing": A GUI widget could be graphically frozen, preventing the OS from
drawing any changes to it, and then when its thawed have all the changes
drawn at once. As another example, an ORM could be frozen to have it not
write to the database while a suite it being executed, and then have it
write all the data at once when thawed.
This class only implements the abstract behavior of a freezer: It is a
reentrant context manager which has handlers for freezing and thawing, and
its level of frozenness can be checked by accessing the attribute
`.frozen`. It's up to subclasses to override `freeze_handler` and
`thaw_handler` to do whatever they should do on freeze and thaw. Note that
you can override either of these methods to be a no-op, sometimes even both
methods, and still have a useful freezer by checking the property `.frozen`
in the logic of the parent object.
'''
delegatee_context_manager = caching.CachedProperty(DelegateeContextManager)
'''The context manager which implements our `__enter__` and `__exit__`.'''
frozen = misc_tools.ProxyProperty(
'.delegatee_context_manager.depth'
)
'''
An integer specifying the freezer's level of frozenness.
If the freezer is not frozen, it's `0`. When it's frozen, it becomes `1`,
and then every time the freezer is used as a context manager the `frozen`
level increases. When reduced to `0` again the freezer is said to have
thawed.
This can be conveniently used as a boolean, i.e. `if my_freezer.frozen:`.
'''
def freeze_handler(self):
'''Do something when the object gets frozen.'''
def thaw_handler(self):
'''Do something when the object gets thawed.'''