11from abc import ABCMeta
22import typing as ty
3+ import logging
34import attrs
4- from .utils import describe_task
5+ from .utils import describe_task , matching_source
56from .exceptions import FileFormatsError
67
78if ty .TYPE_CHECKING :
89 from .datatype import DataType
910
11+ logger = logging .getLogger ("fileformats" )
12+
1013
1114@attrs .define
1215class ConverterWrapper :
@@ -106,11 +109,10 @@ def register_converter(
106109 ----------
107110 source_format : type
108111 the source format to register a converter from
109- task_spec : ty.Callable
110- a callable that resolves to a Pydra task
111- converter_kwargs : dict
112- additional keyword arguments to be passed to the task spec at initialisation
113- time
112+ converter_tuple
113+ a tuple consisting of a `task_spec` callable that resolves to a Pydra task
114+ and a dictionary of keyword arguments to be passed to the task spec at
115+ initialisation time
114116
115117 Raises
116118 ------
@@ -134,12 +136,19 @@ def register_converter(
134136 ]
135137 assert len (prev_registered ) <= 1
136138 if prev_registered :
137- prev = prev_registered [0 ]
138- prev_task = cls .converters [prev ][0 ]
139+ prev_tuple = cls .converters [prev_registered [0 ]]
140+ task , task_kwargs = converter_tuple
141+ prev_task , prev_kwargs = prev_tuple
142+ if matching_source (task , prev_task ) and task_kwargs == prev_kwargs :
143+ logger .warning (
144+ "Ignoring duplicate registrations of the same converter %s" ,
145+ describe_task (task ),
146+ )
147+ return # actually the same task but just imported twice for some reason
139148 raise FileFormatsError (
140- f"There is already a converter registered from { prev } "
141- f"to the generic type '{ tuple (prev .wildcard_classifiers ())[0 ]} ': "
142- f"{ describe_task (prev_task )} "
149+ f"Cannot register converter from { source_format } to the generic type "
150+ f"'{ tuple (prev_task .wildcard_classifiers ())[0 ]} ', { describe_task ( task ) } "
151+ f"because there is already one registered, { describe_task (prev_task )} "
143152 )
144153
145154 cls .converters [source_format ] = converter_tuple
0 commit comments