-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathinsiders.py
More file actions
173 lines (140 loc) · 5.9 KB
/
insiders.py
File metadata and controls
173 lines (140 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Functions related to Insiders funding goals.
from __future__ import annotations
import json
import logging
import os
import posixpath
from dataclasses import dataclass
from datetime import date, datetime, timedelta
from itertools import chain
from pathlib import Path
from typing import TYPE_CHECKING, cast
from urllib.error import HTTPError
from urllib.parse import urljoin
from urllib.request import urlopen
import yaml
if TYPE_CHECKING:
from collections.abc import Iterable
logger = logging.getLogger(f"mkdocs.logs.{__name__}")
def human_readable_amount(amount: int) -> str:
str_amount = str(amount)
if len(str_amount) >= 4: # noqa: PLR2004
return f"{str_amount[: len(str_amount) - 3]},{str_amount[-3:]}"
return str_amount
@dataclass
class Project:
name: str
url: str
@dataclass
class Feature:
name: str
ref: str | None
since: date | None
project: Project | None
def url(self, rel_base: str = "..") -> str | None: # noqa: D102
if not self.ref:
return None
if self.project:
rel_base = self.project.url
return posixpath.join(rel_base, self.ref.lstrip("/"))
def render(self, rel_base: str = "..", *, badge: bool = False) -> None: # noqa: D102
new = ""
if badge:
recent = self.since and date.today() - self.since <= timedelta(days=60) # noqa: DTZ011
if recent:
ft_date = self.since.strftime("%B %d, %Y") # type: ignore[union-attr]
new = f' :material-alert-decagram:{{ .new-feature .vibrate title="Added on {ft_date}" }}'
project = f"[{self.project.name}]({self.project.url}) — " if self.project else ""
feature = f"[{self.name}]({self.url(rel_base)})" if self.ref else self.name
print(f"- [{'x' if self.since else ' '}] {project}{feature}{new}")
@dataclass
class Goal:
name: str
amount: int
features: list[Feature]
complete: bool = False
@property
def human_readable_amount(self) -> str: # noqa: D102
return human_readable_amount(self.amount)
def render(self, rel_base: str = "..") -> None: # noqa: D102
print(f"#### $ {self.human_readable_amount} — {self.name}\n")
if self.features:
for feature in self.features:
feature.render(rel_base)
print("")
else:
print("There are no features in this goal for this project. ")
print(
"[See the features in this goal **for all Insiders projects.**]"
f"(https://pawamoy.github.io/insiders/#{self.amount}-{self.name.lower().replace(' ', '-')})",
)
def load_goals(data: str, funding: int = 0, project: Project | None = None) -> dict[int, Goal]:
goals_data = yaml.safe_load(data)["goals"]
return {
amount: Goal(
name=goal_data["name"],
amount=amount,
complete=funding >= amount,
features=[
Feature(
name=feature_data["name"],
ref=feature_data.get("ref"),
since=feature_data.get("since") and datetime.strptime(feature_data["since"], "%Y/%m/%d").date(), # noqa: DTZ007
project=project,
)
for feature_data in goal_data["features"]
],
)
for amount, goal_data in goals_data.items()
}
def _load_goals_from_disk(path: str, funding: int = 0) -> dict[int, Goal]:
project_dir = os.getenv("MKDOCS_CONFIG_DIR", ".")
try:
data = Path(project_dir, path).read_text()
except OSError as error:
raise RuntimeError(f"Could not load data from disk: {path}") from error
return load_goals(data, funding)
def _load_goals_from_url(source_data: tuple[str, str, str], funding: int = 0) -> dict[int, Goal]:
project_name, project_url, data_fragment = source_data
data_url = urljoin(project_url, data_fragment)
try:
with urlopen(data_url) as response: # noqa: S310
data = response.read()
except HTTPError as error:
raise RuntimeError(f"Could not load data from network: {data_url}") from error
return load_goals(data, funding, project=Project(name=project_name, url=project_url))
def _load_goals(source: str | tuple[str, str, str], funding: int = 0) -> dict[int, Goal]:
if isinstance(source, str):
return _load_goals_from_disk(source, funding)
return _load_goals_from_url(source, funding)
def funding_goals(source: str | list[str | tuple[str, str, str]], funding: int = 0) -> dict[int, Goal]:
if isinstance(source, str):
return _load_goals_from_disk(source, funding)
goals = {}
for src in source:
source_goals = _load_goals(src, funding)
for amount, goal in source_goals.items():
if amount not in goals:
goals[amount] = goal
else:
goals[amount].features.extend(goal.features)
return {amount: goals[amount] for amount in sorted(goals)}
def feature_list(goals: Iterable[Goal]) -> list[Feature]:
return list(chain.from_iterable(goal.features for goal in goals))
def load_json(url: str) -> str | list | dict:
with urlopen(url) as response: # noqa: S310
return json.loads(response.read().decode())
data_source = globals()["data_source"]
sponsor_url = "https://github.com/sponsors/pawamoy"
data_url = "https://raw.githubusercontent.com/pawamoy/sponsors/main"
numbers: dict[str, int] = load_json(f"{data_url}/numbers.json") # type: ignore[assignment]
sponsors: list[dict] = load_json(f"{data_url}/sponsors.json") # type: ignore[assignment]
current_funding = numbers["total"]
sponsors_count = numbers["count"]
goals = funding_goals(data_source, funding=current_funding)
ongoing_goals = [goal for goal in goals.values() if not goal.complete]
unreleased_features = sorted(
(ft for ft in feature_list(ongoing_goals) if ft.since),
key=lambda ft: cast("date", ft.since),
reverse=True,
)