reformatted code using black

This commit is contained in:
tomaae 2020-03-16 18:35:51 +01:00
parent 3cf637d1ba
commit bd82612cb8
2 changed files with 90 additions and 65 deletions

View file

@ -272,7 +272,6 @@ class MikrotikControllerData:
traffic_type = "bps"
traffic_div = 1
for uid in self.data["interface"]:
self.data["interface"][uid]["rx-bits-per-second-attr"] = traffic_type
self.data["interface"][uid]["tx-bits-per-second-attr"] = traffic_type

View file

@ -21,7 +21,16 @@ _LOGGER = logging.getLogger(__name__)
class MikrotikAPI:
"""Handle all communication with the Mikrotik API."""
def __init__(self, host, username, password, port=0, use_ssl=True, login_method=DEFAULT_LOGIN_METHOD, encoding=DEFAULT_ENCODING):
def __init__(
self,
host,
username,
password,
port=0,
use_ssl=True,
login_method=DEFAULT_LOGIN_METHOD,
encoding=DEFAULT_ENCODING,
):
"""Initialize the Mikrotik Client."""
self._host = host
self._use_ssl = use_ssl
@ -76,7 +85,9 @@ class MikrotikAPI:
kwargs["ssl_wrapper"] = self._ssl_wrapper
self.lock.acquire()
try:
self._connection = librouteros.connect(self._host, self._username, self._password, **kwargs)
self._connection = librouteros.connect(
self._host, self._username, self._password, **kwargs
)
except (
librouteros.exceptions.TrapError,
librouteros.exceptions.MultiTrapError,
@ -85,15 +96,19 @@ class MikrotikAPI:
librouteros.exceptions.FatalError,
ssl.SSLError,
BrokenPipeError,
OSError
OSError,
) as api_error:
_LOGGER.error("Mikrotik %s error while connecting: %s", self._host, api_error)
_LOGGER.error(
"Mikrotik %s error while connecting: %s", self._host, api_error
)
self.error_to_strings("%s" % api_error)
self._connection = None
self.lock.release()
return False
except:
_LOGGER.error("Mikrotik %s error while connecting: %s", self._host, "Unknown")
_LOGGER.error(
"Mikrotik %s error while connecting: %s", self._host, "Unknown"
)
self._connection = None
self.lock.release()
return False
@ -152,7 +167,7 @@ class MikrotikAPI:
ssl.SSLError,
BrokenPipeError,
OSError,
ValueError
ValueError,
) as api_error:
_LOGGER.error("Mikrotik %s error while path %s", self._host, api_error)
self.disconnect()
@ -205,10 +220,7 @@ class MikrotikAPI:
continue
entry_found = True
params = {
'.id': tmp['.id'],
mod_param: mod_value
}
params = {".id": tmp[".id"], mod_param: mod_value}
self.lock.acquire()
try:
@ -226,21 +238,25 @@ class MikrotikAPI:
ssl.SSLError,
BrokenPipeError,
OSError,
ValueError
ValueError,
) as api_error:
_LOGGER.error("Mikrotik %s error while update %s", self._host, api_error)
_LOGGER.error(
"Mikrotik %s error while update %s", self._host, api_error
)
self.disconnect()
self.lock.release()
return False
except:
_LOGGER.error("Mikrotik %s error while update %s", self._host, "unknown")
_LOGGER.error(
"Mikrotik %s error while update %s", self._host, "unknown"
)
self.disconnect()
self.lock.release()
return False
self.lock.release()
if not entry_found:
error = "Parameter \"{}\" with value \"{}\" not found".format(param, value)
error = 'Parameter "{}" with value "{}" not found'.format(param, value)
raise ApiEntryNotFound(error)
return True
@ -258,21 +274,21 @@ class MikrotikAPI:
if not self.connect():
return False
response = self.path('/system/script')
response = self.path("/system/script")
if response is None:
return False
for tmp in response:
if 'name' not in tmp:
if "name" not in tmp:
continue
if tmp['name'] != name:
if tmp["name"] != name:
continue
entry_found = True
self.lock.acquire()
try:
run = response('run', **{'.id': tmp['.id']})
run = response("run", **{".id": tmp[".id"]})
tuple(run)
except librouteros.exceptions.ConnectionClosed:
_LOGGER.error("Mikrotik %s connection closed", self._host)
@ -287,21 +303,25 @@ class MikrotikAPI:
ssl.SSLError,
BrokenPipeError,
OSError,
ValueError
ValueError,
) as api_error:
_LOGGER.error("Mikrotik %s error while run_script %s", self._host, api_error)
_LOGGER.error(
"Mikrotik %s error while run_script %s", self._host, api_error
)
self.disconnect()
self.lock.release()
return False
except:
_LOGGER.error("Mikrotik %s error while run_script %s", self._host, "unknown")
_LOGGER.error(
"Mikrotik %s error while run_script %s", self._host, "unknown"
)
self.disconnect()
self.lock.release()
return False
self.lock.release()
if not entry_found:
error = "Script \"{}\" not found".format(name)
error = 'Script "{}" not found'.format(name)
raise ApiEntryNotFound(error)
return True
@ -319,15 +339,17 @@ class MikrotikAPI:
if not self.connect():
return None
response = self.path('/interface')
response = self.path("/interface")
if response is None:
return None
args = {'interface': interfaces, 'once': True}
args = {"interface": interfaces, "once": True}
self.lock.acquire()
try:
traffic = response('monitor-traffic', **args)
_LOGGER.debug("API response (%s): %s", "/interface/monitor-traffic", traffic)
traffic = response("monitor-traffic", **args)
_LOGGER.debug(
"API response (%s): %s", "/interface/monitor-traffic", traffic
)
except librouteros.exceptions.ConnectionClosed:
_LOGGER.error("Mikrotik %s connection closed", self._host)
self.disconnect()
@ -341,14 +363,18 @@ class MikrotikAPI:
ssl.SSLError,
BrokenPipeError,
OSError,
ValueError
ValueError,
) as api_error:
_LOGGER.error("Mikrotik %s error while get_traffic %s", self._host, api_error)
_LOGGER.error(
"Mikrotik %s error while get_traffic %s", self._host, api_error
)
self.disconnect()
self.lock.release()
return None
except:
_LOGGER.error("Mikrotik %s error while get_traffic %s", self._host, "unknown")
_LOGGER.error(
"Mikrotik %s error while get_traffic %s", self._host, "unknown"
)
self.disconnect()
self.lock.release()
return None