Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 6c30002

Browse files
feat: import of annotations during migrate (#1115)
Closes #1110. ### Summary of Changes - Add missing annotation classes - expand the AnnotationStore for the new annotation classes - import api and annotations druing migrate ### Testing Instructions run `parse-package migrate -a1 data/api/sklearn__api.json -a2 data/api/sklearn__apiv2.json -a data/annotations/annotations.json -o out` and view return code (or inspect the objects in debug mode) or run pytest Co-authored-by: Lars Reimann <[email protected]>
1 parent b79634d commit 6c30002

File tree

5 files changed

+865
-144
lines changed

5 files changed

+865
-144
lines changed
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
from pathlib import Path
33

4+
from package_parser.processing.annotations.model import AnnotationStore
45
from package_parser.processing.api.model import API
56

67

@@ -10,11 +11,21 @@ def _run_migrate_command(
1011
apiv2_file_path: Path,
1112
out_dir_path: Path,
1213
) -> None:
13-
pass
14+
# pylint: disable=unused-argument
15+
_read_api_file(apiv1_file_path)
16+
_read_api_file(apiv2_file_path)
17+
_read_annotations_file(annotations_file_path)
18+
19+
20+
def _read_annotations_file(annotations_file_path: Path) -> AnnotationStore:
21+
with open(annotations_file_path, encoding="utf-8") as annotations_file:
22+
annotations_json = json.load(annotations_file)
23+
24+
return AnnotationStore.from_json(annotations_json)
1425

1526

1627
def _read_api_file(api_file_path: Path) -> API:
17-
with open(api_file_path) as api_file:
28+
with open(api_file_path, encoding="utf-8") as api_file:
1829
api_json = json.load(api_file)
1930

2031
return API.from_json(api_json)

package-parser/package_parser/processing/annotations/model/_AnnotationStore.py

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,157 @@
1+
from __future__ import annotations
2+
13
from dataclasses import dataclass, field
4+
from typing import Any
25

3-
from package_parser.processing.annotations.model import (
6+
from ._annotations import (
47
ANNOTATION_SCHEMA_VERSION,
58
BoundaryAnnotation,
9+
CalledAfterAnnotation,
10+
CompleteAnnotation,
11+
DescriptionAnnotation,
612
EnumAnnotation,
13+
GroupAnnotation,
14+
MoveAnnotation,
15+
PureAnnotation,
716
RemoveAnnotation,
17+
RenameAnnotation,
18+
TodoAnnotation,
819
ValueAnnotation,
920
)
1021

1122

1223
@dataclass
1324
class AnnotationStore:
1425
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)
1529
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)
1633
removeAnnotations: list[RemoveAnnotation] = field(default_factory=list)
34+
renameAnnotations: list[RenameAnnotation] = field(default_factory=list)
35+
todoAnnotations: list[TodoAnnotation] = field(default_factory=list)
1736
valueAnnotations: list[ValueAnnotation] = field(default_factory=list)
1837

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+
19108
def to_json(self) -> dict:
20109
return {
21110
"schemaVersion": ANNOTATION_SCHEMA_VERSION,
22111
"boundaryAnnotations": {
23112
annotation.target: annotation.to_json()
24113
for annotation in self.boundaryAnnotations
25114
},
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+
},
26127
"enumAnnotations": {
27128
annotation.target: annotation.to_json()
28129
for annotation in self.enumAnnotations
29130
},
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+
},
30147
"removeAnnotations": {
31148
annotation.target: annotation.to_json()
32149
for annotation in self.removeAnnotations
33150
},
151+
"todoAnnotations": {
152+
annotation.target: annotation.to_json()
153+
for annotation in self.todoAnnotations
154+
},
34155
"valueAnnotations": {
35156
annotation.target: annotation.to_json()
36157
for annotation in self.valueAnnotations

package-parser/package_parser/processing/annotations/model/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@
22
ANNOTATION_SCHEMA_VERSION,
33
AbstractAnnotation,
44
BoundaryAnnotation,
5+
CalledAfterAnnotation,
6+
CompleteAnnotation,
57
ConstantAnnotation,
8+
DescriptionAnnotation,
69
EnumAnnotation,
710
EnumPair,
11+
ExpertAnnotation,
12+
GroupAnnotation,
813
Interval,
14+
MoveAnnotation,
915
OmittedAnnotation,
1016
OptionalAnnotation,
1117
ParameterInfo,
1218
ParameterType,
19+
PureAnnotation,
1320
RemoveAnnotation,
21+
RenameAnnotation,
1422
RequiredAnnotation,
23+
TodoAnnotation,
1524
ValueAnnotation,
1625
)
1726
from ._AnnotationStore import AnnotationStore

0 commit comments

Comments
 (0)