summaryrefslogtreecommitdiff
path: root/src/otp2289/__main__.py
diff options
context:
space:
mode:
authorSimeon Simeonov2026-04-28 14:27:05 +0200
committerSimeon Simeonov2026-04-28 14:27:05 +0200
commit751c74e7de1c78a151fdd8b76f220d8411a2108a (patch)
tree0b13345aad6949a3325ca8f72b4eece14a0fe973 /src/otp2289/__main__.py
parent82ef6adec6e59f9cc9dc9fa1a13a2b43e534bada (diff)
Restructure the entire project, enforce linting and add support for type checkers
Diffstat (limited to 'src/otp2289/__main__.py')
-rw-r--r--src/otp2289/__main__.py22
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
36import errno 36import errno
37import getpass 37import getpass
38import os 38import os
39import pathlib
39import secrets 40import secrets
40import string 41import string
41import sys 42import sys
@@ -43,9 +44,11 @@ import sys
43import otp2289 44import otp2289
44 45
45 46
46def eprint(*arg, **kwargs): 47def 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
51def generate_otp_response(args: argparse.Namespace) -> str: 54def 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
216def main(args=None): 219def 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))