Skip to content

Commit

Permalink
refactor: update the teamwork header class
Browse files Browse the repository at this point in the history
  • Loading branch information
daxartio committed Oct 16, 2023
1 parent 7287dcf commit d496461
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 51 deletions.
11 changes: 10 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ default_item_type = "ref"

[tool.poe.tasks.test]
help = "Test with coverage"
cmd = "pytest -vv --cov"
sequence = [
{ref = "generate-mo"},
{cmd = "pytest -vv --cov"},
]

[tool.poe.tasks.test-fast]
help = "Test until error"
Expand All @@ -91,6 +94,7 @@ cmd = "pytest -vv --last-failed"
[tool.poe.tasks.lint]
help = "Check code"
sequence = [
{ref = "generate-mo"},
# {cmd = "flake8 --jobs 4 --statistics --show-source ${CODE}"},
# {cmd = "pylint --jobs 1 --rcfile=setup.cfg ${CODE}"},
{cmd = "black --skip-string-normalization --line-length=88 --check ${CODE}"},
Expand All @@ -101,12 +105,17 @@ sequence = [
[tool.poe.tasks.format]
help = "Formating code"
sequence = [
{ref = "generate-mo"},
{cmd = "autoflake --recursive --in-place --remove-all-unused-imports ${CODE}"},
{cmd = "isort ${CODE}"},
{cmd = "black --skip-string-normalization --line-length=88 ${CODE}"},
{cmd = "unify --in-place --recursive ${CODE}"},
]

[tool.poe.tasks.generate-mo]
help = "Generate mo files"
cmd = "python -m sportorg.language"

[tool.poe.tasks.run]
help = "Run"
cmd = "python SportOrg.pyw"
5 changes: 5 additions & 0 deletions sportorg/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ def get_languages() -> List[str]:


translate = locale()


if __name__ == '__main__':
logger.info('Generate mo files')
generate_mo()
2 changes: 1 addition & 1 deletion sportorg/modules/teamwork/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def run(self):
if len(full_data) >= hdr.size:
command = Command(
orjson.loads(full_data[: hdr.size].decode()),
Operations(hdr.opType).name,
Operations(hdr.op_type).name,
)
self._out_queue.put(command) # for local
full_data = full_data[hdr.size :]
Expand Down
99 changes: 51 additions & 48 deletions sportorg/modules/teamwork/packet_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@


class ObjectTypes(Enum):
"""
```python
obj_type = ObjectTypes['Race']
obj_type.value
```
"""

Race = 0
Course = 1
Group = 2
Expand All @@ -24,6 +31,15 @@ def __repr__(self):


class Operations(Enum):
"""
```python
op = Operations['Create']
Operations(0).name
'Create'
```
"""

Create = 0
Read = 1
Update = 2
Expand All @@ -39,46 +55,33 @@ def __repr__(self):
return self.__str__()


"""
```python
obj_type = ObjectTypes['Race']
obj_type.value
op = Operations['Create']
Operations(0).name
'Create'
```
"""

"""
```
?: boolean
H: Unsigned short
L: unsigned long
i: int
f: float
Q: Unsigned long long int
s: bytes string
Header Tag: 2 Bytes
Operation type: 1 Byte (Unsigned Short)
Object type: 1 Byte (Unsigned Short)
uuid: 36 Bytes string
version: 4 Bytes (Unsigned Long)
size: 8 Bytes (Unsigned Long long)
Total Header Size = 56 Bytes
```
"""


class Header:
"""
```
?: boolean
H: Unsigned short
L: unsigned long
i: int
f: float
Q: Unsigned long long int
s: bytes string
Header Tag: 2 Bytes
Operation type: 1 Byte (Unsigned Short)
Object type: 1 Byte (Unsigned Short)
uuid: 36 Bytes string
version: 4 Bytes (Unsigned Long)
size: 8 Bytes (Unsigned Long long)
Total Header Size = 56 Bytes
```
"""

header_struck = '=2s2H36sLQ'
header_size = struct.calcsize(header_struck)

def __init__(self, obj_data=None, op_type=Operations.Update.name):
self.packTag = b'SO'
self.pack_tag = b'SO'
if obj_data:
try:
obj_type = obj_data['object']
Expand All @@ -91,23 +94,23 @@ def __init__(self, obj_data=None, op_type=Operations.Update.name):
obj_type, op_type, obj_uuid
)
)
self.opType = Operations[op_type].value
self.objType = ObjectTypes[obj_type].value
self.op_type = Operations[op_type].value
self.obj_type = ObjectTypes[obj_type].value
self.uuid = obj_uuid
self.version = 0 # int(obj_ver)
self.size = len(obj_data)
else:
self.opType = Operations[op_type].value
self.objType = 0
self.op_type = Operations[op_type].value
self.obj_type = 0
self.uuid = ''
self.version = 0 # int(obj_ver)
self.size = 0

def unpack_header(self, header):
(
self.packTag,
self.opType,
self.objType,
self.pack_tag,
self.op_type,
self.obj_type,
self.uuid,
self.version,
self.size,
Expand All @@ -120,12 +123,12 @@ def prepare_header(self, obj_data, op_type):
obj_uuid = obj_data['id']
except AttributeError:
return False
self.opType = Operations[op_type].value
self.op_type = Operations[op_type].value

try:
self.objType = ObjectTypes[obj_type].value
self.obj_type = ObjectTypes[obj_type].value
except Exception:
self.objType = 255 # Unknown
self.obj_type = 255 # Unknown

self.uuid = obj_uuid
self.version = 0 # int(obj_ver)
Expand All @@ -136,9 +139,9 @@ def pack_header(self, psize):
self.size = psize
return struct.pack(
Header.header_struck,
self.packTag,
self.opType,
self.objType,
self.pack_tag,
self.op_type,
self.obj_type,
bytes(self.uuid, 'ascii'),
self.version,
self.size,
Expand Down
2 changes: 1 addition & 1 deletion sportorg/modules/teamwork/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def run(self):
if len(full_data) >= hdr.size:
command = Command(
json.loads(full_data[: hdr.size].decode()),
Operations(hdr.opType).name,
Operations(hdr.op_type).name,
self.connect.addr,
)
command.exclude(self.connect.addr)
Expand Down

0 comments on commit d496461

Please sign in to comment.