Debian Codegraph @

setup/iso_codes.py

raw
1 #!/usr/bin/env python
2 # License: GPLv3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net>
3
4 import fnmatch
5 import optparse
6 import os
7 import shutil
8 import time
9 import zipfile
10 from contextlib import suppress
11 from functools import lru_cache
12 from io import BytesIO
13
14 from setup import Command, download_securely
15
16
17 @lru_cache(2)
18 def iso_codes_data(ignore_cache=False):
19 URL = 'https://download.calibre-ebook.com/iso-codes.zip'
20 return download_securely(URL, ignore_cache=ignore_cache)
21
22
23 class ISOData(Command):
24 description = 'Get ISO codes name localization data'
25 top_level_filename = 'iso-codes-main'
26 _zip_data = None
27
28 def add_options(self, parser):
29 with suppress(optparse.OptionConflictError): # ignore if option already added
30 parser.add_option('--path-to-isocodes', help='Path to previously downloaded iso-codes-main.zip')
31
32 def run(self, opts):
33 if self._zip_data is None and opts.path_to_isocodes:
34 with open(opts.path_to_isocodes, 'rb') as f:
35 self._zip_data = f.read()
36 # get top level directory
37 top = {item.split('/')[0] for item in zipfile.ZipFile(BytesIO(self.zip_data)).namelist()}
38 assert len(top) == 1
39 self.top_level_filename = top.pop()
40 else:
41 try:
42 zipfile.ZipFile(BytesIO(self.zip_data)).close()
43 except Exception:
44 self.info('Corrupted zip file detected, re-downloading...')
45 iso_codes_data(ignore_cache=True)
46
47 @property
48 def zip_data(self):
49 return self._zip_data or iso_codes_data()
50
51 def db_data(self, name: str) -> bytes:
52 with zipfile.ZipFile(BytesIO(self.zip_data)) as zf:
53 with zf.open(f'{self.top_level_filename}/data/{name}') as f:
54 return f.read()
55
56 def extract_po_files(self, name: str, output_dir: str) -> None:
57 name = name.split('.', 1)[0]
58 pat = f'{self.top_level_filename}/{name}/*.po'
59 with zipfile.ZipFile(BytesIO(self.zip_data)) as zf:
60 for name in fnmatch.filter(zf.namelist(), pat):
61 dest = os.path.join(output_dir, name.split('/')[-1])
62 zi = zf.getinfo(name)
63 with zf.open(zi) as src, open(dest, 'wb') as d:
64 shutil.copyfileobj(src, d)
65 date_time = time.mktime(zi.date_time + (0, 0, -1))
66 os.utime(dest, (date_time, date_time))
67
68
69 iso_data = ISOData()