Coverage for lobster/common/level_definition.py: 100%

27 statements  

« prev     ^ index     » next       coverage.py v7.10.5, created at 2025-08-27 13:02 +0000

1from dataclasses import dataclass, field 

2from typing import List, Any 

3 

4 

5@dataclass 

6class LevelDefinition: 

7 name: str 

8 kind: str 

9 traces: List[str] = field(default_factory=list) 

10 source: List[Any] = field(default_factory=list) 

11 needs_tracing_up: bool = False 

12 needs_tracing_down: bool = False 

13 breakdown_requirements: List[Any] = field(default_factory=list) 

14 raw_trace_requirements: List[Any] = field(default_factory=list) 

15 

16 # Flags for serialization - they are a workaround due to the fact that the system 

17 # tests do a text comparison of a generated LOBSTER file against a reference file. 

18 # The serialization pattern should be updated such that empty lists are not 

19 # serialized, but that means to update all system tests. 

20 # This is a temporary solution to avoid breaking existing tests. 

21 serialize_needs_tracing_up: bool = True 

22 serialize_needs_tracing_down: bool = True 

23 serialize_breakdown_requirements: bool = True 

24 

25 def to_json(self) -> dict: 

26 """Convert the Level instance to a JSON-compatible dictionary.""" 

27 result = { 

28 "name": self.name, 

29 "kind": self.kind, 

30 "traces": self.traces, 

31 "source": self.source, 

32 } 

33 if self.serialize_needs_tracing_up: 

34 result["needs_tracing_up"] = self.needs_tracing_up 

35 if self.serialize_needs_tracing_down: 

36 result["needs_tracing_down"] = self.needs_tracing_down 

37 if self.serialize_breakdown_requirements: 

38 result["breakdown_requirements"] = self.breakdown_requirements 

39 return result 

40 

41 @classmethod 

42 def from_json(cls, data: dict) -> 'LevelDefinition': 

43 """Create a Level instance from a JSON-compatible dictionary.""" 

44 return cls( 

45 name=data["name"], 

46 kind=data["kind"], 

47 traces=data.get("traces", []), 

48 source=data.get("source", []), 

49 needs_tracing_up=data.get("needs_tracing_up", False), 

50 needs_tracing_down=data.get("needs_tracing_down", False), 

51 breakdown_requirements=data.get("breakdown_requirements", []), 

52 serialize_needs_tracing_up="needs_tracing_up" in data, 

53 serialize_needs_tracing_down="needs_tracing_down" in data, 

54 serialize_breakdown_requirements="breakdown_requirements" in data, 

55 )