PM8 code optimization

This commit is contained in:
Andrey Khrolenok 2020-03-21 19:02:28 +03:00
parent f0b4c5e723
commit 7ccaf0d9c3
No known key found for this signature in database
GPG key ID: 9D4D4A25BDFFA655
10 changed files with 557 additions and 536 deletions

View file

@ -1,22 +1,23 @@
"""Mikrotik API for Mikrotik Router."""
import ssl
import importlib
import logging
import os
import ssl
import sys
import time
from threading import Lock
from voluptuous import Optional
from .exceptions import ApiEntryNotFound
from .const import (
DEFAULT_LOGIN_METHOD,
DEFAULT_ENCODING,
)
from .exceptions import ApiEntryNotFound
import os
import sys
import importlib
MODULE_PATH = os.path.join(os.path.dirname(__file__), "librouteros_custom", "__init__.py")
MODULE_PATH = os.path.join(os.path.dirname(__file__), "librouteros_custom",
"__init__.py")
MODULE_NAME = "librouteros_custom"
spec = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH)
librouteros_custom = importlib.util.module_from_spec(spec)
@ -112,7 +113,8 @@ class MikrotikAPI:
) as api_error:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while connecting: %s", self._host, api_error
"Mikrotik %s error while connecting: %s", self._host,
api_error
)
self.connection_error_reported = True
@ -123,7 +125,8 @@ class MikrotikAPI:
except:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while connecting: %s", self._host, "Unknown"
"Mikrotik %s error while connecting: %s", self._host,
"Unknown"
)
self.connection_error_reported = True
@ -196,7 +199,8 @@ class MikrotikAPI:
ValueError,
) as api_error:
if not self.connection_error_reported:
_LOGGER.error("Mikrotik %s error while path %s", self._host, api_error)
_LOGGER.error("Mikrotik %s error while path %s", self._host,
api_error)
self.connection_error_reported = True
self.disconnect()
@ -204,7 +208,8 @@ class MikrotikAPI:
return None
except:
if not self.connection_error_reported:
_LOGGER.error("Mikrotik %s error while path %s", self._host, "unknown")
_LOGGER.error("Mikrotik %s error while path %s", self._host,
"unknown")
self.connection_error_reported = True
self.disconnect()
@ -215,7 +220,8 @@ class MikrotikAPI:
tuple(response)
except librouteros_custom.exceptions.ConnectionClosed as api_error:
if not self.connection_error_reported:
_LOGGER.error("Mikrotik %s error while path %s", self._host, api_error)
_LOGGER.error("Mikrotik %s error while path %s", self._host,
api_error)
self.connection_error_reported = True
self.disconnect()
@ -223,7 +229,8 @@ class MikrotikAPI:
return None
except:
if not self.connection_error_reported:
_LOGGER.error("Mikrotik %s error while path %s", self._host, "unknown")
_LOGGER.error("Mikrotik %s error while path %s", self._host,
"unknown")
self.connection_error_reported = True
self.disconnect()
@ -283,7 +290,8 @@ class MikrotikAPI:
) as api_error:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while update %s", self._host, api_error
"Mikrotik %s error while update %s", self._host,
api_error
)
self.connection_error_reported = True
@ -293,7 +301,8 @@ class MikrotikAPI:
except:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while update %s", self._host, "unknown"
"Mikrotik %s error while update %s", self._host,
"unknown"
)
self.connection_error_reported = True
@ -357,7 +366,8 @@ class MikrotikAPI:
) as api_error:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while run_script %s", self._host, api_error
"Mikrotik %s error while run_script %s", self._host,
api_error
)
self.connection_error_reported = True
@ -367,7 +377,8 @@ class MikrotikAPI:
except:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while run_script %s", self._host, "unknown"
"Mikrotik %s error while run_script %s", self._host,
"unknown"
)
self.connection_error_reported = True
@ -426,7 +437,8 @@ class MikrotikAPI:
) as api_error:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while get_traffic %s", self._host, api_error
"Mikrotik %s error while get_traffic %s", self._host,
api_error
)
self.connection_error_reported = True
@ -436,7 +448,8 @@ class MikrotikAPI:
except:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while get_traffic %s", self._host, "unknown"
"Mikrotik %s error while get_traffic %s", self._host,
"unknown"
)
self.connection_error_reported = True
@ -449,7 +462,8 @@ class MikrotikAPI:
except librouteros_custom.exceptions.ConnectionClosed as api_error:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while get_traffic %s", self._host, api_error
"Mikrotik %s error while get_traffic %s", self._host,
api_error
)
self.connection_error_reported = True
@ -459,7 +473,8 @@ class MikrotikAPI:
except:
if not self.connection_error_reported:
_LOGGER.error(
"Mikrotik %s error while get_traffic %s", self._host, "unknown"
"Mikrotik %s error while get_traffic %s", self._host,
"unknown"
)
self.connection_error_reported = True