Skip to content

Commit d858440

Browse files
authored
Merge pull request #894 from python-babel/downloader-improvements
Small downloader improvements
2 parents 558f26c + 3dbbeec commit d858440

File tree

3 files changed

+17
-42
lines changed

3 files changed

+17
-42
lines changed

babel/localtime/_win32.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
try:
2-
import _winreg as winreg
2+
import winreg
33
except ImportError:
4-
try:
5-
import winreg
6-
except ImportError:
7-
winreg = None
4+
winreg = None
85

96
from babel.core import get_global
107
import pytz

scripts/download_import_cldr.py

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
import hashlib
88
import zipfile
99
import subprocess
10-
try:
11-
from urllib.request import urlretrieve
12-
except ImportError:
13-
from urllib import urlretrieve
10+
from urllib.request import urlretrieve
1411

1512

1613
URL = 'http://unicode.org/Public/cldr/41/cldr-common-41.0.zip'
@@ -20,39 +17,24 @@
2017
BLKSIZE = 131072
2118

2219

23-
def get_terminal_width():
24-
try:
25-
import fcntl
26-
import termios
27-
import struct
28-
fd = sys.stdin.fileno()
29-
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
30-
return cr[1]
31-
except Exception:
32-
return 80
33-
34-
3520
def reporthook(block_count, block_size, total_size):
3621
bytes_transmitted = block_count * block_size
37-
cols = get_terminal_width()
22+
cols = shutil.get_terminal_size().columns
3823
buffer = 6
3924
percent = float(bytes_transmitted) / (total_size or 1)
4025
done = int(percent * (cols - buffer))
41-
sys.stdout.write('\r')
42-
sys.stdout.write(' ' + '=' * done + ' ' * (cols - done - buffer))
43-
sys.stdout.write('% 4d%%' % (percent * 100))
26+
bar = ('=' * done).ljust(cols - buffer)
27+
sys.stdout.write(f'\r{bar}{int(percent * 100): 4d}%')
4428
sys.stdout.flush()
4529

4630

47-
def log(message, *args):
48-
if args:
49-
message = message % args
50-
sys.stderr.write(message + '\n')
31+
def log(message):
32+
sys.stderr.write(f'{message}\n')
5133

5234

5335
def is_good_file(filename):
5436
if not os.path.isfile(filename):
55-
log('Local copy \'%s\' not found', filename)
37+
log(f"Local copy '{filename}' not found")
5638
return False
5739
h = hashlib.sha512()
5840
with open(filename, 'rb') as f:
@@ -63,8 +45,7 @@ def is_good_file(filename):
6345
h.update(blk)
6446
digest = h.hexdigest()
6547
if digest != FILESUM:
66-
raise RuntimeError('Checksum mismatch: %r != %r'
67-
% (digest, FILESUM))
48+
raise RuntimeError(f'Checksum mismatch: {digest!r} != {FILESUM!r}')
6849
else:
6950
return True
7051

@@ -79,20 +60,20 @@ def main():
7960
show_progress = (False if os.environ.get("BABEL_CLDR_NO_DOWNLOAD_PROGRESS") else sys.stdout.isatty())
8061

8162
while not is_good_file(zip_path):
82-
log("Downloading '%s' from %s", FILENAME, URL)
83-
if os.path.isfile(zip_path):
84-
os.remove(zip_path)
85-
urlretrieve(URL, zip_path, (reporthook if show_progress else None))
63+
log(f"Downloading '{FILENAME}' from {URL}")
64+
tmp_path = f"{zip_path}.tmp"
65+
urlretrieve(URL, tmp_path, (reporthook if show_progress else None))
66+
os.replace(tmp_path, zip_path)
8667
changed = True
8768
print()
8869
common_path = os.path.join(cldr_path, 'common')
8970

9071
if changed or not os.path.isdir(common_path):
9172
if os.path.isdir(common_path):
92-
log('Deleting old CLDR checkout in \'%s\'', cldr_path)
73+
log(f"Deleting old CLDR checkout in '{cldr_path}'")
9374
shutil.rmtree(common_path)
9475

95-
log('Extracting CLDR to \'%s\'', cldr_path)
76+
log(f"Extracting CLDR to '{cldr_path}'")
9677
with contextlib.closing(zipfile.ZipFile(zip_path)) as z:
9778
z.extractall(cldr_path)
9879

scripts/import_cldr.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
import sys
2020
import logging
2121

22-
try:
23-
from xml.etree import cElementTree as ElementTree
24-
except ImportError:
25-
from xml.etree import ElementTree
22+
from xml.etree import ElementTree
2623

2724
# Make sure we're using Babel source, and not some previously installed version
2825
CHECKOUT_ROOT = os.path.abspath(os.path.join(

0 commit comments

Comments
 (0)