Coverage for src/artemis_sg/item.py: 92%
45 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-10-12 17:31 -0700
« prev ^ index » next coverage.py v7.3.1, created at 2023-10-12 17:31 -0700
1import logging
2import re
4import isbnlib
7class Item:
8 def __init__(self, keys, values, row_num, isbn_key):
9 clean_keys = []
10 for x in keys:
11 if x:
12 clean_keys.append(str(x).strip().upper())
13 self.data = dict(zip(clean_keys, values))
14 self.data_row_num = row_num
15 self.isbn_key = isbn_key
16 self.isbn = self.validate_isbn(self.data[isbn_key])
17 self.isbn10 = isbnlib.to_isbn10(self.isbn)
18 self.image_urls = []
19 if "DESCRIPTION" not in self.data:
20 self.data["DESCRIPTION"] = ""
21 self._sort_data()
23 def _sort_data(self):
24 namespace = f"{type(self).__name__}.{self._sort_data.__name__}"
26 def sort_order(e):
27 # TODO: (#163) move to CFG
28 defined_order = [
29 "TITLE",
30 "SUBTITLE",
31 "AUTHOR",
32 "PUBLISHER",
33 "PUB DATE",
34 "PUBLISHERDATE",
35 "FORMAT",
36 ]
37 if e in defined_order: 37 ↛ 38line 37 didn't jump to line 38, because the condition on line 37 was never true
38 return defined_order.index(e)
39 return 99
41 sorted_keys = list(self.data.keys())
42 # sort by defined order
43 sorted_keys.sort(key=sort_order)
44 # move ISBN and DESCRIPTION to end of list
45 sorted_keys.sort(key=self.isbn_key.__eq__)
46 sorted_keys.sort(key="DESCRIPTION".__eq__)
47 logging.debug(f"{namespace}: Sorted keys: {sorted_keys}")
49 sorted_data = {key: self.data[key] for key in sorted_keys}
50 self.data = sorted_data
52 def validate_isbn(self, isbn):
53 namespace = f"{type(self).__name__}.{self.validate_isbn.__name__}"
54 valid_isbn = ""
55 if isinstance(isbn, str): 55 ↛ 59line 55 didn't jump to line 59, because the condition on line 55 was never false
56 m = re.search('="(.*)"', isbn)
57 if m: 57 ↛ 58line 57 didn't jump to line 58, because the condition on line 57 was never true
58 isbn = m.group(1)
59 try:
60 valid_isbn = str(int(isbn)).strip()
61 except Exception as e:
62 logging.error(f"{namespace}: Err reading isbn '{isbn}', err: '{e}'")
63 valid_isbn = ""
64 return valid_isbn