11"""
22A module containing the mechanics of the specutils io registry.
33"""
4+ import inspect
45import os
56import logging
67import pathlib
1516__all__ = ['data_loader' , 'custom_writer' , 'get_loaders_by_extension' , 'identify_spectrum_format' ]
1617
1718
19+ def _astropy_has_priorities ():
20+ """
21+ Check if astropy has support for loader priorities
22+ """
23+ sig = inspect .signature (io_registry .register_reader )
24+ if sig .parameters .get ("priority" ) is not None :
25+ return True
26+ return False
27+
28+
1829def data_loader (label , identifier = None , dtype = Spectrum1D , extensions = None ,
1930 priority = 0 ):
2031 """
@@ -51,7 +62,10 @@ def wrapper(*args, **kwargs):
5162 return wrapper
5263
5364 def decorator (func ):
54- io_registry .register_reader (label , dtype , func )
65+ if _astropy_has_priorities ():
66+ io_registry .register_reader (label , dtype , func , priority = priority )
67+ else :
68+ io_registry .register_reader (label , dtype , func )
5569
5670 if identifier is None :
5771 # If the identifier is not defined, but the extensions are, create
@@ -77,17 +91,6 @@ def decorator(func):
7791 # Include the file extensions as attributes on the function object
7892 func .extensions = extensions
7993
80- # Include priority on the loader function attribute
81- func .priority = priority
82-
83- # Sort the io_registry based on priority
84- sorted_loaders = sorted (io_registry ._readers .items (),
85- key = lambda item : getattr (item [1 ], 'priority' , 0 ))
86-
87- # Update the registry with the sorted dictionary
88- io_registry ._readers .clear ()
89- io_registry ._readers .update (sorted_loaders )
90-
9194 logging .debug ("Successfully loaded reader \" {}\" ." .format (label ))
9295
9396 # Automatically register a SpectrumList reader for any data_loader that
@@ -101,7 +104,14 @@ def load_spectrum_list(*args, **kwargs):
101104 load_spectrum_list .extensions = extensions
102105 load_spectrum_list .priority = priority
103106
104- io_registry .register_reader (label , SpectrumList , load_spectrum_list )
107+ if _astropy_has_priorities ():
108+ io_registry .register_reader (
109+ label , SpectrumList , load_spectrum_list , priority = priority ,
110+ )
111+ else :
112+ io_registry .register_reader (
113+ label , SpectrumList , load_spectrum_list ,
114+ )
105115 io_registry .register_identifier (label , SpectrumList , id_func )
106116 logging .debug ("Created SpectrumList reader for \" {}\" ." .format (label ))
107117
@@ -112,9 +122,14 @@ def wrapper(*args, **kwargs):
112122 return decorator
113123
114124
115- def custom_writer (label , dtype = Spectrum1D ):
125+ def custom_writer (label , dtype = Spectrum1D , priority = 0 ):
116126 def decorator (func ):
117- io_registry .register_writer (label , Spectrum1D , func )
127+ if _astropy_has_priorities ():
128+ io_registry .register_writer (
129+ label , Spectrum1D , func , priority = priority ,
130+ )
131+ else :
132+ io_registry .register_writer (label , Spectrum1D , func )
118133
119134 @wraps (func )
120135 def wrapper (* args , ** kwargs ):
0 commit comments