66
77import numpy as np
88
9- from .base import IORegistryError , UnifiedIORegistryBase
9+ from .base import IORegistryError , _UnifiedIORegistryBase
1010
1111__all__ = ['UnifiedIORegistry' , 'UnifiedInputRegistry' , 'UnifiedOutputRegistry' ]
1212
1616
1717# -----------------------------------------------------------------------------
1818
19- class UnifiedInputRegistry (UnifiedIORegistryBase ):
20- """Read-only Registry."""
19+ class UnifiedInputRegistry (_UnifiedIORegistryBase ):
20+ """Read-only Unified Registry.
21+
22+ .. versionadded:: 5.0
23+
24+ Examples
25+ --------
26+ First let's start by creating a read-only registry.
27+
28+ .. code-block:: python
29+
30+ >>> from astropy.io.registry import UnifiedInputRegistry
31+ >>> read_reg = UnifiedInputRegistry()
32+
33+ There is nothing in this registry. Let's make a reader for the
34+ :class:`~astropy.table.Table` class::
35+
36+ from astropy.table import Table
37+
38+ def my_table_reader(filename, some_option=1):
39+ # Read in the table by any means necessary
40+ return table # should be an instance of Table
41+
42+ Such a function can then be registered with the I/O registry::
43+
44+ read_reg.register_reader('my-table-format', Table, my_table_reader)
45+
46+ Note that we CANNOT then read in a table with::
47+
48+ d = Table.read('my_table_file.mtf', format='my-table-format')
49+
50+ Why? because ``Table.read`` uses Astropy's default global registry and this
51+ is a separate registry.
52+ Instead we can read by the read method on the registry::
53+
54+ d = read_reg.read(Table, 'my_table_file.mtf', format='my-table-format')
55+
56+ """
2157
2258 def __init__ (self ):
2359 super ().__init__ () # set _identifiers
2460 self ._readers = OrderedDict ()
2561 self ._registries ["read" ] = dict (attr = "_readers" , column = "Read" )
2662 self ._registries_order = ("read" , "identify" )
2763
28- def get_formats (self , data_class = None , * args ):
29- return super ().get_formats (data_class , filter_on = "Read" )
64+ def get_formats (self , data_class = None , filter_on = "Read" ):
65+ """
66+ Get the list of registered formats as a Table.
67+
68+ Parameters
69+ ----------
70+ data_class : class or None, optional
71+ Filter readers/writer to match data class (default = all classes).
72+ filter_on : str or None, optional
73+ Which registry to show. E.g. "identify"
74+ If None search for both. Default is "Read".
75+
76+ Returns
77+ -------
78+ format_table : :class:`~astropy.table.Table`
79+ Table of available I/O formats.
80+ """
81+ return super ().get_formats (data_class , filter_on )
3082
3183 # =========================================================================
3284 # Read methods
@@ -116,9 +168,22 @@ def read(self, cls, *args, format=None, cache=False, **kwargs):
116168 """
117169 Read in data.
118170
119- The arguments passed to this method depend on the format.
120- """
171+ Parameters
172+ ----------
173+ cls : class
174+ *args
175+ The arguments passed to this method depend on the format.
176+ format : str or None
177+ cache : bool
178+ Whether to cache the results of reading in the data.
179+ **kwargs
180+ The arguments passed to this method depend on the format.
121181
182+ Returns
183+ -------
184+ object or None
185+ The output of the registered reader.
186+ """
122187 ctx = None
123188 try :
124189 if format is None :
@@ -170,17 +235,20 @@ def read(self, cls, *args, format=None, cache=False, **kwargs):
170235
171236# -----------------------------------------------------------------------------
172237
173- class UnifiedOutputRegistry (UnifiedIORegistryBase ):
174- """Write-only Registry."""
238+ class UnifiedOutputRegistry (_UnifiedIORegistryBase ):
239+ """Write-only Registry.
240+
241+ .. versionadded:: 5.0
242+ """
175243
176244 def __init__ (self ):
177245 super ().__init__ ()
178246 self ._writers = OrderedDict ()
179247 self ._registries ["write" ] = dict (attr = "_writers" , column = "Write" )
180248 self ._registries_order = ("write" , "identify" , )
181249
182- def get_formats (self , data_class = None , * args ):
183- return super ().get_formats (data_class , filter_on = "Write" )
250+ def get_formats (self , data_class = None , filter_on = "Write" ):
251+ return super ().get_formats (data_class , filter_on )
184252
185253 # =========================================================================
186254 # Write Methods
@@ -269,7 +337,22 @@ def write(self, data, *args, format=None, **kwargs):
269337 """
270338 Write out data.
271339
272- The arguments passed to this method depend on the format.
340+ Parameters
341+ ----------
342+ data : object
343+ The data to write.
344+ *args
345+ The arguments passed to this method depend on the format.
346+ format : str or None
347+ **kwargs
348+ The arguments passed to this method depend on the format.
349+
350+ Returns
351+ -------
352+ object or None
353+ The output of the registered writer. Most often `None`.
354+
355+ .. versionadded:: 4.3
273356 """
274357
275358 if format is None :
@@ -296,15 +379,18 @@ def write(self, data, *args, format=None, **kwargs):
296379# -----------------------------------------------------------------------------
297380
298381class UnifiedIORegistry (UnifiedInputRegistry , UnifiedOutputRegistry ):
299- """Unified I/O Registry"""
382+ """Unified I/O Registry.
383+
384+ .. versionadded:: 5.0
385+ """
300386
301387 def __init__ (self ):
302388 super ().__init__ ()
303389 self ._registries_order = ("read" , "write" , "identify" )
304390
305391 def get_formats (self , data_class = None , readwrite = None ):
306392 """
307- Get the list of registered I/O formats as a Table.
393+ Get the list of registered I/O formats as a `~astropy.table. Table` .
308394
309395 Parameters
310396 ----------
@@ -322,4 +408,4 @@ def get_formats(self, data_class=None, readwrite=None):
322408 format_table : :class:`~astropy.table.Table`
323409 Table of available I/O formats.
324410 """
325- return UnifiedIORegistryBase .get_formats (self , data_class , filter_on = readwrite )
411+ return super () .get_formats (data_class , readwrite )
0 commit comments