Skip to content

Commit e903e02

Browse files
committed
ENH: allow registering several suffixes for path adapters in a single call
1 parent 01f3779 commit e903e02

File tree

1 file changed

+51
-54
lines changed

1 file changed

+51
-54
lines changed

larray_editor/arrayadapter.py

Lines changed: 51 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -142,35 +142,38 @@ def decorate_callable(adapter_creator):
142142
PATH_SUFFIX_ADAPTERS = {}
143143

144144

145-
def register_path_adapter(suffix, adapter_creator, required_module=None):
145+
def register_path_adapter(suffixes, adapter_creator, required_module=None):
146146
"""Register an adapter to display a file type (extension)
147147
148148
Parameters
149149
----------
150-
suffix : str
151-
File type for which the adapter should be used.
150+
suffixes : str | list[str]
151+
File extension(s) for which the adapter should be used.
152152
adapter_creator : callable
153153
Callable which will return an Adapter instance.
154154
required_module : str
155155
Name of module required to handle this file type.
156156
"""
157-
if suffix in PATH_SUFFIX_ADAPTERS:
158-
logger.warning(f"Replacing path adapter for {suffix}")
159-
PATH_SUFFIX_ADAPTERS[suffix] = (adapter_creator, required_module)
157+
if isinstance(suffixes, str):
158+
suffixes = [suffixes]
159+
for suffix in suffixes:
160+
if suffix in PATH_SUFFIX_ADAPTERS:
161+
logger.warning(f"Replacing path adapter for {suffix}")
162+
PATH_SUFFIX_ADAPTERS[suffix] = (adapter_creator, required_module)
160163

161164

162-
def path_adapter_for(suffix, required_module=None):
165+
def path_adapter_for(suffixes, required_module=None):
163166
"""Class/function decorator to register new file-type adapters
164167
165168
Parameters
166169
----------
167-
suffix : str
168-
File type associated with adapter class.
169-
required_module : str
170+
suffixes : str | list[str]
171+
File extension(s) associated with adapter class.
172+
required_module : str, optional
170173
Name of module required to handle this file type.
171174
"""
172175
def decorate_callable(adapter_creator):
173-
register_path_adapter(suffix, adapter_creator, required_module)
176+
register_path_adapter(suffixes, adapter_creator, required_module)
174177
return adapter_creator
175178
return decorate_callable
176179

@@ -1674,8 +1677,7 @@ def cell_activated(self, row_idx, column_idx):
16741677
return self.data[row_idx]
16751678

16761679

1677-
@path_adapter_for('.xlsx', 'xlwings')
1678-
@path_adapter_for('.xls', 'xlwings')
1680+
@path_adapter_for(('.xlsx', '.xls'), 'xlwings')
16791681
class XlsxPathAdapter(WorkbookAdapter):
16801682
@classmethod
16811683
def open(cls, fpath):
@@ -1972,9 +1974,7 @@ def get_values(self, h_start, v_start, h_stop, v_stop):
19721974
# Contrary to other Path adapters, this one is both a File *and* Path adapter
19731975
# because it is more efficient to NOT keep the file open (because the pyarrow
19741976
# API only allows limiting which columns are read when opening the file)
1975-
@path_adapter_for('.feather', 'pyarrow.ipc')
1976-
@path_adapter_for('.ipc', 'pyarrow.ipc')
1977-
@path_adapter_for('.arrow', 'pyarrow.ipc')
1977+
@path_adapter_for(('.feather', '.ipc', '.arrow'), 'pyarrow.ipc')
19781978
@adapter_for('pyarrow.RecordBatchFileReader')
19791979
class FeatherFileAdapter(AbstractColumnarAdapter):
19801980
def __init__(self, data, attributes):
@@ -2646,8 +2646,7 @@ def get_values(self, h_start, v_start, h_stop, v_stop):
26462646
return np.array(res, dtype=object)
26472647

26482648

2649-
@path_adapter_for('.av', 'iode')
2650-
@path_adapter_for('.var', 'iode')
2649+
@path_adapter_for(('.av', '.var'), 'iode')
26512650
class IodeVariablesPathAdapter(IodeVariablesAdapter):
26522651
@classmethod
26532652
def open(cls, fpath):
@@ -2656,8 +2655,7 @@ def open(cls, fpath):
26562655
return iode.variables
26572656

26582657

2659-
@path_adapter_for('.as', 'iode')
2660-
@path_adapter_for('.scl', 'iode')
2658+
@path_adapter_for(('.as', 'scl'), 'iode')
26612659
class IodeScalarsPathAdapter(IodeScalarsAdapter):
26622660
@classmethod
26632661
def open(cls, fpath):
@@ -2666,8 +2664,7 @@ def open(cls, fpath):
26662664
return iode.scalars
26672665

26682666

