added virtual commands model
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
import inspect
|
||||
import pkgutil
|
||||
import sys
|
||||
import telnetlib
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
# monkey patching of telnet lib
|
||||
from pprint import pprint
|
||||
|
||||
original_read_until = telnetlib.Telnet.read_until
|
||||
original_write = telnetlib.Telnet.write
|
||||
|
||||
@@ -63,14 +68,14 @@ class TelnetAdapter(ABC):
|
||||
self.esc_char = esc_char
|
||||
|
||||
@abstractmethod
|
||||
def login(self):
|
||||
def _login(self):
|
||||
pass
|
||||
|
||||
def run_cmd(self, cmd, timeout=1, auto_connect=True):
|
||||
def _run_cmd(self, cmd, timeout=1, auto_connect=True):
|
||||
if self.tn is None and not auto_connect:
|
||||
raise Exception("Not connected!")
|
||||
elif self.tn is None:
|
||||
self.login()
|
||||
self._login()
|
||||
self.tn.write(cmd)
|
||||
out = self.tn.read_until_non_empty_line()
|
||||
res = out
|
||||
@@ -81,8 +86,53 @@ class TelnetAdapter(ABC):
|
||||
return res
|
||||
|
||||
@staticmethod
|
||||
def get_response_str(tn_response):
|
||||
def _get_response_str(tn_response):
|
||||
if isinstance(tn_response, bytes):
|
||||
return str(tn_response.decode("ascii").rstrip())
|
||||
else:
|
||||
return str(tn_response).rstrip()
|
||||
|
||||
|
||||
class RecorderAdapter:
|
||||
@abstractmethod
|
||||
def _get_name(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def _get_version(self):
|
||||
pass
|
||||
|
||||
|
||||
def get_defined_recorder_adapters():
|
||||
models = []
|
||||
found_packages = list(pkgutil.iter_modules(sys.modules[__name__].__path__))
|
||||
for f_p in found_packages:
|
||||
importer = f_p[0]
|
||||
rec_model_module = importer.find_module(f_p[1]).load_module(f_p[1])
|
||||
rec_model = {'id': f_p[1], 'name': f_p[1], 'commands': {}}
|
||||
if hasattr(rec_model_module, 'RECORDER_MODEL_NAME'):
|
||||
rec_model['name'] = rec_model_module.RECORDER_MODEL_NAME
|
||||
for name, obj in inspect.getmembers(rec_model_module, inspect.isclass):
|
||||
if issubclass(obj, RecorderAdapter):
|
||||
commands = {}
|
||||
for method_name, method in inspect.getmembers(obj, predicate=inspect.isfunction):
|
||||
if len(method_name) > 0 and "_" == method_name[0]:
|
||||
continue
|
||||
signature = inspect.signature(method)
|
||||
parameters = {}
|
||||
for params in signature.parameters:
|
||||
if params == "self":
|
||||
continue
|
||||
param_type = signature.parameters[params].annotation.__name__
|
||||
param_type = "_unknown_type" if param_type == "_empty" else param_type
|
||||
parameters[signature.parameters[params].name] = param_type
|
||||
if len(parameters) <= 0:
|
||||
parameters = None
|
||||
commands[method_name] = parameters
|
||||
rec_model["commands"] = commands
|
||||
models.append(rec_model)
|
||||
pprint(models)
|
||||
return models
|
||||
|
||||
|
||||
get_defined_recorder_adapters()
|
||||
|
||||
Reference in New Issue
Block a user