Change serialization backend from pickle to json

This commit is contained in:
Eric Torres 2019-04-10 10:23:56 -07:00
parent ea0a629fd9
commit 4415a82da0
2 changed files with 11 additions and 9 deletions

View File

@ -3,9 +3,8 @@
.. module:: rbackup.struct.hierarchy
:synopsis: Classes for creating the backup hierarchy.
"""
import json
import logging
import pickle
from os import PathLike
from pathlib import Path
@ -14,8 +13,8 @@ syslog = logging.getLogger(__name__)
# ========== Constants ==========
METADATA_READ = "rb"
METADATA_WRITE = "wb"
METADATA_READ = "r"
METADATA_WRITE = "w"
# ========== Classes ==========
@ -101,22 +100,25 @@ class Hierarchy(PathLike):
"""Read this repository's metadata from its file and
then return it.
:rtype: dict
:rtype: type that the data is serialized as
"""
syslog.debug(f"Reading metadata from {self.metadata_path}")
with self.metadata_path.open(mode=METADATA_READ) as mfile:
return pickle.load(mfile)
return json.load(mfile)
def write_metadata(self):
def write_metadata(self, attr):
"""Write this repository's metadata to its file.
Note that this write operation is atomic to the caller.
:param attr: data to write to file
:type attr: class member to write
"""
syslog.debug(f"Writing metadata to {self.metadata_path}")
tmpfile = self.metadata_path.with_suffix(".tmp")
with tmpfile.open(mode=METADATA_WRITE) as mfile:
pickle.dump(self._data, mfile)
json.dump(attr, mfile)
tmpfile.rename(self.metadata_path)

View File

@ -1,4 +1,4 @@
import doctest
import json
import unittest
from hypothesis import given