mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2024-12-19 13:07:56 +00:00
py2/py3 glue code for json dumping
This commit is contained in:
parent
b47381401c
commit
85fa8fe32e
@ -125,8 +125,7 @@ def _upgrade_pickle_to_json(state_path, convert_pickle):
|
||||
with state_path.open("rb") as f:
|
||||
state = pickle.load(f)
|
||||
new_state = convert_pickle(state)
|
||||
with json_state_path.open("wb") as f:
|
||||
json.dump(new_state, f)
|
||||
_dump_json_to_file(new_state, json_state_path)
|
||||
|
||||
# we've written the JSON, delete the pickle
|
||||
state_path.remove()
|
||||
@ -151,6 +150,18 @@ def _confirm_json_format(fp):
|
||||
return jsonfp
|
||||
|
||||
|
||||
def _dump_json_to_file(js, afile):
|
||||
"""
|
||||
Dump the JSON object `js` to the FilePath `afile`
|
||||
"""
|
||||
with afile.open("wb") as f:
|
||||
data = json.dumps(js)
|
||||
if PY2:
|
||||
f.write(data)
|
||||
else:
|
||||
f.write(data.encode("utf8"))
|
||||
|
||||
|
||||
class _LeaseStateSerializer(object):
|
||||
"""
|
||||
Read and write state for LeaseCheckingCrawler. This understands
|
||||
@ -174,8 +185,7 @@ class _LeaseStateSerializer(object):
|
||||
:returns: None
|
||||
"""
|
||||
tmpfile = self._path.siblingExtension(".tmp")
|
||||
with tmpfile.open("wb") as f:
|
||||
json.dump(data, f)
|
||||
_dump_json_to_file(data, tmpfile)
|
||||
fileutil.move_into_place(tmpfile.path, self._path.path)
|
||||
return None
|
||||
|
||||
|
@ -14,6 +14,7 @@ from allmydata.storage.crawler import (
|
||||
ShareCrawler,
|
||||
_confirm_json_format,
|
||||
_convert_cycle_data,
|
||||
_dump_json_to_file,
|
||||
)
|
||||
from allmydata.storage.shares import get_share_file
|
||||
from allmydata.storage.common import UnknownMutableContainerVersionError, \
|
||||
@ -48,8 +49,7 @@ class _HistorySerializer(object):
|
||||
self._path = _confirm_json_format(FilePath(history_path))
|
||||
|
||||
if not self._path.exists():
|
||||
with self._path.open("wb") as f:
|
||||
json.dump({}, f)
|
||||
_dump_json_to_file({}, self._path)
|
||||
|
||||
def load(self):
|
||||
"""
|
||||
@ -65,8 +65,7 @@ class _HistorySerializer(object):
|
||||
"""
|
||||
Serialize the existing data as JSON.
|
||||
"""
|
||||
with self._path.open("wb") as f:
|
||||
json.dump(new_history, f)
|
||||
_dump_json_to_file(new_history, self._path)
|
||||
return None
|
||||
|
||||
|
||||
|
@ -1163,7 +1163,7 @@ class LeaseCrawler(unittest.TestCase, pollmixin.PollMixin):
|
||||
storage = root.child("storage")
|
||||
storage.makedirs()
|
||||
test_pickle = storage.child("lease_checker.state")
|
||||
with test_pickle.open("w") as local, original_pickle.open("r") as remote:
|
||||
with test_pickle.open("wb") as local, original_pickle.open("rb") as remote:
|
||||
local.write(remote.read())
|
||||
|
||||
# convert from pickle format to JSON
|
||||
@ -1365,7 +1365,7 @@ class LeaseCrawler(unittest.TestCase, pollmixin.PollMixin):
|
||||
storage = root.child("storage")
|
||||
storage.makedirs()
|
||||
test_pickle = storage.child("lease_checker.history")
|
||||
with test_pickle.open("w") as local, original_pickle.open("r") as remote:
|
||||
with test_pickle.open("wb") as local, original_pickle.open("rb") as remote:
|
||||
local.write(remote.read())
|
||||
|
||||
# convert from pickle format to JSON
|
||||
|
Loading…
Reference in New Issue
Block a user