import os
# traceback from exceptions that inherit from this class are suppressed
[docs]class CommandLineError(Exception):
"""The traceback of all CommandLineError's is supressed when the
errors occur on the command line to provide a useful command line
interface.
"""
[docs] def render(self, msg):
return msg % vars(self)
[docs]class ExtensionNotSupported(CommandLineError):
"""This error is raised with unsupported extensions"""
def __init__(self, ext):
self.ext = ext
def __str__(self):
return self.render((
'The filename extension %(ext)s is not yet supported by\n'
'textract. Please suggest this filename extension here:\n\n'
' https://github.com/deanmalmgren/textract/issues\n'
))
[docs]class MissingFileError(CommandLineError):
"""This error is raised when the file can not be located at the
specified path.
"""
def __init__(self, filename):
self.filename = filename
self.root, self.ext = os.path.splitext(filename)
def __str__(self):
return self.render((
'The file "%(filename)s" can not be found.\n'
'Is this the right path/to/file/you/want/to/extract%(ext)s?'
))
[docs]class UnknownMethod(CommandLineError):
"""This error is raised when the specified --method on the command
line is unknown.
"""
def __init__(self, method):
self.method = method
def __str__(self):
return self.render((
'The method "%(method)s" can not be found for this filetype.'
))
[docs]class ShellError(CommandLineError):
"""This error is raised when a shell.run returns a non-zero exit code
(meaning the command failed).
"""
def __init__(self, command, exit_code, stdout, stderr):
self.command = command
self.exit_code = exit_code
self.stdout = stdout
self.stderr = stderr
self.executable = self.command.split()[0]
[docs] def is_uninstalled(self):
return os.name == 'posix' and self.exit_code == 127
[docs] def uninstalled_message(self):
return (
"The command `%(command)s` failed because the executable\n"
"`%(executable)s` is not installed on your system. Please make\n"
"sure the appropriate dependencies are installed before using\n"
"textract:\n\n"
" http://textract.readthedocs.org/en/latest/installation.html\n"
) % vars(self)
[docs] def failed_message(self):
return (
"The command `%(command)s` failed with exit code %(exit_code)d\n"
"------------- stdout -------------\n"
"%(stdout)s"
"------------- stderr -------------\n"
"%(stderr)s"
) % vars(self)
def __str__(self):
if self.is_uninstalled():
return self.uninstalled_message()
else:
return self.failed_message()