mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(generator): scaffold + move widgets/themes/models/utils/processor/config
- Add generator/requirements.txt and generator/pytest.ini - git mv src/widgets, themes, models, utils, data/processor.py, config.py into generator/src/ - Fix relative imports in processor.py (moved up one level: .. → .) - Append service-specific env vars (PORT, DB paths, FETCHER_URL, etc.) to generator/src/config.py - git rm retired src/db, src/data/__init__.py, src/__init__.py Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
cb3ace8dcd
commit
3525144aeb
@@ -0,0 +1,5 @@
|
|||||||
|
[pytest]
|
||||||
|
testpaths = tests
|
||||||
|
python_files = test_*.py
|
||||||
|
pythonpath = .
|
||||||
|
addopts = -ra --strict-markers
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
flask==3.0.3
|
||||||
|
flask-caching==2.3.0
|
||||||
|
flask-compress==1.15
|
||||||
|
requests==2.32.3
|
||||||
|
pytest==8.3.3
|
||||||
|
responses==0.25.3
|
||||||
|
gunicorn==23.0.0
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
"""GitHub Profile Widget Generator package."""
|
|
||||||
|
|
||||||
__version__ = "1.0.0"
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
"""Data fetching and processing for GitHub profiles.
|
|
||||||
|
|
||||||
Note: ``fetch_github_data`` is intentionally NOT re-exported from this package
|
|
||||||
so that importing :mod:`src.data.processor` (used by the generator service)
|
|
||||||
does not transitively pull in the fetcher module and its ``requests`` dependency.
|
|
||||||
Import it directly from ``src.data.fetcher`` when you need it.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from .processor import (
|
|
||||||
compute_grade,
|
|
||||||
compute_impact_timeline,
|
|
||||||
compute_collaborators,
|
|
||||||
compute_focus,
|
|
||||||
compute_languages,
|
|
||||||
generate_widgets_from_github,
|
|
||||||
)
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
"compute_grade",
|
|
||||||
"compute_impact_timeline",
|
|
||||||
"compute_collaborators",
|
|
||||||
"compute_focus",
|
|
||||||
"compute_languages",
|
|
||||||
"generate_widgets_from_github",
|
|
||||||
]
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
from .repository import (
|
|
||||||
DB_PATH,
|
|
||||||
DUMMY_USERNAME,
|
|
||||||
init_db,
|
|
||||||
upsert_user,
|
|
||||||
get_user,
|
|
||||||
delete_user,
|
|
||||||
list_users,
|
|
||||||
seed_dummy_user,
|
|
||||||
REQUIRED_FIELDS,
|
|
||||||
)
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
"DB_PATH",
|
|
||||||
"DUMMY_USERNAME",
|
|
||||||
"init_db",
|
|
||||||
"upsert_user",
|
|
||||||
"get_user",
|
|
||||||
"delete_user",
|
|
||||||
"list_users",
|
|
||||||
"seed_dummy_user",
|
|
||||||
"REQUIRED_FIELDS",
|
|
||||||
]
|
|
||||||
@@ -1,115 +0,0 @@
|
|||||||
"""Hand-written dummy user payload.
|
|
||||||
|
|
||||||
Shape matches what ``fetch_github_data`` returns, so the generator's processor
|
|
||||||
code runs unchanged against it. Used as the fallback whenever a requested user
|
|
||||||
is missing from the DB or has incomplete data.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
|
||||||
|
|
||||||
|
|
||||||
def _recent_iso(days_ago: int) -> str:
|
|
||||||
return (datetime.utcnow() - timedelta(days=days_ago)).strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
||||||
|
|
||||||
|
|
||||||
def _daily_commits() -> list[dict]:
|
|
||||||
out = []
|
|
||||||
for i in range(0, 180, 2):
|
|
||||||
date = (datetime.utcnow() - timedelta(days=i)).strftime("%Y-%m-%d")
|
|
||||||
out.append({"date": date, "count": 3 + (i % 5)})
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
DUMMY_PAYLOAD: dict = {
|
|
||||||
"user": {
|
|
||||||
"login": "octocat",
|
|
||||||
"name": "Demo Developer",
|
|
||||||
"followers": 120,
|
|
||||||
"following": 40,
|
|
||||||
"public_repos": 24,
|
|
||||||
"avatar_url": "",
|
|
||||||
"created_at": "2018-01-15T00:00:00Z",
|
|
||||||
},
|
|
||||||
"repos": [
|
|
||||||
{
|
|
||||||
"id": 1, "name": "stellar-api", "full_name": "octocat/stellar-api",
|
|
||||||
"language": "Python", "stargazers_count": 310, "forks_count": 42,
|
|
||||||
"pushed_at": _recent_iso(3), "topics": ["backend", "api", "fastapi"],
|
|
||||||
"size": 2400,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 2, "name": "pixel-ui", "full_name": "octocat/pixel-ui",
|
|
||||||
"language": "TypeScript", "stargazers_count": 180, "forks_count": 19,
|
|
||||||
"pushed_at": _recent_iso(6), "topics": ["frontend", "react", "design-system"],
|
|
||||||
"size": 1800,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 3, "name": "nimbus-infra", "full_name": "octocat/nimbus-infra",
|
|
||||||
"language": "Go", "stargazers_count": 95, "forks_count": 12,
|
|
||||||
"pushed_at": _recent_iso(10), "topics": ["devops", "kubernetes", "terraform"],
|
|
||||||
"size": 1100,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 4, "name": "ml-playground", "full_name": "octocat/ml-playground",
|
|
||||||
"language": "Jupyter Notebook", "stargazers_count": 64, "forks_count": 8,
|
|
||||||
"pushed_at": _recent_iso(14), "topics": ["ml", "pytorch"],
|
|
||||||
"size": 900,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 5, "name": "dotfiles", "full_name": "octocat/dotfiles",
|
|
||||||
"language": "Shell", "stargazers_count": 22, "forks_count": 3,
|
|
||||||
"pushed_at": _recent_iso(20), "topics": [], "size": 120,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 6, "name": "rustle", "full_name": "octocat/rustle",
|
|
||||||
"language": "Rust", "stargazers_count": 48, "forks_count": 5,
|
|
||||||
"pushed_at": _recent_iso(30), "topics": ["systems", "cli"], "size": 450,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 7, "name": "web-playground", "full_name": "octocat/web-playground",
|
|
||||||
"language": "JavaScript", "stargazers_count": 30, "forks_count": 4,
|
|
||||||
"pushed_at": _recent_iso(40), "topics": ["frontend"], "size": 380,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": 8, "name": "notes-app", "full_name": "octocat/notes-app",
|
|
||||||
"language": "TypeScript", "stargazers_count": 15, "forks_count": 2,
|
|
||||||
"pushed_at": _recent_iso(55), "topics": ["frontend"], "size": 260,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
"events": [
|
|
||||||
{"type": "PushEvent", "created_at": _recent_iso(1),
|
|
||||||
"actor": {"login": "octocat"}, "repo": {"name": "octocat/stellar-api"},
|
|
||||||
"payload": {"commits": [{"sha": "abc"}, {"sha": "def"}]}},
|
|
||||||
{"type": "PullRequestEvent", "created_at": _recent_iso(2),
|
|
||||||
"actor": {"login": "octocat"}, "repo": {"name": "octocat/pixel-ui"},
|
|
||||||
"payload": {}},
|
|
||||||
{"type": "PushEvent", "created_at": _recent_iso(4),
|
|
||||||
"actor": {"login": "octocat"}, "repo": {"name": "octocat/nimbus-infra"},
|
|
||||||
"payload": {"commits": [{"sha": "ghi"}]}},
|
|
||||||
],
|
|
||||||
"commits": _daily_commits(),
|
|
||||||
"total_commits": 2450,
|
|
||||||
"recent_commits": 680,
|
|
||||||
"total_prs": 180,
|
|
||||||
"collaborators_data": [
|
|
||||||
{
|
|
||||||
"login": "alice-dev", "avatar_url": "",
|
|
||||||
"raw_score": 220.0, "shared_repos": 3, "final_score": 660.0,
|
|
||||||
"repos": ["octocat/stellar-api", "octocat/pixel-ui", "octocat/nimbus-infra"],
|
|
||||||
"tight_owned_partner": True,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"login": "bob-builder", "avatar_url": "",
|
|
||||||
"raw_score": 140.0, "shared_repos": 2, "final_score": 280.0,
|
|
||||||
"repos": ["octocat/stellar-api", "octocat/ml-playground"],
|
|
||||||
"tight_owned_partner": False,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"login": "carol-codes", "avatar_url": "",
|
|
||||||
"raw_score": 95.0, "shared_repos": 2, "final_score": 190.0,
|
|
||||||
"repos": ["octocat/pixel-ui", "octocat/web-playground"],
|
|
||||||
"tight_owned_partner": False,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
"avatar_b64": "",
|
|
||||||
}
|
|
||||||
@@ -1,135 +0,0 @@
|
|||||||
"""SQLite repository: the only communication channel between fetcher and generator.
|
|
||||||
|
|
||||||
Stores each user's fetched GitHub payload as a JSON blob in a single table.
|
|
||||||
The generator reads from here; the fetcher writes to here. Neither imports the
|
|
||||||
other.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
import sqlite3
|
|
||||||
from datetime import datetime
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
_PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
|
||||||
DB_PATH = os.environ.get(
|
|
||||||
"GHSTATS_DB_PATH",
|
|
||||||
os.path.join(_PROJECT_ROOT, "data", "ghstats.db"),
|
|
||||||
)
|
|
||||||
|
|
||||||
DUMMY_USERNAME = "__dummy__"
|
|
||||||
|
|
||||||
# Fields the generator considers load-bearing. If any is missing from a row,
|
|
||||||
# the generator treats the row as unusable and falls back to the dummy user.
|
|
||||||
REQUIRED_FIELDS = (
|
|
||||||
"user",
|
|
||||||
"repos",
|
|
||||||
"events",
|
|
||||||
"commits",
|
|
||||||
"total_commits",
|
|
||||||
"recent_commits",
|
|
||||||
"total_prs",
|
|
||||||
"collaborators_data",
|
|
||||||
"avatar_b64",
|
|
||||||
)
|
|
||||||
|
|
||||||
SCHEMA_SQL = """
|
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
|
||||||
username TEXT PRIMARY KEY,
|
|
||||||
data_json TEXT NOT NULL,
|
|
||||||
is_dummy INTEGER NOT NULL DEFAULT 0,
|
|
||||||
updated_at TEXT NOT NULL
|
|
||||||
);
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def _connect() -> sqlite3.Connection:
|
|
||||||
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
|
||||||
conn = sqlite3.connect(DB_PATH)
|
|
||||||
conn.row_factory = sqlite3.Row
|
|
||||||
return conn
|
|
||||||
|
|
||||||
|
|
||||||
def init_db() -> None:
|
|
||||||
"""Create the schema and seed the dummy user if missing."""
|
|
||||||
with _connect() as conn:
|
|
||||||
conn.executescript(SCHEMA_SQL)
|
|
||||||
conn.commit()
|
|
||||||
seed_dummy_user(conn=conn)
|
|
||||||
|
|
||||||
|
|
||||||
def upsert_user(username: str, data: dict, conn: Optional[sqlite3.Connection] = None) -> None:
|
|
||||||
payload = json.dumps(data)
|
|
||||||
now = datetime.utcnow().isoformat(timespec="seconds") + "Z"
|
|
||||||
sql = """
|
|
||||||
INSERT INTO users (username, data_json, is_dummy, updated_at)
|
|
||||||
VALUES (?, ?, 0, ?)
|
|
||||||
ON CONFLICT(username) DO UPDATE SET
|
|
||||||
data_json = excluded.data_json,
|
|
||||||
is_dummy = 0,
|
|
||||||
updated_at = excluded.updated_at
|
|
||||||
"""
|
|
||||||
if conn is None:
|
|
||||||
with _connect() as c:
|
|
||||||
c.execute(sql, (username, payload, now))
|
|
||||||
c.commit()
|
|
||||||
else:
|
|
||||||
conn.execute(sql, (username, payload, now))
|
|
||||||
conn.commit()
|
|
||||||
|
|
||||||
|
|
||||||
def get_user(username: str) -> Optional[dict]:
|
|
||||||
"""Return the stored GitHub-data dict for a user, or None if not present."""
|
|
||||||
with _connect() as conn:
|
|
||||||
row = conn.execute(
|
|
||||||
"SELECT data_json FROM users WHERE username = ?",
|
|
||||||
(username,),
|
|
||||||
).fetchone()
|
|
||||||
if row is None:
|
|
||||||
return None
|
|
||||||
return json.loads(row["data_json"])
|
|
||||||
|
|
||||||
|
|
||||||
def delete_user(username: str) -> bool:
|
|
||||||
if username == DUMMY_USERNAME:
|
|
||||||
return False
|
|
||||||
with _connect() as conn:
|
|
||||||
cur = conn.execute("DELETE FROM users WHERE username = ?", (username,))
|
|
||||||
conn.commit()
|
|
||||||
return cur.rowcount > 0
|
|
||||||
|
|
||||||
|
|
||||||
def list_users() -> list[dict]:
|
|
||||||
with _connect() as conn:
|
|
||||||
rows = conn.execute(
|
|
||||||
"SELECT username, is_dummy, updated_at FROM users ORDER BY username"
|
|
||||||
).fetchall()
|
|
||||||
return [dict(r) for r in rows]
|
|
||||||
|
|
||||||
|
|
||||||
def seed_dummy_user(conn: Optional[sqlite3.Connection] = None) -> None:
|
|
||||||
"""Insert (or replace) the __dummy__ row using an explicit hand-written payload.
|
|
||||||
|
|
||||||
This is the fallback the generator uses whenever a requested user is missing
|
|
||||||
or has incomplete data. Kept deliberately plausible so every widget has
|
|
||||||
something to render.
|
|
||||||
"""
|
|
||||||
from .dummy_seed import DUMMY_PAYLOAD
|
|
||||||
|
|
||||||
payload = json.dumps(DUMMY_PAYLOAD)
|
|
||||||
now = datetime.utcnow().isoformat(timespec="seconds") + "Z"
|
|
||||||
sql = """
|
|
||||||
INSERT INTO users (username, data_json, is_dummy, updated_at)
|
|
||||||
VALUES (?, ?, 1, ?)
|
|
||||||
ON CONFLICT(username) DO UPDATE SET
|
|
||||||
data_json = excluded.data_json,
|
|
||||||
is_dummy = 1,
|
|
||||||
updated_at = excluded.updated_at
|
|
||||||
"""
|
|
||||||
if conn is None:
|
|
||||||
with _connect() as c:
|
|
||||||
c.execute(sql, (DUMMY_USERNAME, payload, now))
|
|
||||||
c.commit()
|
|
||||||
else:
|
|
||||||
conn.execute(sql, (DUMMY_USERNAME, payload, now))
|
|
||||||
conn.commit()
|
|
||||||
Reference in New Issue
Block a user