diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2023-09-13 09:34:49 +0300 |
---|---|---|
committer | Evgeny Sorokin <me@ch1p.io> | 2024-01-13 00:57:00 +0000 |
commit | 7058d0f5063dc9b065248d0a906cf874788caecf (patch) | |
tree | 26468a5d5a7f8c05804911b3fbf649d952bfd8f1 /include/py/homekit/util.py | |
parent | 57955b596485ecce1ffd4395e23c078358cc5ddd (diff) |
save
Diffstat (limited to 'include/py/homekit/util.py')
-rw-r--r-- | include/py/homekit/util.py | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/include/py/homekit/util.py b/include/py/homekit/util.py index 22bba86..3c73440 100644 --- a/include/py/homekit/util.py +++ b/include/py/homekit/util.py @@ -9,6 +9,7 @@ import logging import string import random import re +import os from enum import Enum from datetime import datetime @@ -52,17 +53,21 @@ class Addr: self.host = host self.port = port - @staticmethod - def fromstring(addr: str) -> Addr: - colons = addr.count(':') - if colons != 1: - raise ValueError('invalid host:port format') - - if not colons: - host = addr - port = None + @classmethod + def fromstring(cls, addr: str, port_required=True) -> Addr: + if port_required: + colons = addr.count(':') + if colons != 1: + raise ValueError('invalid host:port format') + + if not colons: + host = addr + port = None + else: + host, port = addr.split(':') else: - host, port = addr.split(':') + port = None + host = addr validate_ipv4_or_hostname(host, raise_exception=True) @@ -73,12 +78,19 @@ class Addr: return Addr(host, port) + @classmethod + def fromipstring(cls, addr: str) -> Addr: + return cls.fromstring(addr, port_required=False) + def __str__(self): buf = self.host if self.port is not None: buf += ':'+str(self.port) return buf + def __repr__(self): + return self.__str__() + def __iter__(self): yield self.host yield self.port @@ -252,4 +264,10 @@ def next_tick_gen(freq): t = time.time() while True: t += freq - yield max(t - time.time(), 0)
\ No newline at end of file + yield max(t - time.time(), 0) + + +def homekit_path(*args) -> str: + return os.path.realpath( + os.path.join(os.path.dirname(__file__), '..', '..', '..', *args) + ) |