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

1# -*- coding: utf-8 -*-

2

3 import logging

4

5 import isbnlib

6

7

8 class Item:

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

10 keys = list(map(lambda x: x.strip().upper(), keys))

11 self.data = dict(zip(keys, values))

12 self.data_row_num = row_num

13 self.isbn_key = isbn_key

14 self.isbn = str(self.data[isbn_key])

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

16 self.image_urls = []

17 if "DESCRIPTION" not in self.data.keys():

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

19 self._sort_data()

20

21 def _sort_data(self):

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

23

24 def sortOrder(e):

25 defined_order = [

26 "TITLE",

27 "SUBTITLE",

28 "AUTHOR",

29 "PUBLISHER",

30 "PUB DATE",

31 "PUBLISHERDATE",

32 "FORMAT",

33 ]

34 if e in defined_order: 34 ↛ 35line 34 didn't jump to line 35, because the condition on line 34 was never true

35 return defined_order.index(e)

36 return 99

37

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

39 # sort by defined order

40 sorted_keys.sort(key=sortOrder)

41 # move ISBN and DESCRIPTION to end of list

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

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

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

45

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

47 self.data = sorted_data