From 9ed9c4d0f2ea0713c2cc811ff92f5c778755f8ed Mon Sep 17 00:00:00 2001 From: Yoshihiro OKUMURA Date: Tue, 23 May 2023 11:48:09 +0900 Subject: [PATCH] catch control-c action. --- mdrsclient/__main__.py | 2 ++ mdrsclient/config.py | 3 +++ mdrsclient/utils.py | 1 - 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/mdrsclient/__main__.py b/mdrsclient/__main__.py index ee712ce..7ccd188 100644 --- a/mdrsclient/__main__.py +++ b/mdrsclient/__main__.py @@ -50,6 +50,8 @@ def main() -> None: except MDRSException as e: print(f"Error: {e}") sys.exit(2) + except KeyboardInterrupt: + sys.exit(130) if __name__ == "__main__": diff --git a/mdrsclient/config.py b/mdrsclient/config.py index 282d543..5aa5abe 100644 --- a/mdrsclient/config.py +++ b/mdrsclient/config.py @@ -6,6 +6,7 @@ import validators from mdrsclient.exceptions import IllegalArgumentException from mdrsclient.settings import CONFIG_DIRNAME +from mdrsclient.utils import FileLock class ConfigFile: @@ -70,7 +71,9 @@ class ConfigFile: def __save(self) -> None: self.__ensure_cache_dir() with open(self.__config_path, "w") as f: + FileLock.lock(f) self.__config.write(f) + FileLock.unlock(f) os.chmod(self.__config_path, 0o600) def __ensure_cache_dir(self) -> None: diff --git a/mdrsclient/utils.py b/mdrsclient/utils.py index a002d30..b4ea263 100644 --- a/mdrsclient/utils.py +++ b/mdrsclient/utils.py @@ -3,7 +3,6 @@ from typing import IO, Any if os.name == "nt": import msvcrt - elif os.name == "posix": import fcntl