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