mirror of
https://github.com/not-lucky/GeminiKeyManagement.git
synced 2025-12-06 08:44:01 +05:30
refactor: enhance code structure and add Terms of Service handling for API interactions
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
"""This module provides utility functions for logging, file handling, and string generation."""
|
||||
"""
|
||||
Utility functions for the Gemini Key Management script.
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
@@ -9,7 +11,7 @@ from colorama import Fore, Style, init
|
||||
from . import config
|
||||
|
||||
class ColoredFormatter(logging.Formatter):
|
||||
"""A logging formatter that adds color to console output for different log levels."""
|
||||
"""A custom logging formatter that adds color to console output."""
|
||||
|
||||
LOG_COLORS = {
|
||||
logging.DEBUG: Fore.CYAN,
|
||||
@@ -20,11 +22,11 @@ class ColoredFormatter(logging.Formatter):
|
||||
}
|
||||
|
||||
def format(self, record):
|
||||
"""Applies color to the formatted log message."""
|
||||
"""Formats the log record with appropriate colors."""
|
||||
color = self.LOG_COLORS.get(record.levelno)
|
||||
message = super().format(record)
|
||||
if color:
|
||||
# For better readability, only color the message part of the log string.
|
||||
# Only color the message part for readability
|
||||
parts = message.split(" - ", 2)
|
||||
if len(parts) > 2:
|
||||
parts[2] = color + parts[2] + Style.RESET_ALL
|
||||
@@ -34,11 +36,8 @@ class ColoredFormatter(logging.Formatter):
|
||||
return message
|
||||
|
||||
def setup_logging():
|
||||
"""
|
||||
Configures the root logger to output to both a timestamped file and the console.
|
||||
Console output is colorized for readability.
|
||||
"""
|
||||
init(autoreset=True) # Required for colorama on Windows
|
||||
"""Sets up logging to both console and a file, with colors for the console."""
|
||||
init(autoreset=True) # Initialize Colorama
|
||||
|
||||
if not os.path.exists(config.LOG_DIR):
|
||||
os.makedirs(config.LOG_DIR)
|
||||
@@ -49,11 +48,11 @@ def setup_logging():
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
# Avoids duplicate log messages if the function is called multiple times.
|
||||
# Clear existing handlers to avoid duplicate logs
|
||||
if logger.hasHandlers():
|
||||
logger.handlers.clear()
|
||||
|
||||
# The file handler logs detailed, non-colored messages.
|
||||
# File handler for detailed, non-colored logging
|
||||
file_handler = logging.FileHandler(log_filepath, encoding='utf-8')
|
||||
file_formatter = logging.Formatter(
|
||||
"%(asctime)s - %(levelname)s - [%(name)s:%(module)s:%(lineno)d] - %(message)s"
|
||||
@@ -61,7 +60,7 @@ def setup_logging():
|
||||
file_handler.setFormatter(file_formatter)
|
||||
logger.addHandler(file_handler)
|
||||
|
||||
# The console handler logs concise, colored messages.
|
||||
# Console handler for concise, colored logging
|
||||
console_handler = logging.StreamHandler(sys.stdout)
|
||||
console_formatter = ColoredFormatter("%(asctime)s - %(levelname)s - %(message)s")
|
||||
console_handler.setFormatter(console_formatter)
|
||||
@@ -70,18 +69,16 @@ def setup_logging():
|
||||
logging.info(f"Logging initialized. Log file: {log_filepath}")
|
||||
|
||||
def load_emails_from_file(filename):
|
||||
"""
|
||||
Reads a list of email addresses from a text file.
|
||||
It ignores empty lines and lines that start with a '#' comment character.
|
||||
"""
|
||||
"""Loads a list of emails from a text file, ignoring comments."""
|
||||
if not os.path.exists(filename):
|
||||
logging.error(f"Email file not found at '{filename}'")
|
||||
logging.info("Please create it and add one email address per line.")
|
||||
return []
|
||||
with open(filename, "r") as f:
|
||||
# Ignore empty lines and lines starting with #
|
||||
return [line.strip() for line in f if line.strip() and not line.startswith("#")]
|
||||
|
||||
def generate_random_string(length=10):
|
||||
"""Generates a random alphanumeric string for creating unique project IDs."""
|
||||
"""Generates a random alphanumeric string of a given length."""
|
||||
letters_and_digits = string.ascii_lowercase + string.digits
|
||||
return ''.join(random.choice(letters_and_digits) for i in range(length))
|
||||
|
||||
Reference in New Issue
Block a user