Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 0f8cda4

Browse files
authored
Merge pull request #5 from camelot-dev/fix-cli-group-name
[MRG] No need to monkey-patch Click.HelpFormatter
2 parents e81e818 + 13616c2 commit 0f8cda4

File tree

4 files changed

+110
-63
lines changed

4 files changed

+110
-63
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ coverage.xml
1212
.pytest_cache/
1313
_build/
1414

15+
.venv/
16+
htmlcov/
17+
1518
# vscode
16-
.vscode
19+
.vscode

camelot/__init__.py

-10
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,11 @@
22

33
import logging
44

5-
from click import HelpFormatter
6-
75
from .__version__ import __version__
86
from .io import read_pdf
97
from .plotting import PlotMethods
108

119

12-
def _write_usage(self, prog, args="", prefix="Usage: "):
13-
return self._write_usage("camelot", args, prefix=prefix)
14-
15-
16-
# monkey patch click.HelpFormatter
17-
HelpFormatter._write_usage = HelpFormatter.write_usage
18-
HelpFormatter.write_usage = _write_usage
19-
2010
# set up logging
2111
logger = logging.getLogger("camelot")
2212

camelot/cli.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def set_config(self, key, value):
2929
pass_config = click.make_pass_decorator(Config)
3030

3131

32-
@click.group()
32+
@click.group(name="camelot")
3333
@click.version_option(version=__version__)
3434
@click.option("-q", "--quiet", is_flag=False, help="Suppress logs and warnings.")
3535
@click.option(

tests/test_cli.py

+105-51
Original file line numberDiff line numberDiff line change
@@ -9,109 +9,163 @@
99

1010

1111
testdir = os.path.dirname(os.path.abspath(__file__))
12-
testdir = os.path.join(testdir, 'files')
12+
testdir = os.path.join(testdir, "files")
13+
14+
15+
def test_help_output():
16+
runner = CliRunner()
17+
prog_name = runner.get_default_prog_name(cli)
18+
result = runner.invoke(cli, ["--help"])
19+
output = result.output
20+
21+
assert prog_name == "camelot"
22+
assert result.output.startswith("Usage: %(prog_name)s [OPTIONS] COMMAND" % locals())
23+
assert all(
24+
v in result.output
25+
for v in ["Options:", "--version", "--help", "Commands:", "lattice", "stream"]
26+
)
1327

1428

1529
def test_cli_lattice():
1630
with TemporaryDirectory() as tempdir:
17-
infile = os.path.join(testdir, 'foo.pdf')
18-
outfile = os.path.join(tempdir, 'foo.csv')
31+
infile = os.path.join(testdir, "foo.pdf")
32+
outfile = os.path.join(tempdir, "foo.csv")
1933
runner = CliRunner()
20-
result = runner.invoke(cli, ['--format', 'csv', '--output', outfile,
21-
'lattice', infile])
34+
result = runner.invoke(
35+
cli, ["--format", "csv", "--output", outfile, "lattice", infile]
36+
)
2237
assert result.exit_code == 0
23-
assert result.output == 'Found 1 tables\n'
38+
assert result.output == "Found 1 tables\n"
2439

25-
result = runner.invoke(cli, ['--format', 'csv',
26-
'lattice', infile])
27-
output_error = 'Error: Please specify output file path using --output'
40+
result = runner.invoke(cli, ["--format", "csv", "lattice", infile])
41+
output_error = "Error: Please specify output file path using --output"
2842
assert output_error in result.output
2943

30-
result = runner.invoke(cli, ['--output', outfile,
31-
'lattice', infile])
32-
format_error = 'Please specify output file format using --format'
44+
result = runner.invoke(cli, ["--output", outfile, "lattice", infile])
45+
format_error = "Please specify output file format using --format"
3346
assert format_error in result.output
3447

3548

3649
def test_cli_stream():
3750
with TemporaryDirectory() as tempdir:
38-
infile = os.path.join(testdir, 'budget.pdf')
39-
outfile = os.path.join(tempdir, 'budget.csv')
51+
infile = os.path.join(testdir, "budget.pdf")
52+
outfile = os.path.join(tempdir, "budget.csv")
4053
runner = CliRunner()
41-
result = runner.invoke(cli, ['--format', 'csv', '--output', outfile,
42-
'stream', infile])
54+
result = runner.invoke(
55+
cli, ["--format", "csv", "--output", outfile, "stream", infile]
56+
)
4357
assert result.exit_code == 0
44-
assert result.output == 'Found 1 tables\n'
58+
assert result.output == "Found 1 tables\n"
4559

46-
result = runner.invoke(cli, ['--format', 'csv', 'stream', infile])
47-
output_error = 'Error: Please specify output file path using --output'
60+
result = runner.invoke(cli, ["--format", "csv", "stream", infile])
61+
output_error = "Error: Please specify output file path using --output"
4862
assert output_error in result.output
4963

50-
result = runner.invoke(cli, ['--output', outfile, 'stream', infile])
51-
format_error = 'Please specify output file format using --format'
64+
result = runner.invoke(cli, ["--output", outfile, "stream", infile])
65+
format_error = "Please specify output file format using --format"
5266
assert format_error in result.output
5367

5468

5569
def test_cli_password():
5670
with TemporaryDirectory() as tempdir:
57-
infile = os.path.join(testdir, 'health_protected.pdf')
58-
outfile = os.path.join(tempdir, 'health_protected.csv')
71+
infile = os.path.join(testdir, "health_protected.pdf")
72+
outfile = os.path.join(tempdir, "health_protected.csv")
5973
runner = CliRunner()
60-
result = runner.invoke(cli, ['--password', 'userpass',
61-
'--format', 'csv', '--output', outfile,
62-
'stream', infile])
74+
result = runner.invoke(
75+
cli,
76+
[
77+
"--password",
78+
"userpass",
79+
"--format",
80+
"csv",
81+
"--output",
82+
outfile,
83+
"stream",
84+
infile,
85+
],
86+
)
6387
assert result.exit_code == 0
64-
assert result.output == 'Found 1 tables\n'
88+
assert result.output == "Found 1 tables\n"
6589

66-
output_error = 'file has not been decrypted'
90+
output_error = "file has not been decrypted"
6791
# no password
68-
result = runner.invoke(cli, ['--format', 'csv', '--output', outfile,
69-
'stream', infile])
92+
result = runner.invoke(
93+
cli, ["--format", "csv", "--output", outfile, "stream", infile]
94+
)
7095
assert output_error in str(result.exception)
7196

7297
# bad password
73-
result = runner.invoke(cli, ['--password', 'wrongpass',
74-
'--format', 'csv', '--output', outfile,
75-
'stream', infile])
98+
result = runner.invoke(
99+
cli,
100+
[
101+
"--password",
102+
"wrongpass",
103+
"--format",
104+
"csv",
105+
"--output",
106+
outfile,
107+
"stream",
108+
infile,
109+
],
110+
)
76111
assert output_error in str(result.exception)
77112

78113

79114
def test_cli_output_format():
80115
with TemporaryDirectory() as tempdir:
81-
infile = os.path.join(testdir, 'health.pdf')
82-
outfile = os.path.join(tempdir, 'health.{}')
116+
infile = os.path.join(testdir, "health.pdf")
117+
outfile = os.path.join(tempdir, "health.{}")
83118
runner = CliRunner()
84119

85120
# json
86-
result = runner.invoke(cli, ['--format', 'json', '--output', outfile.format('json'),
87-
'stream', infile])
121+
result = runner.invoke(
122+
cli,
123+
["--format", "json", "--output", outfile.format("json"), "stream", infile],
124+
)
88125
assert result.exit_code == 0
89126

90127
# excel
91-
result = runner.invoke(cli, ['--format', 'excel', '--output', outfile.format('xlsx'),
92-
'stream', infile])
128+
result = runner.invoke(
129+
cli,
130+
["--format", "excel", "--output", outfile.format("xlsx"), "stream", infile],
131+
)
93132
assert result.exit_code == 0
94133

95134
# html
96-
result = runner.invoke(cli, ['--format', 'html', '--output', outfile.format('html'),
97-
'stream', infile])
135+
result = runner.invoke(
136+
cli,
137+
["--format", "html", "--output", outfile.format("html"), "stream", infile],
138+
)
98139
assert result.exit_code == 0
99140

100141
# zip
101-
result = runner.invoke(cli, ['--zip', '--format', 'csv', '--output', outfile.format('csv'),
102-
'stream', infile])
142+
result = runner.invoke(
143+
cli,
144+
[
145+
"--zip",
146+
"--format",
147+
"csv",
148+
"--output",
149+
outfile.format("csv"),
150+
"stream",
151+
infile,
152+
],
153+
)
103154
assert result.exit_code == 0
104155

156+
105157
def test_cli_quiet():
106158
with TemporaryDirectory() as tempdir:
107-
infile = os.path.join(testdir, 'blank.pdf')
108-
outfile = os.path.join(tempdir, 'blank.csv')
159+
infile = os.path.join(testdir, "blank.pdf")
160+
outfile = os.path.join(tempdir, "blank.csv")
109161
runner = CliRunner()
110162

111-
result = runner.invoke(cli, ['--format', 'csv', '--output', outfile,
112-
'stream', infile])
113-
assert 'No tables found on page-1' in result.output
163+
result = runner.invoke(
164+
cli, ["--format", "csv", "--output", outfile, "stream", infile]
165+
)
166+
assert "No tables found on page-1" in result.output
114167

115-
result = runner.invoke(cli, ['--quiet', '--format', 'csv',
116-
'--output', outfile, 'stream', infile])
117-
assert 'No tables found on page-1' not in result.output
168+
result = runner.invoke(
169+
cli, ["--quiet", "--format", "csv", "--output", outfile, "stream", infile]
170+
)
171+
assert "No tables found on page-1" not in result.output

0 commit comments

Comments
 (0)