Skip to content

Commit 3523dc5

Browse files
committed
Switch to urllib3
1 parent 11a1b17 commit 3523dc5

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

build_status.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88
import tomllib
99
from collections.abc import Iterator
1010

11-
from requests import Session
11+
from urllib3 import PoolManager
1212

1313

14-
def get_languages(requests: Session) -> Iterator[tuple[str, bool]]:
15-
data = requests.get(
16-
'https://raw.githubusercontent.com/'
17-
'python/docsbuild-scripts/refs/heads/main/config.toml',
18-
timeout=10,
19-
).text
20-
config = tomllib.loads(data)
14+
def get_languages(http: PoolManager) -> Iterator[tuple[str, bool]]:
15+
data = http.request(
16+
'GET',
17+
'https://raw.githubusercontent.com/python/docsbuild-scripts/refs/heads/main/config.toml',
18+
).data
19+
config = tomllib.loads(data.decode())
2120
for code, language in config['languages'].items():
2221
language_code = code.lower().replace('_', '-')
2322
in_switcher = language.get('in_prod', config['defaults']['in_prod'])
@@ -26,7 +25,7 @@ def get_languages(requests: Session) -> Iterator[tuple[str, bool]]:
2625

2726
def main() -> None:
2827
languages = {
29-
language: in_switcher for language, in_switcher in get_languages(Session())
28+
language: in_switcher for language, in_switcher in get_languages(PoolManager())
3029
}
3130
print(languages)
3231
for code in ('en', 'pl', 'ar', 'zh-cn', 'id'):

generate.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pathlib import Path
1717
from tempfile import TemporaryDirectory
1818

19-
from requests import Session
19+
import urllib3
2020
from git import Repo
2121
from jinja2 import Template
2222

@@ -45,13 +45,14 @@ def get_completion_progress() -> Iterator['LanguageProjectData']:
4545
)
4646
subprocess.run(['make', '-C', cpython_dir / 'Doc', 'venv'], check=True)
4747
subprocess.run(['make', '-C', cpython_dir / 'Doc', 'gettext'], check=True)
48-
languages_built = dict(build_status.get_languages(session := Session()))
48+
http = urllib3.PoolManager()
49+
languages_built = dict(build_status.get_languages(http))
4950
for language, repo in get_languages_and_repos(devguide_dir):
5051
built = language.code in languages_built
5152
if repo:
5253
completion, translators_data = get_completion(clones_dir, repo)
5354
visitors_num = (
54-
get_number_of_visitors(language.code, session) if built else 0
55+
get_number_of_visitors(language.code, http) if built else 0
5556
)
5657
else:
5758
completion = 0.0

visitors.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44
import urllib.parse
55
import zipfile
66

7-
from requests import Session
7+
from urllib3 import PoolManager
88

99

10-
def get_number_of_visitors(language: str, requests: Session) -> int:
10+
def get_number_of_visitors(language: str, http: PoolManager) -> int:
1111
params = urllib.parse.urlencode(
1212
{'filters': f'[["contains","event:page",["/{language}/"]]]', 'period': 'all'}
1313
)
14-
r = requests.get(
15-
f'https://plausible.io/docs.python.org/export?{params}', timeout=20
16-
)
17-
logging.info(f'Plausible export responded with {r.status_code=}')
14+
r = http.request('GET', f'https://plausible.io/docs.python.org/export?{params}')
15+
logging.info(f'Plausible export responded with {r.status=}')
1816
with (
19-
zipfile.ZipFile(io.BytesIO(r.content), 'r') as z,
17+
zipfile.ZipFile(io.BytesIO(r.data), 'r') as z,
2018
z.open('visitors.csv') as csv_file,
2119
):
2220
csv_reader = csv.DictReader(io.TextIOWrapper(csv_file))
2321
return sum(int(row['visitors']) for row in csv_reader)
22+
23+
24+
if __name__ == '__main__':
25+
print(get_number_of_visitors('pl', PoolManager()))

0 commit comments

Comments
 (0)