Merge pull request #225 from socialcopsdev/fix-204

[MRG] Change suppress_warnings to suppress_stdout
pull/2/head
Vinayak Mehta 2018-12-12 10:34:08 +05:30 committed by GitHub
commit 40217bea46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 35 additions and 18 deletions

View File

@ -7,6 +7,7 @@ master
**Improvements** **Improvements**
* [#207](https://github.com/socialcopsdev/camelot/issues/207) Add a plot type for Stream text edges and detected table areas. [#224](https://github.com/socialcopsdev/camelot/pull/224) by Vinayak Mehta. * [#207](https://github.com/socialcopsdev/camelot/issues/207) Add a plot type for Stream text edges and detected table areas. [#224](https://github.com/socialcopsdev/camelot/pull/224) by Vinayak Mehta.
* [#204](https://github.com/socialcopsdev/camelot/issues/204) `suppress_warnings` is now called `suppress_stdout`. [#225](https://github.com/socialcopsdev/camelot/pull/225) by Vinayak Mehta.
**Bugfixes** **Bugfixes**

View File

@ -30,6 +30,7 @@ pass_config = click.make_pass_decorator(Config)
@click.group() @click.group()
@click.version_option(version=__version__) @click.version_option(version=__version__)
@click.option('-q', '--quiet', is_flag=False, help='Suppress logs and warnings.')
@click.option('-p', '--pages', default='1', help='Comma-separated page numbers.' @click.option('-p', '--pages', default='1', help='Comma-separated page numbers.'
' Example: 1,3,4 or 1,4-end.') ' Example: 1,3,4 or 1,4-end.')
@click.option('-pw', '--password', help='Password for decryption.') @click.option('-pw', '--password', help='Password for decryption.')
@ -44,7 +45,6 @@ pass_config = click.make_pass_decorator(Config)
' font size. Useful to detect super/subscripts.') ' font size. Useful to detect super/subscripts.')
@click.option('-M', '--margins', nargs=3, default=(1.0, 0.5, 0.1), @click.option('-M', '--margins', nargs=3, default=(1.0, 0.5, 0.1),
help='PDFMiner char_margin, line_margin and word_margin.') help='PDFMiner char_margin, line_margin and word_margin.')
@click.option('-q', '--quiet', is_flag=True, help='Suppress warnings.')
@click.pass_context @click.pass_context
def cli(ctx, *args, **kwargs): def cli(ctx, *args, **kwargs):
"""Camelot: PDF Table Extraction for Humans""" """Camelot: PDF Table Extraction for Humans"""
@ -96,7 +96,7 @@ def lattice(c, *args, **kwargs):
output = conf.pop('output') output = conf.pop('output')
f = conf.pop('format') f = conf.pop('format')
compress = conf.pop('zip') compress = conf.pop('zip')
suppress_warnings = conf.pop('quiet') quiet = conf.pop('quiet')
plot_type = kwargs.pop('plot_type') plot_type = kwargs.pop('plot_type')
filepath = kwargs.pop('filepath') filepath = kwargs.pop('filepath')
kwargs.update(conf) kwargs.update(conf)
@ -117,7 +117,7 @@ def lattice(c, *args, **kwargs):
raise click.UsageError('Please specify output file format using --format') raise click.UsageError('Please specify output file format using --format')
tables = read_pdf(filepath, pages=pages, flavor='lattice', tables = read_pdf(filepath, pages=pages, flavor='lattice',
suppress_warnings=suppress_warnings, **kwargs) suppress_stdout=quiet, **kwargs)
click.echo('Found {} tables'.format(tables.n)) click.echo('Found {} tables'.format(tables.n))
if plot_type is not None: if plot_type is not None:
for table in tables: for table in tables:
@ -149,7 +149,7 @@ def stream(c, *args, **kwargs):
output = conf.pop('output') output = conf.pop('output')
f = conf.pop('format') f = conf.pop('format')
compress = conf.pop('zip') compress = conf.pop('zip')
suppress_warnings = conf.pop('quiet') quiet = conf.pop('quiet')
plot_type = kwargs.pop('plot_type') plot_type = kwargs.pop('plot_type')
filepath = kwargs.pop('filepath') filepath = kwargs.pop('filepath')
kwargs.update(conf) kwargs.update(conf)
@ -169,7 +169,7 @@ def stream(c, *args, **kwargs):
raise click.UsageError('Please specify output file format using --format') raise click.UsageError('Please specify output file format using --format')
tables = read_pdf(filepath, pages=pages, flavor='stream', tables = read_pdf(filepath, pages=pages, flavor='stream',
suppress_warnings=suppress_warnings, **kwargs) suppress_stdout=quiet, **kwargs)
click.echo('Found {} tables'.format(tables.n)) click.echo('Found {} tables'.format(tables.n))
if plot_type is not None: if plot_type is not None:
for table in tables: for table in tables:

