2121# SOFTWARE.
2222
2323"""TimeGraph classes file."""
24-
25- from tsp .entry import Entry
24+ import json
25+ from tsp .entry import Entry , EntryElementStyleEncoder
2626
2727TYPE_KEY = "type"
2828START_TIME_KEY = "start"
@@ -70,6 +70,8 @@ def __init__(self, params):
7070 self .has_row_model = params .get (HAS_ROW_MODEL_KEY )
7171 del params [HAS_ROW_MODEL_KEY ]
7272
73+ def __repr__ (self ) -> str :
74+ return 'TimeGraphEntry(start={}, end={})' .format (self .start_time , self .end_time )
7375
7476class TimeGraphModel :
7577 '''
@@ -83,6 +85,8 @@ def __init__(self, params):
8385 self .rows .append (TimeGraphRow (row ))
8486 del params [ROWS_KEY ]
8587
88+ def __repr__ (self ) -> str :
89+ return 'TimeGraphModel({})' .format (', ' .join (str (row ) for row in self .rows ))
8690
8791class TimeGraphRow :
8892 '''
@@ -104,6 +108,9 @@ def __init__(self, params):
104108 self .states .append (TimeGraphState (state ))
105109 del params [STATES_KEY ]
106110
111+ def __repr__ (self ) -> str :
112+ return 'TimeGraphRow({})' .format (', ' .join ([str (state ) for state in self .states ]))
113+
107114
108115class TimeGraphState :
109116 '''
@@ -143,6 +150,9 @@ def __init__(self, params):
143150 self .style = params .get (STYLE_KEY )
144151 del params [STYLE_KEY ]
145152
153+ def __repr__ (self ) -> str :
154+ return 'TimeGraphState({}start={}, end={})' .format (f'label={ self .label } , ' ,self .start_time , self .end_time )
155+
146156
147157class TimeGraphArrow :
148158 '''
@@ -186,3 +196,59 @@ def __init__(self, params):
186196 if STYLE_KEY in params :
187197 self .style = params .get (STYLE_KEY )
188198 del params [STYLE_KEY ]
199+
200+ class TimeGraphEntryEncoder (json .JSONEncoder ):
201+ def default (self , obj ):
202+ if isinstance (obj , TimeGraphEntry ):
203+ return {
204+ 'id' : obj .id ,
205+ 'parent_id' : obj .parent_id ,
206+ 'labels' : obj .labels ,
207+ 'style' : EntryElementStyleEncoder ().default (obj .style ) if obj .style else None ,
208+ 'start' : obj .start_time if obj .start_time else None ,
209+ 'end' : obj .end_time if obj .end_time else None ,
210+ 'hasData' : obj .has_row_model
211+ }
212+ return super ().default (obj )
213+
214+ class TimeGraphModelEncoder (json .JSONEncoder ):
215+ def default (self , obj ):
216+ if isinstance (obj , TimeGraphModel ):
217+ return {
218+ 'rows' : [TimeGraphRowEncoder ().default (row ) for row in obj .rows ]
219+ }
220+ return super ().default (obj )
221+
222+ class TimeGraphRowEncoder (json .JSONEncoder ):
223+ def default (self , obj ):
224+ if isinstance (obj , TimeGraphRow ):
225+ return {
226+ 'entry_id' : obj .entry_id ,
227+ 'states' : [TimeGraphStateEncoder ().default (state ) for state in obj .states ]
228+ }
229+ return super ().default (obj )
230+
231+ class TimeGraphStateEncoder (json .JSONEncoder ):
232+ def default (self , obj ):
233+ if isinstance (obj , TimeGraphState ):
234+ return {
235+ 'start' : obj .start_time ,
236+ 'end' : obj .end_time ,
237+ # 'duration': obj.duration,
238+ # 'values': obj.value,
239+ # 'tags': obj.tags,
240+ 'style' : obj .style
241+ }
242+ return super ().default (obj )
243+
244+ class TimeGraphArrowEncoder (json .JSONEncoder ):
245+ def default (self , obj ):
246+ if isinstance (obj , TimeGraphArrow ):
247+ return {
248+ 'sourceId' : obj .source_id ,
249+ 'targetId' : obj .target_id ,
250+ 'start' : obj .start ,
251+ 'end' : obj .end ,
252+ 'style' : obj .style
253+ }
254+ return super ().default (obj )
0 commit comments