mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-31 14:33:54 -04:00
py3: port plistlib
This commit is contained in:
parent
e0205790b9
commit
ce007d8bab
@ -10,7 +10,6 @@ Manage application-wide preferences.
|
|||||||
|
|
||||||
import optparse
|
import optparse
|
||||||
import os
|
import os
|
||||||
import plistlib
|
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
from calibre.constants import (
|
from calibre.constants import (
|
||||||
@ -22,7 +21,6 @@ from calibre.utils.config_base import (
|
|||||||
tweaks, from_json, to_json
|
tweaks, from_json, to_json
|
||||||
)
|
)
|
||||||
from calibre.utils.lock import ExclusiveFile
|
from calibre.utils.lock import ExclusiveFile
|
||||||
from polyglot.builtins import string_or_bytes
|
|
||||||
|
|
||||||
|
|
||||||
# optparse uses gettext.gettext instead of _ from builtins, so we
|
# optparse uses gettext.gettext instead of _ from builtins, so we
|
||||||
@ -335,10 +333,12 @@ class XMLConfig(dict):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def raw_to_object(self, raw):
|
def raw_to_object(self, raw):
|
||||||
return plistlib.readPlistFromString(raw)
|
from polyglot.plistlib import loads
|
||||||
|
return loads(raw)
|
||||||
|
|
||||||
def to_raw(self):
|
def to_raw(self):
|
||||||
return plistlib.writePlistToString(self)
|
from polyglot.plistlib import dumps
|
||||||
|
return dumps(self)
|
||||||
|
|
||||||
def decouple(self, prefix):
|
def decouple(self, prefix):
|
||||||
self.file_path = os.path.join(os.path.dirname(self.file_path), prefix + os.path.basename(self.file_path))
|
self.file_path = os.path.join(os.path.dirname(self.file_path), prefix + os.path.basename(self.file_path))
|
||||||
@ -362,26 +362,29 @@ class XMLConfig(dict):
|
|||||||
self.update(d)
|
self.update(d)
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
|
from polyglot.plistlib import Data
|
||||||
try:
|
try:
|
||||||
ans = dict.__getitem__(self, key)
|
ans = dict.__getitem__(self, key)
|
||||||
if isinstance(ans, plistlib.Data):
|
if isinstance(ans, Data):
|
||||||
ans = ans.data
|
ans = ans.data
|
||||||
return ans
|
return ans
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return self.defaults.get(key, None)
|
return self.defaults.get(key, None)
|
||||||
|
|
||||||
def get(self, key, default=None):
|
def get(self, key, default=None):
|
||||||
|
from polyglot.plistlib import Data
|
||||||
try:
|
try:
|
||||||
ans = dict.__getitem__(self, key)
|
ans = dict.__getitem__(self, key)
|
||||||
if isinstance(ans, plistlib.Data):
|
if isinstance(ans, Data):
|
||||||
ans = ans.data
|
ans = ans.data
|
||||||
return ans
|
return ans
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return self.defaults.get(key, default)
|
return self.defaults.get(key, default)
|
||||||
|
|
||||||
def __setitem__(self, key, val):
|
def __setitem__(self, key, val):
|
||||||
if isinstance(val, string_or_bytes):
|
from polyglot.plistlib import Data
|
||||||
val = plistlib.Data(val)
|
if isinstance(val, bytes):
|
||||||
|
val = Data(val)
|
||||||
dict.__setitem__(self, key, val)
|
dict.__setitem__(self, key, val)
|
||||||
self.commit()
|
self.commit()
|
||||||
|
|
||||||
|
@ -6,12 +6,13 @@ from __future__ import (unicode_literals, division, absolute_import,
|
|||||||
__license__ = 'GPL v3'
|
__license__ = 'GPL v3'
|
||||||
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
|
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||||
|
|
||||||
import os, plistlib, re, mimetypes, subprocess
|
import os, re, mimetypes, subprocess
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from calibre.ptempfile import TemporaryDirectory
|
from calibre.ptempfile import TemporaryDirectory
|
||||||
from calibre.utils.icu import numeric_sort_key
|
from calibre.utils.icu import numeric_sort_key
|
||||||
from polyglot.builtins import iteritems, unicode_type, string_or_bytes
|
from polyglot.builtins import iteritems, unicode_type, string_or_bytes
|
||||||
|
from polyglot.plistlib import loads
|
||||||
|
|
||||||
application_locations = ('/Applications', '~/Applications', '~/Desktop')
|
application_locations = ('/Applications', '~/Applications', '~/Desktop')
|
||||||
|
|
||||||
@ -266,7 +267,8 @@ def get_bundle_data(path):
|
|||||||
'path': path,
|
'path': path,
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
plist = plistlib.readPlist(info)
|
with open(info, 'rb') as f:
|
||||||
|
plist = loads(f.read())
|
||||||
except Exception:
|
except Exception:
|
||||||
return None
|
return None
|
||||||
ans['name'] = plist.get('CFBundleDisplayName') or plist.get('CFBundleName') or ans['name']
|
ans['name'] = plist.get('CFBundleDisplayName') or plist.get('CFBundleName') or ans['name']
|
||||||
|
12
src/polyglot/plistlib.py
Normal file
12
src/polyglot/plistlib.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#!/usr/bin/env python2
|
||||||
|
# vim:fileencoding=utf-8
|
||||||
|
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
|
from polyglot.builtins import is_py3
|
||||||
|
|
||||||
|
if is_py3:
|
||||||
|
from plistlib import loads, dumps, Data # noqa
|
||||||
|
else:
|
||||||
|
from plistlib import readPlistFromString as loads, writePlistToString as dumps, Data # noqa
|
Loading…
x
Reference in New Issue
Block a user