3131import sys
3232import os
3333import posixpath
34- import time
3534import gc
36- import re
37- from operator import itemgetter
3835import logging
3936_logger = logging .getLogger (__name__ )
4037
@@ -67,6 +64,8 @@ def h5open(filename):
6764 return h5py .File (filename , "r" , libver = 'latest' , swmr = True )
6865
6966from PyMca5 .PyMcaGui import PyMcaQt as qt
67+ from PyMca5 .PyMcaIO .HDF5Utils import sort_h5items
68+
7069safe_str = qt .safe_str
7170
7271if hasattr (qt , 'QStringList' ):
@@ -78,95 +77,6 @@ def MyQVariant(x=None):
7877QVERSION = qt .qVersion ()
7978
8079
81- #sorting method
82- def h5py_sorting (object_list , sorting_list = None ):
83- if sorting_list is None :
84- sorting_list = ['start_time' , 'end_time' , 'name' ]
85- n = len (object_list )
86- if n < 2 :
87- return object_list
88-
89- # we have received items, not values
90- # perform a first sort based on received names
91- # this solves a problem with Eiger data where all the
92- # external data have the same posixName. Without this sorting
93- # they arrive "unsorted"
94- object_list .sort ()
95- try :
96- posixNames = [item [1 ].name for item in object_list ]
97- except AttributeError :
98- # Typical of broken external links
99- _logger .debug ("HDF5Widget: Cannot get posixNames" )
100- return object_list
101-
102- # This implementation only sorts entries
103- if posixpath .dirname (posixNames [0 ]) != "/" :
104- return object_list
105-
106- sorting_key = None
107- if hasattr (object_list [0 ][1 ], "items" ):
108- for key in sorting_list :
109- if key in [x [0 ] for x in object_list [0 ][1 ].items ()]:
110- sorting_key = key
111- break
112-
113- if sorting_key is None :
114- if 'name' in sorting_list :
115- sorting_key = 'name'
116- else :
117- return object_list
118-
119- try :
120- if sorting_key == 'title' :
121- def getTitle (x ):
122- try :
123- title = x ["title" ][()]
124- except Exception :
125- # allow the title to be missing
126- title = ""
127- if hasattr (title , "dtype" ):
128- if hasattr (title , "__len__" ):
129- if len (title ) == 1 :
130- title = title [0 ]
131- if hasattr (title , "decode" ):
132- title = title .decode ("utf-8" )
133- return title
134- # sort first by the traditional keys in order to be sorted
135- # by title and respecting actquisition order for equal title
136- try :
137- ordered_list = h5py_sorting (object_list )
138- except Exception :
139- ordered_list = object_list
140- sorting_list = [(getTitle (o [1 ]), o ) for o in ordered_list ]
141- sorted_list = sorted (sorting_list , key = itemgetter (0 ))
142- return [x [1 ] for x in sorted_list ]
143-
144- if sorting_key != 'name' :
145- sorting_list = [(o [1 ][sorting_key ][()], o )
146- for o in object_list ]
147- sorted_list = sorted (sorting_list , key = itemgetter (0 ))
148- return [x [1 ] for x in sorted_list ]
149-
150- if sorting_key == 'name' :
151- sorting_list = [(_get_number_list (o [1 ].name ),o )
152- for o in object_list ]
153- sorting_list .sort ()
154- return [x [1 ] for x in sorting_list ]
155- except Exception :
156- #The only way to reach this point is to have different
157- #structures among the different entries. In that case
158- #defaults to the unfiltered case
159- _logger .warning ("WARNING: Default ordering" )
160- _logger .warning ("Probably all entries do not have the key %s" % sorting_key )
161- return object_list
162-
163-
164- def _get_number_list (txt ):
165- rexpr = '[/a-zA-Z:_-]'
166- nbs = [float (w ) for w in re .split (rexpr , txt ) if w not in ['' ,' ' ]]
167- return nbs
168-
169-
17080class BrokenLink (object ):
17181 pass
17282
@@ -275,7 +185,7 @@ def children(self):
275185 items = list (self .getNode (self .name ).items ())
276186 try :
277187 # better handling of external links
278- finalList = h5py_sorting (items , sorting_list = self .__sorting_list )
188+ finalList = sort_h5items (items , sorting_list = self .__sorting_list )
279189 for i in range (len (finalList )):
280190 # avoid an error at silx level with the linechecking "if finalList[i][1] and "
281191 finalListIsTrue = True
0 commit comments