+25
-2
src/atpasser/recordKeys/__init__.py
src/atpasser/rKey/__init__.py
+25
-2
src/atpasser/recordKeys/__init__.py
src/atpasser/rKey/__init__.py
···
1
-
class RecordKey:
1
+
class RKey:
2
2
"""
3
+
3
4
A class representing a RecordKey.
5
+
4
6
5
7
6
8
Attributes:
9
+
7
10
recordKey (str): The RecordKey URI.
8
11
"""
12
+
9
13
10
14
def __init__(self, recordKey: str) -> None:
11
15
"""
16
+
12
17
Initalizes an RecordKey object.
13
18
19
+
14
20
Parameters:
21
+
15
22
recordKey (str): The RecordKey.
16
23
"""
17
24
25
+
18
26
if recordKey == "" or len(recordKey) > 512:
27
+
19
28
raise ValueError("null record key or record key longer than 512 chars")
20
29
30
+
21
31
if recordKey == ".." or recordKey == ".":
32
+
22
33
raise ValueError("reserved value . and ..")
34
+
23
35
24
36
if not set(recordKey).issubset(
37
+
25
38
set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_:~")
26
39
):
40
+
27
41
raise ValueError("invalid char")
28
42
43
+
29
44
self.recordKey = recordKey
45
+
30
46
31
47
def __str__(self) -> str:
32
48
"""
33
49
50
+
34
51
Convert the RecordKey to a string by given the URI.
35
52
"""
36
53
return self.recordKey
54
+
37
55
38
56
def __eq__(self, value: object, /) -> bool:
39
57
"""
58
+
40
59
41
60
Check if the 2 values are exactly the same.
42
61
"""
43
62
44
-
if isinstance(value, RecordKey):
63
+
64
+
if isinstance(value, RKey):
65
+
45
66
46
67
return str(self) == str(value)
47
68
else:
69
+
48
70
49
71
return False
72
+
+26
-2
src/atpasser/uri/restricted.py
+26
-2
src/atpasser/uri/restricted.py
···
1
-
from atpasser import uri, handle, nsid, recordKeys
1
+
from atpasser import uri, handle, nsid, rKey
2
+
2
3
3
4
4
5
class RestrictedURI(uri.URI):
5
6
"""
7
+
6
8
A class representing a restricted URI.
7
9
10
+
8
11
Attributes:
12
+
9
13
collection (atpasser.nsid.NSID): Collection as NSID.
14
+
10
15
rkey (atpasser.rKey.RKey): Record key as a RKey object.
11
16
"""
12
17
18
+
13
19
def __init__(self, uri: str) -> None:
14
20
"""
21
+
15
22
Initalizes a restricted URI.
16
23
24
+
17
25
Parameters:
26
+
18
27
uri (str): The AT URI.
19
28
"""
29
+
20
30
super().__init__(uri)
31
+
21
32
if self.query != None and self.fragment != None:
33
+
22
34
raise ValueError("has query and/or fragments")
35
+
23
36
if self.authority == None:
37
+
24
38
raise ValueError("invalid DID or handle")
39
+
25
40
if len(self.path) == 0:
41
+
26
42
self.collection, self.rkey = None, None
43
+
27
44
elif len(self.path) == 1:
45
+
28
46
self.collection = nsid.NSID(self.path[0])
47
+
29
48
self.rkey = None
49
+
30
50
elif len(self.path) == 2:
51
+
31
52
self.collection = nsid.NSID(self.path[0])
32
-
self.rkey = recordKeys.RecordKey(self.path[1])
53
+
54
+
self.rkey = rKey.RKey(self.path[1])
33
55
else:
56
+
34
57
raise ValueError("ttoo much path segments")
58
+
+83
-4
tests/_strings.py
+83
-4
tests/_strings.py
···
1
-
from atpasser import did, handle, nsid, recordKeys, uri
1
+
from atpasser import did, handle, nsid, rKey, uri
2
+
2
3
3
4
testStrings, testMethods = {}, {}
4
5
6
+
5
7
testStrings[
6
8
"did"
9
+
7
10
] = """did:plc:z72i7hdynmk6r22z27h6tvur
11
+
8
12
did:web:blueskyweb.xyz
13
+
9
14
did:method:val:two
15
+
10
16
did:m:v
17
+
11
18
did:method::::val
19
+
12
20
did:method:-:_:.
21
+
13
22
did:key:zQ3shZc2QzApp2oymGvQbzP8eKheVshBHbU4ZYjeXqwSKEn6N
23
+
14
24
did:METHOD:val
25
+
15
26
did:m123:val
27
+
16
28
DID:method:val
17
29
did:method:
30
+
18
31
did:method:val/two
32
+
19
33
did:method:val?two
34
+
20
35
did:method:val#two"""
36
+
21
37
testMethods["did"] = did.DID
38
+
22
39
23
40
testStrings[
24
41
"handle"
42
+
25
43
] = """jay.bsky.social
44
+
26
45
8.cn
46
+
27
47
name.t--t
48
+
28
49
XX.LCS.MIT.EDU
29
50
a.co
51
+
30
52
xn--notarealidn.com
53
+
31
54
xn--fiqa61au8b7zsevnm8ak20mc4a87e.xn--fiqs8s
55
+
32
56
xn--ls8h.test
33
57
example.t
58
+
34
59
jo@hn.test
35
-
💩.test
60
+
61
+
💩.tes
62
+
t
36
63
john..test
64
+
37
65
xn--bcher-.tld
66
+
38
67
john.0
68
+
39
69
cn.8
70
+
40
71
www.masełkowski.pl.com
72
+
41
73
org
74
+
42
75
name.org.
76
+
43
77
2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion
44
78
laptop.local
79
+
45
80
blah.arpa"""
81
+
46
82
testMethods["handle"] = handle.Handle
83
+
47
84
48
85
testStrings[
49
86
"nsid"
87
+
50
88
] = """com.example.fooBar
89
+
51
90
net.users.bob.ping
91
+
52
92
a-0.b-1.c
93
+
53
94
a.b.c
95
+
54
96
com.example.fooBarV2
97
+
55
98
cn.8.lex.stuff
56
-
com.exa💩ple.thing
99
+
100
+
com.exa💩ple.thin
57
101
com.example
102
+
58
103
com.example.3"""
104
+
59
105
testMethods["nsid"] = nsid.NSID
106
+
60
107
61
108
testStrings[
109
+
62
110
"rkey"
111
+
63
112
] = """3jui7kd54zh2y
64
113
self
65
114
example.com
115
+
66
116
~1.2-3_
117
+
67
118
dHJ1ZQ
68
119
pre:fix
120
+
69
121
_
122
+
70
123
alpha/beta
71
124
.
72
125
..
126
+
73
127
#extra
128
+
74
129
@handle
130
+
75
131
any space
132
+
76
133
any+space
134
+
77
135
number[3]
136
+
78
137
number(3)
138
+
79
139
"quote"
140
+
80
141
dHJ1ZQ=="""
81
-
testMethods["rkey"] = recordKeys.RecordKey
142
+
143
+
testMethods["rkey"] = rKey.RKey
144
+
82
145
83
146
testStrings[
84
147
"uri"
148
+
85
149
] = """at://foo.com/com.example.foo/123
150
+
86
151
at://foo.com/example/123
152
+
87
153
at://computer
154
+
88
155
at://example.com:3000
156
+
89
157
at://foo.com/
158
+
90
159
at://user:pass@foo.com"""
160
+
91
161
testMethods["uri"] = uri.URI
162
+
92
163
93
164
for item in testMethods:
165
+
94
166
print(f"START TEST {item}")
167
+
95
168
for value in testStrings[item].splitlines():
169
+
96
170
print(f"Value: {value}")
171
+
97
172
try:
173
+
98
174
print(f"str(): {str(testMethods[item](value))}")
175
+
99
176
except Exception as e:
177
+
100
178
print(f"× {e}")
179
+