forked from Code0x58/python-jsonstore
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonstore.py
178 lines (155 loc) · 5.18 KB
/
jsonstore.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
# -*- encoding: UTF-8 -*-
"""
Provides a Python class that maps values to/from a JSON file
"""
from __future__ import absolute_import
import json
import os.path
import sys
from collections import OrderedDict
from copy import deepcopy
__all__ = ["JsonStore"]
class JsonStore(object):
"""A class to provide object based access to a JSON file"""
def __enter__(self):
current_state = self.__dict__["_data"]
self.__dict__["_states"].append(current_state)
self.__dict__["_data"] = deepcopy(current_state)
return self
def __exit__(self, *args):
previous_state = self.__dict__["_states"].pop()
if any(args):
self.__dict__["_data"] = previous_state
elif not self.__dict__["_states"]:
self._save()
def _do_auto_commit(self):
if self._auto_commit and not self.__dict__["_states"]:
self._save()
def _load(self):
if not os.path.exists(self._path):
with open(self._path, "w+b") as store:
store.write("{}".encode("utf-8"))
with open(self._path, "r+b") as store:
raw_data = store.read().decode("utf-8")
if not raw_data:
data = OrderedDict()
else:
data = json.loads(raw_data, object_pairs_hook=OrderedDict)
if not isinstance(data, dict):
raise ValueError("Root element is not an object")
self.__dict__["_data"] = data
def _save(self):
temp = self._path + "~"
with open(temp, "wb") as store:
output = json.dumps(self._data, indent=self._indent)
store.write(output.encode("utf-8"))
if sys.version_info >= (3, 3):
os.replace(temp, self._path)
elif os.name == "windows":
os.remove(self._path)
os.rename(temp, self._path)
else:
os.rename(temp, self._path)
def __init__(self, path, indent=2, auto_commit=True):
self.__dict__.update(
{
"_auto_commit": auto_commit,
"_data": None,
"_path": path,
"_indent": indent,
"_states": [],
}
)
self._load()
def __getattr__(self, key):
if key in self._data:
return deepcopy(self._data[key])
else:
raise AttributeError(key)
@classmethod
def _valid_object(cls, obj, parents=None):
"""
Determine if the object can be encoded into JSON
"""
# pylint: disable=unicode-builtin,long-builtin
if isinstance(obj, (dict, list)):
if parents is None:
parents = []
elif any(o is obj for o in parents):
raise ValueError("Cycle detected in list/dictionary")
parents.append(obj)
if isinstance(obj, dict):
return all(
cls._valid_string(k) and cls._valid_object(v, parents)
for k, v in obj.items()
)
elif isinstance(obj, (list, tuple)):
return all(cls._valid_object(o, parents) for o in obj)
else:
return cls._valid_value(obj)
@classmethod
def _valid_value(cls, value):
if isinstance(value, (bool, int, float, type(None))):
return True
elif sys.version_info < (3,) and isinstance(value, long):
return True
else:
return cls._valid_string(value)
@classmethod
def _valid_string(cls, value):
if isinstance(value, str):
return True
elif sys.version_info < (3,):
return isinstance(value, unicode)
else:
return False
def __setattr__(self, key, value):
if not self._valid_object(value):
raise AttributeError
self._data[key] = deepcopy(value)
self._do_auto_commit()
def __delattr__(self, key):
del self._data[key]
def __get_obj(self, full_path):
"""
Returns the object which is under the given path
"""
if isinstance(full_path, (tuple, list)):
steps = full_path
else:
steps = full_path.split(".")
path = []
obj = self._data
if not full_path:
return obj
for step in steps:
path.append(step)
try:
obj = obj[step]
except KeyError:
raise KeyError(".".join(path))
return obj
def __setitem__(self, name, value):
path, _, key = name.rpartition(".")
if self._valid_object(value):
dictionary = self.__get_obj(path)
dictionary[key] = deepcopy(value)
self._do_auto_commit()
else:
raise AttributeError
def __getitem__(self, key):
obj = self.__get_obj(key)
if obj is self._data:
raise KeyError
return deepcopy(obj)
def __delitem__(self, name):
if isinstance(name, (tuple, list)):
path = name[:-1]
key = name[-1]
else:
path, _, key = name.rpartition(".")
obj = self.__get_obj(path)
del obj[key]
def __contains__(self, key):
return key in self._data