Coverage for lobster/tools/trlc/conversion_rule.py: 88%

68 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-09-30 04:59 +0000

1 

2from dataclasses import dataclass 

3from typing import Dict, Iterable, List, Optional, Union 

4 

5from lobster.tools.trlc.tag_entry import TagEntry 

6 

7 

8@dataclass 

9class ConversionRule: 

10 """Specifies how to convert TRLC Record_Objects to LOBSTER items.""" 

11 

12 def __init__( 

13 self, 

14 package: str, 

15 record_type: str, 

16 namespace: str, 

17 version_field: Optional[str] = None, 

18 description_fields: Optional[Union[str, Iterable[str]]] = None, 

19 justification_up_fields: Optional[Union[str, Iterable[str]]] = None, 

20 justification_down_fields: Optional[Union[str, Iterable[str]]] = None, 

21 justification_global_fields: Optional[Union[str, Iterable[str]]] = None, 

22 tags: Optional[Iterable[Union[str, Dict[str, str]]]] = None, 

23 applies_to_derived_types: bool = True, 

24 ): 

25 self._record_type_name = record_type 

26 self._package_name = package 

27 self._lobster_namespace = namespace 

28 self._version = version_field 

29 self._description_fields = self._as_string_list(description_fields) 

30 self._justification_up_fields = self._as_string_list(justification_up_fields) 

31 self._justification_down_fields = self._as_string_list( 

32 justification_down_fields) 

33 self._justification_global_fields = self._as_string_list( 

34 justification_global_fields) 

35 self._tags = self._as_tag_list(tags) 

36 self._applies_to_derived_types = applies_to_derived_types 

37 

38 def __hash__(self) -> int: 

39 return id(self) 

40 

41 @property 

42 def type_name(self) -> str: 

43 return self._record_type_name 

44 

45 @property 

46 def package_name(self) -> str: 

47 return self._package_name 

48 

49 @property 

50 def applies_to_derived_types(self) -> bool: 

51 return self._applies_to_derived_types 

52 

53 @property 

54 def lobster_namespace(self) -> str: 

55 return self._lobster_namespace 

56 

57 @property 

58 def version(self) -> str: 

59 return self._version 

60 

61 @staticmethod 

62 def _as_string_list(value: Optional[Union[str, Iterable[str]]]) -> List[str]: 

63 if value is None: 

64 return [] 

65 elif isinstance(value, str): 65 ↛ 66line 65 didn't jump to line 66 because the condition on line 65 was never true

66 return [value] 

67 elif isinstance(value, list): 67 ↛ 70line 67 didn't jump to line 70 because the condition on line 67 was always true

68 return value 

69 else: 

70 raise ValueError(f"Expected str or list, got {type(value)}") 

71 

72 @staticmethod 

73 def _as_tag_list( 

74 tags: Optional[Iterable[Union[str, Dict[str, str]]]], 

75 ) -> List[TagEntry]: 

76 result = [] 

77 if tags is not None: 

78 for tag in tags: 

79 if isinstance(tag, str): 79 ↛ 81line 79 didn't jump to line 81 because the condition on line 79 was always true

80 result.append(TagEntry(field=tag)) 

81 elif isinstance(tag, dict): 

82 result.append(TagEntry(**tag)) 

83 else: 

84 raise ValueError(f"Expected str or dict, got {type(tag)}") 

85 return result 

86 

87 @property 

88 def tags(self) -> List[TagEntry]: 

89 return self._tags 

90 

91 @property 

92 def description_fields(self) -> List[str]: 

93 return self._description_fields 

94 

95 @property 

96 def justification_up_fields(self) -> List[str]: 

97 return self._justification_up_fields 

98 

99 @property 

100 def justification_down_fields(self) -> List[str]: 

101 return self._justification_down_fields 

102 

103 @property 

104 def justification_global_fields(self) -> List[str]: 

105 return self._justification_global_fields