Skip to content

Commit

Permalink
more debug
Browse files Browse the repository at this point in the history
  • Loading branch information
shaiu committed Apr 19, 2023
1 parent 04ae7fe commit b7cbca3
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
9 changes: 9 additions & 0 deletions technicolorgateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down
12 changes: 10 additions & 2 deletions technicolorgateway/modal.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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]
Expand Down
180 changes: 180 additions & 0 deletions tests/resources/device-modal_.lp

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions tests/test_modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit b7cbca3

Please sign in to comment.