File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
binding/python/py/synlink/swarm Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Protocol object that allows use to interface
3
+ with supported protocols on the synlink network.
4
+ """
5
+ from synlink .typing import TProtocol
6
+ from synlink .utils import _check_minimum_version
7
+
8
+ if _check_minimum_version (3 , 11 ):
9
+ from typing import Self
10
+ else :
11
+ from typing_extensions import Self
12
+
13
+
14
+ class Protocol (object ):
15
+ """Network object allows us to see our StreamNetwork struct."""
16
+ _protocol : TProtocol
17
+
18
+ def __init__ (self , data : TProtocol ):
19
+ if not data .startswith ('/' ):
20
+ raise ValueError ("Protocols should start with a /." )
21
+ self ._protocol = data
22
+
23
+ @classmethod
24
+ def from_bytes (cls , data : bytes ) -> Self :
25
+ data = data .decode ()
26
+ cls (data )
27
+
28
+ def to_bytes (self ) -> bytes :
29
+ data = self ._protocol .encode ("utf-8" )
30
+ return data
31
+
32
+ @classmethod
33
+ def from_str (cls , data : str ) -> Self :
34
+ if not data .startswith ('/' ):
35
+ raise ValueError ("Protocols should start with a /." )
36
+ cls (data )
37
+
38
+ @property
39
+ def protocol (self ):
40
+ self ._protocol
41
+
42
+ def __repr__ (self ) -> str :
43
+ return f"<synlin.swarm.protocol { self ._protocol !s} >"
44
+
45
+ def __str__ (self ) -> str :
46
+ return self ._protocol
You can’t perform that action at this time.
0 commit comments