aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2019-07-20 17:30:42 +0200
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2019-07-20 17:31:14 +0200
commit4852ba0f09ececeb3f542801bedf981217ba6a4c (patch)
tree539a184b0130f9444d30e99387aa7d419010a01a
parent1396f2ef54b71d4344e28329a4d874a73e2e8e12 (diff)
Refactor Object to be a dataclass
-rw-r--r--pyapd/objects.py98
-rw-r--r--tests/test_app.py2
-rw-r--r--tests/test_memory_store.py4
-rw-r--r--tests/test_objects.py4
-rw-r--r--tests/test_sqlite_store.py4
5 files changed, 51 insertions, 61 deletions
diff --git a/pyapd/objects.py b/pyapd/objects.py
index 690c3f9..c8bbbb0 100644
--- a/pyapd/objects.py
+++ b/pyapd/objects.py
@@ -1,72 +1,62 @@
+import dataclasses
import json
-class Object():
- PROPERTIES = [
- 'type',
- 'attachment',
- 'attributedTo',
- 'audience',
- 'content',
- 'context',
- 'contentMap',
- 'name',
- 'nameMap',
- 'endTime',
- 'generator',
- 'icon',
- 'image',
- 'inReplyTo',
- 'location',
- 'preview',
- 'published',
- 'replies',
- 'startTime',
- 'summary',
- 'summaryMap',
- 'tag',
- 'updated',
- 'url',
- 'to',
- 'bto',
- 'cc',
- 'bcc',
- 'mediaType',
- 'duration',
- ]
-
- def __init__(self, *args, **kw):
- self.context = "https://www.w3.org/ns/activitystreams"
- for p in self.PROPERTIES:
- if p in kw:
- setattr(self, "ap_"+p, kw[p])
- if 'id' in kw:
- self.ap_id = kw['id']
- else:
+@dataclasses.dataclass
+class Object:
+ # TODO: assign the correct type
+ ap_id: str = ''
+ ap_type: str = ''
+ ap_attachment: str = ''
+ ap_attributedTo: str = ''
+ ap_audience: str = ''
+ ap_content: str = ''
+ ap_context: str = ''
+ ap_contentMap: str = ''
+ ap_name: str = ''
+ ap_nameMap: str = ''
+ ap_endTime: str = ''
+ ap_generator: str = ''
+ ap_icon: str = ''
+ ap_image: str = ''
+ ap_inReplyTo: str = ''
+ ap_location: str = ''
+ ap_preview: str = ''
+ ap_published: str = ''
+ ap_replies: str = ''
+ ap_startTime: str = ''
+ ap_summary: str = ''
+ ap_summaryMap: str = ''
+ ap_tag: str = ''
+ ap_updated: str = ''
+ ap_url: str = ''
+ ap_to: str = ''
+ ap_bto: str = ''
+ ap_cc: str = ''
+ ap_bcc: str = ''
+ ap_mediaType: str = ''
+ ap_duration: str = ''
+
+ def __post_init__(self):
+ if not self.ap_id:
self.ap_id = self._generate_id()
@classmethod
def from_jsons(cls, data: str):
- return cls(**json.loads(data))
+ kw = {}
+ for k, v in json.loads(data).items():
+ kw['ap_'+k] = v
+ return cls(**kw)
def to_jsonable(self):
data = {
- 'id': self.ap_id,
}
- for p in self.PROPERTIES:
- v = getattr(self, "ap_"+p, None)
- if v is not None:
- data[p] = v
+ for k, v in dataclasses.asdict(self).items():
+ data[k[3:]] = v
return data
def to_jsons(self):
return json.dumps(self.to_jsonable())
- def __eq__(self, other):
- for i in self.PROPERTIES:
- if getattr(self, "ap_"+i, "") != getattr(other, "ap_"+i, ""):
- return False
- return True
-
def _generate_id(self):
raise NotImplementedError
diff --git a/tests/test_app.py b/tests/test_app.py
index f78149b..1f9116b 100644
--- a/tests/test_app.py
+++ b/tests/test_app.py
@@ -15,7 +15,7 @@ class TestApp(AsyncHTTPTestCase):
def test_object_view(self):
oid = self.get_url('/object/12345')
- obj = objects.Object(id=oid)
+ obj = objects.Object(ap_id=oid)
self.app.store.add(obj)
res = self.fetch('/object/12345')
self.assertEqual(res.code, 200)
diff --git a/tests/test_memory_store.py b/tests/test_memory_store.py
index 899a776..56f6185 100644
--- a/tests/test_memory_store.py
+++ b/tests/test_memory_store.py
@@ -8,12 +8,12 @@ class TestMemoryStore(unittest.TestCase):
def setUp(self):
self.store = memory.Store()
self.oid = 'https://test/object/12345'
- self.obj = objects.Object(id=self.oid)
+ self.obj = objects.Object(ap_id=self.oid)
self.store.add(self.obj)
def test_add_object(self):
oid = 'https://test/object/12345'
- obj = objects.Object(id=oid)
+ obj = objects.Object(ap_id=oid)
self.store.add(obj)
self.assertIn(oid, self.store.objects)
diff --git a/tests/test_objects.py b/tests/test_objects.py
index 63228f5..4f27424 100644
--- a/tests/test_objects.py
+++ b/tests/test_objects.py
@@ -16,8 +16,8 @@ class TestObjects(unittest.TestCase):
def test_activity_to_json(self):
act = objects.Object(
- id="http://example.org/123456",
- type="object",
+ ap_id="http://example.org/123456",
+ ap_type="object",
)
data = act.to_jsons()
self.assertIsInstance(data, str)
diff --git a/tests/test_sqlite_store.py b/tests/test_sqlite_store.py
index ea3fef6..70a4b17 100644
--- a/tests/test_sqlite_store.py
+++ b/tests/test_sqlite_store.py
@@ -9,12 +9,12 @@ class TestSqliteStore(unittest.TestCase):
self.store = sqlite.Store(file=':memory:')
self.store.createdb()
self.oid = 'https://test/object/12345'
- self.obj = objects.Object(id=self.oid)
+ self.obj = objects.Object(ap_id=self.oid)
self.store.add(self.obj)
def test_add_object(self):
oid = 'https://test/object/12345'
- obj = objects.Object(id=oid)
+ obj = objects.Object(ap_id=oid)
self.store.add(obj)
# self.assertIn('object', self.store.objects)
# self.assertIn(oid, self.store.objects['object'])