1919
2020__doctest_skip__ = ['register_identifier' ]
2121
22-
22+ _builtin_registered = False
2323_readers = OrderedDict ()
2424_writers = OrderedDict ()
2525_identifiers = OrderedDict ()
2626
27+ # Add built-in readers/writers to the registry manually, and refer to the
28+ # functions by strings to avoid importing those modules.
29+ def _register_builtins ():
30+
31+ global _builtin_registered
32+
33+ if not _builtin_registered :
34+
35+ from ..table import Table
36+
37+ _builtin_registered = True
38+
39+ # FITS
40+ register_reader ("fits" , Table , 'astropy.io.fits.connect.read_table_fits' )
41+ register_writer ("fits" , Table , 'astropy.io.fits.connect.write_table_fits' )
42+ register_identifier ("fits" , Table , 'astropy.io.fits.connect.is_fits' )
43+
44+ # VOTABLE
45+ register_reader ('votable' , Table , 'astropy.io.votable.connect.read_table_votable' )
46+ register_writer ('votable' , Table , 'astropy.io.votable.connect.write_table_votable' )
47+ register_identifier ('votable' , Table , 'astropy.io.votable.connect.is_votable' )
48+
49+ # HDF5
50+ register_reader ('hdf5' , Table , 'astropy.io.misc.hdf5.read_table_hdf5' )
51+ register_writer ('hdf5' , Table , 'astropy.io.misc.hdf5.write_table_hdf5' )
52+ register_identifier ('hdf5' , Table , 'astropy.io.misc.hdf5.is_hdf5' )
53+
54+ # JSViewer
55+ register_writer ('jsviewer' , Table , 'astropy.table.jsviewer.write_table_jsviewer' )
56+
57+ # ASCII
58+ register_reader ('ascii' , Table , 'astropy.io.ascii.connect.read_asciitable' )
59+ register_writer ('ascii' , Table , 'astropy.io.ascii.connect.write_asciitable' )
60+
61+ # Remaining ASCII formats - for now can only import io.ascii to force
62+ # meta-classes to register.
63+ from . import ascii
64+
65+ def _get_function_by_string (path_to_function ):
66+ import importlib
67+ module , function = path_to_function .rsplit ('.' , 1 )
68+ return getattr (importlib .import_module (module ), function )
69+
2770
2871def get_formats (data_class = None ):
2972 """
@@ -39,6 +82,9 @@ def get_formats(data_class=None):
3982 format_table: Table
4083 Table of available I/O formats
4184 """
85+
86+ _register_builtins ()
87+
4288 from ..table import Table
4389 format_classes = sorted (set (_readers ) | set (_writers ),
4490 key = lambda tup : tup [0 ])
@@ -242,11 +288,16 @@ def register_identifier(data_format, data_class, identifier, force=False):
242288
243289
244290def identify_format (origin , data_class_required , path , fileobj , args , kwargs ):
291+ _register_builtins ()
245292 # Loop through identifiers to see which formats match
246293 valid_formats = []
247294 for data_format , data_class in _identifiers :
248295 if _is_best_match (data_class_required , data_class , _identifiers ):
249- if _identifiers [(data_format , data_class )](
296+ identifier = _identifiers [(data_format , data_class )]
297+ if isinstance (identifier , six .string_types ):
298+ identifier = _get_function_by_string (identifier )
299+ _identifiers [(data_format , data_class )] = identifier
300+ if identifier (
250301 origin , path , fileobj , * args , ** kwargs ):
251302 valid_formats .append (data_format )
252303
@@ -264,11 +315,16 @@ def _get_format_table_str(data_class, readwrite):
264315
265316
266317def get_reader (data_format , data_class ):
318+ _register_builtins ()
267319 # Get all the readers that work for `data_format`
268320 readers = [(fmt , cls ) for fmt , cls in _readers if fmt == data_format ]
269321 for reader_format , reader_class in readers :
270322 if _is_best_match (data_class , reader_class , readers ):
271- return _readers [(reader_format , reader_class )]
323+ reader = _readers [(reader_format , reader_class )]
324+ if isinstance (reader , six .string_types ):
325+ reader = _get_function_by_string (reader )
326+ _readers [(reader_format , reader_class )] = reader
327+ return reader
272328 else :
273329 format_table_str = _get_format_table_str (data_class , 'Read' )
274330 raise Exception ("No reader defined for format '{0}' and class '{1}'.\n "
@@ -278,10 +334,15 @@ def get_reader(data_format, data_class):
278334
279335
280336def get_writer (data_format , data_class ):
337+ _register_builtins ()
281338 writers = [(fmt , cls ) for fmt , cls in _writers if fmt == data_format ]
282339 for writer_format , writer_class in writers :
283340 if _is_best_match (data_class , writer_class , writers ):
284- return _writers [(writer_format , writer_class )]
341+ writer = _writers [(writer_format , writer_class )]
342+ if isinstance (writer , six .string_types ):
343+ writer = _get_function_by_string (writer )
344+ _writers [(writer_format , writer_class )] = writer
345+ return writer
285346 else :
286347 format_table_str = _get_format_table_str (data_class , 'Write' )
287348 raise Exception ("No writer defined for format '{0}' and class '{1}'.\n "
0 commit comments