Skip to content

Commit

Permalink
Implement protocol_util
Browse files Browse the repository at this point in the history
  • Loading branch information
vzahradnik committed Aug 24, 2023
1 parent a23c999 commit e6b8082
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 11 deletions.
11 changes: 7 additions & 4 deletions setup.py
@@ -1,8 +1,7 @@
#!/usr/bin/env python3
import os
import subprocess

from setuptools import setup, find_packages
from pathlib import Path

this_directory = os.path.abspath(os.path.dirname(__file__))

Expand All @@ -19,11 +18,15 @@ def read_lines(file_name):
filter(lambda line: not line.startswith("--extra-index-url"), read_lines("requirements-test.txt"))
)

APP_VERSION = "0.1.0"

def read_version():
with open("tcmenu/__init__.py") as f:
return next((line.split('=')[1].strip().strip('"').strip("'")
for line in f if line.startswith("__version__")), "0.0.0")

setup(
name="tcmenu-python",
version=APP_VERSION,
version=read_version(),
author="Vladimír Záhradník",
author_email="vladimir@zahradnik.io",
description="A series of domain and serialisation components for the TcMenu library.",
Expand Down
9 changes: 9 additions & 0 deletions tcmenu/__init__.py
@@ -0,0 +1,9 @@
"""
TcMenu Python library.
"""

__all__ = [
"__version__",
]

__version__ = "4.1.1"
4 changes: 2 additions & 2 deletions tcmenu/tagval/commands/command_factory.py
Expand Up @@ -64,13 +64,13 @@ def new_join_command(name: str, uuid: Optional[UUID] = None) -> MenuJoinCommand:
"""
if not uuid:
return MenuJoinCommand(
my_name=name, platform=ApiPlatform.PYTHON_API, api_version=ProtocolUtil.get_version_from_properties()
my_name=name, platform=ApiPlatform.PYTHON_API, api_version=ProtocolUtil.get_module_version_code()
)
else:
return MenuJoinCommand(
my_name=name,
platform=ApiPlatform.PYTHON_API,
api_version=ProtocolUtil.get_version_from_properties(),
api_version=ProtocolUtil.get_module_version_code(),
app_uuid=uuid,
)

Expand Down
14 changes: 14 additions & 0 deletions tcmenu/tagval/protocol/api_platform.py
@@ -1,5 +1,6 @@
from collections import namedtuple
from enum import Enum
from functools import lru_cache


class ApiPlatform(Enum):
Expand Down Expand Up @@ -29,3 +30,16 @@ def key(self) -> str:
def description(self) -> str:
"""Platform description."""
return self._value_.description

@staticmethod
@lru_cache(maxsize=6)
def _from_key_cached(key: int):
for platform in ApiPlatform:
if platform.key == key:
return platform
raise ValueError(f"No ApiPlatform member found with key {key}")

@classmethod
def from_key(cls, key: int) -> "ApiPlatform":
"""Returns the enum member matching the given key."""
return cls._from_key_cached(key)
13 changes: 9 additions & 4 deletions tcmenu/tagval/protocol/protocol_util.py
Expand Up @@ -7,10 +7,15 @@ class ProtocolUtil:
"""

@staticmethod
def get_version_from_properties() -> int:
def get_module_version_code() -> int:
"""
Gets and caches the current version from the version properties file.
Gets the current version from the version properties file.
:return: the current version as major * 100 + minor.
"""
# TODO: Implement.
return 100
from tcmenu import __version__
major, minor, patch = map(int, __version__.split('.'))
return major * 100 + minor

@staticmethod
def from_key_to_api_platform(key: int) -> ApiPlatform:
return ApiPlatform.from_key(key)
11 changes: 10 additions & 1 deletion test/tagval/protocol/test_api_platform.py
@@ -1,5 +1,5 @@
from tcmenu.tagval.protocol.api_platform import ApiPlatform

import pytest

def test_api_platform_arduino():
api_platform = ApiPlatform.Platform.ARDUINO
Expand Down Expand Up @@ -41,3 +41,12 @@ def test_api_platform_python():

assert api_platform.key == 5
assert api_platform.description == "Python API"


def test_api_platform_from_valid_key():
assert ApiPlatform.from_key(4) == ApiPlatform.Platform.JAVASCRIPT_API


def test_api_platform_from_invalid_key():
with pytest.raises(ValueError):
ApiPlatform.from_key(7)
12 changes: 12 additions & 0 deletions test/tagval/protocol/test_protocol_util.py
@@ -0,0 +1,12 @@
import tcmenu
from tcmenu.tagval.protocol.protocol_util import ProtocolUtil
from tcmenu.tagval.protocol.api_platform import ApiPlatform


def test_module_version_code(mocker):
mocker.patch.object(tcmenu, '__version__', new="3.2.1")
assert ProtocolUtil.get_module_version_code() == 302


def test_from_key_to_api_platform():
assert ProtocolUtil.from_key_to_api_platform(5) == ApiPlatform.PYTHON_API

0 comments on commit e6b8082

Please sign in to comment.