|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | from dataclasses import dataclass, field
|
| 4 | +from typing import Any |
2 | 5 |
|
3 |
| -from package_parser.processing.annotations.model import ( |
| 6 | +from ._annotations import ( |
4 | 7 | ANNOTATION_SCHEMA_VERSION,
|
5 | 8 | BoundaryAnnotation,
|
| 9 | + CalledAfterAnnotation, |
| 10 | + CompleteAnnotation, |
| 11 | + DescriptionAnnotation, |
6 | 12 | EnumAnnotation,
|
| 13 | + GroupAnnotation, |
| 14 | + MoveAnnotation, |
| 15 | + PureAnnotation, |
7 | 16 | RemoveAnnotation,
|
| 17 | + RenameAnnotation, |
| 18 | + TodoAnnotation, |
8 | 19 | ValueAnnotation,
|
9 | 20 | )
|
10 | 21 |
|
11 | 22 |
|
12 | 23 | @dataclass
|
13 | 24 | class AnnotationStore:
|
14 | 25 | boundaryAnnotations: list[BoundaryAnnotation] = field(default_factory=list)
|
| 26 | + calledAfterAnnotations: list[CalledAfterAnnotation] = field(default_factory=list) |
| 27 | + completeAnnotations: list[CompleteAnnotation] = field(default_factory=list) |
| 28 | + descriptionAnnotations: list[DescriptionAnnotation] = field(default_factory=list) |
15 | 29 | enumAnnotations: list[EnumAnnotation] = field(default_factory=list)
|
| 30 | + groupAnnotations: list[GroupAnnotation] = field(default_factory=list) |
| 31 | + moveAnnotations: list[MoveAnnotation] = field(default_factory=list) |
| 32 | + pureAnnotations: list[PureAnnotation] = field(default_factory=list) |
16 | 33 | removeAnnotations: list[RemoveAnnotation] = field(default_factory=list)
|
| 34 | + renameAnnotations: list[RenameAnnotation] = field(default_factory=list) |
| 35 | + todoAnnotations: list[TodoAnnotation] = field(default_factory=list) |
17 | 36 | valueAnnotations: list[ValueAnnotation] = field(default_factory=list)
|
18 | 37 |
|
| 38 | + @staticmethod |
| 39 | + def from_json(json: Any) -> AnnotationStore: |
| 40 | + if json["schemaVersion"] == 1: |
| 41 | + raise Exception( |
| 42 | + "Incompatible Annotation File: This file is not compatible with the current version." |
| 43 | + ) |
| 44 | + |
| 45 | + boundaryAnnotations = [] |
| 46 | + for annotation in json["boundaryAnnotations"].values(): |
| 47 | + boundaryAnnotations.append(BoundaryAnnotation.from_json(annotation)) |
| 48 | + |
| 49 | + calledAfterAnnotations = [] |
| 50 | + for annotation in json["calledAfterAnnotations"].values(): |
| 51 | + calledAfterAnnotations.append(CalledAfterAnnotation.from_json(annotation)) |
| 52 | + |
| 53 | + completeAnnotations = [] |
| 54 | + for annotation in json["completeAnnotations"].values(): |
| 55 | + completeAnnotations.append(CompleteAnnotation.from_json(annotation)) |
| 56 | + |
| 57 | + descriptionAnnotations = [] |
| 58 | + for annotation in json["descriptionAnnotations"].values(): |
| 59 | + descriptionAnnotations.append(DescriptionAnnotation.from_json(annotation)) |
| 60 | + |
| 61 | + enumAnnotations = [] |
| 62 | + for annotation in json["enumAnnotations"].values(): |
| 63 | + enumAnnotations.append(EnumAnnotation.from_json(annotation)) |
| 64 | + |
| 65 | + groupAnnotations = [] |
| 66 | + for annotation in json["groupAnnotations"].values(): |
| 67 | + groupAnnotations.append(GroupAnnotation.from_json(annotation)) |
| 68 | + |
| 69 | + moveAnnotations = [] |
| 70 | + for annotation in json["moveAnnotations"].values(): |
| 71 | + moveAnnotations.append(MoveAnnotation.from_json(annotation)) |
| 72 | + |
| 73 | + pureAnnotations = [] |
| 74 | + for annotation in json["pureAnnotations"].values(): |
| 75 | + pureAnnotations.append(PureAnnotation.from_json(annotation)) |
| 76 | + |
| 77 | + removeAnnotations = [] |
| 78 | + for annotation in json["removeAnnotations"].values(): |
| 79 | + removeAnnotations.append(RemoveAnnotation.from_json(annotation)) |
| 80 | + |
| 81 | + renameAnnotations = [] |
| 82 | + for annotation in json["renameAnnotations"].values(): |
| 83 | + renameAnnotations.append(RenameAnnotation.from_json(annotation)) |
| 84 | + |
| 85 | + todoAnnotations = [] |
| 86 | + for annotation in json["todoAnnotations"].values(): |
| 87 | + todoAnnotations.append(TodoAnnotation.from_json(annotation)) |
| 88 | + |
| 89 | + valueAnnotations = [] |
| 90 | + for annotation in json["valueAnnotations"].values(): |
| 91 | + valueAnnotations.append(ValueAnnotation.from_json(annotation)) |
| 92 | + |
| 93 | + return AnnotationStore( |
| 94 | + boundaryAnnotations, |
| 95 | + calledAfterAnnotations, |
| 96 | + completeAnnotations, |
| 97 | + descriptionAnnotations, |
| 98 | + enumAnnotations, |
| 99 | + groupAnnotations, |
| 100 | + moveAnnotations, |
| 101 | + pureAnnotations, |
| 102 | + removeAnnotations, |
| 103 | + renameAnnotations, |
| 104 | + todoAnnotations, |
| 105 | + valueAnnotations, |
| 106 | + ) |
| 107 | + |
19 | 108 | def to_json(self) -> dict:
|
20 | 109 | return {
|
21 | 110 | "schemaVersion": ANNOTATION_SCHEMA_VERSION,
|
22 | 111 | "boundaryAnnotations": {
|
23 | 112 | annotation.target: annotation.to_json()
|
24 | 113 | for annotation in self.boundaryAnnotations
|
25 | 114 | },
|
| 115 | + "calledAfterAnnotations": { |
| 116 | + annotation.target: annotation.to_json() |
| 117 | + for annotation in self.calledAfterAnnotations |
| 118 | + }, |
| 119 | + "completeAnnotations": { |
| 120 | + annotation.target: annotation.to_json() |
| 121 | + for annotation in self.completeAnnotations |
| 122 | + }, |
| 123 | + "descriptionAnnotations": { |
| 124 | + annotation.target: annotation.to_json() |
| 125 | + for annotation in self.descriptionAnnotations |
| 126 | + }, |
26 | 127 | "enumAnnotations": {
|
27 | 128 | annotation.target: annotation.to_json()
|
28 | 129 | for annotation in self.enumAnnotations
|
29 | 130 | },
|
| 131 | + "groupAnnotations": { |
| 132 | + annotation.target: annotation.to_json() |
| 133 | + for annotation in self.groupAnnotations |
| 134 | + }, |
| 135 | + "moveAnnotations": { |
| 136 | + annotation.target: annotation.to_json() |
| 137 | + for annotation in self.moveAnnotations |
| 138 | + }, |
| 139 | + "pureAnnotations": { |
| 140 | + annotation.target: annotation.to_json() |
| 141 | + for annotation in self.pureAnnotations |
| 142 | + }, |
| 143 | + "renameAnnotations": { |
| 144 | + annotation.target: annotation.to_json() |
| 145 | + for annotation in self.renameAnnotations |
| 146 | + }, |
30 | 147 | "removeAnnotations": {
|
31 | 148 | annotation.target: annotation.to_json()
|
32 | 149 | for annotation in self.removeAnnotations
|
33 | 150 | },
|
| 151 | + "todoAnnotations": { |
| 152 | + annotation.target: annotation.to_json() |
| 153 | + for annotation in self.todoAnnotations |
| 154 | + }, |
34 | 155 | "valueAnnotations": {
|
35 | 156 | annotation.target: annotation.to_json()
|
36 | 157 | for annotation in self.valueAnnotations
|
|
0 commit comments