style: Format code according to linter rules

This commit is contained in:
Paul Gauthier (aider) 2025-03-21 08:52:04 -07:00
parent 8c78e09f03
commit aad6c63206

View file

@ -29,49 +29,50 @@ def get_downloads_from_bigquery(credentials_path=None, package_name="aider-chat"
credentials = None credentials = None
if credentials_path: if credentials_path:
credentials = service_account.Credentials.from_service_account_file( credentials = service_account.Credentials.from_service_account_file(
credentials_path, credentials_path, scopes=["https://www.googleapis.com/auth/cloud-platform"]
scopes=["https://www.googleapis.com/auth/cloud-platform"]
) )
# Create a client # Create a client
client = bigquery.Client(credentials=credentials) client = bigquery.Client(credentials=credentials)
# Query to get total downloads for the package # Query to get total downloads for the package
query = f""" query = f"""
SELECT COUNT(*) as total_downloads SELECT COUNT(*) as total_downloads
FROM `bigquery-public-data.pypi.file_downloads` FROM `bigquery-public-data.pypi.file_downloads`
WHERE file.project = '{package_name}' WHERE file.project = '{package_name}'
""" """
# Execute the query # Execute the query
query_job = client.query(query) query_job = client.query(query)
results = query_job.result() results = query_job.result()
# Get the first (and only) row # Get the first (and only) row
for row in results: for row in results:
return row.total_downloads return row.total_downloads
return 0 return 0
except Exception as e: except Exception as e:
print(f"Error fetching download statistics from BigQuery: {e}", file=sys.stderr) print(f"Error fetching download statistics from BigQuery: {e}", file=sys.stderr)
return None return None
def get_total_downloads(api_key=None, package_name="aider-chat", use_bigquery=False, credentials_path=None): def get_total_downloads(
api_key=None, package_name="aider-chat", use_bigquery=False, credentials_path=None
):
""" """
Fetch total downloads for a Python package Fetch total downloads for a Python package
If use_bigquery is True, fetches from BigQuery. If use_bigquery is True, fetches from BigQuery.
Otherwise uses pepy.tech API (requires api_key). Otherwise uses pepy.tech API (requires api_key).
""" """
if use_bigquery: if use_bigquery:
return get_downloads_from_bigquery(credentials_path, package_name) return get_downloads_from_bigquery(credentials_path, package_name)
# Fall back to pepy.tech API # Fall back to pepy.tech API
if not api_key: if not api_key:
print("API key not provided for pepy.tech", file=sys.stderr) print("API key not provided for pepy.tech", file=sys.stderr)
sys.exit(1) sys.exit(1)
url = f"https://api.pepy.tech/api/v2/projects/{package_name}" url = f"https://api.pepy.tech/api/v2/projects/{package_name}"
headers = {"X-API-Key": api_key} headers = {"X-API-Key": api_key}
@ -209,14 +210,17 @@ def get_badges_md():
# Check if we should use BigQuery # Check if we should use BigQuery
use_bigquery = os.environ.get("USE_BIGQUERY", "false").lower() in ("true", "1", "yes") use_bigquery = os.environ.get("USE_BIGQUERY", "false").lower() in ("true", "1", "yes")
credentials_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS") credentials_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
# Get API key from environment variable if not using BigQuery # Get API key from environment variable if not using BigQuery
api_key = None api_key = None
if not use_bigquery: if not use_bigquery:
api_key = os.environ.get("PEPY_API_KEY") api_key = os.environ.get("PEPY_API_KEY")
if not api_key: if not api_key:
print( print(
"API key not provided and BigQuery not enabled. Please set PEPY_API_KEY environment variable", (
"API key not provided and BigQuery not enabled. Please set PEPY_API_KEY"
" environment variable"
),
file=sys.stderr, file=sys.stderr,
) )
sys.exit(1) sys.exit(1)
@ -244,14 +248,17 @@ def get_badges_html():
# Check if we should use BigQuery # Check if we should use BigQuery
use_bigquery = os.environ.get("USE_BIGQUERY", "false").lower() in ("true", "1", "yes") use_bigquery = os.environ.get("USE_BIGQUERY", "false").lower() in ("true", "1", "yes")
credentials_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS") credentials_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
# Get API key from environment variable if not using BigQuery # Get API key from environment variable if not using BigQuery
api_key = None api_key = None
if not use_bigquery: if not use_bigquery:
api_key = os.environ.get("PEPY_API_KEY") api_key = os.environ.get("PEPY_API_KEY")
if not api_key: if not api_key:
print( print(
"API key not provided and BigQuery not enabled. Please set PEPY_API_KEY environment variable", (
"API key not provided and BigQuery not enabled. Please set PEPY_API_KEY"
" environment variable"
),
file=sys.stderr, file=sys.stderr,
) )
sys.exit(1) sys.exit(1)
@ -327,19 +334,23 @@ def main():
) )
parser.add_argument("--markdown", action="store_true", help="Generate markdown badges block") parser.add_argument("--markdown", action="store_true", help="Generate markdown badges block")
parser.add_argument( parser.add_argument(
"--use-bigquery", action="store_true", "--use-bigquery",
help="Use BigQuery to fetch download statistics instead of pepy.tech" action="store_true",
help="Use BigQuery to fetch download statistics instead of pepy.tech",
) )
parser.add_argument( parser.add_argument(
"--credentials-path", "--credentials-path", help="Path to Google Cloud service account credentials JSON file"
help="Path to Google Cloud service account credentials JSON file"
) )
args = parser.parse_args() args = parser.parse_args()
# Determine whether to use BigQuery # Determine whether to use BigQuery
use_bigquery = args.use_bigquery or os.environ.get("USE_BIGQUERY", "false").lower() in ("true", "1", "yes") use_bigquery = args.use_bigquery or os.environ.get("USE_BIGQUERY", "false").lower() in (
"true",
"1",
"yes",
)
credentials_path = args.credentials_path or os.environ.get("GOOGLE_APPLICATION_CREDENTIALS") credentials_path = args.credentials_path or os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
# Check for required parameters # Check for required parameters
api_key = None api_key = None
if not use_bigquery: if not use_bigquery:
@ -347,15 +358,19 @@ def main():
api_key = args.api_key or os.environ.get("PEPY_API_KEY") api_key = args.api_key or os.environ.get("PEPY_API_KEY")
if not api_key: if not api_key:
print( print(
"API key not provided and BigQuery not enabled. Please set PEPY_API_KEY environment variable, " (
"use --api-key, or enable BigQuery with --use-bigquery", "API key not provided and BigQuery not enabled. Please set PEPY_API_KEY"
" environment variable, use --api-key, or enable BigQuery with --use-bigquery"
),
file=sys.stderr, file=sys.stderr,
) )
sys.exit(1) sys.exit(1)
elif use_bigquery and not credentials_path and not args.credentials_path: elif use_bigquery and not credentials_path and not args.credentials_path:
print( print(
"BigQuery enabled but no credentials provided. Please set GOOGLE_APPLICATION_CREDENTIALS " (
"environment variable or use --credentials-path", "BigQuery enabled but no credentials provided. Please set"
" GOOGLE_APPLICATION_CREDENTIALS environment variable or use --credentials-path"
),
file=sys.stderr, file=sys.stderr,
) )
# Continue execution - BigQuery might work without explicit credentials in some environments # Continue execution - BigQuery might work without explicit credentials in some environments