The following function doesn't properly ask the handlers if they can "detect" and instead utilizes the key of the dictionary which is assigned to handler.FM_BOUNDARY.
# global handlers
handlers = {
Handler.FM_BOUNDARY: Handler()
for Handler in [YAMLHandler, JSONHandler, TOMLHandler]
if Handler is not None
}
def detect_format(text, handlers):
"""
Figure out which handler to use, based on metadata.
Returns a handler instance or None.
``text`` should be unicode text about to be parsed.
``handlers`` is a dictionary where keys are opening delimiters
and values are handler instances.
"""
for pattern, handler in handlers.items():
if pattern.match(text):
return handler
# nothing matched, give nothing back
return None
The following function doesn't properly ask the handlers if they can "detect" and instead utilizes the key of the dictionary which is assigned to
handler.FM_BOUNDARY.