Coverage for src/artemis_sg/item.py: 97%

1 import logging

2 import re

3

4 import isbnlib

5

6 from artemis_sg.config import CFG

7

8

9 class Item:

10 def __init__(self, keys, values, row_num, isbn_key):

11 clean_keys = []

12 for x in keys:

13 if x:

14 clean_keys.append(str(x).strip().upper())

15 self.data = dict(zip(clean_keys, values))

16 self.data_row_num = row_num

17 self.isbn_key = isbn_key

18 self.isbn = self.validate_isbn(self.data[isbn_key])

19 self.isbn10 = isbnlib.to_isbn10(self.isbn)

20 self.image_urls = []

21 if "DESCRIPTION" not in self.data:

22 self.data["DESCRIPTION"] = ""

23 if "DIMENSION" not in self.data:

24 self.data["DIMENSION"] = ""

25 self._sort_data()

26

27 def _sort_data(self):

28 namespace = f"{type(self).__name__}.{self._sort_data.__name__}"

29

30 def sort_order(e):

31 defined_order = CFG["asg"]["item"]["sort_order"]

32 if e in defined_order:

33 return defined_order.index(e)

34 return 99

35

36 sorted_keys = list(self.data.keys())

37 # sort by defined order

38 sorted_keys.sort(key=sort_order)

39 # move ISBN and DESCRIPTION to end of list

40 sorted_keys.sort(key=self.isbn_key.__eq__)

41 sorted_keys.sort(key="DESCRIPTION".__eq__)

42 logging.debug(f"{namespace}: Sorted keys: {sorted_keys}")

43

44 sorted_data = {key: self.data[key] for key in sorted_keys}

45 self.data = sorted_data

46

47 def validate_isbn(self, isbn):

48 namespace = f"{type(self).__name__}.{self.validate_isbn.__name__}"

49 valid_isbn = ""

50 if isinstance(isbn, str):

51 m = re.search('="(.*)"', isbn)

52 if m: 52 ↛ 53line 52 didn't jump to line 53, because the condition on line 52 was never true

53 isbn = m.group(1)

54 try:

55 valid_isbn = str(int(isbn)).strip()

56 except Exception as e:

57 logging.error(f"{namespace}: Err reading isbn '{isbn}', err: '{e}'")

58 valid_isbn = ""

59 return valid_isbn