Recursively parse activities
This commit is contained in:
@ -2,7 +2,7 @@
|
||||
import inspect
|
||||
import sys
|
||||
|
||||
from .base_activity import ActivityEncoder, Signature
|
||||
from .base_activity import ActivityEncoder, Signature, naive_parse
|
||||
from .base_activity import Link, Mention
|
||||
from .base_activity import ActivitySerializerError, resolve_remote_id
|
||||
from .image import Image
|
||||
@ -23,3 +23,7 @@ from .verbs import Add, AddBook, AddListItem, Remove
|
||||
cls_members = inspect.getmembers(sys.modules[__name__], inspect.isclass)
|
||||
activity_objects = {c[0]: c[1] for c in cls_members \
|
||||
if hasattr(c[1], 'to_model')}
|
||||
|
||||
def parse(activity_json):
|
||||
''' figure out what activity this is and parse it '''
|
||||
return naive_parse(activity_objects, activity_json)
|
||||
|
@ -40,6 +40,17 @@ class Signature:
|
||||
signatureValue: str
|
||||
type: str = 'RsaSignature2017'
|
||||
|
||||
def naive_parse(activity_objects, activity_json):
|
||||
''' this navigates circular import issues '''
|
||||
try:
|
||||
activity_type = activity_json['type']
|
||||
print(activity_type)
|
||||
serializer = activity_objects[activity_type]
|
||||
print(serializer)
|
||||
except KeyError:
|
||||
raise ActivitySerializerError('Invalid type "%s"' % activity_type)
|
||||
|
||||
return serializer(activity_objects=activity_objects, **activity_json)
|
||||
|
||||
@dataclass(init=False)
|
||||
class ActivityObject:
|
||||
@ -47,13 +58,17 @@ class ActivityObject:
|
||||
id: str
|
||||
type: str
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
def __init__(self, activity_objects=None, **kwargs):
|
||||
''' this lets you pass in an object with fields that aren't in the
|
||||
dataclass, which it ignores. Any field in the dataclass is required or
|
||||
has a default value '''
|
||||
for field in fields(self):
|
||||
try:
|
||||
print(field.name, field.type)
|
||||
value = kwargs[field.name]
|
||||
if field.type == 'ActivityObject' and activity_objects:
|
||||
value = naive_parse(activity_objects, value)
|
||||
|
||||
except KeyError:
|
||||
if field.default == MISSING and \
|
||||
field.default_factory == MISSING:
|
||||
|
Reference in New Issue
Block a user