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
No related branches found
No related tags found
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:
- [websocket-client](https://pypi.org/project/websocket-client)
- [jsonschema](https://pypi.org/project/jsonschema)
- [pyPEG2](https://pypi.org/project/pyPEG2)
- [attrs](https://pypi.org/project/attrs)
- [base58](https://pypi.org/project/base58)
- [libnacl](https://pypi.org/project/libnacl)
- [pyaes](https://pypi.org/project/pyaes)
......
......@@ -14,9 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import attr
import attrs
from dataclasses import dataclass
from ...constants import (
BLOCK_ID_REGEX,
......@@ -32,10 +30,10 @@ from ..block_id import BlockID
from ..document import MalformedDocumentError
@attr.s()
@dataclass
class API:
private = attr.ib(type=str)
public = attr.ib(type=str)
private: str
public: str
re_inline = re.compile(
f"WS2P({WS2P_PRIVATE_PREFIX_REGEX})?({WS2P_PUBLIC_PREFIX_REGEX})?"
......@@ -54,9 +52,9 @@ class API:
return f"WS2P{self.private}{self.public}"
@attr.s()
@dataclass
class Head:
version = attr.ib(type=int)
version: int
re_inline = re.compile(WS2P_HEAD_REGEX)
......@@ -76,13 +74,13 @@ class Head:
return "HEAD" if self.version == 0 else f"HEAD:{str(self.version)}"
@attr.s()
@dataclass
class HeadV0(Head):
signature = attr.ib(type=str)
api = attr.ib(type=API)
head = attr.ib(type=Head)
pubkey = attr.ib(type=str)
block_id = attr.ib(type=BlockID)
signature: str
api: API
head: Head
pubkey: str
block_id: BlockID
re_inline = re.compile(
f"^(WS2P(?:{WS2P_PRIVATE_PREFIX_REGEX})?(?:{WS2P_PUBLIC_PREFIX_REGEX})?):\
......@@ -107,19 +105,7 @@ class HeadV0(Head):
raise MalformedDocumentError("HeadV0") from AttributeError
def inline(self) -> str:
values = (
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)}'
return f"{str(self.api)}:{str(self.head)}:{self.pubkey}:{str(self.block_id)}"
def check_signature(self, pubkey: str) -> bool:
"""
......@@ -133,12 +119,12 @@ class HeadV0(Head):
return verifying_key.check_signature(self.inline(), self.signature)
@attr.s()
@dataclass
class HeadV1(HeadV0):
ws2pid = attr.ib(type=str)
software = attr.ib(type=str)
software_version = attr.ib(type=str)
pow_prefix = attr.ib(type=int)
ws2pid: str
software: str
software_version: str
pow_prefix: int
re_inline = re.compile(
"({ws2pid}):({software}):({software_version}):({pow_prefix})(?::)?(.*)".format(
......@@ -179,11 +165,14 @@ class HeadV1(HeadV0):
except 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):
free_member_room = attr.ib(type=int)
free_mirror_room = attr.ib(type=int)
free_member_room: int
free_mirror_room: int
re_inline = re.compile(
"({free_member_room}):({free_mirror_room})(?::)?(.*)".format(
......@@ -219,3 +208,6 @@ class HeadV2(HeadV1):
)
except 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"
websocket-client = "^1.5.2"
jsonschema = "^4.17.3"
pypeg2 = "^2.15.2"
attrs = "^23.1.0"
base58 = "^2.1.1"
libnacl = "^1.8.0"
pyaes = "^1.6.1"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment