mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
DRYer
This commit is contained in:
parent
6d3fcc853e
commit
d9ab752f94
@ -11,6 +11,7 @@ import sys, os, functools
|
||||
from calibre.utils.config import OptionParser
|
||||
from calibre.constants import iswindows
|
||||
from calibre import prints
|
||||
from polyglot.builtins import exec_path
|
||||
|
||||
|
||||
def get_debug_executable():
|
||||
@ -248,8 +249,7 @@ def run_script(path, args):
|
||||
g = globals()
|
||||
g['__name__'] = '__main__'
|
||||
g['__file__'] = ef
|
||||
with open(ef, 'rb') as f:
|
||||
exec(compile(f.read(), ef, 'exec'), g)
|
||||
exec_path(ef, g)
|
||||
|
||||
|
||||
def inspect_mobi(path):
|
||||
|
@ -42,7 +42,7 @@ from calibre.utils.icu import capitalize, collation_order, sort_key
|
||||
from calibre.utils.img import scale_image
|
||||
from calibre.utils.localization import get_lang, lang_as_iso639_1
|
||||
from calibre.utils.zipfile import ZipFile
|
||||
from polyglot.builtins import unicode_type, iteritems
|
||||
from polyglot.builtins import unicode_type, iteritems, exec_path
|
||||
|
||||
NBSP = u'\u00a0'
|
||||
|
||||
@ -4616,9 +4616,7 @@ class CatalogBuilder(object):
|
||||
"""
|
||||
|
||||
templates = {}
|
||||
ef = P('catalog/section_list_templates.py')
|
||||
with open(ef, 'rb') as f:
|
||||
exec(compile(f.read(), ef, 'exec'), templates)
|
||||
exec_path(P('catalog/section_list_templates.py'), templates)
|
||||
for name, template in iteritems(templates):
|
||||
if name.startswith('by_') and name.endswith('_template'):
|
||||
setattr(self, name, force_unicode(template, 'utf-8'))
|
||||
|
@ -9,6 +9,7 @@ __docformat__ = 'restructuredtext en'
|
||||
|
||||
import os, re, sys
|
||||
from calibre.constants import iswindows, cache_dir, get_version
|
||||
from polyglot.builtins import exec_path
|
||||
|
||||
ipydir = os.path.join(cache_dir(), 'ipython')
|
||||
|
||||
@ -214,8 +215,7 @@ def ipython(user_ns=None):
|
||||
c = Config()
|
||||
user_conf = os.path.expanduser('~/.ipython/profile_default/ipython_config.py')
|
||||
if os.path.exists(user_conf):
|
||||
with open(user_conf, 'rb') as f:
|
||||
exec(compile(f.read(), user_conf, 'exec'), {'get_config': lambda: c})
|
||||
exec_path(user_conf, {'get_config': lambda: c})
|
||||
c.TerminalInteractiveShell.prompts_class = CustomPrompt
|
||||
c.InteractiveShellApp.exec_lines = [
|
||||
'from __future__ import division, absolute_import, unicode_literals, print_function',
|
||||
|
@ -22,7 +22,7 @@ from calibre.utils.filenames import atomic_rename
|
||||
from calibre.utils.terminal import ANSIStream
|
||||
from duktape import Context, JSError, to_python
|
||||
from lzma.xz import compress, decompress
|
||||
from polyglot.builtins import itervalues, range
|
||||
from polyglot.builtins import itervalues, range, exec_path
|
||||
from polyglot.queue import Empty, Queue
|
||||
|
||||
COMPILER_PATH = 'rapydscript/compiler.js.xz'
|
||||
@ -203,8 +203,7 @@ def compile_srv():
|
||||
base = base_dir()
|
||||
iconf = os.path.join(base, 'imgsrc', 'srv', 'generate.py')
|
||||
g = {'__file__': iconf}
|
||||
with open(iconf, 'rb') as f:
|
||||
exec(compile(f.read(), iconf, 'exec'), g)
|
||||
exec_path(iconf, g)
|
||||
icons = g['merge']().encode('utf-8')
|
||||
with lopen(os.path.join(base, 'resources', 'content-server', 'reset.css'), 'rb') as f:
|
||||
reset = f.read()
|
||||
|
@ -47,6 +47,14 @@ if is_py3:
|
||||
if isinstance(x, bytes):
|
||||
x = x.decode('utf-8')
|
||||
return x
|
||||
|
||||
def exec_path(path, ctx=None):
|
||||
ctx = ctx or {}
|
||||
with open(path, 'rb') as f:
|
||||
code = f.read()
|
||||
code = compile(code, f.name, 'exec')
|
||||
exec(code, ctx)
|
||||
|
||||
else:
|
||||
exec("""def reraise(tp, value, tb=None):
|
||||
try:
|
||||
@ -63,6 +71,7 @@ else:
|
||||
unicode_type = unicode
|
||||
string_or_bytes = unicode, bytes
|
||||
long_type = long
|
||||
exec_path = execfile
|
||||
|
||||
def iteritems(d):
|
||||
return d.iteritems()
|
||||
|
Loading…
x
Reference in New Issue
Block a user