-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor jsonpickle logic out of visitors
- Loading branch information
Showing
9 changed files
with
103 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/localstack_persist/serialization/jsonpickle/serializer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import json | ||
import logging | ||
from typing import Any | ||
import jsonpickle.handlers | ||
import jsonpickle.tags | ||
|
||
from .handlers import register_handlers | ||
|
||
# Track version for future handling of backward (or forward) incompatible changes. | ||
# This is the "serialisation format" version, which is different to the localstack-persist version. | ||
SER_VERSION_KEY = "v" | ||
SER_VERSION = 1 | ||
|
||
DATA_KEY = "data" | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class JsonPickleSerializer: | ||
_json_encoder = json.JSONEncoder(check_circular=False, separators=(",", ":")) | ||
|
||
def serialize(self, file_path: str, data: Any): | ||
register_handlers() | ||
|
||
pickler = jsonpickle.Pickler(keys=True, warn=True) | ||
|
||
envelope = {SER_VERSION_KEY: SER_VERSION, DATA_KEY: pickler.flatten(data)} | ||
|
||
with open(file_path, "w") as file: | ||
for chunk in self._json_encoder.iterencode(envelope): | ||
file.write(chunk) | ||
|
||
def deserialize(self, file_path: str) -> Any: | ||
register_handlers() | ||
|
||
with open(file_path) as file: | ||
envelope: dict = json.load(file) | ||
|
||
version = envelope.get(SER_VERSION_KEY, None) | ||
if version != SER_VERSION: | ||
LOG.warning( | ||
"Persisted state at %s has unsupported version %s - trying to load it anyway...", | ||
file_path, | ||
version, | ||
) | ||
|
||
unpickler = jsonpickle.Unpickler(keys=True, safe=True, on_missing="error") | ||
return unpickler.restore(envelope[DATA_KEY]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from collections.abc import Callable | ||
|
||
|
||
def once(f: Callable[[], None]) -> Callable[[], None]: | ||
has_run = False | ||
|
||
def wrapper(): | ||
nonlocal has_run | ||
if not has_run: | ||
has_run = True | ||
return f() | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters