Coverage for lobster/tools/trlc/conversion_rule.py: 87%
64 statements
« prev ^ index » next coverage.py v7.10.5, created at 2025-08-27 13:02 +0000
« prev ^ index » next coverage.py v7.10.5, created at 2025-08-27 13:02 +0000
2from dataclasses import dataclass
3from typing import Dict, Iterable, List, Optional, Union
5from lobster.tools.trlc.tag_entry import TagEntry
8@dataclass
9class ConversionRule:
10 """Specifies how to convert TRLC Record_Objects to LOBSTER items."""
12 def __init__(
13 self,
14 package: str,
15 record_type: str,
16 namespace: str,
17 description_fields: Optional[Union[str, Iterable[str]]] = None,
18 justification_up_fields: Optional[Union[str, Iterable[str]]] = None,
19 justification_down_fields: Optional[Union[str, Iterable[str]]] = None,
20 justification_global_fields: Optional[Union[str, Iterable[str]]] = None,
21 tags: Optional[Iterable[Union[str, Dict[str, str]]]] = None,
22 applies_to_derived_types: bool = True,
23 ):
24 self._record_type_name = record_type
25 self._package_name = package
26 self._lobster_namespace = namespace
27 self._description_fields = self._as_string_list(description_fields)
28 self._justification_up_fields = self._as_string_list(justification_up_fields)
29 self._justification_down_fields = self._as_string_list(
30 justification_down_fields)
31 self._justification_global_fields = self._as_string_list(
32 justification_global_fields)
33 self._tags = self._as_tag_list(tags)
34 self._applies_to_derived_types = applies_to_derived_types
36 def __hash__(self) -> int:
37 return id(self)
39 @property
40 def type_name(self) -> str:
41 return self._record_type_name
43 @property
44 def package_name(self) -> str:
45 return self._package_name
47 @property
48 def applies_to_derived_types(self) -> bool:
49 return self._applies_to_derived_types
51 @property
52 def lobster_namespace(self) -> str:
53 return self._lobster_namespace
55 @staticmethod
56 def _as_string_list(value: Optional[Union[str, Iterable[str]]]) -> List[str]:
57 if value is None:
58 return []
59 elif isinstance(value, str): 59 ↛ 60line 59 didn't jump to line 60 because the condition on line 59 was never true
60 return [value]
61 elif isinstance(value, list): 61 ↛ 64line 61 didn't jump to line 64 because the condition on line 61 was always true
62 return value
63 else:
64 raise ValueError(f"Expected str or list, got {type(value)}")
66 @staticmethod
67 def _as_tag_list(
68 tags: Optional[Iterable[Union[str, Dict[str, str]]]],
69 ) -> List[TagEntry]:
70 result = []
71 if tags is not None:
72 for tag in tags:
73 if isinstance(tag, str): 73 ↛ 75line 73 didn't jump to line 75 because the condition on line 73 was always true
74 result.append(TagEntry(field=tag))
75 elif isinstance(tag, dict):
76 result.append(TagEntry(**tag))
77 else:
78 raise ValueError(f"Expected str or dict, got {type(tag)}")
79 return result
81 @property
82 def tags(self) -> List[TagEntry]:
83 return self._tags
85 @property
86 def description_fields(self) -> List[str]:
87 return self._description_fields
89 @property
90 def justification_up_fields(self) -> List[str]:
91 return self._justification_up_fields
93 @property
94 def justification_down_fields(self) -> List[str]:
95 return self._justification_down_fields
97 @property
98 def justification_global_fields(self) -> List[str]:
99 return self._justification_global_fields