Coverage for amazonorders/entity/parsable.py: 83.33%

18 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-17 01:52 +0000

1import logging 

2from typing import Callable, Any 

3 

4from bs4 import Tag 

5 

6from amazonorders.exception import AmazonOrdersError 

7 

8__author__ = "Alex Laird" 

9__copyright__ = "Copyright 2024, Alex Laird" 

10__version__ = "1.0.0" 

11 

12logger = logging.getLogger(__name__) 

13 

14 

15class Parsable: 

16 """ 

17 

18 """ 

19 

20 def __init__(self, 

21 parsed: Tag) -> None: 

22 #: 

23 self.parsed: Tag = parsed 

24 

25 def safe_parse(self, 

26 parse_function: Callable[[], Any]) -> Any: 

27 """ 

28 

29 :param parse_function: The parse function to attempt safe execution. 

30 :return: The return value from ``parse_function``. 

31 """ 

32 if not parse_function.__name__.startswith("_parse_"): 

33 raise AmazonOrdersError("This name of the `parse_function` passed to this method must start with `_parse_`") 

34 

35 try: 

36 return parse_function() 

37 except (AttributeError, IndexError, ValueError): 

38 logger.warning("When building {}, `{}` could not be parsed.".format(self.__class__.__name__, 

39 parse_function.__name__.split( 

40 "_parse_")[1]), 

41 exc_info=True)