11"""
22A module containing the mechanics of the specutils io registry.
33"""
4+ import inspect
45import os
56import pathlib
67import sys
1617log = logging .getLogger (__name__ )
1718
1819
20+ def _astropy_has_priorities ():
21+ """
22+ Check if astropy has support for loader priorities
23+ """
24+ sig = inspect .signature (io_registry .register_reader )
25+ if sig .parameters .get ("priority" ) is not None :
26+ return True
27+ return False
28+
29+
1930def data_loader (label , identifier = None , dtype = Spectrum1D , extensions = None ,
2031 priority = 0 ):
2132 """
@@ -52,7 +63,10 @@ def wrapper(*args, **kwargs):
5263 return wrapper
5364
5465 def decorator (func ):
55- io_registry .register_reader (label , dtype , func )
66+ if _astropy_has_priorities ():
67+ io_registry .register_reader (label , dtype , func , priority = priority )
68+ else :
69+ io_registry .register_reader (label , dtype , func )
5670
5771 if identifier is None :
5872 # If the identifier is not defined, but the extensions are, create
@@ -78,17 +92,6 @@ def decorator(func):
7892 # Include the file extensions as attributes on the function object
7993 func .extensions = extensions
8094
81- # Include priority on the loader function attribute
82- func .priority = priority
83-
84- # Sort the io_registry based on priority
85- sorted_loaders = sorted (io_registry ._readers .items (),
86- key = lambda item : getattr (item [1 ], 'priority' , 0 ))
87-
88- # Update the registry with the sorted dictionary
89- io_registry ._readers .clear ()
90- io_registry ._readers .update (sorted_loaders )
91-
9295 log .debug ("Successfully loaded reader \" {}\" ." .format (label ))
9396
9497 # Automatically register a SpectrumList reader for any data_loader that
@@ -102,7 +105,14 @@ def load_spectrum_list(*args, **kwargs):
102105 load_spectrum_list .extensions = extensions
103106 load_spectrum_list .priority = priority
104107
105- io_registry .register_reader (label , SpectrumList , load_spectrum_list )
108+ if _astropy_has_priorities ():
109+ io_registry .register_reader (
110+ label , SpectrumList , load_spectrum_list , priority = priority ,
111+ )
112+ else :
113+ io_registry .register_reader (
114+ label , SpectrumList , load_spectrum_list ,
115+ )
106116 io_registry .register_identifier (label , SpectrumList , id_func )
107117 log .debug ("Created SpectrumList reader for \" {}\" ." .format (label ))
108118
@@ -113,9 +123,12 @@ def wrapper(*args, **kwargs):
113123 return decorator
114124
115125
116- def custom_writer (label , dtype = Spectrum1D ):
126+ def custom_writer (label , dtype = Spectrum1D , priority = 0 ):
117127 def decorator (func ):
118- io_registry .register_writer (label , Spectrum1D , func )
128+ if _astropy_has_priorities ():
129+ io_registry .register_writer (label , dtype , func , priority = priority )
130+ else :
131+ io_registry .register_writer (label , dtype , func )
119132
120133 @wraps (func )
121134 def wrapper (* args , ** kwargs ):
0 commit comments