vmcp.osc.channel.common

Common module of channel package.

  1#!/usr/bin/env python3
  2# -*- coding: utf-8 -*-
  3# SPDX-License-Identifier: AGPL-3.0-or-later
  4
  5"""Common module of ``channel`` package."""
  6
  7from abc import ABCMeta, abstractmethod
  8from typing import TYPE_CHECKING
  9if TYPE_CHECKING:
 10    from .. import OSC
 11
 12
 13class AbstractChannel(metaclass=ABCMeta):
 14    """Abstract channel."""
 15
 16    @property
 17    def system(self) -> 'OSC':
 18        """OSC: Returns ``OSC`` system instance."""
 19        return self._system
 20
 21    @property
 22    def is_open(self) -> bool:
 23        """bool: Returns whether the channel is currently open."""
 24        return self._opened
 25
 26    @property
 27    def host(self) -> str:
 28        """str: Host DNS or IP address."""
 29        return str(self._host)
 30
 31    @property
 32    def port(self) -> int:
 33        """int: Host port."""
 34        return int(self._port)
 35
 36    @property
 37    def name(self) -> str:
 38        """str: Channel name."""
 39        return str(self._name)
 40
 41    def __init__(
 42        self,
 43        system: 'OSC',
 44        host: str,
 45        port: int,
 46        name: str
 47    ) -> None:
 48        """Channel constructor.
 49
 50        Args:
 51            system (OSC):
 52                OSC system instance.
 53            host (str):
 54                Host DNS or IP (recommended).
 55            port (int):
 56                Host port.
 57            name (str):
 58                Channel name (arbitrary, but needs to be unique).
 59
 60        Raises:
 61            ValueError:
 62                If ``name`` is already used.
 63
 64        """
 65        self._opened = False
 66        self._host, self._port, self._name = host, port, name
 67        self._system = system
 68
 69    @abstractmethod
 70    def open(self) -> 'AbstractChannel':
 71        """Open channel.
 72
 73        Returns:
 74            Class:
 75                Object instance.
 76
 77        """
 78        self._opened = True
 79        return self
 80
 81    def __eq__(self, other) -> bool:
 82        """Return ``True`` if an object is identical to ``other`` object.
 83
 84        Args:
 85            other (Client):
 86                Other object for comparison.
 87
 88        Returns:
 89            bool:
 90                ``True`` if identical, otherwise ``False``.
 91
 92        """
 93        return (
 94            self.host == other.host and
 95            self.port == other.port and
 96            self.name == other.name
 97        )
 98
 99    def __hash__(self) -> int:
