File: parser.py

package info (click to toggle)
python-anthemav 1.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 200 kB
  • sloc: python: 1,408; makefile: 12
file content (42 lines) | stat: -rw-r--r-- 1,367 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Module containing the parser for Anthem command."""


class ParsedMessage:
    """Class containing parsed message information."""

    command: str
    value: str
    input_number: int


def parse_message(message: str) -> ParsedMessage:
    """Try to parse a message to a ParsedMessage object."""
    return parse_x40_message(message)


def parse_x40_message(message: str) -> ParsedMessage:
    """Try to parse a message for the x40 models."""
    return parse_x40_input_message(message, "ARC")


def parse_x40_input_message(message: str, command: str) -> ParsedMessage:
    """Try to parse a message associated to a specific input for the x40 models."""
    if (
        message.startswith("IS")
        and command in message
        and len(message) >= len(command) + 4
    ):
        parsed_message = ParsedMessage()
        command_position = message.index(command)
        parsed_message.command = message[0 : command_position + len(command)]
        parsed_message.input_number = int(message[2:command_position])
        parsed_message.value = message[command_position + len(command) :]
        return parsed_message
    return None


def get_x40_input_command(self, input_number: int, command: str) -> str:
    """Return a formatted message for a specific input."""
    if input_number > 0:
        return f"IS{self.input_number}{command}"
    return None