mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Merge branch 'py3' of https://github.com/eli-schwartz/calibre
This commit is contained in:
commit
d98b2e0b9c
@ -72,7 +72,8 @@ gcc = os.environ.get('CC', 'clang')
|
|||||||
def compile_launchers(contents_dir, xprograms, pyver):
|
def compile_launchers(contents_dir, xprograms, pyver):
|
||||||
base = dirname(abspath(__file__))
|
base = dirname(abspath(__file__))
|
||||||
lib = compile_launcher_lib(contents_dir, gcc, base)
|
lib = compile_launcher_lib(contents_dir, gcc, base)
|
||||||
src = open(join(base, 'launcher.c'), 'rb').read().decode('utf-8')
|
with open(join(base, 'launcher.c'), 'rb') as f:
|
||||||
|
src = f.read().decode('utf-8')
|
||||||
env, env_vals = [], []
|
env, env_vals = [], []
|
||||||
for key, val in ENV.items():
|
for key, val in ENV.items():
|
||||||
env.append('"%s"' % key)
|
env.append('"%s"' % key)
|
||||||
@ -450,7 +451,8 @@ class Freeze(object):
|
|||||||
src = os.path.join(PREFIX, 'etc', 'fonts')
|
src = os.path.join(PREFIX, 'etc', 'fonts')
|
||||||
shutil.copytree(src, dst, symlinks=False)
|
shutil.copytree(src, dst, symlinks=False)
|
||||||
fc = os.path.join(dst, 'fonts.conf')
|
fc = os.path.join(dst, 'fonts.conf')
|
||||||
raw = open(fc, 'rb').read().decode('utf-8')
|
with open(fc, 'rb') as f:
|
||||||
|
raw = f.read().decode('utf-8')
|
||||||
raw = raw.replace('<dir>/usr/share/fonts</dir>', '''\
|
raw = raw.replace('<dir>/usr/share/fonts</dir>', '''\
|
||||||
<dir>/Library/Fonts</dir>
|
<dir>/Library/Fonts</dir>
|
||||||
<dir>/System/Library/Fonts</dir>
|
<dir>/System/Library/Fonts</dir>
|
||||||
|
@ -42,7 +42,8 @@ def make_certificate_useable():
|
|||||||
# Unlock keychain
|
# Unlock keychain
|
||||||
run('security unlock-keychain -p "{}" "{}"'.format(KEYCHAIN_PASSWORD, KEYCHAIN))
|
run('security unlock-keychain -p "{}" "{}"'.format(KEYCHAIN_PASSWORD, KEYCHAIN))
|
||||||
# Add certificate to keychain
|
# Add certificate to keychain
|
||||||
cert_pass = open(CODESIGN_CREDS).read().strip()
|
with open(CODESIGN_CREDS, 'r') as f:
|
||||||
|
cert_pass = f.read().strip()
|
||||||
# Add certificate to keychain and allow codesign to use it
|
# Add certificate to keychain and allow codesign to use it
|
||||||
# Use -A instead of -T /usr/bin/codesign to allow all apps to use it
|
# Use -A instead of -T /usr/bin/codesign to allow all apps to use it
|
||||||
run('security import {} -k "{}" -P "{}" -T "/usr/bin/codesign"'.format(
|
run('security import {} -k "{}" -P "{}" -T "/usr/bin/codesign"'.format(
|
||||||
|
@ -305,7 +305,8 @@ def embed_resources(env, module, desc=None, extra_data=None, product_description
|
|||||||
icon_map = {'calibre': 'library', 'ebook-viewer': 'viewer', 'ebook-edit': 'ebook-edit',
|
icon_map = {'calibre': 'library', 'ebook-viewer': 'viewer', 'ebook-edit': 'ebook-edit',
|
||||||
'lrfviewer': 'viewer', 'calibre-portable': 'library'}
|
'lrfviewer': 'viewer', 'calibre-portable': 'library'}
|
||||||
file_type = 'DLL' if module.endswith('.dll') else 'APP'
|
file_type = 'DLL' if module.endswith('.dll') else 'APP'
|
||||||
template = open(env.rc_template, 'rb').read().decode('utf-8')
|
with open(env.rc_template, 'rb') as f:
|
||||||
|
template = f.read().decode('utf-8')
|
||||||
bname = b(module)
|
bname = b(module)
|
||||||
internal_name = os.path.splitext(bname)[0]
|
internal_name = os.path.splitext(bname)[0]
|
||||||
icon = icon_map.get(internal_name, 'command-prompt')
|
icon = icon_map.get(internal_name, 'command-prompt')
|
||||||
|
@ -29,7 +29,8 @@ def create_installer(env):
|
|||||||
shutil.rmtree(env.installer_dir)
|
shutil.rmtree(env.installer_dir)
|
||||||
os.makedirs(env.installer_dir)
|
os.makedirs(env.installer_dir)
|
||||||
|
|
||||||
template = open(j(d(__file__), 'wix-template.xml'), 'rb').read().decode('utf-8')
|
with open(j(d(__file__), 'wix-template.xml'), 'rb') as f:
|
||||||
|
template = f.read().decode('utf-8')
|
||||||
|
|
||||||
components, smap = get_components_from_files(env)
|
components, smap = get_components_from_files(env)
|
||||||
wxs = template.format(
|
wxs = template.format(
|
||||||
@ -50,7 +51,8 @@ def create_installer(env):
|
|||||||
editor_icon=j(env.src_root, 'icons', 'ebook-edit.ico'),
|
editor_icon=j(env.src_root, 'icons', 'ebook-edit.ico'),
|
||||||
web_icon=j(env.src_root, 'icons', 'web.ico'),
|
web_icon=j(env.src_root, 'icons', 'web.ico'),
|
||||||
)
|
)
|
||||||
template = open(j(d(__file__), 'en-us.xml'), 'rb').read().decode('utf-8')
|
with open(j(d(__file__), 'en-us.xml'), 'rb') as f:
|
||||||
|
template = f.read().decode('utf-8')
|
||||||
enus = template.format(app=calibre_constants['appname'])
|
enus = template.format(app=calibre_constants['appname'])
|
||||||
|
|
||||||
enusf = j(env.installer_dir, 'en-us.wxl')
|
enusf = j(env.installer_dir, 'en-us.wxl')
|
||||||
|
@ -95,7 +95,8 @@ def require_clean_git():
|
|||||||
def initialize_constants():
|
def initialize_constants():
|
||||||
global __version__, __appname__, modules, functions, basenames, scripts
|
global __version__, __appname__, modules, functions, basenames, scripts
|
||||||
|
|
||||||
src = open(os.path.join(SRC, 'calibre/constants.py'), 'rb').read().decode('utf-8')
|
with open(os.path.join(SRC, 'calibre/constants.py'), 'rb') as f:
|
||||||
|
src = f.read().decode('utf-8')
|
||||||
nv = re.search(r'numeric_version\s+=\s+\((\d+), (\d+), (\d+)\)', src)
|
nv = re.search(r'numeric_version\s+=\s+\((\d+), (\d+), (\d+)\)', src)
|
||||||
__version__ = '%s.%s.%s'%(nv.group(1), nv.group(2), nv.group(3))
|
__version__ = '%s.%s.%s'%(nv.group(1), nv.group(2), nv.group(3))
|
||||||
__appname__ = re.search(r'__appname__\s+=\s+(u{0,1})[\'"]([^\'"]+)[\'"]',
|
__appname__ = re.search(r'__appname__\s+=\s+(u{0,1})[\'"]([^\'"]+)[\'"]',
|
||||||
|
@ -454,7 +454,8 @@ class Build(Command):
|
|||||||
self.info(' '.join(cmd))
|
self.info(' '.join(cmd))
|
||||||
self.check_call(cmd)
|
self.check_call(cmd)
|
||||||
self.info('')
|
self.info('')
|
||||||
raw = open(sbf, 'rb').read().decode('utf-8')
|
with open(sbf, 'rb') as f:
|
||||||
|
raw = f.read().decode('utf-8')
|
||||||
|
|
||||||
def read(x):
|
def read(x):
|
||||||
ans = re.search(r'^%s\s*=\s*(.+)$' % x, raw, flags=re.M).group(1).strip()
|
ans = re.search(r'^%s\s*=\s*(.+)$' % x, raw, flags=re.M).group(1).strip()
|
||||||
|
@ -748,7 +748,8 @@ except NameError:
|
|||||||
|
|
||||||
def update_intaller_wrapper():
|
def update_intaller_wrapper():
|
||||||
# To run: python3 -c "import runpy; runpy.run_path('setup/linux-installer.py', run_name='update_wrapper')"
|
# To run: python3 -c "import runpy; runpy.run_path('setup/linux-installer.py', run_name='update_wrapper')"
|
||||||
src = open(__file__, 'rb').read().decode('utf-8')
|
with open(__file__, 'rb') as f:
|
||||||
|
src = f.read().decode('utf-8')
|
||||||
wrapper = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'linux-installer.sh')
|
wrapper = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'linux-installer.sh')
|
||||||
with open(wrapper, 'r+b') as f:
|
with open(wrapper, 'r+b') as f:
|
||||||
raw = f.read().decode('utf-8')
|
raw = f.read().decode('utf-8')
|
||||||
|
@ -211,7 +211,8 @@ class IteratorsCheck(Base):
|
|||||||
|
|
||||||
def get_errors_in_file(self, f):
|
def get_errors_in_file(self, f):
|
||||||
pat = re.compile(r'\b(range|map|filter|zip)\(')
|
pat = re.compile(r'\b(range|map|filter|zip)\(')
|
||||||
text = open(f, 'rb').read().decode('utf-8')
|
with open(f, 'rb') as f:
|
||||||
|
text = f.read().decode('utf-8')
|
||||||
matches = tuple(pat.finditer(text))
|
matches = tuple(pat.finditer(text))
|
||||||
if not matches:
|
if not matches:
|
||||||
return []
|
return []
|
||||||
|
@ -665,7 +665,8 @@ class GetTranslations(Translations): # {{{
|
|||||||
return errs
|
return errs
|
||||||
|
|
||||||
def check_for_control_chars(f):
|
def check_for_control_chars(f):
|
||||||
raw = open(f, 'rb').read().decode('utf-8')
|
with open(f, 'rb') as f:
|
||||||
|
raw = f.read().decode('utf-8')
|
||||||
pat = re.compile(type(u'')(r'[\0-\x08\x0b\x0c\x0e-\x1f\x7f\x80-\x9f]'))
|
pat = re.compile(type(u'')(r'[\0-\x08\x0b\x0c\x0e-\x1f\x7f\x80-\x9f]'))
|
||||||
errs = []
|
errs = []
|
||||||
for i, line in enumerate(raw.splitlines()):
|
for i, line in enumerate(raw.splitlines()):
|
||||||
|
@ -74,12 +74,11 @@ class FilesystemTest(BaseTest):
|
|||||||
cl = self.cloned_library
|
cl = self.cloned_library
|
||||||
cache = self.init_cache(cl)
|
cache = self.init_cache(cl)
|
||||||
fpath = cache.format_abspath(1, 'FMT1')
|
fpath = cache.format_abspath(1, 'FMT1')
|
||||||
f = open(fpath, 'rb')
|
with open(fpath, 'rb') as f:
|
||||||
with self.assertRaises(IOError):
|
with self.assertRaises(IOError):
|
||||||
cache.set_field('title', {1:'Moved'})
|
cache.set_field('title', {1:'Moved'})
|
||||||
with self.assertRaises(IOError):
|
with self.assertRaises(IOError):
|
||||||
cache.remove_books({1})
|
cache.remove_books({1})
|
||||||
f.close()
|
|
||||||
self.assertNotEqual(cache.field_for('title', 1), 'Moved', 'Title was changed despite file lock')
|
self.assertNotEqual(cache.field_for('title', 1), 'Moved', 'Title was changed despite file lock')
|
||||||
|
|
||||||
# Test on folder with hardlinks
|
# Test on folder with hardlinks
|
||||||
|
@ -38,22 +38,21 @@ class DJVUInput(InputFormatPlugin):
|
|||||||
setattr(options, opt.option.name, opt.recommended_value)
|
setattr(options, opt.option.name, opt.recommended_value)
|
||||||
options.input_encoding = 'utf-8'
|
options.input_encoding = 'utf-8'
|
||||||
base = getcwd()
|
base = getcwd()
|
||||||
fname = os.path.join(base, 'index.html')
|
htmlfile = os.path.join(base, 'index.html')
|
||||||
c = 0
|
c = 0
|
||||||
while os.path.exists(fname):
|
while os.path.exists(htmlfile):
|
||||||
c += 1
|
c += 1
|
||||||
fname = os.path.join(base, 'index%d.html'%c)
|
htmlfile = os.path.join(base, 'index%d.html'%c)
|
||||||
htmlfile = open(fname, 'wb')
|
with open(htmlfile, 'wb') as f:
|
||||||
with htmlfile:
|
f.write(html.encode('utf-8'))
|
||||||
htmlfile.write(html.encode('utf-8'))
|
|
||||||
odi = options.debug_pipeline
|
odi = options.debug_pipeline
|
||||||
options.debug_pipeline = None
|
options.debug_pipeline = None
|
||||||
# Generate oeb from html conversion.
|
# Generate oeb from html conversion.
|
||||||
with open(htmlfile.name, 'rb') as f:
|
with open(htmlfile, 'rb') as f:
|
||||||
oeb = html_input.convert(f, options, 'html', log,
|
oeb = html_input.convert(f, options, 'html', log,
|
||||||
{})
|
{})
|
||||||
options.debug_pipeline = odi
|
options.debug_pipeline = odi
|
||||||
os.remove(htmlfile.name)
|
os.remove(htmlfile)
|
||||||
|
|
||||||
# Set metadata from file.
|
# Set metadata from file.
|
||||||
from calibre.customize.ui import get_file_type_metadata
|
from calibre.customize.ui import get_file_type_metadata
|
||||||
|
@ -307,7 +307,8 @@ class HTMLInput(InputFormatPlugin):
|
|||||||
if link is None or not os.access(link, os.R_OK) or os.path.isdir(link):
|
if link is None or not os.access(link, os.R_OK) or os.path.isdir(link):
|
||||||
return (None, None)
|
return (None, None)
|
||||||
try:
|
try:
|
||||||
raw = open(link, 'rb').read().decode('utf-8', 'replace')
|
with open(link, 'rb') as f:
|
||||||
|
raw = f.read().decode('utf-8', 'replace')
|
||||||
raw = self.oeb.css_preprocessor(raw, add_namespace=False)
|
raw = self.oeb.css_preprocessor(raw, add_namespace=False)
|
||||||
except:
|
except:
|
||||||
self.log.exception('Failed to read CSS file: %r'%link)
|
self.log.exception('Failed to read CSS file: %r'%link)
|
||||||
|
@ -88,21 +88,21 @@ class HTMLZInput(InputFormatPlugin):
|
|||||||
setattr(options, opt.option.name, opt.recommended_value)
|
setattr(options, opt.option.name, opt.recommended_value)
|
||||||
options.input_encoding = 'utf-8'
|
options.input_encoding = 'utf-8'
|
||||||
base = getcwd()
|
base = getcwd()
|
||||||
fname = os.path.join(base, u'index.html')
|
htmlfile = os.path.join(base, u'index.html')
|
||||||
c = 0
|
c = 0
|
||||||
while os.path.exists(fname):
|
while os.path.exists(htmlfile):
|
||||||
c += 1
|
c += 1
|
||||||
fname = u'index%d.html'%c
|
htmlfile = u'index%d.html'%c
|
||||||
htmlfile = open(fname, 'wb')
|
with open(htmlfile, 'wb') as f:
|
||||||
with htmlfile:
|
f.write(html.encode('utf-8'))
|
||||||
htmlfile.write(html.encode('utf-8'))
|
|
||||||
odi = options.debug_pipeline
|
odi = options.debug_pipeline
|
||||||
options.debug_pipeline = None
|
options.debug_pipeline = None
|
||||||
# Generate oeb from html conversion.
|
# Generate oeb from html conversion.
|
||||||
oeb = html_input.convert(open(htmlfile.name, 'rb'), options, 'html', log,
|
with open(htmlfile, 'rb') as f:
|
||||||
|
oeb = html_input.convert(f, options, 'html', log,
|
||||||
{})
|
{})
|
||||||
options.debug_pipeline = odi
|
options.debug_pipeline = odi
|
||||||
os.remove(htmlfile.name)
|
os.remove(htmlfile)
|
||||||
|
|
||||||
# Set metadata from file.
|
# Set metadata from file.
|
||||||
from calibre.customize.ui import get_file_type_metadata
|
from calibre.customize.ui import get_file_type_metadata
|
||||||
|
@ -1191,7 +1191,8 @@ OptionRecommendation(name='search_replace',
|
|||||||
self.log('Structured HTML written to:', out_dir)
|
self.log('Structured HTML written to:', out_dir)
|
||||||
|
|
||||||
if self.opts.extra_css and os.path.exists(self.opts.extra_css):
|
if self.opts.extra_css and os.path.exists(self.opts.extra_css):
|
||||||
self.opts.extra_css = open(self.opts.extra_css, 'rb').read()
|
with open(self.opts.extra_css, 'rb') as f:
|
||||||
|
self.opts.extra_css = f.read()
|
||||||
|
|
||||||
oibl = self.opts.insert_blank_line
|
oibl = self.opts.insert_blank_line
|
||||||
orps = self.opts.remove_paragraph_spacing
|
orps = self.opts.remove_paragraph_spacing
|
||||||
|
@ -734,7 +734,8 @@ class BZZDecoder():
|
|||||||
def main():
|
def main():
|
||||||
import sys
|
import sys
|
||||||
from calibre.constants import plugins
|
from calibre.constants import plugins
|
||||||
raw = open(sys.argv[1], "rb").read()
|
with open(sys.argv[1], "rb") as f:
|
||||||
|
raw = f.read()
|
||||||
d = plugins['bzzdec'][0]
|
d = plugins['bzzdec'][0]
|
||||||
print(d.decompress(raw))
|
print(d.decompress(raw))
|
||||||
|
|
||||||
|
@ -266,7 +266,8 @@ class HTMLConverter(object):
|
|||||||
|
|
||||||
if self._override_css is not None:
|
if self._override_css is not None:
|
||||||
if os.access(self._override_css, os.R_OK):
|
if os.access(self._override_css, os.R_OK):
|
||||||
src = open(self._override_css, 'rb').read()
|
with open(self._override_css, 'rb') as f:
|
||||||
|
src = f.read()
|
||||||
else:
|
else:
|
||||||
src = self._override_css
|
src = self._override_css
|
||||||
if isinstance(src, bytes):
|
if isinstance(src, bytes):
|
||||||
|
@ -712,7 +712,8 @@ def main(args=sys.argv):
|
|||||||
lrf.book_id = options.book_id
|
lrf.book_id = options.book_id
|
||||||
if options.comment:
|
if options.comment:
|
||||||
path = os.path.expanduser(os.path.expandvars(options.comment))
|
path = os.path.expanduser(os.path.expandvars(options.comment))
|
||||||
lrf.free_text = open(path, 'rb').read().decode('utf-8', 'replace')
|
with open(path, 'rb') as f:
|
||||||
|
lrf.free_text = f.read().decode('utf-8', 'replace')
|
||||||
if options.get_thumbnail:
|
if options.get_thumbnail:
|
||||||
t = lrf.thumbnail
|
t = lrf.thumbnail
|
||||||
td = "None"
|
td = "None"
|
||||||
|
@ -391,14 +391,13 @@ if __name__ == '__main__':
|
|||||||
print(get_metadata(open(sys.argv[1], 'rb')))
|
print(get_metadata(open(sys.argv[1], 'rb')))
|
||||||
else:
|
else:
|
||||||
# Test set_metadata()
|
# Test set_metadata()
|
||||||
data = open(sys.argv[1], 'rb')
|
|
||||||
stream = io.BytesIO()
|
stream = io.BytesIO()
|
||||||
stream.write(data.read())
|
with open(sys.argv[1], 'rb') as data:
|
||||||
|
stream.write(data.read())
|
||||||
mi = MetaInformation(title="Updated Title", authors=['Author, Random'])
|
mi = MetaInformation(title="Updated Title", authors=['Author, Random'])
|
||||||
set_metadata(stream, mi)
|
set_metadata(stream, mi)
|
||||||
|
|
||||||
# Write the result
|
# Write the result
|
||||||
tokens = sys.argv[1].rpartition('.')
|
tokens = sys.argv[1].rpartition('.')
|
||||||
updated_data = open(tokens[0]+'-updated' + '.' + tokens[2],'wb')
|
with open(tokens[0]+'-updated' + '.' + tokens[2],'wb') as updated_data:
|
||||||
updated_data.write(stream.getvalue())
|
updated_data.write(stream.getvalue())
|
||||||
updated_data.close()
|
|
||||||
|
@ -87,12 +87,12 @@ class BookmarksMixin(object):
|
|||||||
if not no_copy_to_file and self.copy_bookmarks_to_file and os.path.splitext(
|
if not no_copy_to_file and self.copy_bookmarks_to_file and os.path.splitext(
|
||||||
self.pathtoebook)[1].lower() == '.epub' and os.access(self.pathtoebook, os.W_OK):
|
self.pathtoebook)[1].lower() == '.epub' and os.access(self.pathtoebook, os.W_OK):
|
||||||
try:
|
try:
|
||||||
zf = open(self.pathtoebook, 'r+b')
|
with open(self.pathtoebook, 'r+b') as zf:
|
||||||
|
safe_replace(zf, 'META-INF/calibre_bookmarks.txt',
|
||||||
|
BytesIO(dat.encode('utf-8')),
|
||||||
|
add_missing=True)
|
||||||
except IOError:
|
except IOError:
|
||||||
return
|
return
|
||||||
safe_replace(zf, 'META-INF/calibre_bookmarks.txt',
|
|
||||||
BytesIO(dat.encode('utf-8')),
|
|
||||||
add_missing=True)
|
|
||||||
|
|
||||||
def add_bookmark(self, bm, no_copy_to_file=False):
|
def add_bookmark(self, bm, no_copy_to_file=False):
|
||||||
self.bookmarks = [x for x in self.bookmarks if x['title'] !=
|
self.bookmarks = [x for x in self.bookmarks if x['title'] !=
|
||||||
|
@ -124,9 +124,10 @@ class MergeMetadata(object):
|
|||||||
self.oeb.metadata.add('identifier', mi.application_id, scheme='calibre')
|
self.oeb.metadata.add('identifier', mi.application_id, scheme='calibre')
|
||||||
|
|
||||||
def set_cover(self, mi, prefer_metadata_cover):
|
def set_cover(self, mi, prefer_metadata_cover):
|
||||||
cdata, ext = '', 'jpg'
|
cdata, ext = b'', 'jpg'
|
||||||
if mi.cover and os.access(mi.cover, os.R_OK):
|
if mi.cover and os.access(mi.cover, os.R_OK):
|
||||||
cdata = open(mi.cover, 'rb').read()
|
with open(mi.cover, 'rb') as f:
|
||||||
|
cdata = f.read()
|
||||||
ext = mi.cover.rpartition('.')[-1].lower().strip()
|
ext = mi.cover.rpartition('.')[-1].lower().strip()
|
||||||
elif mi.cover_data and mi.cover_data[-1]:
|
elif mi.cover_data and mi.cover_data[-1]:
|
||||||
cdata = mi.cover_data[1]
|
cdata = mi.cover_data[1]
|
||||||
@ -137,7 +138,7 @@ class MergeMetadata(object):
|
|||||||
if 'cover' in self.oeb.guide:
|
if 'cover' in self.oeb.guide:
|
||||||
old_cover = self.oeb.guide['cover']
|
old_cover = self.oeb.guide['cover']
|
||||||
if prefer_metadata_cover and old_cover is not None:
|
if prefer_metadata_cover and old_cover is not None:
|
||||||
cdata = ''
|
cdata = b''
|
||||||
if cdata:
|
if cdata:
|
||||||
self.oeb.guide.remove('cover')
|
self.oeb.guide.remove('cover')
|
||||||
self.oeb.guide.remove('titlepage')
|
self.oeb.guide.remove('titlepage')
|
||||||
|
@ -368,15 +368,13 @@ if __name__ == "__main__":
|
|||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
print("Usage %prog rtfFileToConvert")
|
print("Usage %prog rtfFileToConvert")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
f = open(sys.argv[1], 'rb')
|
with open(sys.argv[1], 'rb') as f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
f.close()
|
|
||||||
|
|
||||||
tokenizer = RtfTokenizer(data)
|
tokenizer = RtfTokenizer(data)
|
||||||
parsedTokens = RtfTokenParser(tokenizer.tokens)
|
parsedTokens = RtfTokenParser(tokenizer.tokens)
|
||||||
|
|
||||||
data = parsedTokens.toRTF()
|
data = parsedTokens.toRTF()
|
||||||
|
|
||||||
f = open(sys.argv[1], 'w')
|
with open(sys.argv[1], 'w') as f:
|
||||||
f.write(data)
|
f.write(data)
|
||||||
f.close()
|
|
||||||
|
@ -39,10 +39,9 @@ class SNBFile:
|
|||||||
def Open(self, inputFile):
|
def Open(self, inputFile):
|
||||||
self.fileName = inputFile
|
self.fileName = inputFile
|
||||||
|
|
||||||
snbFile = open(self.fileName, "rb")
|
with open(self.fileName, "rb") as f:
|
||||||
snbFile.seek(0)
|
f.seek(0)
|
||||||
self.Parse(snbFile)
|
self.Parse(f)
|
||||||
snbFile.close()
|
|
||||||
|
|
||||||
def Parse(self, snbFile, metaOnly=False):
|
def Parse(self, snbFile, metaOnly=False):
|
||||||
# Read header
|
# Read header
|
||||||
@ -158,7 +157,8 @@ class SNBFile:
|
|||||||
f = FileStream()
|
f = FileStream()
|
||||||
f.attr = 0x41000000
|
f.attr = 0x41000000
|
||||||
f.fileSize = os.path.getsize(os.path.join(tdir,fileName))
|
f.fileSize = os.path.getsize(os.path.join(tdir,fileName))
|
||||||
f.fileBody = open(os.path.join(tdir,fileName), 'rb').read()
|
with open(os.path.join(tdir,fileName), 'rb') as data:
|
||||||
|
f.fileBody = data.read()
|
||||||
f.fileName = fileName.replace(os.sep, '/')
|
f.fileName = fileName.replace(os.sep, '/')
|
||||||
if isinstance(f.fileName, unicode_type):
|
if isinstance(f.fileName, unicode_type):
|
||||||
f.fileName = f.fileName.encode("ascii", "ignore")
|
f.fileName = f.fileName.encode("ascii", "ignore")
|
||||||
@ -168,7 +168,8 @@ class SNBFile:
|
|||||||
f = FileStream()
|
f = FileStream()
|
||||||
f.attr = 0x01000000
|
f.attr = 0x01000000
|
||||||
f.fileSize = os.path.getsize(os.path.join(tdir,fileName))
|
f.fileSize = os.path.getsize(os.path.join(tdir,fileName))
|
||||||
f.fileBody = open(os.path.join(tdir,fileName), 'rb').read()
|
with open(os.path.join(tdir,fileName), 'rb') as data:
|
||||||
|
f.fileBody = data.read()
|
||||||
f.fileName = fileName.replace(os.sep, '/')
|
f.fileName = fileName.replace(os.sep, '/')
|
||||||
if isinstance(f.fileName, unicode_type):
|
if isinstance(f.fileName, unicode_type):
|
||||||
f.fileName = f.fileName.encode("ascii", "ignore")
|
f.fileName = f.fileName.encode("ascii", "ignore")
|
||||||
@ -186,9 +187,8 @@ class SNBFile:
|
|||||||
fname = os.path.basename(f.fileName)
|
fname = os.path.basename(f.fileName)
|
||||||
root, ext = os.path.splitext(fname)
|
root, ext = os.path.splitext(fname)
|
||||||
if ext in ['.jpeg', '.jpg', '.gif', '.svg', '.png']:
|
if ext in ['.jpeg', '.jpg', '.gif', '.svg', '.png']:
|
||||||
file = open(os.path.join(path, fname), 'wb')
|
with open(os.path.join(path, fname), 'wb') as outfile:
|
||||||
file.write(f.fileBody)
|
outfile.write(f.fileBody)
|
||||||
file.close()
|
|
||||||
fileNames.append((fname, guess_type('a'+ext)[0]))
|
fileNames.append((fname, guess_type('a'+ext)[0]))
|
||||||
return fileNames
|
return fileNames
|
||||||
|
|
||||||
@ -297,9 +297,8 @@ class SNBFile:
|
|||||||
print("File Size: ", f.fileSize)
|
print("File Size: ", f.fileSize)
|
||||||
print("Block Index: ", f.blockIndex)
|
print("Block Index: ", f.blockIndex)
|
||||||
print("Content Offset: ", f.contentOffset)
|
print("Content Offset: ", f.contentOffset)
|
||||||
tempFile = open("/tmp/" + f.fileName, 'wb')
|
with open("/tmp/" + f.fileName, 'wb') as tempFile:
|
||||||
tempFile.write(f.fileBody)
|
tempFile.write(f.fileBody)
|
||||||
tempFile.close()
|
|
||||||
|
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
|
@ -937,7 +937,8 @@ class EditMetadataAction(InterfaceAction):
|
|||||||
if old != prefs['read_file_metadata']:
|
if old != prefs['read_file_metadata']:
|
||||||
prefs['read_file_metadata'] = old
|
prefs['read_file_metadata'] = old
|
||||||
if mi.cover and os.access(mi.cover, os.R_OK):
|
if mi.cover and os.access(mi.cover, os.R_OK):
|
||||||
cdata = open(mi.cover).read()
|
with open(mi.cover, 'rb') as f:
|
||||||
|
cdata = f.read()
|
||||||
elif mi.cover_data[1] is not None:
|
elif mi.cover_data[1] is not None:
|
||||||
cdata = mi.cover_data[1]
|
cdata = mi.cover_data[1]
|
||||||
if cdata is None:
|
if cdata is None:
|
||||||
|
@ -198,10 +198,10 @@ class MetadataWidget(Widget, Ui_Form):
|
|||||||
_('You do not have permission to read the file: ') + _file)
|
_('You do not have permission to read the file: ') + _file)
|
||||||
d.exec_()
|
d.exec_()
|
||||||
return
|
return
|
||||||
cf, cover = None, None
|
cover = None
|
||||||
try:
|
try:
|
||||||
cf = open(_file, "rb")
|
with open(_file, "rb") as f:
|
||||||
cover = cf.read()
|
cover = f.read()
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
d = error_dialog(self.parent(), _('Error reading file'),
|
d = error_dialog(self.parent(), _('Error reading file'),
|
||||||
_("<p>There was an error reading from file: <br /><b>") + _file + "</b></p><br />"+str(e))
|
_("<p>There was an error reading from file: <br /><b>") + _file + "</b></p><br />"+str(e))
|
||||||
|
@ -606,7 +606,8 @@ class CustomRecipes(Dialog):
|
|||||||
if files:
|
if files:
|
||||||
path = files[0]
|
path = files[0]
|
||||||
try:
|
try:
|
||||||
src = open(path, 'rb').read().decode('utf-8')
|
with open(path, 'rb') as f:
|
||||||
|
src = f.read().decode('utf-8')
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
error_dialog(self, _('Invalid input'),
|
error_dialog(self, _('Invalid input'),
|
||||||
_('<p>Could not create recipe. Error:<br>%s')%err, show=True)
|
_('<p>Could not create recipe. Error:<br>%s')%err, show=True)
|
||||||
|
@ -577,7 +577,8 @@ if __name__ == '__main__':
|
|||||||
from PyQt5.Qt import QErrorMessage
|
from PyQt5.Qt import QErrorMessage
|
||||||
logfile = os.path.join(os.path.expanduser('~'), 'calibre.log')
|
logfile = os.path.join(os.path.expanduser('~'), 'calibre.log')
|
||||||
if os.path.exists(logfile):
|
if os.path.exists(logfile):
|
||||||
log = open(logfile).read().decode('utf-8', 'ignore')
|
with open(logfile) as f:
|
||||||
|
log = f.read().decode('utf-8', 'ignore')
|
||||||
d = QErrorMessage()
|
d = QErrorMessage()
|
||||||
d.showMessage(('<b>Error:</b>%s<br><b>Traceback:</b><br>'
|
d.showMessage(('<b>Error:</b>%s<br><b>Traceback:</b><br>'
|
||||||
'%s<b>Log:</b><br>%s')%(unicode_type(err),
|
'%s<b>Log:</b><br>%s')%(unicode_type(err),
|
||||||
|
@ -1168,10 +1168,10 @@ class Cover(ImageView): # {{{
|
|||||||
_('You do not have permission to read the file: ') + _file)
|
_('You do not have permission to read the file: ') + _file)
|
||||||
d.exec_()
|
d.exec_()
|
||||||
return
|
return
|
||||||
cf, cover = None, None
|
cover = None
|
||||||
try:
|
try:
|
||||||
cf = open(_file, "rb")
|
with open(_file, "rb") as f:
|
||||||
cover = cf.read()
|
cover = f.read()
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
d = error_dialog(
|
d = error_dialog(
|
||||||
self, _('Error reading file'),
|
self, _('Error reading file'),
|
||||||
|
@ -461,7 +461,8 @@ class MetadataSingleDialogBase(QDialog):
|
|||||||
return
|
return
|
||||||
cdata = None
|
cdata = None
|
||||||
if mi.cover and os.access(mi.cover, os.R_OK):
|
if mi.cover and os.access(mi.cover, os.R_OK):
|
||||||
cdata = open(mi.cover).read()
|
with open(mi.cover, 'rb') as f:
|
||||||
|
cdata = f.read()
|
||||||
elif mi.cover_data[1] is not None:
|
elif mi.cover_data[1] is not None:
|
||||||
cdata = mi.cover_data[1]
|
cdata = mi.cover_data[1]
|
||||||
if cdata is None:
|
if cdata is None:
|
||||||
|
@ -139,7 +139,8 @@ def string_diff(left, right, left_syntax=None, right_syntax=None, left_name='lef
|
|||||||
def file_diff(left, right):
|
def file_diff(left, right):
|
||||||
(raw1, syntax1), (raw2, syntax2) = map(get_decoded_raw, (left, right))
|
(raw1, syntax1), (raw2, syntax2) = map(get_decoded_raw, (left, right))
|
||||||
if type(raw1) is not type(raw2):
|
if type(raw1) is not type(raw2):
|
||||||
raw1, raw2 = open(left, 'rb').read(), open(right, 'rb').read()
|
with open(left, 'rb') as f1, open(right, 'rb') as f2:
|
||||||
|
raw1, raw2 = f1.read(), f2.read()
|
||||||
cache = Cache()
|
cache = Cache()
|
||||||
cache.set_left(left, raw1), cache.set_right(right, raw2)
|
cache.set_left(left, raw1), cache.set_right(right, raw2)
|
||||||
changed_names = {} if raw1 == raw2 else {left:right}
|
changed_names = {} if raw1 == raw2 else {left:right}
|
||||||
|
@ -606,7 +606,8 @@ def profile():
|
|||||||
from calibre.gui2.tweak_book.editor.themes import get_theme
|
from calibre.gui2.tweak_book.editor.themes import get_theme
|
||||||
app = Application([])
|
app = Application([])
|
||||||
set_book_locale('en')
|
set_book_locale('en')
|
||||||
raw = open(sys.argv[-2], 'rb').read().decode('utf-8')
|
with open(sys.argv[-2], 'rb') as f:
|
||||||
|
raw = f.read().decode('utf-8')
|
||||||
doc = QTextDocument()
|
doc = QTextDocument()
|
||||||
doc.setPlainText(raw)
|
doc.setPlainText(raw)
|
||||||
h = Highlighter()
|
h = Highlighter()
|
||||||
|
@ -487,9 +487,10 @@ def test():
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
import sys, os
|
import sys, os
|
||||||
for f in sys.argv[1:]:
|
for arg in sys.argv[1:]:
|
||||||
print(os.path.basename(f))
|
print(os.path.basename(arg))
|
||||||
raw = open(f, 'rb').read()
|
with open(arg, 'rb') as f:
|
||||||
|
raw = f.read()
|
||||||
print(get_font_names(raw))
|
print(get_font_names(raw))
|
||||||
characs = get_font_characteristics(raw)
|
characs = get_font_characteristics(raw)
|
||||||
print(characs)
|
print(characs)
|
||||||
|
@ -147,9 +147,10 @@ def load_winfonts():
|
|||||||
|
|
||||||
|
|
||||||
def test_ttf_reading():
|
def test_ttf_reading():
|
||||||
for f in sys.argv[1:]:
|
for arg in sys.argv[1:]:
|
||||||
raw = open(f).read()
|
with open(arg, 'rb') as f:
|
||||||
print(os.path.basename(f))
|
raw = f.read()
|
||||||
|
print(os.path.basename(arg))
|
||||||
get_font_characteristics(raw)
|
get_font_characteristics(raw)
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
@ -70,7 +70,8 @@ else:
|
|||||||
|
|
||||||
def get_default_route_src_address():
|
def get_default_route_src_address():
|
||||||
# Use /proc/net/ipv6_route for IPv6 addresses
|
# Use /proc/net/ipv6_route for IPv6 addresses
|
||||||
raw = open('/proc/net/route', 'rb').read().decode('utf-8')
|
with open('/proc/net/route', 'rb') as f:
|
||||||
|
raw = f.read().decode('utf-8')
|
||||||
for line in raw.splitlines():
|
for line in raw.splitlines():
|
||||||
parts = line.split()
|
parts = line.split()
|
||||||
if len(parts) > 1 and parts[1] == '00000000':
|
if len(parts) > 1 and parts[1] == '00000000':
|
||||||
|
@ -1299,7 +1299,8 @@ class BasicNewsRecipe(Recipe):
|
|||||||
cdata = cu.read()
|
cdata = cu.read()
|
||||||
cu = getattr(cu, 'name', 'cover.jpg')
|
cu = getattr(cu, 'name', 'cover.jpg')
|
||||||
elif os.access(cu, os.R_OK):
|
elif os.access(cu, os.R_OK):
|
||||||
cdata = open(cu, 'rb').read()
|
with open(cu, 'rb') as f:
|
||||||
|
cdata = f.read()
|
||||||
else:
|
else:
|
||||||
self.report_progress(1, _('Downloading cover from %s')%cu)
|
self.report_progress(1, _('Downloading cover from %s')%cu)
|
||||||
with closing(self.browser.open(cu)) as r:
|
with closing(self.browser.open(cu)) as r:
|
||||||
|
@ -114,7 +114,8 @@ def get_custom_recipe_collection(*args):
|
|||||||
title, fname = x
|
title, fname = x
|
||||||
recipe = os.path.join(bdir, fname)
|
recipe = os.path.join(bdir, fname)
|
||||||
try:
|
try:
|
||||||
recipe = open(recipe, 'rb').read().decode('utf-8')
|
with open(recipe, 'rb') as f:
|
||||||
|
recipe = f.read().decode('utf-8')
|
||||||
recipe_class = compile_recipe(recipe)
|
recipe_class = compile_recipe(recipe)
|
||||||
if recipe_class is not None:
|
if recipe_class is not None:
|
||||||
rmap['custom:%s'%id_] = recipe_class
|
rmap['custom:%s'%id_] = recipe_class
|
||||||
|
@ -35,7 +35,8 @@ HOST = 'ox'
|
|||||||
|
|
||||||
base = os.path.dirname(os.path.abspath(__file__))
|
base = os.path.dirname(os.path.abspath(__file__))
|
||||||
if True:
|
if True:
|
||||||
raw = open(os.path.join(base, 'src/calibre/constants.py')).read()
|
with open(os.path.join(base, 'src/calibre/constants.py')) as f:
|
||||||
|
raw = f.read()
|
||||||
v = re.search(r'numeric_version\s*=\s*(\(.+?\))', raw).group(1)
|
v = re.search(r'numeric_version\s*=\s*(\(.+?\))', raw).group(1)
|
||||||
v = '.'.join(map(str, ast.literal_eval(v)))
|
v = '.'.join(map(str, ast.literal_eval(v)))
|
||||||
dmg = f'calibre-{v}.dmg'
|
dmg = f'calibre-{v}.dmg'
|
||||||
@ -47,7 +48,8 @@ def run(what):
|
|||||||
raise SystemExit(ret.returncode)
|
raise SystemExit(ret.returncode)
|
||||||
|
|
||||||
|
|
||||||
script = open(__file__, 'rb').read().decode('utf-8')
|
with open(__file__, 'rb') as f:
|
||||||
|
script = f.read().decode('utf-8')
|
||||||
script = script[:script.find('# EOF_REMOTE')].replace('if False:', 'if True:', 1)
|
script = script[:script.find('# EOF_REMOTE')].replace('if False:', 'if True:', 1)
|
||||||
os.chdir(os.path.expanduser('~/work/build-calibre'))
|
os.chdir(os.path.expanduser('~/work/build-calibre'))
|
||||||
with tempfile.NamedTemporaryFile(prefix='install-dmg-', suffix='.py') as f:
|
with tempfile.NamedTemporaryFile(prefix='install-dmg-', suffix='.py') as f:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user