Skip to content

Commit

Permalink
Refactor ExtendParser (parameters from instance to method)
Browse files Browse the repository at this point in the history
  • Loading branch information
wvanhed committed Jul 5, 2024
1 parent 5f694ff commit 36000fe
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/mijnbib/mijnbibliotheek.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def __init__(self, username: str, password: str, city: str | None = None, login_
self._loans_page_parser = LoansListPageParser()
self._accounts_page_parser = AccountsListPageParser()
self._reservations_parser = ReservationsPageParser()
self._extend_response_page_parser = ExtendResponsePageParser()

# *** PUBLIC METHODS ***

Expand Down Expand Up @@ -278,7 +279,7 @@ def extend_loans(self, extend_url: str, execute: bool = False) -> tuple[bool, di
# On submit, we arrive at "uitleningen" (loans) page, which lists the result
html_string = response.read().decode("utf-8") # type:ignore
# Path("response.html").write_text("html_string") # for debugging
details = ExtendResponsePageParser(html_string).parse()
details = self._extend_response_page_parser.parse(html_string)
if "likely_success" in details and details["likely_success"] is False:
# Probably valid page (http=200) but with 'Foutmelding'
success = False
Expand Down
24 changes: 8 additions & 16 deletions src/mijnbib/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,12 @@


class Parser(ABC):
@abstractmethod
def parse(self):
pass


class ParserNew(ABC):
@abstractmethod
def parse(self, html: str, *args, **kwargs):
pass


class LoansListPageParser(ParserNew):
class LoansListPageParser(Parser):
def parse(self, html: str, base_url: str, account_id: str) -> list[Loan]:
"""Return loans.
Expand Down Expand Up @@ -227,7 +221,7 @@ def _get_loan_info_from_div(
)


class AccountsListPageParser(ParserNew):
class AccountsListPageParser(Parser):
def parse(self, html: str, base_url: str) -> list[Account]:
"""Return list of accounts.
Expand Down Expand Up @@ -391,7 +385,7 @@ def _parse_item_count_from_li(acc_div, class_: str) -> int | None:
return item_count


class ReservationsPageParser(ParserNew):
class ReservationsPageParser(Parser):
def parse(self, html: str) -> list[Reservation]:
"""Return list of holds.
Expand Down Expand Up @@ -537,15 +531,13 @@ def parse(self, html: str) -> list[Reservation]:


class ExtendResponsePageParser(Parser):
def __init__(self, html: str):
self._html = html

def parse(self) -> dict:
def parse(self, html) -> dict:
"""For dict structure, see the called method."""
html_blob = self._extract_html_from_response_script_tag()
html_blob = self._extract_html_from_response_script_tag(html)
return self._parse_extend_response_status_blob(html_blob)

def _extract_html_from_response_script_tag(self):
@classmethod
def _extract_html_from_response_script_tag(cls, html: str):
"""Return html-encoded data from ajax encoded data.
The extending loan response contains the result in a ajax script thingy.
Expand All @@ -558,7 +550,7 @@ def find_between(s: str, start: str, end: str):
return s[s.find(start) + len(start) : s.rfind(end)]

# find relevant snippet
soup = BeautifulSoup(self._html, "html.parser")
soup = BeautifulSoup(html, "html.parser")
script_txt = soup.find(
"script", string=re.compile("(Statusbericht|Foutmelding)")
).get_text()
Expand Down
3 changes: 3 additions & 0 deletions tests/test_mijnbibliotheek.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,6 @@ def parse(self, _html):

# Assert
assert mb.get_reservations(account_id="whatever") == [res]

# def test_extendresponse_parser_can_be_overridden(self):
# Not so easy to write test for
4 changes: 2 additions & 2 deletions tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ def test_extract_html_from_response_script_tag(self):
def clean_whitespace(s: str) -> str:
return s.replace(" ", "").replace("\n", "")

actual_result = ExtendResponsePageParser(
actual_result = ExtendResponsePageParser._extract_html_from_response_script_tag(
raw_html
)._extract_html_from_response_script_tag()
)
assert clean_whitespace(actual_result) == clean_whitespace(expected_result)

def test_parse_extend_response_status_blob__empty_case(self):
Expand Down

0 comments on commit 36000fe

Please sign in to comment.