1
1
"""
2
2
A module containing the mechanics of the specutils io registry.
3
3
"""
4
+ import inspect
4
5
import os
5
6
import pathlib
6
7
import sys
16
17
log = logging .getLogger (__name__ )
17
18
18
19
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
+
19
30
def data_loader (label , identifier = None , dtype = Spectrum1D , extensions = None ,
20
31
priority = 0 ):
21
32
"""
@@ -52,7 +63,10 @@ def wrapper(*args, **kwargs):
52
63
return wrapper
53
64
54
65
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 )
56
70
57
71
if identifier is None :
58
72
# If the identifier is not defined, but the extensions are, create
@@ -78,17 +92,6 @@ def decorator(func):
78
92
# Include the file extensions as attributes on the function object
79
93
func .extensions = extensions
80
94
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
-
92
95
log .debug ("Successfully loaded reader \" {}\" ." .format (label ))
93
96
94
97
# Automatically register a SpectrumList reader for any data_loader that
@@ -102,7 +105,14 @@ def load_spectrum_list(*args, **kwargs):
102
105
load_spectrum_list .extensions = extensions
103
106
load_spectrum_list .priority = priority
104
107
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
+ )
106
116
io_registry .register_identifier (label , SpectrumList , id_func )
107
117
log .debug ("Created SpectrumList reader for \" {}\" ." .format (label ))
108
118
@@ -113,9 +123,12 @@ def wrapper(*args, **kwargs):
113
123
return decorator
114
124
115
125
116
- def custom_writer (label , dtype = Spectrum1D ):
126
+ def custom_writer (label , dtype = Spectrum1D , priority = 0 ):
117
127
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 )
119
132
120
133
@wraps (func )
121
134
def wrapper (* args , ** kwargs ):
0 commit comments