Skip to content

Commit 97e6d21

Browse files
authored
Merge pull request #11 from BlockScience/dev
Merging updates
2 parents 686e61f + 06dd4c8 commit 97e6d21

File tree

13 files changed

+849
-137
lines changed

13 files changed

+849
-137
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ orn:slack.message:TA2E6KPK3/C07BKQX0EVC/1721669683.087619
9797

9898
By representing Slack messages through ORNs, a stable identifier can be assigned to a resource which can be mapped to existing locators for different use cases. For example, a Slack message can be represented as a shareable link which redirects to the Slack app or in browser app:
9999
```
100-
https://blockscienceteam.slack.com/archives/C07BKQX0EVC/p1721669683087619`
100+
https://blockscienceteam.slack.com/archives/C07BKQX0EVC/p1721669683087619
101101
```
102102
There's also a "deep link" which can open the Slack app directly (but only to a channel):
103103
```
@@ -118,13 +118,13 @@ The RID class provides a template for all RID types and access to a global const
118118
class RID:
119119
scheme: str
120120

121-
# defined for ORNs only
121+
# defined for namespaces schemes (ORN, URN, ...) only
122122
namespace: str | None
123123

124-
# "orn:<namespace>" for ORNs, otherwise equal to 'scheme'
124+
# "<scheme>:<namespace>" for namespaces schemes, otherwise equal to scheme component
125125
context: str
126126

127-
# the component after namespace component for ORNs, otherwise after the scheme component
127+
# the component after the context component
128128
reference: str
129129

130130
@classmethod
@@ -177,10 +177,10 @@ assert SlackMessage == RIDType.from_string("orn:slack.message")
177177

178178
In order to create an RID type, follow this minimal implementation:
179179
```python
180-
class MyRIDType(RID): # inherit from `RID` OR `ORN` base classes
180+
class MyRIDType(RID): # inherit from `RID` or namespace scheme (`ORN`, `URN`, ...) base classes
181181
# define scheme for a generic URI type
182182
scheme = "scheme"
183-
# OR a namespace for a ORN type
183+
# OR a namespace if using a namespace scheme
184184
namespace = "namespace"
185185

186186
# instantiates a new RID from internal components
@@ -247,13 +247,14 @@ print(rid_obj1.scheme, rid_obj1.context, rid_obj1.reference)
247247
print(rid_obj1.team_id, rid_obj1.channel_id, rid_obj1.ts)
248248
```
249249

250-
If an RID type hasn't been implemented as a class, it can still be parsed by the general constructor if provisional contexts are allowed (enabled by default). In this case a provisional context class is generated on the fly providing the minimal RID type implementation (`reference` property, `from_reference` class method, `__init__` function).
250+
If an RID type doesn't have a class implementation, it can still be parsed by both the RID and RIDType constructors. A default type implementation will be generated on the fly with a minimal implementation (`reference` property, `from_reference` class method, `__init__` function).
251251

252252
```python
253253
test_obj1 = RID.from_string("test:one")
254254
test_obj2 = RID.from_string("test:one")
255255

256256
assert test_obj1 == test_obj2
257+
assert type(test_obj1) == RIDType.from_string("test")
257258
```
258259

259260
## Development
@@ -305,7 +306,7 @@ class Bundle(BaseModel):
305306
manifest: Manifest
306307
contents: dict
307308

308-
@classmethod
309+
@classmethod
309310
def generate(cls, rid: RID, contents: dict) -> Bundle: ...
310311
```
311312

src/rid_lib/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __new__(mcls, name, bases, dct):
4242

4343
# check for abstract method implementation
4444
if getattr(cls, "__abstractmethods__", None):
45-
raise TypeError(f"RID type '{name}' is missing implemenation(s) for abstract method(s) {set(cls.__abstractmethods__)}")
45+
raise TypeError(f"RID type '{name}' is missing implementation(s) for abstract method(s) {set(cls.__abstractmethods__)}")
4646

4747
# save RID type to lookup table
4848
mcls.type_table[str(cls)] = cls

src/rid_lib/ext/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
from .manifest import Manifest
22
from .bundle import Bundle
3-
from .cache import Cache
4-
from .effector import ActionType, Effector
3+
from .cache import Cache

src/rid_lib/ext/cache.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ def file_path_to(self, rid: RID) -> str:
1313
encoded_rid_str = b64_encode(str(rid))
1414
return f"{self.directory_path}/{encoded_rid_str}.json"
1515

16-
def write(self, cache_bundle: Bundle) -> Bundle:
16+
def write(self, bundle: Bundle) -> Bundle:
1717
"""Writes bundle to cache, returns a Bundle."""
1818
if not os.path.exists(self.directory_path):
1919
os.makedirs(self.directory_path)
2020

2121
with open(
22-
file=self.file_path_to(cache_bundle.manifest.rid),
22+
file=self.file_path_to(bundle.manifest.rid),
2323
mode="w",
2424
encoding="utf-8"
2525
) as f:
26-
f.write(cache_bundle.model_dump_json(indent=2))
26+
f.write(bundle.model_dump_json(indent=2))
2727

28-
return cache_bundle
28+
return bundle
2929

3030
def exists(self, rid: RID) -> bool:
3131
return os.path.exists(

src/rid_lib/ext/effector.py

Lines changed: 0 additions & 97 deletions
This file was deleted.

src/rid_lib/ext/utils.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,24 @@
1-
import json
21
import hashlib
32
from base64 import urlsafe_b64encode, urlsafe_b64decode
4-
from dataclasses import asdict, is_dataclass
5-
from datetime import datetime
63
from pydantic import BaseModel
7-
from rid_lib import RID
4+
from vendor.org.webpki.json.Canonicalize import canonicalize
85

96

10-
def sha256_hash_json(data: dict | BaseModel):
11-
if isinstance(data, BaseModel):
12-
data = json.loads(data.model_dump_json())
13-
json_str = json.dumps(data, separators=(',', ':'), sort_keys=True)
14-
json_bytes = json_str.encode()
7+
def sha256_hash(data: str) -> str:
158
hash = hashlib.sha256()
16-
hash.update(json_bytes)
9+
hash.update(data.encode())
1710
return hash.hexdigest()
1811

12+
def sha256_hash_json(data: dict | BaseModel):
13+
if isinstance(data, BaseModel):
14+
data = data.model_dump(mode="json")
15+
canonicalized_data = canonicalize(data, utf8=False)
16+
return sha256_hash(canonicalized_data)
17+
1918
def b64_encode(string: str):
2019
return urlsafe_b64encode(
2120
string.encode()).decode().rstrip("=")
2221

2322
def b64_decode(string: str):
2423
return urlsafe_b64decode(
25-
(string + "=" * (-len(string) % 4)).encode()).decode()
26-
27-
def json_serialize(obj):
28-
if isinstance(obj, RID):
29-
return str(obj)
30-
elif is_dataclass(obj) and not isinstance(obj, type):
31-
return json_serialize(asdict(obj))
32-
elif isinstance(obj, datetime):
33-
return obj.isoformat()
34-
elif isinstance(obj, (list, tuple)):
35-
return [json_serialize(item) for item in obj]
36-
elif isinstance(obj, dict):
37-
return {key: json_serialize(value) for key, value in obj.items()}
38-
else:
39-
return obj
24+
(string + "=" * (-len(string) % 4)).encode()).decode()

src/vendor/org/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

src/vendor/org/webpki/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)