Skip to content

Commit

Permalink
changed librouteros library name to avoid conflict with loaded library
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaae committed Dec 8, 2019
1 parent 1089876 commit 0896d0c
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
from socket import create_connection
from collections import ChainMap

from librouteros.exceptions import (
from .exceptions import (
ConnectionClosed,
FatalError,
)
from librouteros.connections import SocketTransport
from librouteros.protocol import ApiProtocol
from librouteros.login import (
from .connections import SocketTransport
from .protocol import ApiProtocol
from .login import (
plain,
token,
)
from librouteros.api import Api
from .api import Api

DEFAULTS = {
'timeout': 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from posixpath import join as pjoin

from librouteros.exceptions import TrapError, MultiTrapError
from librouteros.protocol import (
from .exceptions import TrapError, MultiTrapError
from .protocol import (
compose_word,
parse_word,
)
from librouteros.query import Query
from .query import Query


class Api:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: UTF-8 -*-

from librouteros.exceptions import ConnectionClosed
from .exceptions import ConnectionClosed


class SocketTransport:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from struct import pack, unpack
from logging import getLogger, NullHandler

from librouteros.exceptions import (
from .exceptions import (
ProtocolError,
FatalError,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: UTF-8 -*-
from itertools import chain
from librouteros.protocol import (
from .protocol import (
cast_to_api,
)

Expand Down
62 changes: 31 additions & 31 deletions custom_components/mikrotik_router/mikrotikapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
DEFAULT_ENCODING,
)

MODULE_PATH = os.path.join(os.path.dirname(__file__), "librouteros", "__init__.py")
MODULE_NAME = "librouteros"
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 = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = librouteros
spec.loader.exec_module(librouteros)
librouteros_custom = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = librouteros_custom
spec.loader.exec_module(librouteros_custom)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -69,13 +69,13 @@ def connect(self):
kwargs["ssl_wrapper"] = self._ssl_wrapper

try:
self._connection = librouteros.connect(self._host, self._username, self._password, **kwargs)
self._connection = librouteros_custom.connect(self._host, self._username, self._password, **kwargs)
except (
librouteros.exceptions.TrapError,
librouteros.exceptions.MultiTrapError,
librouteros.exceptions.ConnectionClosed,
librouteros.exceptions.ProtocolError,
librouteros.exceptions.FatalError,
librouteros_custom.exceptions.TrapError,
librouteros_custom.exceptions.MultiTrapError,
librouteros_custom.exceptions.ConnectionClosed,
librouteros_custom.exceptions.ProtocolError,
librouteros_custom.exceptions.FatalError,
ssl.SSLError
) as api_error:
_LOGGER.error("Mikrotik %s: %s", self._host, api_error)
Expand Down Expand Up @@ -121,16 +121,16 @@ def path(self, path):
try:
response = self._connection.path(path)
tuple(response)
except librouteros.exceptions.ConnectionClosed:
except librouteros_custom.exceptions.ConnectionClosed:
_LOGGER.error("Mikrotik %s connection closed", self._host)
self._connected = False
self._connection = None
return None
except (
librouteros.exceptions.TrapError,
librouteros.exceptions.MultiTrapError,
librouteros.exceptions.ProtocolError,
librouteros.exceptions.FatalError
librouteros_custom.exceptions.TrapError,
librouteros_custom.exceptions.MultiTrapError,
librouteros_custom.exceptions.ProtocolError,
librouteros_custom.exceptions.FatalError
) as api_error:
_LOGGER.error("Mikrotik %s connection error %s", self._host, api_error)
return None
Expand Down Expand Up @@ -166,16 +166,16 @@ def update(self, path, param, value, mod_param, mod_value):

try:
response.update(**params)
except librouteros.exceptions.ConnectionClosed:
except librouteros_custom.exceptions.ConnectionClosed:
_LOGGER.error("Mikrotik %s connection closed", self._host)
self._connected = False
self._connection = None
return None
except (
librouteros.exceptions.TrapError,
librouteros.exceptions.MultiTrapError,
librouteros.exceptions.ProtocolError,
librouteros.exceptions.FatalError
librouteros_custom.exceptions.TrapError,
librouteros_custom.exceptions.MultiTrapError,
librouteros_custom.exceptions.ProtocolError,
librouteros_custom.exceptions.FatalError
) as api_error:
_LOGGER.error("Mikrotik %s connection error %s", self._host, api_error)
return None
Expand Down Expand Up @@ -210,16 +210,16 @@ def run_script(self, name):
entry_found = True
try:
run = response('run', **{'.id': tmp['.id']})
except librouteros.exceptions.ConnectionClosed:
except librouteros_custom.exceptions.ConnectionClosed:
_LOGGER.error("Mikrotik %s connection closed", self._host)
self._connected = False
self._connection = None
return None
except (
librouteros.exceptions.TrapError,
librouteros.exceptions.MultiTrapError,
librouteros.exceptions.ProtocolError,
librouteros.exceptions.FatalError
librouteros_custom.exceptions.TrapError,
librouteros_custom.exceptions.MultiTrapError,
librouteros_custom.exceptions.ProtocolError,
librouteros_custom.exceptions.FatalError
) as api_error:
_LOGGER.error("Mikrotik %s connection error %s", self._host, api_error)
return None
Expand Down Expand Up @@ -249,16 +249,16 @@ def get_traffic(self, interfaces):
args = {'interface': interfaces, 'once': True}
try:
traffic = response('monitor-traffic', **args)
except librouteros.exceptions.ConnectionClosed:
except librouteros_custom.exceptions.ConnectionClosed:
_LOGGER.error("Mikrotik %s connection closed", self._host)
self._connected = False
self._connection = None
return None
except (
librouteros.exceptions.TrapError,
librouteros.exceptions.MultiTrapError,
librouteros.exceptions.ProtocolError,
librouteros.exceptions.FatalError
librouteros_custom.exceptions.TrapError,
librouteros_custom.exceptions.MultiTrapError,
librouteros_custom.exceptions.ProtocolError,
librouteros_custom.exceptions.FatalError
) as api_error:
_LOGGER.error("Mikrotik %s connection error %s", self._host, api_error)
return None
Expand Down

0 comments on commit 0896d0c

Please sign in to comment.