From 770cee88dfc9c69bd2ddade4e1f3c52e18f5ea59 Mon Sep 17 00:00:00 2001 From: scc Date: Thu, 23 May 2024 02:02:39 +0800 Subject: [PATCH] refactor: Update imports in commit_msg_generator.py Import requests was moved to the top of the file for consistency and better readability. - Move import requests above import json for clarity and organization. --- src/commit_msg_generator.py | 9 +++++++-- src/git_utils.py | 6 +++--- src/user_interaction.py | 12 ++++++------ 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/commit_msg_generator.py b/src/commit_msg_generator.py index 1649d09..85c2ac6 100644 --- a/src/commit_msg_generator.py +++ b/src/commit_msg_generator.py @@ -7,8 +7,8 @@ from random import shuffle from typing import Generator import re -import requests import json +import requests from .config_parser import Config from .git_diff import git_diff @@ -23,6 +23,7 @@ class TokenManager: @classmethod def get_instance(cls, tokens: list[str]): + """Return the singleton instance.""" if cls._instance is None: cls._instance = cls(tokens) return cls._instance @@ -35,6 +36,7 @@ def __init__(self, tokens: list[str]): shuffle(self._access_pool) def pop_token(self): + """Return a token from the pool.""" if not self._access_pool: self._access_pool = list(self._constant_pool) shuffle(self._access_pool) @@ -68,7 +70,10 @@ def make_api_request(config: Config, messages: list[dict], token: str) -> dict: "presence_penalty": config["presence_penalty"], } # # Print as curl command for debugging purposes - # curl_line = f"""curl -X POST "{url}" -H "Authorization : Bearer {token}" -H "Content-Type: application/json" -d "{json.dumps(data)}" """ + # curl_line = f"""curl -X POST "{url}" \ + # -H "Authorization : Bearer {token}" \ + # -H "Content-Type: application/json" \ + # -d "{json.dumps(data)}" """ # print(curl_line) response = requests.post( url, headers=headers, data=json.dumps(data), timeout=config["timeout"] diff --git a/src/git_utils.py b/src/git_utils.py index ef45989..df67e3b 100644 --- a/src/git_utils.py +++ b/src/git_utils.py @@ -64,7 +64,7 @@ def check_git_config(repo: Repo): missing_configs.append("user.email") if missing_configs: - logger.warning(f"Missing Git configuration: {', '.join(missing_configs)}") + logger.warning("Missing Git configuration: %s", ", ".join(missing_configs)) print("Please set them using the following commands:") if "user.name" in missing_configs: print(" git config --global user.name 'Your Name'") @@ -94,8 +94,8 @@ def commit_full_text_message(repo_path: str, message: str) -> str: committer = Actor(author, author_email) return repo.index.commit(message=message, committer=committer).hexsha - except Exception as e: - logger.error(f"Failed to commit the message. Details: {e}") + except Exception as e: # pylint: disable=broad-except + logger.error("Failed to commit the message. Details: %s", e) sys.exit(1) diff --git a/src/user_interaction.py b/src/user_interaction.py index 27bedca..78b250f 100644 --- a/src/user_interaction.py +++ b/src/user_interaction.py @@ -20,10 +20,10 @@ def edit_message(message: str) -> str: Open the provided message in a text editor and return the edited message. """ editor = get_git_editor() - with tempfile.NamedTemporaryFile(suffix=".tmp") as tf: - tf.write(message.encode()) - tf.flush() - subprocess.call([editor, tf.name]) - tf.seek(0) - edited_message = tf.read().decode() + with tempfile.NamedTemporaryFile(suffix=".tmp") as temp_file: + temp_file.write(message.encode()) + temp_file.flush() + subprocess.call([editor, temp_file.name]) + temp_file.seek(0) + edited_message = temp_file.read().decode() return edited_message