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 + 1] Add __main__ #159

Merged
merged 3 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions camelot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

import logging

from click import HelpFormatter

from .__version__ import __version__
from .io import read_pdf


def _write_usage(self, prog, args='', prefix='Usage: '):
return self._write_usage('camelot', args, prefix=prefix)


# monkey patch click.HelpFormatter
HelpFormatter._write_usage = HelpFormatter.write_usage
HelpFormatter.write_usage = _write_usage

# set up logging
logger = logging.getLogger('camelot')

Expand Down
16 changes: 16 additions & 0 deletions camelot/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import


__all__ = ('main',)


def main():
from camelot.cli import cli

cli()


if __name__ == "__main__":
main()
186 changes: 93 additions & 93 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,93 @@
# -*- coding: utf-8 -*-
import os
from click.testing import CliRunner
from camelot.cli import cli
from camelot.utils import TemporaryDirectory
testdir = os.path.dirname(os.path.abspath(__file__))
testdir = os.path.join(testdir, 'files')
def test_cli_lattice():
with TemporaryDirectory() as tempdir:
infile = os.path.join(testdir, 'foo.pdf')
outfile = os.path.join(tempdir, 'foo.csv')
runner = CliRunner()
result = runner.invoke(cli, ['--format', 'csv', '--output', outfile,
'lattice', infile])
assert result.exit_code == 0
assert result.output == 'Found 1 tables\n'
result = runner.invoke(cli, ['--format', 'csv',
'lattice', infile])
output_error = 'Error: Please specify output file path using --output'
assert output_error in result.output
result = runner.invoke(cli, ['--output', outfile,
'lattice', infile])
format_error = 'Please specify output file format using --format'
assert format_error in result.output
def test_cli_stream():
with TemporaryDirectory() as tempdir:
infile = os.path.join(testdir, 'budget.pdf')
outfile = os.path.join(tempdir, 'budget.csv')
runner = CliRunner()
result = runner.invoke(cli, ['--format', 'csv', '--output', outfile,
'stream', infile])
assert result.exit_code == 0
assert result.output == 'Found 1 tables\n'
result = runner.invoke(cli, ['--format', 'csv', 'stream', infile])
output_error = 'Error: Please specify output file path using --output'
assert output_error in result.output
result = runner.invoke(cli, ['--output', outfile, 'stream', infile])
format_error = 'Please specify output file format using --format'
assert format_error in result.output
def test_cli_output_format():
with TemporaryDirectory() as tempdir:
infile = os.path.join(testdir, 'health.pdf')
outfile = os.path.join(tempdir, 'health.{}')
runner = CliRunner()
# json
result = runner.invoke(cli, ['--format', 'json', '--output', outfile.format('json'),
'stream', infile])
assert result.exit_code == 0
# excel
result = runner.invoke(cli, ['--format', 'excel', '--output', outfile.format('xlsx'),
'stream', infile])
assert result.exit_code == 0
# html
result = runner.invoke(cli, ['--format', 'html', '--output', outfile.format('html'),
'stream', infile])
assert result.exit_code == 0
# zip
result = runner.invoke(cli, ['--zip', '--format', 'csv', '--output', outfile.format('csv'),
'stream', infile])
assert result.exit_code == 0
def test_cli_quiet_flag():
with TemporaryDirectory() as tempdir:
infile = os.path.join(testdir, 'blank.pdf')
outfile = os.path.join(tempdir, 'blank.csv')
runner = CliRunner()
result = runner.invoke(cli, ['--format', 'csv', '--output', outfile,
'stream', infile])
assert 'No tables found on page-1' in result.output
result = runner.invoke(cli, ['--quiet', '--format', 'csv',
'--output', outfile, 'stream', infile])
assert 'No tables found on page-1' not in result.output
# -*- coding: utf-8 -*-

import os

from click.testing import CliRunner

from camelot.cli import cli
from camelot.utils import TemporaryDirectory


testdir = os.path.dirname(os.path.abspath(__file__))
testdir = os.path.join(testdir, 'files')


def test_cli_lattice():
with TemporaryDirectory() as tempdir:
infile = os.path.join(testdir, 'foo.pdf')
outfile = os.path.join(tempdir, 'foo.csv')
runner = CliRunner()
result = runner.invoke(cli, ['--format', 'csv', '--output', outfile,
'lattice', infile])
assert result.exit_code == 0
assert result.output == 'Found 1 tables\n'

result = runner.invoke(cli, ['--format', 'csv',
'lattice', infile])
output_error = 'Error: Please specify output file path using --output'
assert output_error in result.output

result = runner.invoke(cli, ['--output', outfile,
'lattice', infile])
format_error = 'Please specify output file format using --format'
assert format_error in result.output


def test_cli_stream():
with TemporaryDirectory() as tempdir:
infile = os.path.join(testdir, 'budget.pdf')
outfile = os.path.join(tempdir, 'budget.csv')
runner = CliRunner()
result = runner.invoke(cli, ['--format', 'csv', '--output', outfile,
'stream', infile])
assert result.exit_code == 0
assert result.output == 'Found 1 tables\n'

result = runner.invoke(cli, ['--format', 'csv', 'stream', infile])
output_error = 'Error: Please specify output file path using --output'
assert output_error in result.output

result = runner.invoke(cli, ['--output', outfile, 'stream', infile])
format_error = 'Please specify output file format using --format'
assert format_error in result.output


def test_cli_output_format():
with TemporaryDirectory() as tempdir:
infile = os.path.join(testdir, 'health.pdf')
outfile = os.path.join(tempdir, 'health.{}')
runner = CliRunner()

# json
result = runner.invoke(cli, ['--format', 'json', '--output', outfile.format('json'),
'stream', infile])
assert result.exit_code == 0

# excel
result = runner.invoke(cli, ['--format', 'excel', '--output', outfile.format('xlsx'),
'stream', infile])
assert result.exit_code == 0

# html
result = runner.invoke(cli, ['--format', 'html', '--output', outfile.format('html'),
'stream', infile])
assert result.exit_code == 0

# zip
result = runner.invoke(cli, ['--zip', '--format', 'csv', '--output', outfile.format('csv'),
'stream', infile])
assert result.exit_code == 0

def test_cli_quiet_flag():
with TemporaryDirectory() as tempdir:
infile = os.path.join(testdir, 'blank.pdf')
outfile = os.path.join(tempdir, 'blank.csv')
runner = CliRunner()

result = runner.invoke(cli, ['--format', 'csv', '--output', outfile,
'stream', infile])
assert 'No tables found on page-1' in result.output

result = runner.invoke(cli, ['--quiet', '--format', 'csv',
'--output', outfile, 'stream', infile])
assert 'No tables found on page-1' not in result.output