View File

@ -125,7 +125,7 @@ class PDFHandler(object):
with open(fpath, 'wb') as f: with open(fpath, 'wb') as f:
outfile.write(f) outfile.write(f)
def parse(self, flavor='lattice', **kwargs): def parse(self, flavor='lattice', suppress_stdout=False, **kwargs):
"""Extracts tables by calling parser.get_tables on all single """Extracts tables by calling parser.get_tables on all single
page PDFs. page PDFs.
@ -134,6 +134,8 @@ class PDFHandler(object):
flavor : str (default: 'lattice') flavor : str (default: 'lattice')
The parsing method to use ('lattice' or 'stream'). The parsing method to use ('lattice' or 'stream').
Lattice is used by default. Lattice is used by default.
suppress_stdout : str (default: False)
Suppress logs and warnings.
kwargs : dict kwargs : dict
See camelot.read_pdf kwargs. See camelot.read_pdf kwargs.
@ -151,6 +153,6 @@ class PDFHandler(object):
for p in self.pages] for p in self.pages]
parser = Lattice(**kwargs) if flavor == 'lattice' else Stream(**kwargs) parser = Lattice(**kwargs) if flavor == 'lattice' else Stream(**kwargs)
for p in pages: for p in pages:
t = parser.extract_tables(p) t = parser.extract_tables(p, suppress_stdout=suppress_stdout)
tables.extend(t) tables.extend(t)
return TableList(tables) return TableList(tables)

View File

@ -6,7 +6,7 @@ from .utils import validate_input, remove_extra
def read_pdf(filepath, pages='1', password=None, flavor='lattice', def read_pdf(filepath, pages='1', password=None, flavor='lattice',
suppress_warnings=False, **kwargs): suppress_stdout=False, **kwargs):
"""Read PDF and return extracted tables. """Read PDF and return extracted tables.
Note: kwargs annotated with ^ can only be used with flavor='stream' Note: kwargs annotated with ^ can only be used with flavor='stream'
@ -24,8 +24,8 @@ def read_pdf(filepath, pages='1', password=None, flavor='lattice',
flavor : str (default: 'lattice') flavor : str (default: 'lattice')
The parsing method to use ('lattice' or 'stream'). The parsing method to use ('lattice' or 'stream').
Lattice is used by default. Lattice is used by default.
suppress_warnings : bool, optional (default: False) suppress_stdout : bool, optional (default: True)
Prevent warnings from being emitted by Camelot. Print all logs and warnings.
table_areas : list, optional (default: None) table_areas : list, optional (default: None)
List of table area strings of the form x1,y1,x2,y2 List of table area strings of the form x1,y1,x2,y2
where (x1, y1) -> left-top and (x2, y2) -> right-bottom where (x1, y1) -> left-top and (x2, y2) -> right-bottom
@ -92,11 +92,11 @@ def read_pdf(filepath, pages='1', password=None, flavor='lattice',
" Use either 'lattice' or 'stream'") " Use either 'lattice' or 'stream'")
with warnings.catch_warnings(): with warnings.catch_warnings():
if suppress_warnings: if suppress_stdout:
warnings.simplefilter("ignore") warnings.simplefilter("ignore")
validate_input(kwargs, flavor=flavor) validate_input(kwargs, flavor=flavor)
p = PDFHandler(filepath, pages=pages, password=password) p = PDFHandler(filepath, pages=pages, password=password)
kwargs = remove_extra(kwargs, flavor=flavor) kwargs = remove_extra(kwargs, flavor=flavor)
tables = p.parse(flavor=flavor, **kwargs) tables = p.parse(flavor=flavor, suppress_stdout=suppress_stdout, **kwargs)
return tables return tables

