Use msgpack instead of pickle for a couple more databases

This commit is contained in:
Kovid Goyal 2018-09-10 17:36:57 +05:30
parent 1d54c2f874
commit d8e24e4a19
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 14 additions and 9 deletions

View File

@ -7,7 +7,7 @@ __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import os, tempfile, shutil, subprocess, glob, re, time, textwrap, cPickle, shlex, json, errno, hashlib, sys import os, tempfile, shutil, subprocess, glob, re, time, textwrap, shlex, json, errno, hashlib, sys
from collections import defaultdict from collections import defaultdict
from locale import normalize as normalize_locale from locale import normalize as normalize_locale
from functools import partial from functools import partial
@ -338,9 +338,10 @@ class Translations(POT): # {{{
ln = normalize_locale(locale).partition('.')[0] ln = normalize_locale(locale).partition('.')[0]
if ln in lcdata: if ln in lcdata:
ld = lcdata[ln] ld = lcdata[ln]
lcdest = self.j(self.d(dest), 'lcdata.pickle') lcdest = self.j(self.d(dest), 'lcdata.calibre_msgpack')
from calibre.utils.serialize import msgpack_dumps
with open(lcdest, 'wb') as lcf: with open(lcdest, 'wb') as lcf:
lcf.write(cPickle.dumps(ld, -1)) lcf.write(msgpack_dumps(ld))
stats = {} stats = {}
@ -379,7 +380,9 @@ class Translations(POT): # {{{
except EnvironmentError as err: except EnvironmentError as err:
if err.errno != errno.EEXIST: if err.errno != errno.EEXIST:
raise raise
cPickle.dump(stats, open(dest, 'wb'), -1) from calibre.utils.serialize import msgpack_dumps
with open(dest, 'wb') as f:
f.write(msgpack_dumps(stats))
def hash_and_data(self, f): def hash_and_data(self, f):
with open(f, 'rb') as s: with open(f, 'rb') as s:
@ -453,7 +456,7 @@ class Translations(POT): # {{{
@property @property
def stats(self): def stats(self):
return self.j(self.d(self.DEST), 'stats.pickle') return self.j(self.d(self.DEST), 'stats.calibre_msgpack')
def compile_website_translations(self): def compile_website_translations(self):
from calibre.utils.zipfile import ZipFile, ZipInfo, ZIP_STORED from calibre.utils.zipfile import ZipFile, ZipInfo, ZIP_STORED

View File

@ -7,7 +7,7 @@ __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import os, locale, re, cStringIO, cPickle import os, locale, re, cStringIO
from gettext import GNUTranslations, NullTranslations from gettext import GNUTranslations, NullTranslations
_available_translations = None _available_translations = None
@ -16,9 +16,10 @@ _available_translations = None
def available_translations(): def available_translations():
global _available_translations global _available_translations
if _available_translations is None: if _available_translations is None:
stats = P('localization/stats.pickle', allow_user_override=False) stats = P('localization/stats.calibre_msgpack', allow_user_override=False)
if os.path.exists(stats): if os.path.exists(stats):
stats = cPickle.load(open(stats, 'rb')) from calibre.utils.serialize import msgpack_loads
stats = msgpack_loads(open(stats, 'rb').read())
else: else:
stats = {} stats = {}
_available_translations = [x for x in stats if stats[x] > 0.1] _available_translations = [x for x in stats if stats[x] > 0.1]
@ -223,8 +224,9 @@ def set_translators():
except: except:
pass # No iso639 translations for this lang pass # No iso639 translations for this lang
if buf is not None: if buf is not None:
from calibre.utils.serialize import msgpack_loads
try: try:
lcdata = cPickle.loads(zf.read(mpath + '/lcdata.pickle')) lcdata = msgpack_loads(zf.read(mpath + '/lcdata.calibre_msgpack'))
except: except:
pass # No lcdata pass # No lcdata