[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Change suppress_warnings to suppress_stdout #225

Merged
merged 3 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Change kwarg name
  • Loading branch information
vinayak-mehta committed Dec 12, 2018
commit 591cfd529105bc27e648e5bca59672d1dfc1bb99
2 changes: 1 addition & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ master
**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.
* [#204](https://github.com/socialcopsdev/camelot/issues/204) `suppress_warnings` is now called `verbose`. By default, all logs and warnings will be printed. [#225](https://github.com/socialcopsdev/camelot/pull/225) 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**

Expand Down
10 changes: 5 additions & 5 deletions camelot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def set_config(self, key, value):

@click.group()
@click.version_option(version=__version__)
@click.option('-v', '--verbose', is_flag=True, help='Verbose.')
@click.option('-q', '--quiet', is_flag=False, help='Suppress logs and warnings.')
@click.option('-p', '--pages', default='1', help='Comma-separated page numbers.'
' Example: 1,3,4 or 1,4-end.')
@click.option('-pw', '--password', help='Password for decryption.')
Expand Down Expand Up @@ -96,7 +96,7 @@ def lattice(c, *args, **kwargs):
output = conf.pop('output')
f = conf.pop('format')
compress = conf.pop('zip')
verbose = conf.pop('verbose')
quiet = conf.pop('quiet')
plot_type = kwargs.pop('plot_type')
filepath = kwargs.pop('filepath')
kwargs.update(conf)
Expand All @@ -117,7 +117,7 @@ def lattice(c, *args, **kwargs):
raise click.UsageError('Please specify output file format using --format')

tables = read_pdf(filepath, pages=pages, flavor='lattice',
verbose=verbose, **kwargs)
suppress_stdout=quiet, **kwargs)
click.echo('Found {} tables'.format(tables.n))
if plot_type is not None:
for table in tables:
Expand Down Expand Up @@ -149,7 +149,7 @@ def stream(c, *args, **kwargs):
output = conf.pop('output')
f = conf.pop('format')
compress = conf.pop('zip')
verbose = conf.pop('verbose')
quiet = conf.pop('quiet')
plot_type = kwargs.pop('plot_type')
filepath = kwargs.pop('filepath')
kwargs.update(conf)
Expand All @@ -169,7 +169,7 @@ def stream(c, *args, **kwargs):
raise click.UsageError('Please specify output file format using --format')

tables = read_pdf(filepath, pages=pages, flavor='stream',
verbose=verbose, **kwargs)
suppress_stdout=quiet, **kwargs)
click.echo('Found {} tables'.format(tables.n))
if plot_type is not None:
for table in tables:
Expand Down
8 changes: 4 additions & 4 deletions camelot/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def _save_page(self, filename, page, temp):
with open(fpath, 'wb') as f:
outfile.write(f)

def parse(self, flavor='lattice', verbose=True, **kwargs):
def parse(self, flavor='lattice', suppress_stdout=False, **kwargs):
"""Extracts tables by calling parser.get_tables on all single
page PDFs.

Expand All @@ -134,8 +134,8 @@ def parse(self, flavor='lattice', verbose=True, **kwargs):
flavor : str (default: 'lattice')
The parsing method to use ('lattice' or 'stream').
Lattice is used by default.
verbose : str (default: True)
Print all logs and warnings.
suppress_stdout : str (default: False)
Suppress logs and warnings.
kwargs : dict
See camelot.read_pdf kwargs.

Expand All @@ -153,6 +153,6 @@ def parse(self, flavor='lattice', verbose=True, **kwargs):
for p in self.pages]
parser = Lattice(**kwargs) if flavor == 'lattice' else Stream(**kwargs)
for p in pages:
t = parser.extract_tables(p, verbose=verbose)
t = parser.extract_tables(p, suppress_stdout=suppress_stdout)
tables.extend(t)
return TableList(tables)
8 changes: 4 additions & 4 deletions camelot/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def read_pdf(filepath, pages='1', password=None, flavor='lattice',
verbose=True, **kwargs):
suppress_stdout=False, **kwargs):
"""Read PDF and return extracted tables.

Note: kwargs annotated with ^ can only be used with flavor='stream'
Expand All @@ -24,7 +24,7 @@ def read_pdf(filepath, pages='1', password=None, flavor='lattice',
flavor : str (default: 'lattice')
The parsing method to use ('lattice' or 'stream').
Lattice is used by default.
verbose : bool, optional (default: True)
suppress_stdout : bool, optional (default: True)
Print all logs and warnings.
table_areas : list, optional (default: None)
List of table area strings of the form x1,y1,x2,y2
Expand Down Expand Up @@ -92,11 +92,11 @@ def read_pdf(filepath, pages='1', password=None, flavor='lattice',
" Use either 'lattice' or 'stream'")

with warnings.catch_warnings():
if not verbose:
if suppress_stdout:
warnings.simplefilter("ignore")

validate_input(kwargs, flavor=flavor)
p = PDFHandler(filepath, pages=pages, password=password)
kwargs = remove_extra(kwargs, flavor=flavor)
tables = p.parse(flavor=flavor, verbose=verbose, **kwargs)
tables = p.parse(flavor=flavor, suppress_stdout=suppress_stdout, **kwargs)
return tables
4 changes: 2 additions & 2 deletions camelot/parsers/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ def _generate_table(self, table_idx, cols, rows, **kwargs):

return table

def extract_tables(self, filename, verbose=True):
def extract_tables(self, filename, suppress_stdout=False):
self._generate_layout(filename)
if verbose:
if not suppress_stdout:
logger.info('Processing {}'.format(os.path.basename(self.rootname)))

if not self.horizontal_text:
Expand Down
4 changes: 2 additions & 2 deletions camelot/parsers/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,9 @@ def _generate_table(self, table_idx, cols, rows, **kwargs):

return table

def extract_tables(self, filename, verbose=True):
def extract_tables(self, filename, suppress_stdout=False):
self._generate_layout(filename)
if verbose:
if not suppress_stdout:
logger.info('Processing {}'.format(os.path.basename(self.rootname)))

if not self.horizontal_text:
Expand Down
14 changes: 13 additions & 1 deletion tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,25 @@ def test_no_tables_found():
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():
filename = os.path.join(testdir, 'blank.pdf')
with warnings.catch_warnings():
# the test should fail if any warning is thrown
warnings.simplefilter('error')
try:
tables = camelot.read_pdf(filename, verbose=False)
tables = camelot.read_pdf(filename, suppress_stdout=True)
except Warning as e:
warning_text = str(e)
pytest.fail('Unexpected warning: {}'.format(warning_text))
Expand Down