View File

@ -345,8 +345,9 @@ class Lattice(BaseParser):
return table return table
def extract_tables(self, filename): def extract_tables(self, filename, suppress_stdout=False):
self._generate_layout(filename) self._generate_layout(filename)
if not suppress_stdout:
logger.info('Processing {}'.format(os.path.basename(self.rootname))) logger.info('Processing {}'.format(os.path.basename(self.rootname)))
if not self.horizontal_text: if not self.horizontal_text:

View File

@ -384,8 +384,9 @@ class Stream(BaseParser):
return table return table
def extract_tables(self, filename): def extract_tables(self, filename, suppress_stdout=False):
self._generate_layout(filename) self._generate_layout(filename)
if not suppress_stdout:
logger.info('Processing {}'.format(os.path.basename(self.rootname))) logger.info('Processing {}'.format(os.path.basename(self.rootname)))
if not self.horizontal_text: if not self.horizontal_text:

View File

@ -15,6 +15,7 @@ You can print the help for the interface by typing ``camelot --help`` in your fa
Options: Options:
--version Show the version and exit. --version Show the version and exit.
-v, --verbose Verbose.
-p, --pages TEXT Comma-separated page numbers. Example: 1,3,4 -p, --pages TEXT Comma-separated page numbers. Example: 1,3,4
or 1,4-end. or 1,4-end.
-pw, --password TEXT Password for decryption. -pw, --password TEXT Password for decryption.
@ -28,7 +29,6 @@ You can print the help for the interface by typing ``camelot --help`` in your fa
-M, --margins <FLOAT FLOAT FLOAT>... -M, --margins <FLOAT FLOAT FLOAT>...
PDFMiner char_margin, line_margin and PDFMiner char_margin, line_margin and
word_margin. word_margin.
-q, --quiet Suppress warnings.
--help Show this message and exit. --help Show this message and exit.
Commands: Commands:

View File

@ -50,13 +50,25 @@ def test_no_tables_found():
assert str(e.value) == 'No tables found on page-1' assert str(e.value) == 'No tables found on page-1'
def test_no_tables_found_logs_suppressed():
filename = os.path.join(testdir, 'foo.pdf')
with warnings.catch_warnings():
# the test should fail if any warning is thrown
warnings.simplefilter('error')
try:
tables = camelot.read_pdf(filename, suppress_stdout=True)
except Warning as e:
warning_text = str(e)
pytest.fail('Unexpected warning: {}'.format(warning_text))
def test_no_tables_found_warnings_suppressed(): def test_no_tables_found_warnings_suppressed():
filename = os.path.join(testdir, 'blank.pdf') filename = os.path.join(testdir, 'blank.pdf')
with warnings.catch_warnings(): with warnings.catch_warnings():
# the test should fail if any warning is thrown # the test should fail if any warning is thrown
warnings.simplefilter('error') warnings.simplefilter('error')
try: try:
tables = camelot.read_pdf(filename, suppress_warnings=True) tables = camelot.read_pdf(filename, suppress_stdout=True)
except Warning as e: except Warning as e:
warning_text = str(e) warning_text = str(e)
pytest.fail('Unexpected warning: {}'.format(warning_text)) pytest.fail('Unexpected warning: {}'.format(warning_text))