-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Store JSON objects instead of pickle objects #1051
Open
masipcat
wants to merge
9
commits into
master
Choose a base branch
from
json-storage
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+438
−18
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
00557c7
Poc
masipcat 42e80a2
flake8
masipcat 40adcb0
mypy
masipcat 23d9613
Update orjson to 3.4.4
masipcat 1391002
fix
masipcat eab8124
fix
masipcat 0c5a267
Fix test commands
masipcat 81a80e2
Fixes
masipcat 46e14ac
Update fixtures.py
masipcat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import json # noqa | ||
from . import pickle # noqa |
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,4 @@ | ||
from .reader import * # noqa | ||
from .value_deserializers import * # noqa | ||
from .value_serializers import * # noqa | ||
from .writer import * # noqa |
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,9 @@ | ||
from zope.interface import Interface | ||
|
||
|
||
class IStorageDeserializer(Interface): | ||
pass | ||
|
||
|
||
class IStorageSerializer(Interface): | ||
pass |
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,47 @@ | ||
from .interfaces import IStorageDeserializer | ||
from guillotina import configure | ||
from guillotina.component import get_adapter | ||
from guillotina.component import query_adapter | ||
from guillotina.db.interfaces import IReader | ||
from guillotina.db.orm.interfaces import IBaseObject | ||
from guillotina.utils import resolve_dotted_name | ||
from zope.interface.interface import Interface | ||
|
||
import asyncpg | ||
import orjson | ||
|
||
|
||
def recursive_load(d): | ||
if isinstance(d, dict): | ||
if "__class__" in d: | ||
adapter = query_adapter(d, IStorageDeserializer, name=d["__class__"]) | ||
if adapter is None: | ||
adapter = get_adapter(d, IStorageDeserializer, name="$.AnyObjectDict") | ||
return adapter() | ||
else: | ||
for k, v in d.items(): | ||
d[k] = recursive_load(v) | ||
return d | ||
elif isinstance(d, list): | ||
return [recursive_load(v) for v in d] | ||
else: | ||
return d | ||
|
||
|
||
@configure.adapter(for_=(Interface), provides=IReader, name="json") | ||
def reader(result_: asyncpg.Record) -> IBaseObject: | ||
result = dict(result_) | ||
state = orjson.loads(result["state"]) | ||
dotted_class = state.pop("__class__") | ||
type_class = resolve_dotted_name(dotted_class) | ||
obj = type_class.__new__(type_class) | ||
|
||
state = recursive_load(state) | ||
|
||
obj.__dict__.update(state) | ||
obj.__uuid__ = result["zoid"] | ||
obj.__serial__ = result["tid"] | ||
obj.__name__ = result["id"] | ||
obj.__provides__ = obj.__providedBy__ | ||
|
||
return obj |
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,128 @@ | ||
from .interfaces import IStorageDeserializer | ||
from .reader import recursive_load | ||
from dateutil.parser import parse | ||
from guillotina import configure | ||
from guillotina.interfaces.security import PermissionSetting | ||
from guillotina.security.securitymap import SecurityMap | ||
from guillotina.utils import resolve_dotted_name | ||
from zope.interface import Interface | ||
from zope.interface.declarations import Implements | ||
|
||
import base64 | ||
import pickle | ||
|
||
|
||
class PickleDeserializer: | ||
def __init__(self, data): | ||
self.data = data | ||
|
||
def __call__(self): | ||
return pickle.loads(base64.b64decode(self.data["__pickle__"])) | ||
|
||
|
||
@configure.adapter( | ||
for_=Interface, provides=IStorageDeserializer, name="$.AnyObjectDict", | ||
) | ||
class AnyObjectDeserializer: | ||
def __init__(self, data): | ||
self.data = data | ||
|
||
def __call__(self): | ||
klass = self.data.pop("__class__") | ||
type_class = resolve_dotted_name(klass) | ||
obj = type_class.__new__(type_class) | ||
obj.__dict__ = recursive_load(self.data) | ||
return obj | ||
|
||
|
||
@configure.adapter( | ||
for_=Interface, provides=IStorageDeserializer, name="guillotina.fields.annotation.BucketListValue", | ||
) | ||
class BucketListValueDeserializer(PickleDeserializer): | ||
pass | ||
|
||
|
||
@configure.adapter( | ||
for_=Interface, provides=IStorageDeserializer, name="guillotina.blob.Blob", | ||
) | ||
class BlobDeserializer(PickleDeserializer): | ||
pass | ||
|
||
|
||
@configure.adapter(for_=Interface, provides=IStorageDeserializer, name="builtins.set") | ||
@configure.adapter(for_=Interface, provides=IStorageDeserializer, name="builtins.frozenset") | ||
class SetDeseializer: | ||
def __init__(self, data): | ||
self.data = data | ||
|
||
def __call__(self): | ||
data = self.data | ||
if data["__class__"] == "builtins.set": | ||
return set(data["__value__"]) | ||
else: | ||
return frozenset(data["__value__"]) | ||
|
||
|
||
@configure.adapter(for_=Interface, provides=IStorageDeserializer, name="datetime.datetime") | ||
class DatetimeDeserializer: | ||
def __init__(self, data): | ||
self.data = data | ||
|
||
def __call__(self): | ||
return parse(self.data["__value__"]) | ||
|
||
|
||
@configure.adapter( | ||
for_=Interface, provides=IStorageDeserializer, name="zope.interface.declarations.Implements", | ||
) | ||
class ImplementsDeseializer: | ||
def __init__(self, data): | ||
self.data = data | ||
|
||
def __call__(self): | ||
data = self.data | ||
obj = Implements() | ||
obj.__bases__ = [resolve_dotted_name(iface) for iface in data["__ifaces__"]] | ||
obj.__name__ = data["__name__"] | ||
return obj | ||
|
||
|
||
@configure.adapter( | ||
for_=Interface, provides=IStorageDeserializer, name="zope.interface.Provides", | ||
) | ||
class ProvidesDeserializer: | ||
def __init__(self, data): | ||
self.data = data | ||
|
||
def __call__(self): | ||
# from zope.interface import Provides # type: ignore | ||
# obj = Provides(*[resolve_dotted_name(iface) for iface in self.data["__ifaces__"]]) | ||
# return obj | ||
return pickle.loads(base64.b64decode(self.data["__pickle__"])) | ||
|
||
|
||
@configure.adapter( | ||
for_=Interface, provides=IStorageDeserializer, name="guillotina.security.securitymap.SecurityMap", | ||
) | ||
class SecurityMapDeserializer: | ||
def __init__(self, data): | ||
self.data = data | ||
|
||
def __call__(self): | ||
sec_map = SecurityMap.__new__(SecurityMap) | ||
sec_map.__dict__.update( | ||
{ | ||
# byrow | ||
k: { | ||
# Role | ||
k2: { | ||
# Principal | ||
k3: PermissionSetting(v3) | ||
for k3, v3 in v2.items() | ||
} | ||
for k2, v2 in v.items() | ||
} | ||
for k, v in self.data["__dict__"].items() | ||
} | ||
) | ||
return sec_map |
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,127 @@ | ||
from .interfaces import IStorageSerializer | ||
from guillotina import configure | ||
from guillotina.utils import get_dotted_name | ||
from zope.interface import Interface | ||
|
||
import base64 | ||
import pickle | ||
|
||
|
||
class PickleSerializer: | ||
dotted_name: str | ||
|
||
def __init__(self, obj): | ||
self.obj = obj | ||
|
||
def __call__(self): | ||
return { | ||
"__class__": self.dotted_name, | ||
"__pickle__": base64.b64encode(pickle.dumps(self.obj)).decode(), | ||
} | ||
|
||
|
||
@configure.adapter( | ||
for_=Interface, provides=IStorageSerializer, name="$.AnyObjectDict", | ||
) | ||
class AnyObjectSerializer: | ||
def __init__(self, obj): | ||
self.obj = obj | ||
|
||
def __call__(self): | ||
return { | ||
**{"__class__": get_dotted_name(self.obj)}, | ||
**self.obj.__dict__, | ||
} | ||
|
||
|
||
@configure.adapter( | ||
for_=Interface, provides=IStorageSerializer, name="guillotina.fields.annotation.BucketListValue", | ||
) | ||
class BucketListValueSerializer(PickleSerializer): | ||
""" | ||
The internal structure of the BucketListValue contains a dictionary | ||
whose keys are of type other than string | ||
""" | ||
|
||
dotted_name = "guillotina.fields.annotation.BucketListValue" | ||
|
||
|
||
@configure.adapter( | ||
for_=Interface, provides=IStorageSerializer, name="guillotina.blob.Blob", | ||
) | ||
class BlobSerializer(PickleSerializer): | ||
""" | ||
The internal structure of the Blob contains a dictionary | ||
whose keys are of type other than string | ||
""" | ||
|
||
dotted_name = "guillotina.blob.Blob" | ||
|
||
|
||
@configure.adapter(for_=Interface, provides=IStorageSerializer, name="builtins.set") | ||
@configure.adapter(for_=Interface, provides=IStorageSerializer, name="builtins.frozenset") | ||
class SetSerializer: | ||
def __init__(self, obj): | ||
self.obj = obj | ||
|
||
def __call__(self): | ||
return { | ||
"__class__": get_dotted_name(type(self.obj)), | ||
"__value__": list(self.obj), | ||
} | ||
|
||
|
||
@configure.adapter(for_=Interface, provides=IStorageSerializer, name="datetime.datetime") | ||
class DatetimeSerializer: | ||
def __init__(self, obj): | ||
self.obj = obj | ||
|
||
def __call__(self): | ||
return { | ||
"__class__": get_dotted_name(type(self.obj)), | ||
"__value__": self.obj.isoformat(), | ||
} | ||
|
||
|
||
@configure.adapter( | ||
for_=Interface, provides=IStorageSerializer, name="zope.interface.declarations.Implements", | ||
) | ||
class ImplementsSerializer: | ||
def __init__(self, obj): | ||
self.obj = obj | ||
|
||
def __call__(self): | ||
return { | ||
"__class__": "zope.interface.declarations.Implements", | ||
"__name__": self.obj.__name__, | ||
"__ifaces__": [i.__identifier__ for i in self.obj.flattened()], | ||
} | ||
|
||
|
||
@configure.adapter( | ||
for_=Interface, provides=IStorageSerializer, name="zope.interface.Provides", | ||
) | ||
class ProvidesSerializer: | ||
def __init__(self, obj): | ||
self.obj = obj | ||
|
||
def __call__(self): | ||
return { | ||
"__class__": "zope.interface.Provides", | ||
# "__ifaces__": [i.__identifier__ for i in self.obj.flattened()], | ||
"__pickle__": base64.b64encode(pickle.dumps(self.obj)).decode(), | ||
} | ||
|
||
|
||
@configure.adapter( | ||
for_=Interface, provides=IStorageSerializer, name="guillotina.security.securitymap.SecurityMap", | ||
) | ||
class SecurityMapSerializer: | ||
def __init__(self, obj): | ||
self.obj = obj | ||
|
||
def __call__(self): | ||
return { | ||
"__class__": "guillotina.security.securitymap.SecurityMap", | ||
"__dict__": self.obj.__dict__, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does anyone know how to make it work?