json-store 4.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- json_store/__init__.py +5 -0
- json_store/json_store.py +90 -0
- json_store/shelve2json.py +42 -0
- json_store-4.1.dist-info/METADATA +62 -0
- json_store-4.1.dist-info/RECORD +8 -0
- json_store-4.1.dist-info/WHEEL +4 -0
- json_store-4.1.dist-info/entry_points.txt +2 -0
- json_store-4.1.dist-info/licenses/LICENSE.txt +19 -0
json_store/__init__.py
ADDED
json_store/json_store.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
"""A JSON store to use in place of shelve. Unicode keys, FTW!
|
|
3
|
+
|
|
4
|
+
This is for small stores. Everything is in memory and sync() always writes
|
|
5
|
+
everything out to disk.
|
|
6
|
+
"""
|
|
7
|
+
import json
|
|
8
|
+
import os
|
|
9
|
+
from collections.abc import MutableMapping
|
|
10
|
+
from tempfile import NamedTemporaryFile
|
|
11
|
+
|
|
12
|
+
__all__ = ["JSONStore"]
|
|
13
|
+
|
|
14
|
+
MODE_600 = int("600", 8)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class JSONStore(MutableMapping):
|
|
18
|
+
def __init__(self, path, json_kw=None, mode=MODE_600):
|
|
19
|
+
"""Create a JSONStore object backed by the file at `path`.
|
|
20
|
+
|
|
21
|
+
If a dict is passed in as `json_kw`, it will be used as keyword
|
|
22
|
+
arguments to the json module.
|
|
23
|
+
"""
|
|
24
|
+
self.path = path
|
|
25
|
+
self.json_kw = json_kw or {}
|
|
26
|
+
self.mode = mode
|
|
27
|
+
|
|
28
|
+
self._data = {}
|
|
29
|
+
|
|
30
|
+
self._synced_json_kw = None
|
|
31
|
+
self._needs_sync = False
|
|
32
|
+
|
|
33
|
+
if not os.path.exists(path):
|
|
34
|
+
self.sync(force=True) # write empty dict to disk
|
|
35
|
+
return
|
|
36
|
+
|
|
37
|
+
# load the whole store
|
|
38
|
+
with open(path, "r") as fp:
|
|
39
|
+
self.update(json.load(fp))
|
|
40
|
+
|
|
41
|
+
def __getitem__(self, key):
|
|
42
|
+
return self._data[key]
|
|
43
|
+
|
|
44
|
+
def __setitem__(self, key, value):
|
|
45
|
+
self._data[key] = value
|
|
46
|
+
self._needs_sync = True
|
|
47
|
+
|
|
48
|
+
def __delitem__(self, key):
|
|
49
|
+
del self._data[key]
|
|
50
|
+
self._needs_sync = True
|
|
51
|
+
|
|
52
|
+
def keys(self):
|
|
53
|
+
return self._data.keys()
|
|
54
|
+
|
|
55
|
+
def __iter__(self):
|
|
56
|
+
return iter(self._data)
|
|
57
|
+
|
|
58
|
+
def __len__(self):
|
|
59
|
+
return len(self._data)
|
|
60
|
+
|
|
61
|
+
def _mktemp(self):
|
|
62
|
+
prefix = os.path.basename(self.path) + "."
|
|
63
|
+
dirname = os.path.dirname(self.path)
|
|
64
|
+
return NamedTemporaryFile(mode="w", prefix=prefix, dir=dirname, delete=False)
|
|
65
|
+
|
|
66
|
+
def sync(self, json_kw=None, force=False):
|
|
67
|
+
"""Atomically write the entire store to disk if it's changed.
|
|
68
|
+
|
|
69
|
+
If a dict is passed in as `json_kw`, it will be used as keyword
|
|
70
|
+
arguments to the json module.
|
|
71
|
+
|
|
72
|
+
If force is set True, a new file will be written even if the store
|
|
73
|
+
hasn't changed since last sync.
|
|
74
|
+
"""
|
|
75
|
+
json_kw = json_kw or self.json_kw
|
|
76
|
+
if self._synced_json_kw != json_kw:
|
|
77
|
+
self._needs_sync = True
|
|
78
|
+
|
|
79
|
+
if not (self._needs_sync or force):
|
|
80
|
+
return False
|
|
81
|
+
|
|
82
|
+
with self._mktemp() as fp:
|
|
83
|
+
json.dump(self._data, fp, **json_kw)
|
|
84
|
+
if self.mode != MODE_600: # _mktemp uses 0600 by default
|
|
85
|
+
os.chmod(fp.name, self.mode)
|
|
86
|
+
os.replace(fp.name, self.path)
|
|
87
|
+
|
|
88
|
+
self._synced_json_kw = json_kw
|
|
89
|
+
self._needs_sync = False
|
|
90
|
+
return True
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#! /usr/bin/env python
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
"""Naïvely create a json_store file from a shelve DB."""
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
import shelve
|
|
8
|
+
|
|
9
|
+
import json_store
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def convert(oldfile: str):
|
|
13
|
+
if not os.path.isfile(oldfile):
|
|
14
|
+
raise ValueError("No such file: {}".format(oldfile))
|
|
15
|
+
|
|
16
|
+
name = oldfile
|
|
17
|
+
# remove extensions that are implicitly added by the underlying DBM module
|
|
18
|
+
name = name.rsplit(".dat")[0] # Windows
|
|
19
|
+
name = name.rsplit(".db")[0] # macOS
|
|
20
|
+
|
|
21
|
+
data = shelve.open(name)
|
|
22
|
+
newfile = name + ".json"
|
|
23
|
+
store = json_store.open(newfile)
|
|
24
|
+
store.update(data)
|
|
25
|
+
store.sync()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def main(argv=sys.argv):
|
|
29
|
+
if len(argv) < 2:
|
|
30
|
+
print("Usage: {0[0]} <shelve.db>".format(sys.argv))
|
|
31
|
+
return 1
|
|
32
|
+
|
|
33
|
+
try:
|
|
34
|
+
convert(argv[1])
|
|
35
|
+
except Exception as e:
|
|
36
|
+
print(str(e), file=sys.stderr)
|
|
37
|
+
return 1
|
|
38
|
+
return 0
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
if __name__ == "__main__":
|
|
42
|
+
sys.exit(main())
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: json-store
|
|
3
|
+
Version: 4.1
|
|
4
|
+
Summary: A shelve-like store using JSON serialization.
|
|
5
|
+
Project-URL: Homepage, https://github.com/brainsik/json-store
|
|
6
|
+
Project-URL: Repository, https://github.com/brainsik/json-store.git
|
|
7
|
+
Project-URL: Issues, https://github.com/brainsik/json-store/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/brainsik/json-store/releases
|
|
9
|
+
Author-email: jeremy avnet <json-store@theory.org>
|
|
10
|
+
License: Copyright (c) 2010-2024 Jeremy Avnet
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in
|
|
20
|
+
all copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
28
|
+
THE SOFTWARE.
|
|
29
|
+
License-File: LICENSE.txt
|
|
30
|
+
Keywords: json,shelve
|
|
31
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
32
|
+
Classifier: Intended Audience :: Developers
|
|
33
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
34
|
+
Classifier: Operating System :: OS Independent
|
|
35
|
+
Classifier: Programming Language :: Python :: 3
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
41
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
42
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
43
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
44
|
+
Requires-Python: >=3.8
|
|
45
|
+
Description-Content-Type: text/markdown
|
|
46
|
+
|
|
47
|
+
[](https://github.com/brainsik/json-store/actions/workflows/main.yml)
|
|
48
|
+

|
|
49
|
+

|
|
50
|
+
|
|
51
|
+
# JSON Store
|
|
52
|
+
|
|
53
|
+
JSON store is a simple replacement for shelve. It writes JSON serialized files,
|
|
54
|
+
accepts unicode keys, and tracks whether the store has been changed since last
|
|
55
|
+
sync. It has no dependencies.
|
|
56
|
+
|
|
57
|
+
JSON store is intended for smaller stores. Everything is kept in memory and `sync()`
|
|
58
|
+
writes the whole store to disk.
|
|
59
|
+
|
|
60
|
+
For issues and development, see:
|
|
61
|
+
|
|
62
|
+
https://github.com/brainsik/json-store
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
json_store/__init__.py,sha256=CN4YZ7ExdI603zBDfHshBRqJEUJ5vik630Tin3nkfdQ,73
|
|
2
|
+
json_store/json_store.py,sha256=4HkIRYTIiBI9FYnXTMLqI5taOeaf-aiXNro_BD_vV7k,2538
|
|
3
|
+
json_store/shelve2json.py,sha256=SQVs-6rFcTeyKFF-R4E32paMb9yOMn5bts2spp0s6o4,909
|
|
4
|
+
json_store-4.1.dist-info/METADATA,sha256=Hz75FfVpwO_mOBvCPFn3lXaUQI9Pd930-1_PUbxDzRo,3130
|
|
5
|
+
json_store-4.1.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
|
6
|
+
json_store-4.1.dist-info/entry_points.txt,sha256=yuGm6KJT2rMFFV_Cy7fU8wc6mIt4a1UypNkNTaLcp2g,60
|
|
7
|
+
json_store-4.1.dist-info/licenses/LICENSE.txt,sha256=_AHzh2pqgHGGB_7VPIlfLxo0C9VORfV4eoev9CeSCCw,1061
|
|
8
|
+
json_store-4.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2010-2024 Jeremy Avnet
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|