1
1
""" Gene Expression Omnibus datasets widget """
2
+
2
3
import sys
3
4
from types import SimpleNamespace
4
- from typing import Any , Optional , DefaultDict
5
+ from typing import Any , Union , Optional , DefaultDict
5
6
from collections import defaultdict
6
7
7
8
import requests
8
9
9
- from AnyQt .QtCore import Qt
10
+ from AnyQt .QtCore import Qt , QTimer
10
11
from AnyQt .QtWidgets import (
11
12
QSplitter ,
12
13
QTreeWidget ,
@@ -63,6 +64,8 @@ def callback():
63
64
nonlocal current_iter
64
65
current_iter += 1
65
66
state .set_progress_value (100 * (current_iter / max_iter ))
67
+ if state .is_interruption_requested ():
68
+ raise KeyboardInterrupt
66
69
67
70
state .set_status ("Downloading..." )
68
71
res .gds_dataset = dataset_download (
@@ -71,6 +74,16 @@ def callback():
71
74
return res
72
75
73
76
77
+ class GDSInfoResult (SimpleNamespace ):
78
+ gds_info : Optional [GDSInfo ]
79
+
80
+
81
+ def run_gds_info_task (state : TaskState ):
82
+ state .set_status ("Fetching index..." )
83
+ gds_info = GDSInfo ()
84
+ return GDSInfoResult (gds_info = gds_info )
85
+
86
+
74
87
class GEODatasetsModel (TableModel ):
75
88
(
76
89
indicator_col ,
@@ -108,9 +121,9 @@ def render_pubmed_url(row):
108
121
TableModel .Column (
109
122
title ("" ),
110
123
{
111
- Qt .DisplayRole : lambda row : " "
112
- if is_cached (items [row ]["name" ])
113
- else "" ,
124
+ Qt .DisplayRole : lambda row : (
125
+ " " if is_cached (items [row ]["name" ]) else ""
126
+ ) ,
114
127
Qt .UserRole : lambda row : items [row ],
115
128
},
116
129
),
@@ -211,6 +224,7 @@ class Warning(OWWidget.Warning):
211
224
212
225
class Error (OWWidget .Error ):
213
226
no_connection = Msg ("Widget can't connect to serverfiles." )
227
+ error = Msg ("{}" )
214
228
215
229
class Outputs :
216
230
gds_data = Output ("Expression Data" , Table )
@@ -232,14 +246,7 @@ class Outputs:
232
246
def __init__ (self ):
233
247
OWWidget .__init__ (self )
234
248
ConcurrentWidgetMixin .__init__ (self )
235
-
236
- try :
237
- self .gds_info : Optional [GDSInfo ] = GDSInfo ()
238
- except requests .exceptions .ConnectionError :
239
- self .gds_info = {}
240
- self .Error .no_connection ()
241
- return
242
-
249
+ self .gds_info : Optional [GDSInfo ] = None
243
250
self .gds_data : Optional [Table ] = None
244
251
self .__updating_filter = False
245
252
@@ -291,7 +298,7 @@ def __init__(self):
291
298
self .table_view .verticalHeader ().setVisible (False )
292
299
self .table_view .viewport ().setMouseTracking (True )
293
300
294
- self .table_model = GEODatasetsModel (self . gds_info )
301
+ self .table_model = GEODatasetsModel ({} )
295
302
self .proxy_model = FilterProxyModel ()
296
303
self .proxy_model .setSourceModel (self .table_model )
297
304
self .table_view .setModel (self .proxy_model )
@@ -343,7 +350,7 @@ def __init__(self):
343
350
)
344
351
self ._apply_filter ()
345
352
346
- self .commit ( )
353
+ self .start ( run_gds_info_task )
347
354
348
355
def _splitter_moved (self , * args ):
349
356
self .splitter_settings = [bytes (sp .saveState ()) for sp in self .splitters ]
@@ -452,7 +459,7 @@ def current_changed():
452
459
453
460
def _run (self ):
454
461
self .Warning .using_local_files .clear ()
455
- if self .selected_gds is not None :
462
+ if self .gds_info and self . selected_gds is not None :
456
463
self .gds_data = None
457
464
self .start (
458
465
run_download_task ,
@@ -541,16 +548,28 @@ def commit(self):
541
548
self ._run ()
542
549
543
550
def on_exception (self , ex : Exception ):
544
- self .Warning .using_local_files ()
545
-
546
- def on_done (self , result : Result ):
547
- assert isinstance (result .gds_dataset , Table )
548
- self .gds_data = result .gds_dataset
549
-
550
- if self .gds_info :
551
- self .table_model .update_cache_indicator ()
552
-
553
- self .Outputs .gds_data .send (self .gds_data )
551
+ if isinstance (ex , requests .exceptions .ConnectionError ):
552
+ self .Warning .using_local_files ()
553
+ else :
554
+ self .Error .error ("" , exc_info = ex )
555
+
556
+ def on_done (self , result : Union [GDSInfoResult , Result ]):
557
+ self .Error .error .clear ()
558
+ if isinstance (result , GDSInfoResult ):
559
+ self .gds_info = result .gds_info
560
+ self .table_model = GEODatasetsModel (result .gds_info )
561
+ self .proxy_model .setSourceModel (self .table_model )
562
+ self .table_view .resizeColumnsToContents ()
563
+ self ._set_selection ()
564
+ QTimer .singleShot (0 , lambda : self .unconditional_commit ())
565
+ else :
566
+ assert isinstance (result .gds_dataset , Table )
567
+ self .gds_data = result .gds_dataset
568
+
569
+ if self .gds_info :
570
+ self .table_model .update_cache_indicator ()
571
+
572
+ self .Outputs .gds_data .send (self .gds_data )
554
573
555
574
def on_partial_result (self , result : Any ) -> None :
556
575
pass
0 commit comments