From e9e3958d2b8fd04fd2f7945b2aff1c080e6e5d2a Mon Sep 17 00:00:00 2001 From: Blallo Date: Thu, 25 Aug 2022 13:11:33 +0200 Subject: [PATCH] Improve logging helper --- latecomers/helpers.py | 17 +++++++++++++---- latecomers/parse.py | 6 ++++++ latecomers/retrieve.py | 6 ++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/latecomers/helpers.py b/latecomers/helpers.py index d8b7bd2..eb5267b 100644 --- a/latecomers/helpers.py +++ b/latecomers/helpers.py @@ -1,17 +1,23 @@ # -*- encoding: utf-8 -*- +import functools import logging +import sys import typing as T def logit(logger: logging.Logger) -> T.Callable[..., T.Any]: def wrapper(func: T.Callable[..., T.Any]) -> T.Callable[..., T.Any]: + @functools.wraps(func) def inner(*args, **kwargs): try: value = func(*args, **kwargs) - logger.info(f"Success: {func.__name__}(args={args}, kwargs={kwargs})") + logger.info(f"Success: {func.__name__}") + logger.debug(f"{func.__name__}(args={args}, kwargs={kwargs})") return value except Exception as e: logger.error(f"Failure: {e}") + logger.debug(f"{func.__name__}(args={args}, kwargs={kwargs})") + sys.exit(1) return inner @@ -25,16 +31,19 @@ def retry_and_log(logger: logging.Logger, retries: int): """ def wrapper(func: T.Callable[..., T.Any]) -> T.Callable[..., T.Any]: + @functools.wraps(func) def inner(*args, **kwargs): for i in range(retries): try: value = func(*args, **kwargs) - logger.info( - f"Success ({i+1} tentative): {func.__name__}(args={args}, kwargs={kwargs})" # noqa: E501 - ) + logger.info(f"Success ({i+1} tentative): {func.__name__}") + logger.debug(f"{func.__name__}(args={args}, kwargs={kwargs})") return value except Exception as e: logger.error(f"Failure ({i+1} tentative): {e}") + logger.debug(f"{func.__name__}(args={args}, kwargs={kwargs})") + + sys.exit(2) return inner diff --git a/latecomers/parse.py b/latecomers/parse.py index b3e5302..e1a3724 100644 --- a/latecomers/parse.py +++ b/latecomers/parse.py @@ -1,9 +1,12 @@ # -*- encoding: utf-8 -*- from dataclasses import dataclass from enum import Enum +import logging import re import typing as T +from latecomers.helpers import logit + from lxml import etree as et TIME_RE = re.compile(r"\d\d?:\d\d") @@ -11,6 +14,8 @@ AIRPORT_RE = re.compile(r"[\w\d\s\S]+") STATUS_RE = re.compile(r"(Arrivato|In Arrivo|Schedulato|Cancellato)") PARSER = et.HTMLParser() +logger = logging.getLogger(__name__) + def not_empty(obj: et._Element) -> bool: if type(obj) is et._Element: @@ -20,6 +25,7 @@ def not_empty(obj: et._Element) -> bool: raise RuntimeError(f"provided argument is of unsupported type: {type(obj)}") +@logit(logger) def find_table(html_content: T.Text) -> T.List[et._ElementTree]: """ Find the table that holds the data in the html response diff --git a/latecomers/retrieve.py b/latecomers/retrieve.py index 85515e9..e0babcd 100644 --- a/latecomers/retrieve.py +++ b/latecomers/retrieve.py @@ -1,13 +1,19 @@ # -*- encoding: utf-8 -*- +import logging import typing as T +from latecomers.helpers import logit + import requests HOW_MANY = 200 URL = f"https://www.adr.it/pax-cia-voli-in-tempo-reale?p_p_id=3_WAR_realtimeflightsportlet&p_p_lifecycle=0&p_p_state=normal&p_p_mode=view&_3_WAR_realtimeflightsportlet_tab=arrival&_3_WAR_realtimeflightsportlet_delta={HOW_MANY}&_3_WAR_realtimeflightsportlet_airport=&_3_WAR_realtimeflightsportlet_carrier=&_3_WAR_realtimeflightsportlet_codNat=&_3_WAR_realtimeflightsportlet_codScaOpe=&_3_WAR_realtimeflightsportlet_codVet=&_3_WAR_realtimeflightsportlet_date=&_3_WAR_realtimeflightsportlet_dataNumVol=&_3_WAR_realtimeflightsportlet_numVol=&_3_WAR_realtimeflightsportlet_rouIata=&_3_WAR_realtimeflightsportlet_orario=&_3_WAR_realtimeflightsportlet_searchType=completeSmall&_3_WAR_realtimeflightsportlet_orderByCol=comparationTime&_3_WAR_realtimeflightsportlet_orderByType=asc&_3_WAR_realtimeflightsportlet_resetCur=false&_3_WAR_realtimeflightsportlet_cur=1" # noqa: E501 +logger = logging.getLogger(__name__) + +@logit(logger) def retrieve() -> T.Text: """This function retrieves the body from the website page""" r = requests.get(URL)