Path: blob/main/deskutils/calibre/files/patch-setup_iso__codes.py
16461 views
--- setup/iso_codes.py.orig 2025-04-18 03:14:01 UTC1+++ setup/iso_codes.py2@@ -2,6 +2,7 @@ import fnmatch3# License: GPLv3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net>45import fnmatch6+import glob7import optparse8import os9import shutil10@@ -24,40 +25,60 @@ class ISOData(Command):11description = 'Get ISO codes name localization data'12top_level_filename = 'iso-codes-main'13_zip_data = None14+ extracted = False1516def add_options(self, parser):17with suppress(optparse.OptionConflictError): # ignore if option already added18parser.add_option('--path-to-isocodes', help='Path to previously downloaded iso-codes-main.zip')19+ parser.add_option('--isocodes-extracted', default=False, action='store_true',20+ help='Isocodes is a directory')2122def run(self, opts):23- if self._zip_data is None and opts.path_to_isocodes:24- with open(opts.path_to_isocodes, 'rb') as f:25- self._zip_data = f.read()26- # get top level directory27- top = {item.split('/')[0] for item in zipfile.ZipFile(self.zip_data).namelist()}28- assert len(top) == 129- self.top_level_filename = top.pop()30+ if opts.isocodes_extracted:31+ self.top_level = opts.path_to_isocodes32+ self.extracted = True33+ else:34+ if self._zip_data is None and opts.path_to_isocodes:35+ with open(opts.path_to_isocodes, 'rb') as f:36+ self._zip_data = f.read()37+ # get top level directory38+ top = {item.split('/')[0] for item in zipfile.ZipFile(self.zip_data).namelist()}39+ assert len(top) == 140+ self.top_level_filename = top.pop()4142@property43def zip_data(self):44return self._zip_data or iso_codes_data()4546def db_data(self, name: str) -> bytes:47- with zipfile.ZipFile(BytesIO(self.zip_data)) as zf:48- with zf.open(f'{self.top_level_filename}/data/{name}') as f:49+ if self.extracted:50+ src = f'{self.top_level}/data/{name}'51+ if not os.path.exists(src):52+ raise Exception(src + ' does not exist')53+ with open(src, 'rb') as f:54return f.read()55+ else:56+ with zipfile.ZipFile(BytesIO(self.zip_data)) as zf:57+ with zf.open(f'{self.top_level_filename}/data/{name}') as f:58+ return f.read()5960def extract_po_files(self, name: str, output_dir: str) -> None:61name = name.split('.', 1)[0]62- pat = f'{self.top_level_filename}/{name}/*.po'63- with zipfile.ZipFile(BytesIO(self.zip_data)) as zf:64- for name in fnmatch.filter(zf.namelist(), pat):65+ if self.extracted:66+ pat = f'{self.top_level}/{name}/*.po'67+ for name in glob.glob(pat):68dest = os.path.join(output_dir, name.split('/')[-1])69- zi = zf.getinfo(name)70- with zf.open(zi) as src, open(dest, 'wb') as d:71- shutil.copyfileobj(src, d)72- date_time = time.mktime(zi.date_time + (0, 0, -1))73- os.utime(dest, (date_time, date_time))74+ shutil.copy2(name, dest)75+ else:76+ pat = f'{self.top_level_filename}/{name}/*.po'77+ with zipfile.ZipFile(BytesIO(self.zip_data)) as zf:78+ for name in fnmatch.filter(zf.namelist(), pat):79+ dest = os.path.join(output_dir, name.split('/')[-1])80+ zi = zf.getinfo(name)81+ with zf.open(zi) as src, open(dest, 'wb') as d:82+ shutil.copyfileobj(src, d)83+ date_time = time.mktime(zi.date_time + (0, 0, -1))84+ os.utime(dest, (date_time, date_time))858687iso_data = ISOData()888990