Skip to content
Snippets Groups Projects
Commit 016e7029 authored by Moul's avatar Moul
Browse files

WS2P head: Switch from attrs to dataclass (#88)

Couldn’t come with a beautiful generic implementation
such as the one with attrs expanding the astuple,
since there is no filter exclude in dataclass lib

Appending new attributes values to inheritated super().inline()

Remove attrs dependency
parent 41b60ea4
Branches
Tags
1 merge request!186WS2P head: Switch from attrs to dataclass (#88)
Pipeline #32592 waiting for manual action
...@@ -37,7 +37,6 @@ It is currently used by following programs: ...@@ -37,7 +37,6 @@ It is currently used by following programs:
- [websocket-client](https://pypi.org/project/websocket-client) - [websocket-client](https://pypi.org/project/websocket-client)
- [jsonschema](https://pypi.org/project/jsonschema) - [jsonschema](https://pypi.org/project/jsonschema)
- [pyPEG2](https://pypi.org/project/pyPEG2) - [pyPEG2](https://pypi.org/project/pyPEG2)
- [attrs](https://pypi.org/project/attrs)
- [base58](https://pypi.org/project/base58) - [base58](https://pypi.org/project/base58)
- [libnacl](https://pypi.org/project/libnacl) - [libnacl](https://pypi.org/project/libnacl)
- [pyaes](https://pypi.org/project/pyaes) - [pyaes](https://pypi.org/project/pyaes)
......
...@@ -14,9 +14,7 @@ ...@@ -14,9 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import re import re
from dataclasses import dataclass
import attr
import attrs
from ...constants import ( from ...constants import (
BLOCK_ID_REGEX, BLOCK_ID_REGEX,
...@@ -32,10 +30,10 @@ from ..block_id import BlockID ...@@ -32,10 +30,10 @@ from ..block_id import BlockID
from ..document import MalformedDocumentError from ..document import MalformedDocumentError
@attr.s() @dataclass
class API: class API:
private = attr.ib(type=str) private: str
public = attr.ib(type=str) public: str
re_inline = re.compile( re_inline = re.compile(
f"WS2P({WS2P_PRIVATE_PREFIX_REGEX})?({WS2P_PUBLIC_PREFIX_REGEX})?" f"WS2P({WS2P_PRIVATE_PREFIX_REGEX})?({WS2P_PUBLIC_PREFIX_REGEX})?"
...@@ -54,9 +52,9 @@ class API: ...@@ -54,9 +52,9 @@ class API:
return f"WS2P{self.private}{self.public}" return f"WS2P{self.private}{self.public}"
@attr.s() @dataclass
class Head: class Head:
version = attr.ib(type=int) version: int
re_inline = re.compile(WS2P_HEAD_REGEX) re_inline = re.compile(WS2P_HEAD_REGEX)
...@@ -76,13 +74,13 @@ class Head: ...@@ -76,13 +74,13 @@ class Head:
return "HEAD" if self.version == 0 else f"HEAD:{str(self.version)}" return "HEAD" if self.version == 0 else f"HEAD:{str(self.version)}"
@attr.s() @dataclass
class HeadV0(Head): class HeadV0(Head):
signature = attr.ib(type=str) signature: str
api = attr.ib(type=API) api: API
head = attr.ib(type=Head) head: Head
pubkey = attr.ib(type=str) pubkey: str
block_id = attr.ib(type=BlockID) block_id: BlockID
re_inline = re.compile( re_inline = re.compile(
f"^(WS2P(?:{WS2P_PRIVATE_PREFIX_REGEX})?(?:{WS2P_PUBLIC_PREFIX_REGEX})?):\ f"^(WS2P(?:{WS2P_PRIVATE_PREFIX_REGEX})?(?:{WS2P_PUBLIC_PREFIX_REGEX})?):\
...@@ -107,19 +105,7 @@ class HeadV0(Head): ...@@ -107,19 +105,7 @@ class HeadV0(Head):
raise MalformedDocumentError("HeadV0") from AttributeError raise MalformedDocumentError("HeadV0") from AttributeError
def inline(self) -> str: def inline(self) -> str:
values = ( return f"{str(self.api)}:{str(self.head)}:{self.pubkey}:{str(self.block_id)}"
str(v)
for v in attrs.astuple(
self,
recurse=False,
filter=attrs.filters.exclude(
attrs.fields(HeadV0).version,
attrs.fields(HeadV0).signature,
attrs.fields(HeadV0).api,
),
)
)
return f'{str(self.api)}:{":".join(values)}'
def check_signature(self, pubkey: str) -> bool: def check_signature(self, pubkey: str) -> bool:
""" """
...@@ -133,12 +119,12 @@ class HeadV0(Head): ...@@ -133,12 +119,12 @@ class HeadV0(Head):
return verifying_key.check_signature(self.inline(), self.signature) return verifying_key.check_signature(self.inline(), self.signature)
@attr.s() @dataclass
class HeadV1(HeadV0): class HeadV1(HeadV0):
ws2pid = attr.ib(type=str) ws2pid: str
software = attr.ib(type=str) software: str
software_version = attr.ib(type=str) software_version: str
pow_prefix = attr.ib(type=int) pow_prefix: int
re_inline = re.compile( re_inline = re.compile(
"({ws2pid}):({software}):({software_version}):({pow_prefix})(?::)?(.*)".format( "({ws2pid}):({software}):({software_version}):({pow_prefix})(?::)?(.*)".format(
...@@ -179,11 +165,14 @@ class HeadV1(HeadV0): ...@@ -179,11 +165,14 @@ class HeadV1(HeadV0):
except AttributeError: except AttributeError:
raise MalformedDocumentError("HeadV1") from AttributeError raise MalformedDocumentError("HeadV1") from AttributeError
def inline(self) -> str:
return f"{super().inline()}:{self.ws2pid}:{self.software}:{self.software_version}:{self.pow_prefix}"
@attr.s @dataclass
class HeadV2(HeadV1): class HeadV2(HeadV1):
free_member_room = attr.ib(type=int) free_member_room: int
free_mirror_room = attr.ib(type=int) free_mirror_room: int
re_inline = re.compile( re_inline = re.compile(
"({free_member_room}):({free_mirror_room})(?::)?(.*)".format( "({free_member_room}):({free_mirror_room})(?::)?(.*)".format(
...@@ -219,3 +208,6 @@ class HeadV2(HeadV1): ...@@ -219,3 +208,6 @@ class HeadV2(HeadV1):
) )
except AttributeError: except AttributeError:
raise MalformedDocumentError("HeadV2") from AttributeError raise MalformedDocumentError("HeadV2") from AttributeError
def inline(self) -> str:
return f"{super().inline()}:{self.free_member_room}:{self.free_mirror_room}"
...@@ -31,7 +31,6 @@ graphql-core = "^3.2.3" ...@@ -31,7 +31,6 @@ graphql-core = "^3.2.3"
websocket-client = "^1.5.2" websocket-client = "^1.5.2"
jsonschema = "^4.17.3" jsonschema = "^4.17.3"
pypeg2 = "^2.15.2" pypeg2 = "^2.15.2"
attrs = "^23.1.0"
base58 = "^2.1.1" base58 = "^2.1.1"
libnacl = "^1.8.0" libnacl = "^1.8.0"
pyaes = "^1.6.1" pyaes = "^1.6.1"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment