text
stringlengths
0
828
:return: a tuple of 3 booleans : (does match?, strict source match? (None if no match), strict dest match?
(None if no match))
""""""
# (1) first handle the easy joker+joker case
if from_type is JOKER and to_type is JOKER:
return True, None, None
# Don't validate types -- this is called too often at the initial RootParser instance creation time,
# and this is quite 'internal' so the risk is very low
#
# check_var(strict, var_types=bool, var_name='strict')
# if from_type is not JOKER:
# check_var(from_type, var_types=type, var_name='from_type')
# if to_type is not JOKER:
# check_var(to_type, var_types=type, var_name='to_type')
# -- first call custom checker if provided
if self.is_able_to_convert_func is not None:
# TODO Maybe one day, rather push the JOKER to the function ? not sure that it will be more explicit..
if not self.is_able_to_convert_func(strict,
from_type=None if from_type is JOKER else from_type,
to_type=None if to_type is JOKER else to_type):
return False, None, None
# -- from_type strict match
if (from_type is JOKER) or (from_type is self.from_type) or is_any_type(from_type):
# -- check to type strict
if (to_type is JOKER) or self.is_generic() or (to_type is self.to_type):
return True, True, True # strict to_type match
# -- check to type non-strict
elif (not strict) and issubclass(self.to_type, to_type):
return True, True, False # approx to_type match
# -- from_type non-strict match
elif (not strict) and issubclass(from_type, self.from_type):
# -- check to type strict
if (to_type is JOKER) or self.is_generic() or (to_type is self.to_type):
return True, False, True # exact to_type match
# -- check to type non-strict
elif (not strict) and issubclass(self.to_type, to_type):
return True, False, False # approx to_type match
# -- otherwise no match
return False, None, None"
1621,"def are_worth_chaining(left_converter, right_converter) -> bool:
""""""
Utility method to check if it makes sense to chain these two converters. Returns True if it brings value to
chain the first converter with the second converter. To bring value,
* the second converter's input should not be a parent class of the first converter's input (in that case, it is
always more interesting to use the second converter directly for any potential input)
* the second converter's output should not be a parent class of the first converter's input or output. Otherwise
the chain does not even make any progress :)
* The first converter has to allow chaining (with converter.can_chain=True)
:param left_converter:
:param right_converter:
:return:
""""""
if not left_converter.can_chain:
return False
elif not is_any_type(left_converter.to_type) and is_any_type(right_converter.to_type):
# we gain the capability to generate any type. So it is interesting.
return True
elif issubclass(left_converter.from_type, right_converter.to_type) \
or issubclass(left_converter.to_type, right_converter.to_type) \
or issubclass(left_converter.from_type, right_converter.from_type):
# Not interesting : the outcome of the chain would be not better than one of the converters alone
return False
# Note: we dont say that chaining a generic converter with a converter is useless. Indeed it might unlock some
# capabilities for the user (new file extensions, etc.) that would not be available with the generic parser
# targetting to_type alone. For example parsing object A from its constructor then converting A to B might
# sometimes be interesting, rather than parsing B from its constructor
else:
# interesting
return True"
1622,"def can_be_appended_to(self, left_converter, strict: bool) -> bool:
""""""
Utility method to check if this (self) converter can be appended after the output of the provided converter.
This method does not check if it makes sense, it just checks if the output type of the left converter is
compliant with the input type of this converter. Compliant means:
* strict mode : type equality
* non-strict mode : output type of left_converter should be a subclass of input type of this converter
In addition, the custom function provided in constructor may be used to reject conversion (see
is_able_to_convert for details)
:param left_converter:
:param strict: boolean to
:return:
""""""
is_able_to_take_input = self.is_able_to_convert(strict, from_type=left_converter.to_type, to_type=JOKER)
if left_converter.is_generic():
return is_able_to_take_input \
and left_converter.is_able_to_convert(strict, from_type=JOKER, to_type=self.from_type)
else:
return is_able_to_take_input"