2669-
@path_adapter_for('.ac', 'iode')
2670-
@path_adapter_for('.cmt', 'iode')
2667+
@path_adapter_for(('.ac', '.cmt'), 'iode')
26712668
class IodeCommentsPathAdapter(IodeCommentsAdapter):
26722669
@classmethod
26732670
def open(cls, fpath):
@@ -2676,8 +2673,7 @@ def open(cls, fpath):
26762673
return iode.comments
26772674

26782675

2679-
@path_adapter_for('.at', 'iode')
2680-
@path_adapter_for('.tbl', 'iode')
2676+
@path_adapter_for(('.at', '.tbl'), 'iode')
26812677
class IodeTablesPathAdapter(IodeTablesAdapter):
26822678
@classmethod
26832679
def open(cls, fpath):
@@ -2686,8 +2682,7 @@ def open(cls, fpath):
26862682
return iode.tables
26872683

26882684

2689-
@path_adapter_for('.ae', 'iode')
2690-
@path_adapter_for('.eqs', 'iode')
2685+
@path_adapter_for(('.ae', '.eqs'), 'iode')
26912686
class IodeEquationsPathAdapter(IodeEquationsAdapter):
26922687
@classmethod
26932688
def open(cls, fpath):
@@ -2696,8 +2691,7 @@ def open(cls, fpath):
26962691
return iode.equations
26972692

26982693

2699-
@path_adapter_for('.ai', 'iode')
2700-
@path_adapter_for('.idt', 'iode')
2694+
@path_adapter_for(('.ai', '.idt'), 'iode')
27012695
class IodeIdentitiesPathAdapter(IodeIdentitiesAdapter):
27022696
@classmethod
27032697
def open(cls, fpath):
@@ -2706,8 +2700,7 @@ def open(cls, fpath):
27062700
return iode.identities
27072701

27082702

2709-
@path_adapter_for('.al', 'iode')
2710-
@path_adapter_for('.lst', 'iode')
2703+
@path_adapter_for(('.al', '.lst'), 'iode')
27112704
class IodeListsPathAdapter(IodeListsAdapter):
27122705
@classmethod
27132706
def open(cls, fpath):
@@ -2984,8 +2977,7 @@ def get_values(self, h_start, v_start, h_stop, v_stop):
29842977
raise NotImplementedError('>2d not implemented yet')
29852978

29862979

2987-
@path_adapter_for('.h5', 'tables')
2988-
@path_adapter_for('.hdf', 'tables')
2980+
@path_adapter_for(('.h5', '.hdf'), 'tables')
29892981
class H5PathAdapter(PyTablesFileAdapter):
29902982
@classmethod
29912983
def open(cls, fpath):
@@ -3375,28 +3367,33 @@ def get_values(self, h_start, v_start, h_stop, v_stop):
33753367
return [[line] for line in self._get_lines(v_start, v_stop)]
33763368

33773369

3378-
@path_adapter_for('.bat')
3379-
@path_adapter_for('.c')
3380-
@path_adapter_for('.cfg')
3381-
@path_adapter_for('.cpp')
3382-
@path_adapter_for('.h')
3383-
@path_adapter_for('.htm') # web
3384-
@path_adapter_for('.html') # web
3385-
@path_adapter_for('.ini')
3386-
@path_adapter_for('.log')
3387-
@path_adapter_for('.md')
3388-
@path_adapter_for('.py')
3389-
@path_adapter_for('.pyx') # cython
3390-
@path_adapter_for('.pxd') # cython
3391-
@path_adapter_for('.rep')
3392-
@path_adapter_for('.rst')
3393-
@path_adapter_for('.sh')
3394-
@path_adapter_for('.sql')
3395-
@path_adapter_for('.toml')
3396-
@path_adapter_for('.txt')
3397-
@path_adapter_for('.wsgi')
3398-
@path_adapter_for('.yaml')
3399-
@path_adapter_for('.yml')
3370+
TEXT_FILE_SUFFIXES = (
3371+
'.bat',
3372+
'.c',
3373+
'.cfg',
3374+
'.cpp',
3375+
'.h',
3376+
'.htm', # web
3377+
'.html', # web
3378+
'.ini',
3379+
'.log',
3380+
'.md',
3381+
'.py',
3382+
'.pyx', # cython
3383+
'.pxd', # cython
3384+
'.rep',
3385+
'.rst',
3386+
'.sh',
3387+
'.sql',
3388+
'.toml',
3389+
'.txt',
3390+
'.wsgi',
3391+
'.yaml',
3392+
'.yml',
3393+
)
3394+
3395+
3396+
@path_adapter_for(TEXT_FILE_SUFFIXES)
34003397
class TextPathAdapter(TextFileAdapter):
34013398
@classmethod
34023399
def open(cls, fpath):

0 commit comments

Comments
 (0)