diff options
Diffstat (limited to 'src/otp2289/__main__.py')
| -rw-r--r-- | src/otp2289/__main__.py | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/src/otp2289/__main__.py b/src/otp2289/__main__.py index 9f1aab8..0fdcc52 100644 --- a/src/otp2289/__main__.py +++ b/src/otp2289/__main__.py | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | # SPDX-License-Identifier: BSD-2-Clause-FreeBSD | 1 | # SPDX-License-Identifier: BSD-2-Clause |
| 2 | # | 2 | # |
| 3 | # Copyright (c) 2020-2025 Simeon Simeonov | 3 | # Copyright (c) 2020-2026 Simeon Simeonov |
| 4 | # All rights reserved. | 4 | # All rights reserved. |
| 5 | # | 5 | # |
| 6 | # Redistribution and use in source and binary forms, with or without | 6 | # Redistribution and use in source and binary forms, with or without |
| @@ -36,6 +36,7 @@ import argparse | |||
| 36 | import errno | 36 | import errno |
| 37 | import getpass | 37 | import getpass |
| 38 | import os | 38 | import os |
| 39 | import pathlib | ||
| 39 | import secrets | 40 | import secrets |
| 40 | import string | 41 | import string |
| 41 | import sys | 42 | import sys |
| @@ -43,9 +44,11 @@ import sys | |||
| 43 | import otp2289 | 44 | import otp2289 |
| 44 | 45 | ||
| 45 | 46 | ||
| 46 | def eprint(*arg, **kwargs): | 47 | def eprint( |
| 48 | *value: object, sep: str | None = ' ', end: str | None = '\n' | ||
| 49 | ) -> None: | ||
| 47 | """stdderr print wrapper""" | 50 | """stdderr print wrapper""" |
| 48 | print(*arg, file=sys.stderr, flush=True, **kwargs) | 51 | print(*value, sep=sep, end=end, file=sys.stderr, flush=True) |
| 49 | 52 | ||
| 50 | 53 | ||
| 51 | def generate_otp_response(args: argparse.Namespace) -> str: | 54 | def generate_otp_response(args: argparse.Namespace) -> str: |
| @@ -162,8 +165,8 @@ def get_password(args: argparse.Namespace) -> str: | |||
| 162 | eprint('The passwords do not match') | 165 | eprint('The passwords do not match') |
| 163 | return password | 166 | return password |
| 164 | 167 | ||
| 165 | if os.path.isfile(args.password): | 168 | if pathlib.Path(args.password).is_file(): |
| 166 | with open(args.password, encoding='utf-8') as fp: | 169 | with pathlib.Path(args.password).open(encoding='utf-8') as fp: |
| 167 | return fp.readline().strip() | 170 | return fp.readline().strip() |
| 168 | 171 | ||
| 169 | return args.password | 172 | return args.password |
| @@ -213,7 +216,7 @@ def initiate_new_sequence(args: argparse.Namespace) -> str: | |||
| 213 | return header + generator.generate_otp_hexdigest(args.step) | 216 | return header + generator.generate_otp_hexdigest(args.step) |
| 214 | 217 | ||
| 215 | 218 | ||
| 216 | def main(args=None): | 219 | def main(inargs: list[str] | None = None) -> None: |
| 217 | """the main entry point""" | 220 | """the main entry point""" |
| 218 | parser = argparse.ArgumentParser( | 221 | parser = argparse.ArgumentParser( |
| 219 | prog=__package__, | 222 | prog=__package__, |
| @@ -344,8 +347,10 @@ def main(args=None): | |||
| 344 | version=f'%(prog)s {otp2289.__version__}', | 347 | version=f'%(prog)s {otp2289.__version__}', |
| 345 | help='display program-version and exit', | 348 | help='display program-version and exit', |
| 346 | ) | 349 | ) |
| 347 | args = parser.parse_args(args) | 350 | |
| 351 | args = parser.parse_args(inargs) | ||
| 348 | # handle the password before everything else | 352 | # handle the password before everything else |
| 353 | |||
| 349 | try: | 354 | try: |
| 350 | args.password = get_password(args) | 355 | args.password = get_password(args) |
| 351 | except KeyboardInterrupt: | 356 | except KeyboardInterrupt: |
| @@ -354,6 +359,7 @@ def main(args=None): | |||
| 354 | except Exception as exp: | 359 | except Exception as exp: |
| 355 | eprint(f'Unable to fetch password: {exp}') | 360 | eprint(f'Unable to fetch password: {exp}') |
| 356 | sys.exit(1) | 361 | sys.exit(1) |
| 362 | |||
| 357 | try: | 363 | try: |
| 358 | if args.initiate_new_sequence: | 364 | if args.initiate_new_sequence: |
| 359 | print(initiate_new_sequence(args)) | 365 | print(initiate_new_sequence(args)) |
