This repository was archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotations.py
More file actions
96 lines (80 loc) · 3.04 KB
/
Copy pathannotations.py
File metadata and controls
96 lines (80 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import sys
import argparse
import json
import os
iccvenv = os.environ['ICCVENV']
idx = iccvenv.rfind('/')
sys.path.append(os.environ['ICCVENV'][:idx])
from icc import db, create_app
from icc.models.annotation import Annotation, Tag, Comment
from icc.models.content import Edition
from icc.models.user import User
TAG_DESCRIPTION = "This tag's description is still blank. Please expand it."
def get_annotations():
annotations = []
for annotation in Annotation.query.all():
exp = {
'edition': annotation.edition.id,
'fl': annotation.HEAD.first_line_num,
'll': annotation.HEAD.last_line_num,
'fc': annotation.HEAD.first_char_idx,
'lc': annotation.HEAD.last_char_idx,
'body': annotation.HEAD.body,
'tags': [(tag.tag, tag.locked) for tag in annotation.HEAD.tags],
'locked': annotation.locked,
'comments': [{
'id': comment.id,
'depth': comment.depth,
'weight': comment.weight,
'parent_id': comment.parent_id,
'body': comment.body
} for comment in annotation.comments.all()]
}
annotations.append(exp)
return annotations
def export(file_name):
annotations = get_annotations()
fout = open(file_name, 'wt')
json.dump(annotations, fout)
def recreate(annotation):
annotation['edition'] = Edition.query.get(annotation['edition'])
tags = []
for tag in annotation['tags']:
tag_obj = Tag.query.filter_by(tag=tag[0]).first()
if not tag_obj:
tag_obj = Tag(tag=tag[0], locked=tag[1], description=TAG_DESCRIPTION)
tags.append(tag_obj)
annotation['tags'] = tags
comments = annotation.pop('comments')
return annotation, comments
def _import(file_name):
fin = open(file_name, 'rt')
annotations = json.load(fin)
community = User.query.get(1)
for i, annotation_dict in enumerate(annotations):
annotation_dict, comments = recreate(annotation_dict)
annotation = Annotation(annotator=community, **annotation_dict)
for comment in comments:
annotation.comments.append(Comment(**comment, poster=community))
db.session.add(annotation)
if not i % 25:
print(f"{i} annotations added.")
db.session.commit()
def main(args):
app = create_app()
with app.app_context():
if args.output:
export(args.output)
elif args.input:
_import(args.input)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
"Export or import files from and to the icc database.")
parser.add_argument('-i', '--input', action='store', type=str,
help="The input json file to populate the database with "
"pre-formatted annotations.")
parser.add_argument('-o', '--output', action='store', type=str,
help="The output file to write the jsonified "
"annotations")
args = parser.parse_args()
main(args)