text
stringlengths
0
828
# -- (b) then parsers that do not match at all (not the file extension nor the type): we can find parsing chains
# that make them at least match the type
#
# (we have to reverse it because it was 'best last', now it will be 'best first')
for parser in reversed(no_match):
for typ in parser.supported_types:
for converter in reversed(all_matching_converters):
# if converter is able to source from this parser
if converter.is_able_to_convert(self.is_strict, from_type=typ, to_type=desired_type):
if ParsingChain.are_worth_chaining(parser, typ, converter):
# insert it at the beginning since it should have less priority
no_ext_match_but_type_match.insert(0, ParsingChain(parser, converter, strict=self.is_strict,
base_parser_chosen_dest_type=typ))
# Finally sort by chain length
matching_p_generic = sorted(matching_p_generic, key=len, reverse=True)
matching_p_approx = sorted(matching_p_approx, key=len, reverse=True)
matching_p_exact = sorted(matching_p_exact, key=len, reverse=True)
# Return
return (matching_p_generic, matching_p_approx, matching_p_exact), no_type_match_but_ext_match, \
no_ext_match_but_type_match, no_match"
515,"def _complete_parsers_with_converters(self, parser, parser_supported_type, desired_type, matching_c_generic_to_type,
matching_c_approx_to_type, matching_c_exact_to_type):
""""""
Internal method to create parsing chains made of a parser and converters from the provided lists.
Once again a JOKER for a type means 'joker' here.
:param parser:
:param parser_supported_type:
:param desired_type:
:param matching_c_generic_to_type:
:param matching_c_approx_to_type:
:param matching_c_exact_to_type:
:return:
""""""
matching_p_generic, matching_p_generic_with_approx_chain, \
matching_p_approx, matching_p_approx_with_approx_chain,\
matching_p_exact, matching_p_exact_with_approx_chain = [], [], [], [], [], []
# resolve Union and TypeVar
desired_types = get_alternate_types_resolving_forwardref_union_and_typevar(desired_type)
for desired_type in desired_types:
# first transform any 'Any' type requirement into the official class for that
desired_type = get_validated_type(desired_type, 'desired_type', enforce_not_joker=False)
# ---- Generic converters - only if the parsed type is not already 'any'
if not is_any_type(parser_supported_type):
for cv in matching_c_generic_to_type:
# if the converter can attach to this parser, we have a matching parser !
# --start from strict
if cv.is_able_to_convert(strict=True,
from_type=parser_supported_type,
to_type=desired_type):
if ParsingChain.are_worth_chaining(parser, parser_supported_type, cv):
chain = ParsingChain(parser, cv, strict=True,
base_parser_chosen_dest_type=parser_supported_type)
# insert it at the beginning since it should have less priority
matching_p_generic.append(chain)
# --then non-strict
elif (not self.strict) \
and cv.is_able_to_convert(strict=False,
from_type=parser_supported_type,
to_type=desired_type):
if ParsingChain.are_worth_chaining(parser, parser_supported_type, cv):
chain = ParsingChain(parser, cv, strict=False,
base_parser_chosen_dest_type=parser_supported_type)
# insert it at the beginning since it should have less priority
matching_p_generic_with_approx_chain.append(chain)
# ---- Approx to_type
for cv in matching_c_approx_to_type:
# if the converter can attach to this parser, we have a matching parser !
# -- start from strict
if cv.is_able_to_convert(strict=True,
from_type=parser_supported_type,
to_type=desired_type):
if ParsingChain.are_worth_chaining(parser, parser_supported_type, cv):
chain = ParsingChain(parser, cv, strict=True,
base_parser_chosen_dest_type=parser_supported_type)
# insert it at the beginning since it should have less priority
matching_p_approx.append(chain)
# then non-strict
elif (not self.strict) \
and cv.is_able_to_convert(strict=False,
from_type=parser_supported_type,
to_type=desired_type):
if ParsingChain.are_worth_chaining(parser, parser_supported_type, cv):
chain = ParsingChain(parser, cv, strict=False,
base_parser_chosen_dest_type=parser_supported_type)
# insert it at the beginning since it should have less priority
matching_p_approx_with_approx_chain.append(chain)
# ---- Exact to_type
for cv in matching_c_exact_to_type: