1
0
mirror of synced 2024-11-22 09:14:23 +01:00
mat2/libmat2/parser_factory.py

65 lines
2.1 KiB
Python
Raw Normal View History

import glob
import os
2018-03-19 23:43:49 +01:00
import mimetypes
2018-03-20 01:20:11 +01:00
import importlib
2018-06-04 22:54:01 +02:00
from typing import TypeVar, List, Tuple, Optional
2018-03-19 23:43:49 +01:00
from . import abstract, UNSUPPORTED_EXTENSIONS
2018-03-20 01:20:11 +01:00
2018-04-02 19:11:59 +02:00
T = TypeVar('T', bound='abstract.AbstractParser')
2019-02-21 01:28:11 +01:00
mimetypes.add_type('application/epub+zip', '.epub')
2019-05-09 09:41:05 +02:00
mimetypes.add_type('application/x-dtbncx+xml', '.ncx') # EPUB Navigation Control XML File
2019-02-21 01:28:11 +01:00
# This should be removed after we move to python3.10
# https://github.com/python/cpython/commit/20a5b7e986377bdfd929d7e8c4e3db5847dfdb2d
mimetypes.add_type('image/heic', '.heic')
def __load_all_parsers():
""" Loads every parser in a dynamic way """
current_dir = os.path.dirname(__file__)
2018-07-19 23:10:27 +02:00
for fname in glob.glob(os.path.join(current_dir, '*.py')):
if fname.endswith('abstract.py'):
continue
2018-07-19 23:10:27 +02:00
elif fname.endswith('__init__.py'):
continue
elif fname.endswith('exiftool.py'):
continue
2018-07-19 23:10:27 +02:00
basename = os.path.basename(fname)
name, _ = os.path.splitext(basename)
importlib.import_module('.' + name, package='libmat2')
__load_all_parsers()
2018-06-04 20:29:41 +02:00
def _get_parsers() -> List[T]:
""" Get all our parsers!"""
def __get_parsers(cls):
return cls.__subclasses__() + \
[g for s in cls.__subclasses__() for g in __get_parsers(s)]
return __get_parsers(abstract.AbstractParser)
2018-06-04 22:54:01 +02:00
def get_parser(filename: str) -> Tuple[Optional[T], Optional[str]]:
2020-11-23 19:50:46 +01:00
""" Return the appropriate parser for a given filename.
:raises ValueError: Raised if the instantiation of the parser went wrong.
"""
2018-05-16 00:08:45 +02:00
mtype, _ = mimetypes.guess_type(filename)
_, extension = os.path.splitext(filename)
if extension.lower() in UNSUPPORTED_EXTENSIONS:
return None, mtype
2019-04-27 15:03:09 +02:00
if mtype == 'application/x-tar':
if extension[1:] in ('bz2', 'gz', 'xz'):
mtype = mtype + '+' + extension[1:]
for parser_class in _get_parsers(): # type: ignore
if mtype in parser_class.mimetypes:
2020-11-23 19:50:46 +01:00
# This instantiation might raise a ValueError on malformed files
return parser_class(filename), mtype
return None, mtype