Skip to content

Commit

Permalink
Fix up try/import checks (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
jborean93 authored Mar 26, 2023
1 parent e3c1d10 commit 95d9878
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/spnego/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,22 @@
TlsSupportedGroup,
)

HAS_ARGCOMPLETE = False
try:
import argcomplete

HAS_ARGCOMPLETE = True
except ImportError: # pragma: nocover
argcomplete = None
pass


yaml: typing.Optional[typing.Any]
HAS_YAML = False
try:
from ruamel import yaml

HAS_YAML = True
except ImportError: # pragma: nocover
yaml = None
pass


def _parse_ntlm_version(
Expand Down Expand Up @@ -700,8 +706,8 @@ def main(args: typing.List[str]) -> None:
else:
token_info = parse_token(b_data, secret=parsed_args.secret, encoding=parsed_args.encoding)

if parsed_args.output_format == "yaml":
y = yaml.YAML() # type: ignore
if parsed_args.output_format == "yaml" and HAS_YAML:
y = yaml.YAML()
y.default_flow_style = False
y.dump(token_info, sys.stdout)
else:
Expand Down Expand Up @@ -755,12 +761,12 @@ def parse_args(args: typing.List[str]) -> argparse.Namespace:
"tokens to generate the session key.",
)

if argcomplete:
if HAS_ARGCOMPLETE:
argcomplete.autocomplete(parser)

parsed_args = parser.parse_args(args)

if parsed_args.output_format == "yaml" and not yaml:
if parsed_args.output_format == "yaml" and not HAS_YAML:
raise ValueError("Cannot output as yaml as ruamel.yaml is not installed.")

return parsed_args
Expand Down

0 comments on commit 95d9878

Please sign in to comment.