Skip to content

Commit

Permalink
refactor: Update imports in commit_msg_generator.py
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
25077667 committed May 22, 2024
1 parent 4e31755 commit 770cee8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/commit_msg_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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"]
Expand Down
6 changes: 3 additions & 3 deletions src/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'")
Expand Down Expand Up @@ -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)


Expand Down
12 changes: 6 additions & 6 deletions src/user_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 770cee8

Please sign in to comment.