-
Notifications
You must be signed in to change notification settings - Fork 6
/
Persistence.py
75 lines (61 loc) · 2.16 KB
/
Persistence.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import weakref
import pickle
import os
import sys
from PyReprHelpers import better_repr
# Use this class so that we can add methods to it (such as save()).
class Set(set):
pass
class Saver:
def __init__(self, obj, filename):
self.obj = weakref.ref(obj)
self.filename = filename
def __call__(self):
obj = self.obj()
if obj:
obj_repr = better_repr(obj)
if obj_repr[0] == "<":
raise Exception("non-repr-able object: %s" % obj_repr)
f = open(self.filename, "w")
f.write(obj_repr)
f.close()
def load(filename, default_constructor, env=None):
"""
:param str filename:
:param (()->T)|T default_constructor:
:param None|dict[str] env: locals for evaluating the persistence expressions
:rtype: T
"""
from PickleHelpers import is_pickle_format
from PyReprHelpers import is_python_repr_format, load_python_repr_format
import PickleHelpers
PickleHelpers.setup()
import main
from Threading import DoInMainThreadDecoratorNowait
import Logging
filename = main.RootDir + "/" + filename
if os.path.exists(filename) and os.path.getsize(filename) > 0:
try:
Logging.log("Persistence.load %s" % filename)
if is_python_repr_format(filename):
try:
obj = load_python_repr_format(filename, default_constructor=default_constructor, env=env)
except Exception:
sys.excepthook(*sys.exc_info())
sys.exit(1)
elif is_pickle_format(filename):
obj = pickle.load(open(filename, "rb"))
else:
raise Exception("unknown format in %s" % filename)
except Exception:
Logging.log_exception("Persistence.load %s" % filename, *sys.exc_info())
obj = default_constructor()
else:
obj = default_constructor()
if isinstance(obj, set) and not isinstance(obj, Set):
obj = Set(obj)
# Set obj.save() function.
saver = Saver(obj, filename)
obj.save = DoInMainThreadDecoratorNowait(saver)
saver() # save now
return obj