From b7cbca301be40981f9543442e0bc4a09ab760673 Mon Sep 17 00:00:00 2001 From: shaiu Date: Wed, 19 Apr 2023 21:52:35 +0300 Subject: [PATCH] more debug --- pyproject.toml | 5 + technicolorgateway/__init__.py | 9 ++ technicolorgateway/modal.py | 12 ++- tests/resources/device-modal_.lp | 180 +++++++++++++++++++++++++++++++ tests/test_modal.py | 10 ++ 5 files changed, 214 insertions(+), 2 deletions(-) create mode 100644 pyproject.toml create mode 100644 tests/resources/device-modal_.lp diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..0bb10ae --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,5 @@ +[tool.pytest.ini_options] +log_cli = true +log_cli_level = "DEBUG" +log_cli_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)" +log_cli_date_format = "%Y-%m-%d %H:%M:%S" \ No newline at end of file diff --git a/technicolorgateway/__init__.py b/technicolorgateway/__init__.py index 51879eb..4e9ecb8 100644 --- a/technicolorgateway/__init__.py +++ b/technicolorgateway/__init__.py @@ -60,11 +60,14 @@ def srp6authenticate(self): def authenticate(self): try: + _LOGGER.info("trying to simple authenticate") self._br.open(f'{self._uri}', method='POST', data={"username": self._user, "password": self._password}) _LOGGER.debug("br.response %s", self._br.response) if self._br.response.status_code != 200: + _LOGGER.info("no simple authenticate. trying srp6authenticate") return self.srp6authenticate() + _LOGGER.info("simple authenticate success") return True except Exception as exception: @@ -73,15 +76,21 @@ def authenticate(self): raise def get_device_modal(self): + _LOGGER.debug("trying to device-modal") data = self.get_device_modals(f"{self._uri}/modals/device-modal.lp") if len(data) == 0: + _LOGGER.debug("trying to ipv6devices-modal") data = self.get_device_modals(f"{self._uri}/modals/ipv6devices-modal.lp") return data def get_device_modals(self, device_modal): + _LOGGER.debug("get_device_modals") req = self._br.session.get(device_modal) self._br._update_state(req) content = req.content.decode() + _LOGGER.debug("first and last rows of content") + _LOGGER.debug(f"{content[:30]}") + _LOGGER.debug(f"{content[:-30]}") return get_device_modal(content) def get_broadband_modal(self): diff --git a/technicolorgateway/modal.py b/technicolorgateway/modal.py index 7cf9acd..d6d8d5f 100644 --- a/technicolorgateway/modal.py +++ b/technicolorgateway/modal.py @@ -1,8 +1,11 @@ +import logging import re import html2text from bs4 import BeautifulSoup +_LOGGER = logging.getLogger(__name__) + h = html2text.HTML2Text() h.body_width = 0 @@ -28,16 +31,19 @@ def get_device_modal(content): data = [] soup = BeautifulSoup(content, features="lxml") devices = soup.find_all("div", {"class": "popUp smallcard span4"}) + _LOGGER.debug("devices len %s" % len(devices)) rows = soup.find_all('tr') + _LOGGER.debug("rows len %s" % len(rows)) if len(devices) > 0: get_data_from_devices(data, devices) - - if len(rows) > 0: + elif len(rows) > 0: get_data_from_rows(data, rows) return data def get_data_from_devices(data, devices): + _LOGGER.debug("get_data_from_devices") + _LOGGER.debug(f"first device {devices[0]}") for device in devices: device_contents = device.contents name = device_contents[1].contents[1].contents[1].text @@ -47,6 +53,7 @@ def get_data_from_devices(data, devices): def get_data_from_rows(data, rows): + _LOGGER.debug("get_data_from_rows") headers = [ele.text.strip().lower() for ele in rows[0].find_all('th')] name_index = headers.index('hostname') try: @@ -55,6 +62,7 @@ def get_data_from_rows(data, rows): ip_index = headers.index('ipv4') mac_index = headers.index('mac address') rows.pop(0) + _LOGGER.debug(f"first row {rows[0]}") for row in rows: cols = row.find_all('td') cols = [ele.text.strip() for ele in cols] diff --git a/tests/resources/device-modal_.lp b/tests/resources/device-modal_.lp new file mode 100644 index 0000000..5872cc0 --- /dev/null +++ b/tests/resources/device-modal_.lp @@ -0,0 +1,180 @@ + + + + + + + + diff --git a/tests/test_modal.py b/tests/test_modal.py index ebdc018..b54fefc 100644 --- a/tests/test_modal.py +++ b/tests/test_modal.py @@ -77,3 +77,13 @@ def test_get_device_modal_ipv6devices(self): assert modal_list[0]['name'] == 'DeviceHostName' assert modal_list[0]['ip'] == '192.168.1.111' assert modal_list[0]['mac'] == 'A4:83:e7:32:7e:11' + + def test_get_device_modal(self): + with open('tests/resources/device-modal_.lp', encoding='utf-8') as file: + content = file.read() + modal_list = get_device_modal(content) + print('\n') + print(modal_list) + assert modal_list[0]['name'] == 'Luce-Studio' + assert modal_list[0]['ip'] == '*.*.*.158' + assert modal_list[0]['mac'] == '10:5a:17:12:a0:d6'