mirror of
https://github.com/onyx-and-iris/vban-cmd-python.git
synced 2026-04-18 04:53:31 +00:00
make changes to sockets.
replace black+isort with ruff upd examples
This commit is contained in:
@@ -11,7 +11,7 @@ from .error import VBANCMDError
|
||||
from .event import Event
|
||||
from .packet import RequestHeader
|
||||
from .subject import Subject
|
||||
from .util import Socket, deep_merge, script
|
||||
from .util import deep_merge, script
|
||||
from .worker import Producer, Subscriber, Updater
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -31,8 +31,8 @@ class VbanCmd(metaclass=ABCMeta):
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.logger = logger.getChild(self.__class__.__name__)
|
||||
self.event = Event({k: kwargs.pop(k) for k in ("pdirty", "ldirty")})
|
||||
if not kwargs["ip"]:
|
||||
self.event = Event({k: kwargs.pop(k) for k in ('pdirty', 'ldirty')})
|
||||
if not kwargs['ip']:
|
||||
kwargs |= self._conn_from_toml()
|
||||
for attr, val in kwargs.items():
|
||||
setattr(self, attr, val)
|
||||
@@ -42,9 +42,7 @@ class VbanCmd(metaclass=ABCMeta):
|
||||
bps_index=self.BPS_OPTS.index(self.bps),
|
||||
channel=self.channel,
|
||||
)
|
||||
self.socks = tuple(
|
||||
socket.socket(socket.AF_INET, socket.SOCK_DGRAM) for _ in Socket
|
||||
)
|
||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
self.subject = self.observer = Subject()
|
||||
self.cache = {}
|
||||
self._pdirty = False
|
||||
@@ -65,29 +63,30 @@ class VbanCmd(metaclass=ABCMeta):
|
||||
import tomli as tomllib
|
||||
|
||||
def get_filepath():
|
||||
filepaths = [
|
||||
Path.cwd() / "vban.toml",
|
||||
Path.cwd() / "configs" / "vban.toml",
|
||||
Path.home() / ".config" / "vban-cmd" / "vban.toml",
|
||||
Path.home() / "Documents" / "Voicemeeter" / "configs" / "vban.toml",
|
||||
]
|
||||
for filepath in filepaths:
|
||||
if filepath.exists():
|
||||
return filepath
|
||||
for pn in (
|
||||
Path.cwd() / 'vban.toml',
|
||||
Path.cwd() / 'configs' / 'vban.toml',
|
||||
Path.home() / '.config' / 'vban-cmd' / 'vban.toml',
|
||||
Path.home() / 'Documents' / 'Voicemeeter' / 'configs' / 'vban.toml',
|
||||
):
|
||||
if pn.exists():
|
||||
return pn
|
||||
|
||||
if filepath := get_filepath():
|
||||
with open(filepath, "rb") as f:
|
||||
conn = tomllib.load(f)
|
||||
assert (
|
||||
"connection" in conn and "ip" in conn["connection"]
|
||||
), "expected [connection][ip] in vban config"
|
||||
return conn["connection"]
|
||||
raise VBANCMDError("no ip provided and no vban.toml located.")
|
||||
if not (filepath := get_filepath()):
|
||||
raise VBANCMDError('no ip provided and no vban.toml located.')
|
||||
try:
|
||||
with open(filepath, 'rb') as f:
|
||||
return tomllib.load(f)['connection']
|
||||
except tomllib.TomlDecodeError as e:
|
||||
raise VBANCMDError(f'Error decoding {filepath}: {e}') from e
|
||||
|
||||
def __enter__(self):
|
||||
self.login()
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_value, exc_traceback) -> None:
|
||||
self.logout()
|
||||
|
||||
def login(self) -> None:
|
||||
"""Starts the subscriber and updater threads (unless in outbound mode)"""
|
||||
if not self.outbound:
|
||||
@@ -110,31 +109,41 @@ class VbanCmd(metaclass=ABCMeta):
|
||||
)
|
||||
)
|
||||
|
||||
def logout(self) -> None:
|
||||
if not self.stopped():
|
||||
self.logger.debug('events thread shutdown started')
|
||||
self.stop_event.set()
|
||||
if self.producer is not None:
|
||||
for t in (self.producer, self.subscriber):
|
||||
t.join()
|
||||
self.sock.close()
|
||||
self.logger.info(f'{type(self).__name__}: Successfully logged out of {self}')
|
||||
|
||||
def stopped(self):
|
||||
return self.stop_event is None or self.stop_event.is_set()
|
||||
|
||||
def _set_rt(self, cmd: str, val: Union[str, float]):
|
||||
"""Sends a string request command over a network."""
|
||||
self.socks[Socket.request].sendto(
|
||||
self.packet_request.header + f"{cmd}={val};".encode(),
|
||||
self.sock.sendto(
|
||||
self.packet_request.header + f'{cmd}={val};'.encode(),
|
||||
(socket.gethostbyname(self.ip), self.port),
|
||||
)
|
||||
self.packet_request.framecounter = (
|
||||
int.from_bytes(self.packet_request.framecounter, "little") + 1
|
||||
).to_bytes(4, "little")
|
||||
int.from_bytes(self.packet_request.framecounter, 'little') + 1
|
||||
).to_bytes(4, 'little')
|
||||
self.cache[cmd] = val
|
||||
|
||||
@script
|
||||
def sendtext(self, script):
|
||||
"""Sends a multiple parameter string over a network."""
|
||||
self.socks[Socket.request].sendto(
|
||||
self.sock.sendto(
|
||||
self.packet_request.header + script.encode(),
|
||||
(socket.gethostbyname(self.ip), self.port),
|
||||
)
|
||||
self.packet_request.framecounter = (
|
||||
int.from_bytes(self.packet_request.framecounter, "little") + 1
|
||||
).to_bytes(4, "little")
|
||||
self.logger.debug(f"sendtext: {script}")
|
||||
int.from_bytes(self.packet_request.framecounter, 'little') + 1
|
||||
).to_bytes(4, 'little')
|
||||
self.logger.debug(f'sendtext: {script}')
|
||||
time.sleep(self.DELAY)
|
||||
|
||||
@property
|
||||
@@ -145,7 +154,7 @@ class VbanCmd(metaclass=ABCMeta):
|
||||
@property
|
||||
def version(self) -> str:
|
||||
"""Returns Voicemeeter's version as a string"""
|
||||
return "{0}.{1}.{2}.{3}".format(*self.public_packet.voicemeeterversion)
|
||||
return '{0}.{1}.{2}.{3}'.format(*self.public_packet.voicemeeterversion)
|
||||
|
||||
@property
|
||||
def pdirty(self):
|
||||
@@ -184,16 +193,16 @@ class VbanCmd(metaclass=ABCMeta):
|
||||
"""
|
||||
|
||||
def target(key):
|
||||
match key.split("-"):
|
||||
case ["strip" | "bus" as kls, index] if index.isnumeric():
|
||||
match key.split('-'):
|
||||
case ['strip' | 'bus' as kls, index] if index.isnumeric():
|
||||
target = getattr(self, kls)
|
||||
case [
|
||||
"vban",
|
||||
"in" | "instream" | "out" | "outstream" as direction,
|
||||
'vban',
|
||||
'in' | 'instream' | 'out' | 'outstream' as direction,
|
||||
index,
|
||||
] if index.isnumeric():
|
||||
target = getattr(
|
||||
self.vban, f"{direction.removesuffix('stream')}stream"
|
||||
self.vban, f'{direction.removesuffix("stream")}stream'
|
||||
)
|
||||
case _:
|
||||
ERR_MSG = f"invalid config key '{key}'"
|
||||
@@ -207,36 +216,23 @@ class VbanCmd(metaclass=ABCMeta):
|
||||
"""applies a config from memory"""
|
||||
ERR_MSG = (
|
||||
f"No config with name '{name}' is loaded into memory",
|
||||
f"Known configs: {list(self.configs.keys())}",
|
||||
f'Known configs: {list(self.configs.keys())}',
|
||||
)
|
||||
try:
|
||||
config = self.configs[name]
|
||||
except KeyError as e:
|
||||
self.logger.error(("\n").join(ERR_MSG))
|
||||
raise VBANCMDError(("\n").join(ERR_MSG)) from e
|
||||
self.logger.error(('\n').join(ERR_MSG))
|
||||
raise VBANCMDError(('\n').join(ERR_MSG)) from e
|
||||
|
||||
if "extends" in config:
|
||||
extended = config["extends"]
|
||||
if 'extends' in config:
|
||||
extended = config['extends']
|
||||
config = {
|
||||
k: v
|
||||
for k, v in deep_merge(self.configs[extended], config)
|
||||
if k not in ("extends")
|
||||
if k not in ('extends')
|
||||
}
|
||||
self.logger.debug(
|
||||
f"profile '{name}' extends '{extended}', profiles merged.."
|
||||
)
|
||||
self.apply(config)
|
||||
self.logger.info(f"Profile '{name}' applied!")
|
||||
|
||||
def logout(self) -> None:
|
||||
if not self.stopped():
|
||||
self.logger.debug("events thread shutdown started")
|
||||
self.stop_event.set()
|
||||
if self.producer is not None:
|
||||
for t in (self.producer, self.subscriber):
|
||||
t.join()
|
||||
[sock.close() for sock in self.socks]
|
||||
self.logger.info(f"{type(self).__name__}: Successfully logged out of {self}")
|
||||
|
||||
def __exit__(self, exc_type, exc_value, exc_traceback) -> None:
|
||||
self.logout()
|
||||
|
||||
Reference in New Issue
Block a user