add ability to read conn info from toml

This commit is contained in:
onyx-and-iris 2022-10-07 20:00:56 +01:00
parent be69d905c4
commit d57269f147

View File

@ -2,8 +2,14 @@ import logging
import socket import socket
import time import time
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from pathlib import Path
from typing import Iterable, Optional, Union from typing import Iterable, Optional, Union
try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
from .event import Event from .event import Event
from .packet import RequestHeader from .packet import RequestHeader
from .subject import Subject from .subject import Subject
@ -27,6 +33,10 @@ class VbanCmd(metaclass=ABCMeta):
def __init__(self, **kwargs): def __init__(self, **kwargs):
for attr, val in kwargs.items(): for attr, val in kwargs.items():
setattr(self, attr, val) setattr(self, attr, val)
if self.ip is None:
conn = self._conn_from_toml()
for attr, val in conn.items():
setattr(self, attr, val)
self.packet_request = RequestHeader( self.packet_request = RequestHeader(
name=self.streamname, name=self.streamname,
@ -45,6 +55,12 @@ class VbanCmd(metaclass=ABCMeta):
"""Ensure subclasses override str magic method""" """Ensure subclasses override str magic method"""
pass pass
def _conn_from_toml(self) -> str:
filepath = Path.cwd() / "vban.toml"
with open(filepath, "rb") as f:
conn = tomllib.load(f)
return conn["connection"]
def __enter__(self): def __enter__(self):
self.login() self.login()
return self return self