Coverage for skema/model_assembly/air.py: 23%

44 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-30 17:15 +0000

1from __future__ import annotations 

2from pathlib import Path 

3import json 

4 

5from .metadata import GrFNCreation, CodeCollectionReference 

6from .structures import ( 

7 GenericDefinition, 

8 VariableDefinition, 

9 GenericContainer, 

10 GenericIdentifier, 

11 TypeDefinition, 

12) 

13 

14 

15class AutoMATES_IR: 

16 def __init__(self, e, C, V, T, O, D, M): 

17 self.entrypoint = e 

18 self.containers = C 

19 self.variables = V 

20 self.type_definitions = T 

21 self.objects = O 

22 self.documentation = D 

23 self.metadata = M 

24 

25 def to_json(self): 

26 json_dict = { 

27 "entrypoint": self.entrypoint, 

28 "containers": self.containers, 

29 "variables": self.variables, 

30 "types": self.type_definitions, 

31 "objects": self.objects, 

32 "documentation": self.documentation, 

33 "metadata": self.metadata, 

34 } 

35 

36 with open("test.json", "w") as f: 

37 json.dump(json_dict, f) 

38 

39 @classmethod 

40 def from_json(cls, filepath: str) -> AutoMATES_IR: 

41 data = json.load(open(filepath, "r")) 

42 

43 C, V, O, D = dict(), dict(), dict(), dict() 

44 

45 M = [ 

46 GrFNCreation.from_name(filepath.replace("--AIR.json", "")), 

47 ] 

48 if "sources" in data: 

49 code_refs = CodeCollectionReference.from_sources(data["sources"]) 

50 code_file_uid = code_refs.files[0].uid 

51 M.append(code_refs) 

52 

53 for var_data in data["variables"]: 

54 # new_var = GenericDefinition.from_dict(var_data) 

55 var_data.update({"file_uid": code_file_uid}) 

56 new_var = VariableDefinition.from_data(var_data) 

57 V[new_var.identifier] = new_var 

58 

59 T = list() 

60 for type_data in data["types"]: 

61 type_data.update({"file_uid": code_file_uid}) 

62 T.append(TypeDefinition.from_air_data(type_data)) 

63 

64 for con_data in data["containers"]: 

65 con_data.update({"file_uid": code_file_uid}) 

66 new_container = GenericContainer.from_dict(con_data) 

67 C[new_container.identifier] = new_container 

68 

69 # TODO Paul - is it fine to switch from keying by filename to keying by 

70 # container name? Also, lowercasing? - Adarsh 

71 filename = data["sources"][0] 

72 container_name = Path(filename).stem.lower() 

73 D.update( 

74 { 

75 n if not n.startswith("$") else container_name + n: data 

76 for n, data in data["source_comments"].items() 

77 } 

78 ) 

79 

80 e = GenericIdentifier.from_str(data["entrypoint"]) 

81 

82 return cls(e, C, V, T, O, D, M)