Skip to content

Commit

Permalink
Improve error feedback of importing exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
wajdikhattel committed May 21, 2020
1 parent 9d739f8 commit d3855b0
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions textract/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import importlib
import glob
import re
import pkgutil

from .. import exceptions

Expand Down Expand Up @@ -62,17 +63,24 @@ def process(filename, input_encoding=None, output_encoding=DEFAULT_OUTPUT_ENCODI
# the _parser extension
rel_module = ext + _FILENAME_SUFFIX

# If we can't import the module, the file extension isn't currently
# supported
# check if we can import the parser module related to the file extension
try:
filetype_module = importlib.import_module(
rel_module, 'textract.parsers'
)
except ImportError:
raise exceptions.ExtensionNotSupported(ext)
# check if the module exists in the system
is_module = pkgutil.find_loader('textract.parsers'+rel_module)

if is_module is not None:
filetype_module = importlib.import_module(
rel_module, 'textract.parsers'
)
else:
# If we can't import the module, the file extension isn't currently
# supported
raise exceptions.ExtensionNotSupported(ext)
except ImportError as relativeImportFailed:
# Raise the exception of the relative import failure
raise relativeImportFailed

# do the extraction

parser = filetype_module.Parser()
return parser.process(filename, input_encoding, output_encoding, **kwargs)

Expand Down

0 comments on commit d3855b0

Please sign in to comment.