100        """Return hash value to quickly compare keys for dictionary lookup.
101
102        Returns:
103            int:
104                Hash value.
105
106        """
107        return hash((self.host, self.port, self.name))
108
109    def __str__(self) -> str:
110        """Return string representation of the object.
111
112        Returns:
113            str:
114                Representation of the object.
115
116        """
117        return f"{self.host}, {self.port}, {self.name}"
118
119    @abstractmethod
120    def __repr__(self) -> str:
121        """Return a string, representing the object in a reconstructable way.
122
123        Returns:
124            str:
125                Representing the object in a reconstructable way.
126
127        """
128        return f"({self})"
129
130    @abstractmethod
131    def close(self) -> None:
132        """Close channel."""
133        self._opened = False
class AbstractChannel:
 14class AbstractChannel(metaclass=ABCMeta):
 15    """Abstract channel."""
 16
 17    @property
 18    def system(self) -> 'OSC':
 19        """OSC: Returns ``OSC`` system instance."""
 20        return self._system
 21
 22    @property
 23    def is_open(self) -> bool:
 24        """bool: Returns whether the channel is currently open."""
 25        return self._opened
 26
 27    @property
 28    def host(self) -> str:
 29        """str: Host DNS or IP address."""
 30        return str(self._host)
 31
 32    @property
 33    def port(self) -> int:
 34        """int: Host port."""
 35        return int(self._port)
 36
 37    @property
 38    def name(self) -> str:
 39        """str: Channel name."""
 40        return str(self._name)
 41
 42    def __init__(
 43        self,
 44        system: 'OSC',
 45        host: str,
 46        port: int,
 47        name: str
 48    ) -> None:
 49        """Channel constructor.
 50
 51        Args:
 52            system (OSC):
 53                OSC system instance.
 54            host (str):
 55                Host DNS or IP (recommended).
 56            port (int):
 57                Host port.
 58            name (str):
 59                Channel name (arbitrary, but needs to be unique).
 60
 61        Raises:
 62            ValueError:
 63                If ``name`` is already used.
 64
 65        """
 66        self._opened = False
 67        self._host, self._port, self._name = host, port, name
 68        self._system = system
 69
 70    @abstractmethod
 71    def open(self) -> 'AbstractChannel':
 72        """Open channel.
 73
 74        Returns:
 75            Class:
 76                Object instance.
 77
 78        """
 79        self._opened = True
 80        return self
 81
 82    def __eq__(self, other) -> bool:
 83        """Return ``True`` if an object is identical to ``other`` object.
 84
 85        Args:
 86            other (Client):
 87                Other object for comparison.
 88
 89        Returns:
 90            bool:
 91                ``True`` if identical, otherwise ``False``.
 92
 93        """
 94        return (
 95            self.host == other.host and
 96            self.port == other.port and
 97            self.name == other.name
 98        )
 99
100    def __hash__(self) -> int:
101        """Return hash value to quickly compare keys for dictionary lookup.
102
103        Returns:
104            int:
105                Hash value.
106
107        """
108        return hash((self.host, self.port, self.name))
109
110    def __str__(self) -> str:
111        """Return string representation of the object.
112
113        Returns:
114            str:
115                Representation of the object.
116
117        """
118        return f"{self.host}, {self.port}, {self.name}"
119
120    @abstractmethod
121    def __repr__(self) -> str:
122        """Return a string, representing the object in a reconstructable way.
123
124        Returns:
125            str:
126                Representing the object in a reconstructable way.
127
128        """
129        return f"({self})"
130
131    @abstractmethod
132    def close(self) -> None:
133        """Close channel."""
134        self._opened = False

Abstract channel.

AbstractChannel(system: vmcp.osc.osc.OSC, host: str, port: int, name: str)
42    def __init__(
43        self,
44        system: 'OSC',
45        host: str,
46        port: int,
47        name: str
48    ) -> None:
49        """Channel constructor.
50
51        Args:
52            system (OSC):
53                OSC system instance.
54            host (str):
55                Host DNS or IP (recommended).
56            port (int):
57                Host port.
58            name (str):
59                Channel name (arbitrary, but needs to be unique).
60
61        Raises:
62            ValueError:
63                If ``name`` is already used.
64
65        """
66        self._opened = False
67        self._host, self._port, self._name = host, port, name
68        self._system = system

Channel constructor.

Args: system (OSC): OSC system instance. host (str): Host DNS or IP (recommended). port (int): Host port. name (str): Channel name (arbitrary, but needs to be unique).

Raises: ValueError: If name is already used.

system: vmcp.osc.osc.OSC

OSC: Returns OSC system instance.

is_open: bool

bool: Returns whether the channel is currently open.

host: str

str: Host DNS or IP address.

port: int

int: Host port.

name: str

str: Channel name.

@abstractmethod
def open(self) -> vmcp.osc.channel.common.AbstractChannel:
70    @abstractmethod
71    def open(self) -> 'AbstractChannel':
72        """Open channel.
73
74        Returns:
75            Class:
76                Object instance.
77
78        """
79        self._opened = True
80        return self

Open channel.

Returns: Class: Object instance.

@abstractmethod
def close(self) -> None:
131    @abstractmethod
132    def close(self) -> None:
133        """Close channel."""
134        self._opened = False

Close channel.