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

Replace eval by getattr #84

Merged
merged 4 commits into from
Dec 13, 2020
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
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ tnefparse 1.4.0 (unreleased)
- introduce using type annotations (jugmac00)
- remove deprecated parseFile & raw_mapi functions
- fix str representation for TNEF class (jugmac00)
- prefer `getattr` over `eval` (eumiro)

tnefparse 1.3.1 (2020-09-30)
=============================
Expand Down
27 changes: 27 additions & 0 deletions tests/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import io
import zipfile
import json
import logging
import shutil
import sys
import tempfile
Expand Down Expand Up @@ -115,3 +116,29 @@ def test_print_overview(script_runner):
assert "Attachments" in ret.stdout
assert "Objects" in ret.stdout
assert "Properties" in ret.stdout


def test_cmdline_logging_info(caplog, capsys):
"""Logging level set to INFO returns some INFO log messages"""
retv = tnefparse.cmdline.tnefparse(('-l', 'INFO', '-rb', 'tests/examples/rtf.tnef'))
assert not retv
stdout, _ = capsys.readouterr()
assert len(stdout) == 593
assert caplog.record_tuples == [
('tnefparse', logging.INFO, 'Skipping checksum for performance'),
]


def test_cmdline_logging_warn(caplog, capsys):
"""Logging level set to WARN returns no INFO log messages"""
retv = tnefparse.cmdline.tnefparse(('-l', 'WARN', '-rb', 'tests/examples/rtf.tnef'))
assert not retv
stdout, _ = capsys.readouterr()
assert len(stdout) == 593
assert caplog.record_tuples == []


def test_cmdline_logging_illegal():
"""Logging level set to ILLEGAL is illegal"""
with pytest.raises(SystemExit):
tnefparse.cmdline.tnefparse(('-l', 'ILLEGAL', '-rb', 'tests/examples/rtf.tnef'))
jugmac00 marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 7 additions & 5 deletions tnefparse/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,20 @@
help="extract a json dump of the tnef contents")


def tnefparse() -> None:
def tnefparse(argv=None) -> None:
eumiro marked this conversation as resolved.
Show resolved Hide resolved
"command-line script"

if len(sys.argv) == 1:
if argv is None:
argv = sys.argv[1:]

if not argv:
parser.print_help()
sys.exit(1)

args = parser.parse_args()
args = parser.parse_args(argv)

if args.logging:
level = eval("logging." + args.logging)
logger.setLevel(level)
logger.setLevel(getattr(logging, args.logging))

eumiro marked this conversation as resolved.
Show resolved Hide resolved
for tfp in args.file[0]:
try:
Expand Down