1+ from .compound_file import CompoundFile
2+ from uuid import uuid4
3+ import operator
4+
5+ class Conversational :
6+ def __init__ (self , project , name ):
7+ self .parent = CompoundFile (
8+ project = project ,
9+ name = name ,
10+ directory_id = project .default_directory .id ,
11+ file_type = "compound/conversational"
12+ )
13+ self .project = project
14+ self .messgaes_meta = []
15+
16+ self .add_conversationa_attributes_if_doesnt_exist ()
17+
18+ def add_conversationa_attributes_if_doesnt_exist (self ):
19+ default_schema = self .project .schema .default_schema ()
20+ attribute_list = self .project .attribute .list (default_schema )
21+
22+ message_author_attribute = None
23+ message_time_attribute = None
24+ message_date_attribute = None
25+
26+ for attribute in attribute_list ['attribute_group_list' ]:
27+ if attribute ['name' ] == 'message_author' :
28+ message_author_attribute = attribute
29+ elif attribute ['name' ] == 'message_time' :
30+ message_time_attribute = attribute
31+ elif attribute ['name' ] == 'message_date' :
32+ message_date_attribute = attribute
33+
34+ if message_author_attribute is None :
35+ message_author_attribute = self .project .attribute .new (default_schema )
36+ self .project .attribute .update (
37+ message_author_attribute ,
38+ prompt = "Author" ,
39+ kind = "text" ,
40+ name = "message_author" ,
41+ is_global = True ,
42+ global_type = 'file' ,
43+ is_read_only = True
44+ )
45+
46+ if message_time_attribute is None :
47+ message_time_attribute = self .project .attribute .new (default_schema )
48+ self .project .attribute .update (
49+ message_time_attribute ,
50+ prompt = "Time" ,
51+ kind = "time" ,
52+ name = "message_time" ,
53+ is_global = True ,
54+ global_type = 'file' ,
55+ is_read_only = True
56+ )
57+
58+ if message_date_attribute is None :
59+ message_date_attribute = self .project .attribute .new (default_schema )
60+ self .project .attribute .update (
61+ message_date_attribute ,
62+ prompt = "Date" ,
63+ kind = "date" ,
64+ name = "message_date" ,
65+ is_global = True ,
66+ global_type = 'file' ,
67+ is_read_only = True
68+ )
69+
70+ self .author_attribute = message_author_attribute
71+ self .time_attribute = message_time_attribute
72+ self .date_attribute = message_date_attribute
73+
74+ def add_message (self , message_file , author = None , time = None , date = None ):
75+ message_meta = {
76+ "author" : author ,
77+ "time" : time ,
78+ "date" : date
79+ }
80+
81+ self .messgaes_meta .append (message_meta )
82+
83+ self .parent .add_child_from_local (path = message_file , ordinal = len (self .messgaes_meta ))
84+
85+ def _new_global_instance (self ):
86+ return {
87+ "creation_ref_id" : str (uuid4 ()),
88+ "type" : "global" ,
89+ "attribute_groups" : {}
90+ }
91+
92+
93+ def upload (self ):
94+ self .parent .upload ()
95+ child_files = self .parent .fetch_child_files ()
96+ child_files .sort (key = operator .attrgetter ('id' ))
97+
98+ for index in range (0 , len (child_files )):
99+ global_instance_for_child = self ._new_global_instance ()
100+
101+ if self .messgaes_meta [index ]["author" ] is not None :
102+ global_instance_for_child ["attribute_groups" ][self .author_attribute ["id" ]] = self .messgaes_meta [index ]["author" ]
103+ if self .messgaes_meta [index ]["time" ] is not None :
104+ global_instance_for_child ["attribute_groups" ][self .time_attribute ["id" ]] = self .messgaes_meta [index ]["time" ]
105+ if self .messgaes_meta [index ]["date" ] is not None :
106+ global_instance_for_child ["attribute_groups" ][self .date_attribute ["id" ]] = self .messgaes_meta [index ]["date" ]
107+
108+ payload = {
109+ "instance_list" : [global_instance_for_child ],
110+ "and_complete" : False ,
111+ "child_file_save_id" : child_files [index ].id
112+ }
113+
114+ response = self .project .session .post (url = self .project .host + f"/api/project/{ self .project .project_string_id } /file/{ child_files [index ].id } /annotation/update" , json = payload )
115+ self .project .handle_errors (response )
0 commit comments