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 time
from abc import ABCMeta, abstractmethod
from pathlib import Path
from typing import Iterable, Optional, Union
try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
from .event import Event
from .packet import RequestHeader
from .subject import Subject
@ -27,6 +33,10 @@ class VbanCmd(metaclass=ABCMeta):
def __init__(self, **kwargs):
for attr, val in kwargs.items():
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(
name=self.streamname,
@ -45,6 +55,12 @@ class VbanCmd(metaclass=ABCMeta):
"""Ensure subclasses override str magic method"""
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):
self.login()
return self