def render_html_data(path_to_html, width, height): from calibre.ptempfile import TemporaryDirectory from calibre.utils.ipc.simple_worker import fork_job, WorkerError result = {} def report_error(text=''): prints('Failed to render', path_to_html, 'with errors:', file=sys.stderr) if text: prints(text, file=sys.stderr) if result and result['stdout_stderr']: with open(result['stdout_stderr'], 'rb') as f: prints(f.read(), file=sys.stderr) with TemporaryDirectory('-render-html') as tdir: try: result = fork_job('calibre.ebooks.render_html', 'main', args=(path_to_html, tdir, 'jpeg')) except WorkerError as e: report_error(e.orig_tb) else: if result['result']: with open(os.path.join(tdir, 'rendered.jpeg'), 'rb') as f: return f.read() else: report_error()
def __enter__(self, *args): ''' Add this plugin to the python path so that it's contents become directly importable. Useful when bundling large python libraries into the plugin. Use it like this:: with plugin: import something ''' if self.plugin_path is not None: from calibre.utils.zipfile import ZipFile zf = ZipFile(self.plugin_path) extensions = set([x.rpartition('.')[-1].lower() for x in zf.namelist()]) zip_safe = True for ext in ('pyd', 'so', 'dll', 'dylib'): if ext in extensions: zip_safe = False break if zip_safe: sys.path.insert(0, self.plugin_path) self.sys_insertion_path = self.plugin_path else: from calibre.ptempfile import TemporaryDirectory self._sys_insertion_tdir = TemporaryDirectory('plugin_unzip') self.sys_insertion_path = self._sys_insertion_tdir.__enter__(*args) zf.extractall(self.sys_insertion_path) sys.path.insert(0, self.sys_insertion_path) zf.close()
def download_external_resources(container, urls, timeout=60, progress_report=lambda url, done, total: None): failures = {} replacements = {} data_uri_map = {} with TemporaryDirectory('editor-download') as tdir: pool = Pool(10) with closing(pool): for ok, result in pool.imap_unordered( partial(download_one, tdir, timeout, progress_report, data_uri_map), urls): if ok: url, suggested_filename, downloaded_file, mt = result with lopen(downloaded_file, 'rb') as src: name = container.add_file(suggested_filename, src, mt, modify_name_if_needed=True) replacements[url] = name else: url, err = result failures[url] = err return replacements, failures
def test(): # {{{ from calibre.ptempfile import TemporaryDirectory from calibre import CurrentDir from glob import glob img = image_from_data(I('lt.png', data=True, allow_user_override=False)) with TemporaryDirectory() as tdir, CurrentDir(tdir): save_image(img, 'test.jpg') ret = optimize_jpeg('test.jpg') if ret is not None: raise SystemExit('optimize_jpeg failed: %s' % ret) ret = encode_jpeg('test.jpg') if ret is not None: raise SystemExit('encode_jpeg failed: %s' % ret) shutil.copyfile(I('lt.png'), 'test.png') ret = optimize_png('test.png') if ret is not None: raise SystemExit('optimize_png failed: %s' % ret) if glob('*.bak'): raise SystemExit('Spurious .bak files left behind') quantize_image(img) oil_paint_image(img) gaussian_sharpen_image(img) gaussian_blur_image(img) despeckle_image(img) remove_borders_from_image(img) image_to_data(img, fmt='GIF') raw = subprocess.Popen([get_exe_path('JxrDecApp'), '-h'], creationflags=0x08 if iswindows else 0, stdout=subprocess.PIPE).stdout.read() if b'JPEG XR Decoder Utility' not in raw: raise SystemExit('Failed to run JxrDecApp')
def serve(self): self.connection_map = {} self.socket.listen(min(socket.SOMAXCONN, 128)) self.bound_address = ba = self.socket.getsockname() if isinstance(ba, tuple): ba = ':'.join(map(type(''), ba)) self.pool.start() with TemporaryDirectory(prefix='srv-') as tdir: self.tdir = tdir self.ready = True if self.LISTENING_MSG: self.log(self.LISTENING_MSG, ba) self.plugin_pool.start() while self.ready: try: self.tick() except SystemExit: self.shutdown() raise except KeyboardInterrupt: break except: self.log.exception('Error in ServerLoop.tick') self.shutdown()
def test_log_rotation(self): 'Test log rotation' from calibre.srv.utils import RotatingLog from calibre.ptempfile import TemporaryDirectory with TemporaryDirectory() as tdir: fname = os.path.join(tdir, 'log') l = RotatingLog(fname, max_size=100) def history(): return {int(x.rpartition('.')[-1]) for x in glob(fname + '.*')} def log_size(): ssize = l.outputs[0].stream.tell() self.ae(ssize, l.outputs[0].current_pos) self.ae(ssize, os.path.getsize(fname)) return ssize self.ae(log_size(), 0) l('a' * 99) self.ae(log_size(), 100) l('b'), l('c') self.ae(log_size(), 2) self.ae(history(), {1}) for i in 'abcdefg': l(i * 101) self.assertLessEqual(log_size(), 100) self.ae(history(), {1, 2, 3, 4, 5})
def import_book_as_epub(srcpath, destpath, log=default_log): if not destpath.lower().endswith('.epub'): raise ValueError('Can only import books into the EPUB format, not %s' % (os.path.basename(destpath))) with TemporaryDirectory('eei') as tdir: tdir = os.path.abspath(os.path.realpath(tdir)) # Needed to handle the multiple levels of symlinks for /tmp on OS X plumber = Plumber(srcpath, tdir, log) plumber.setup_options() if srcpath.lower().endswith('.opf'): plumber.opts.dont_package = True if hasattr(plumber.opts, 'no_process'): plumber.opts.no_process = True plumber.input_plugin.for_viewer = True with plumber.input_plugin, open(plumber.input, 'rb') as inf: pathtoopf = plumber.input_plugin(inf, plumber.opts, plumber.input_fmt, log, {}, tdir) if hasattr(pathtoopf, 'manifest'): from calibre.ebooks.oeb.iterator.book import write_oebbook pathtoopf = write_oebbook(pathtoopf, tdir) c = Container(tdir, pathtoopf, log) auto_fill_manifest(c) # Auto fix all HTML/CSS for name, mt in iteritems(c.mime_map): if mt in set(OEB_DOCS) | set(OEB_STYLES): c.parsed(name) c.dirty(name) c.commit() zf = initialize_container(destpath, opf_name=c.opf_name) with zf: for name in c.name_path_map: zf.writestr(name, c.raw_data(name, decode=False))
def get_metadata(stream): from calibre.ebooks.metadata import MetaInformation from calibre.ptempfile import TemporaryDirectory from calibre.ebooks.mobi.reader.headers import MetadataHeader from calibre.ebooks.mobi.reader.mobi6 import MobiReader from calibre.utils.img import save_cover_data_to from calibre import CurrentDir stream.seek(0) try: raw = stream.read(3) except Exception: raw = b'' stream.seek(0) if raw == b'TPZ': from calibre.ebooks.metadata.topaz import get_metadata return get_metadata(stream) from calibre.utils.logging import Log log = Log() try: mi = MetaInformation(os.path.basename(stream.name), [_('Unknown')]) except: mi = MetaInformation(_('Unknown'), [_('Unknown')]) mh = MetadataHeader(stream, log) if mh.title and mh.title != _('Unknown'): mi.title = mh.title if mh.exth is not None: if mh.exth.mi is not None: mi = mh.exth.mi else: size = 1024**3 if hasattr(stream, 'seek') and hasattr(stream, 'tell'): pos = stream.tell() stream.seek(0, 2) size = stream.tell() stream.seek(pos) if size < 4 * 1024 * 1024: with TemporaryDirectory('_mobi_meta_reader') as tdir: with CurrentDir(tdir): mr = MobiReader(stream, log) parse_cache = {} mr.extract_content(tdir, parse_cache) if mr.embedded_mi is not None: mi = mr.embedded_mi if hasattr(mh.exth, 'cover_offset'): cover_index = mh.first_image_index + mh.exth.cover_offset data = mh.section_data(int(cover_index)) else: try: data = mh.section_data(mh.first_image_index) except Exception: data = b'' if data and what(None, data) in {'jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp'}: try: mi.cover_data = ('jpg', save_cover_data_to(data)) except Exception: log.exception('Failed to read MOBI cover') return mi
def convert_text(self, oeb_book): import json from calibre.ebooks.pdf.html_writer import convert self.get_cover_data() self.process_fonts() if self.opts.pdf_use_document_margins and self.stored_page_margins: for href, margins in iteritems(self.stored_page_margins): item = oeb_book.manifest.hrefs.get(href) if item is not None: root = item.data if hasattr(root, 'xpath') and margins: root.set('data-calibre-pdf-output-page-margins', json.dumps(margins)) with TemporaryDirectory('_pdf_out') as oeb_dir: from calibre.customize.ui import plugin_for_output_format oeb_dir = os.path.realpath(oeb_dir) oeb_output = plugin_for_output_format('oeb') oeb_output.convert(oeb_book, oeb_dir, self.input_plugin, self.opts, self.log) opfpath = glob.glob(os.path.join(oeb_dir, '*.opf'))[0] convert(opfpath, self.opts, metadata=self.metadata, output_path=self.output_path, log=self.log, cover_data=self.cover_data, report_progress=self.report_progress)
def format_group(db, notify_changes, is_remote, args): formats, add_duplicates, oautomerge, request_id, cover_data = args with add_ctx(), TemporaryDirectory( 'add-multiple') as tdir, run_import_plugins_before_metadata(tdir): updated_ids = {} if is_remote: paths = [] for name, data in formats: with lopen(os.path.join(tdir, os.path.basename(name)), 'wb') as f: f.write(data) paths.append(f.name) else: paths = list(formats) paths = run_import_plugins(paths) mi = metadata_from_formats(paths) if mi.title is None: return None, set(), set(), False if cover_data and not mi.cover_data or not mi.cover_data[1]: mi.cover_data = 'jpeg', cover_data format_map = create_format_map(paths) added_ids, updated_ids, duplicates = do_adding( db, request_id, notify_changes, is_remote, mi, format_map, add_duplicates, oautomerge) return mi.title, set(added_ids), set(updated_ids), bool(duplicates)
def test(): import repr as reprlib def eq(x, y): if x != y: raise AssertionError('%s != %s' % (reprlib.repr(x), reprlib.repr(y))) from calibre.ptempfile import TemporaryDirectory with TemporaryDirectory() as tdir: fname = os.path.join(tdir, 'test.txt') with share_open(fname, 'wb') as f: f.write(b'a' * 20 * 1024) eq(fname, f.name) f = share_open(fname, 'rb') eq(f.read(1), b'a') if iswindows: os.rename(fname, fname + '.moved') os.remove(fname + '.moved') else: os.remove(fname) eq(f.read(1), b'a') f2 = share_open(fname, 'w+b') f2.write(b'b' * 10 * 1024) f2.seek(0) eq(f.read(10000), b'a' * 10000) eq(f2.read(100), b'b' * 100) f3 = share_open(fname, 'rb') eq(f3.read(100), b'b' * 100)
def test_ssl(self): 'Test serving over SSL' s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM, 0) s.bind(('localhost', 0)) address = s.getsockname()[0] with TemporaryDirectory('srv-test-ssl') as tdir: cert_file, key_file, ca_file = map(lambda x: os.path.join(tdir, x), 'cka') create_server_cert(address, ca_file, cert_file, key_file, key_size=1024) ctx = ssl.create_default_context(cafile=ca_file) with TestServer(lambda data: (data.path[0] + data.read()), ssl_certfile=cert_file, ssl_keyfile=key_file) as server: conn = httplib.HTTPSConnection(server.address[0], server.address[1], strict=True, context=ctx) conn.request('GET', '/test', 'body') r = conn.getresponse() self.ae(r.status, httplib.OK) self.ae(r.read(), b'testbody') cert = conn.sock.getpeercert() subject = dict(x[0] for x in cert['subject']) self.ae(subject['commonName'], address)
def format_group(db, notify_changes, is_remote, args): formats, add_duplicates, cover_data = args with add_ctx(), TemporaryDirectory( 'add-multiple') as tdir, run_import_plugins_before_metadata(tdir): if is_remote: paths = [] for name, data in formats: with lopen(os.path.join(tdir, os.path.basename(name)), 'wb') as f: f.write(data) paths.append(f.name) else: paths = list(formats) paths = run_import_plugins(paths) mi = metadata_from_formats(paths) if mi.title is None: return None, set(), False if cover_data and not mi.cover_data or not mi.cover_data[1]: mi.cover_data = 'jpeg', cover_data ids, dups = db.add_books([(mi, create_format_map(paths))], add_duplicates=add_duplicates, run_hooks=False) if is_remote: notify_changes(books_added(ids)) db.dump_metadata() return mi.title, ids, bool(dups)
def about(self): ''' Display a short help message ''' from os.path import join from calibre.ptempfile import TemporaryDirectory from calibre.gui2.dialogs.message_box import MessageBox from calibre_plugins.prince_pdf.help import help_txt, license_txt from calibre_plugins.prince_pdf import PrincePDFPlugin from calibre_plugins.prince_pdf import __license__ author = PrincePDFPlugin.author version = "%i.%i.%i" % PrincePDFPlugin.version license = __license__ with TemporaryDirectory('xxx') as tdir: for x in ('prince_icon.png', 'small_icon.png'): with open(join(tdir, x), 'w') as f: f.write(get_resources('images/' + x)) help_box = MessageBox(type_ = MessageBox.INFO, \ title = _('About the Prince PDF Plugin'), \ msg = help_txt % {'author':author, 'version':version, 'license':license, 'dir':tdir, 'code':'style="font-family:monospace ; font-weight:bold"'}, \ det_msg = 'Copyright \u00a9 %s\n%s' % (__copyright__, license_txt), \ q_icon = self.icon, \ show_copy_button = False) #help_box.gridLayout.addWidget(help_box.icon_widget,0,0,Qt.AlignTop) help_box.gridLayout.setAlignment(help_box.icon_widget, Qt.AlignTop) help_box.exec_()
def convert_text(self, oeb_book): from calibre.ebooks.metadata.opf2 import OPF from calibre.ebooks.pdf.render.from_html import PDFWriter self.log.debug('Serializing oeb input to disk for processing...') self.get_cover_data() self.process_fonts() if self.opts.pdf_use_document_margins and self.stored_page_margins: import json for href, margins in self.stored_page_margins.iteritems(): item = oeb_book.manifest.hrefs.get(href) root = item.data if hasattr(root, 'xpath') and margins: root.set('data-calibre-pdf-output-page-margins', json.dumps(margins)) with TemporaryDirectory('_pdf_out') as oeb_dir: from calibre.customize.ui import plugin_for_output_format oeb_output = plugin_for_output_format('oeb') oeb_output.convert(oeb_book, oeb_dir, self.input_plugin, self.opts, self.log) opfpath = glob.glob(os.path.join(oeb_dir, '*.opf'))[0] opf = OPF(opfpath, os.path.dirname(opfpath)) self.write(PDFWriter, [s.path for s in opf.spine], getattr(opf, 'toc', None))
def get_icon(path, pixmap_to_data=None, as_data=False, size=64): if not path: return with TemporaryDirectory() as tdir: iconset = os.path.join(tdir, 'output.iconset') try: subprocess.check_call( ['iconutil', '-c', 'iconset', '-o', 'output.iconset', path], cwd=tdir) except subprocess.CalledProcessError: return try: names = os.listdir(iconset) except EnvironmentError: return if not names: return from PyQt5.Qt import QImage, Qt names.sort(key=numeric_sort_key) for name in names: m = re.search('(\d+)x\d+', name) if m is not None and int(m.group(1)) >= size: ans = QImage(os.path.join(iconset, name)) if not ans.isNull(): break else: return ans = ans.scaled(size, size, transformMode=Qt.SmoothTransformation) if as_data: ans = pixmap_to_data(ans) return ans
def test_ssl(self): 'Test serving over SSL' address = '127.0.0.1' with TemporaryDirectory('srv-test-ssl') as tdir: cert_file, key_file, ca_file = map(lambda x: os.path.join(tdir, x), 'cka') create_server_cert(address, ca_file, cert_file, key_file, key_size=1024) ctx = ssl.create_default_context(cafile=ca_file) with TestServer(lambda data: (data.path[0] + data.read()), ssl_certfile=cert_file, ssl_keyfile=key_file, listen_on=address, port=0) as server: conn = httplib.HTTPSConnection(address, server.address[1], strict=True, context=ctx) conn.request('GET', '/test', 'body') r = conn.getresponse() self.ae(r.status, httplib.OK) self.ae(r.read(), b'testbody') cert = conn.sock.getpeercert() subject = dict(x[0] for x in cert['subject']) self.ae(subject['commonName'], address)
def test_library_restrictions(self): # {{{ from calibre.srv.opts import Options from calibre.srv.handler import Handler from calibre.db.legacy import create_backend opts = Options(userdb=':memory:') Data = namedtuple('Data', 'username') with TemporaryDirectory() as base: l1, l2, l3 = map(lambda x: os.path.join(base, 'l' + x), '123') for l in (l1, l2, l3): create_backend(l).close() ctx = Handler((l1, l2, l3), opts).router.ctx um = ctx.user_manager def get_library(username=None, library_id=None): ans = ctx.get_library(Data(username), library_id=library_id) return os.path.basename(ans.backend.library_path) def library_info(username=None): lmap, defaultlib = ctx.library_info(Data(username)) lmap = {k:os.path.basename(v) for k, v in iteritems(lmap)} return lmap, defaultlib self.assertEqual(get_library(), 'l1') self.assertEqual(library_info()[0], {'l%d'%i:'l%d'%i for i in range(1, 4)}) self.assertEqual(library_info()[1], 'l1') self.assertRaises(HTTPForbidden, get_library, 'xxx') um.add_user('a', 'a') self.assertEqual(library_info('a')[0], {'l%d'%i:'l%d'%i for i in range(1, 4)}) um.update_user_restrictions('a', {'blocked_library_names': ['L2']}) self.assertEqual(library_info('a')[0], {'l%d'%i:'l%d'%i for i in range(1, 4) if i != 2}) um.update_user_restrictions('a', {'allowed_library_names': ['l3']}) self.assertEqual(library_info('a')[0], {'l%d'%i:'l%d'%i for i in range(1, 4) if i == 3}) self.assertEqual(library_info('a')[1], 'l3') self.assertRaises(HTTPForbidden, get_library, 'a', 'l1') self.assertRaises(HTTPForbidden, get_library, 'xxx')
def get_simple_book(fmt='epub'): cache = get_cache() ans = os.path.join(cache, 'simple.' + fmt) src = os.path.join(os.path.dirname(__file__), 'simple.html') if needs_recompile(ans, src): with TemporaryDirectory('bpt') as tdir: with CurrentDir(tdir): with lopen(src, 'rb') as sf: raw = sf.read().decode('utf-8') raw = add_resources( raw, { 'LMONOI': P('fonts/liberation/LiberationMono-Italic.ttf'), 'LMONOR': P('fonts/liberation/LiberationMono-Regular.ttf'), 'IMAGE1': I('marked.png'), 'IMAGE2': I('textures/light_wood.png'), }) shutil.copy2(I('lt.png'), '.') x = 'index.html' with lopen(x, 'wb') as f: f.write(raw.encode('utf-8')) build_book(x, ans, args=[ '--level1-toc=//h:h2', '--language=en', '--authors=Kovid Goyal', '--cover=lt.png' ]) return ans
def __enter__(self, *args): ''' Add this plugin to the python path so that it's contents become directly importable. Useful when bundling large python libraries into the plugin. Use it like this:: with plugin: import something ''' if self.plugin_path is not None: from calibre.utils.zipfile import ZipFile from importlib.machinery import EXTENSION_SUFFIXES with ZipFile(self.plugin_path) as zf: extensions = {x.lower() for x in EXTENSION_SUFFIXES} zip_safe = True for name in zf.namelist(): for q in extensions: if name.endswith(q): zip_safe = False break if not zip_safe: break if zip_safe: sys.path.append(self.plugin_path) self.sys_insertion_path = self.plugin_path else: from calibre.ptempfile import TemporaryDirectory self._sys_insertion_tdir = TemporaryDirectory('plugin_unzip') self.sys_insertion_path = self._sys_insertion_tdir.__enter__(*args) zf.extractall(self.sys_insertion_path) sys.path.append(self.sys_insertion_path)
def get_metadata(stream): from calibre.ebooks.metadata.meta import get_metadata from calibre.ebooks.metadata.archive import is_comic stream_type = None zf = ZipFile(stream, 'r') names = zf.namelist() if is_comic(names): # Is probably a comic return get_metadata(stream, 'cbz') for f in names: stream_type = os.path.splitext(f)[1].lower() if stream_type: stream_type = stream_type[1:] if stream_type in ('lit', 'opf', 'prc', 'mobi', 'fb2', 'epub', 'rb', 'imp', 'pdf', 'lrf', 'azw', 'azw1', 'azw3'): with TemporaryDirectory() as tdir: with CurrentDir(tdir): path = zf.extract(f) mi = get_metadata(open(path, 'rb'), stream_type) if stream_type == 'opf' and mi.application_id is None: try: # zip archive opf files without an application_id were assumed not to have a cover # reparse the opf and if cover exists read its data from zip archive for the metadata nmi = zip_opf_metadata(path, zf) nmi.timestamp = None return nmi except: pass mi.timestamp = None return mi raise ValueError( 'No ebook found in ZIP archive (%s)' % os.path.basename(getattr(stream, 'name', '') or '<stream>'))
def test_dir_container(self): def create_book(source): with ZipFile(P('quick_start/eng.epub', allow_user_override=False)) as zf: zf.extractall(source) with CurrentDir(source): self.assertTrue(os.path.exists('images/cover.jpg')) with open('.gitignore', 'wb') as f: f.write(b'nothing') os.mkdir('.git') with open('.git/xxx', 'wb') as f: f.write(b'xxx') with TemporaryDirectory('-polish-dir-container') as source: create_book(source) c = get_container(source) c.remove_item('images/cover.jpg') with c.open('images/test-container.xyz', 'wb') as f: f.write(b'xyz') c.commit() with CurrentDir(source): self.assertTrue(os.path.exists('.gitignore')) self.assertTrue(os.path.exists('.git/xxx')) self.assertTrue(os.path.exists('images/test-container.xyz')) self.assertFalse(os.path.exists('images/cover.jpg'))
def test_lopen(): from calibre.ptempfile import TemporaryDirectory from calibre import CurrentDir n = u'f\xe4llen' with TemporaryDirectory() as tdir: with CurrentDir(tdir): with lopen(n, 'w') as f: f.write('one') print 'O_CREAT tested' with lopen(n, 'w+b') as f: f.write('two') with lopen(n, 'r') as f: if f.read() == 'two': print 'O_TRUNC tested' else: raise Exception('O_TRUNC failed') with lopen(n, 'ab') as f: f.write('three') with lopen(n, 'r+') as f: if f.read() == 'twothree': print 'O_APPEND tested' else: raise Exception('O_APPEND failed') with lopen(n, 'r+') as f: f.seek(3) f.write('xxxxx') f.seek(0) if f.read() == 'twoxxxxx': print 'O_RANDOM tested' else: raise Exception('O_RANDOM failed')
def set_metadata(stream, mi): with TemporaryDirectory('_podofo_set_metadata') as tdir: with open(os.path.join(tdir, 'input.pdf'), 'wb') as f: shutil.copyfileobj(stream, f) from calibre.ebooks.metadata.xmp import metadata_to_xmp_packet xmp_packet = metadata_to_xmp_packet(mi) try: result = fork_job('calibre.utils.podofo', 'set_metadata_', (tdir, mi.title, mi.authors, mi.book_producer, mi.tags, xmp_packet)) touched = result['result'] except WorkerError as e: raise Exception('Failed to set PDF metadata in (%s): %s' % (mi.title, e.orig_tb)) if touched: with open(os.path.join(tdir, 'output.pdf'), 'rb') as f: f.seek(0, 2) if f.tell() > 100: f.seek(0) stream.seek(0) stream.truncate() shutil.copyfileobj(f, stream) stream.flush() stream.seek(0)
def test(): # {{{ from calibre.ptempfile import TemporaryDirectory from calibre import CurrentDir from glob import glob with TemporaryDirectory() as tdir, CurrentDir(tdir): shutil.copyfile(I('devices/kindle.jpg', allow_user_override=False), 'test.jpg') ret = optimize_jpeg('test.jpg') if ret is not None: raise SystemExit('optimize_jpeg failed: %s' % ret) ret = encode_jpeg('test.jpg') if ret is not None: raise SystemExit('encode_jpeg failed: %s' % ret) shutil.copyfile(I('lt.png'), 'test.png') ret = optimize_png('test.png') if ret is not None: raise SystemExit('optimize_png failed: %s' % ret) if glob('*.bak'): raise SystemExit('Spurious .bak files left behind') img = image_from_data( I('devices/kindle.jpg', data=True, allow_user_override=False)) quantize_image(img) oil_paint_image(img) gaussian_sharpen_image(img) gaussian_blur_image(img) despeckle_image(img) remove_borders_from_image(img)
def any2lit(opts, path): ext = os.path.splitext(path)[1] if not ext: raise ValueError('Unknown file type: '+path) ext = ext.lower()[1:] if opts.output is None: opts.output = os.path.splitext(os.path.basename(path))[0]+'.lit' opts.output = os.path.abspath(opts.output) orig_output = opts.output with TemporaryDirectory('_any2lit') as tdir: oebdir = os.path.join(tdir, 'oeb') os.mkdir(oebdir) opts.output = os.path.join(tdir, 'dummy.epub') opts.profile = 'None' orig_bfs = opts.base_font_size2 opts.base_font_size2 = 0 any2epub(opts, path, create_epub=False, oeb_cover=True, extract_to=oebdir) opts.base_font_size2 = orig_bfs opf = glob.glob(os.path.join(oebdir, '*.opf'))[0] opts.output = orig_output logging.getLogger('html2epub').info(_('Creating LIT file from EPUB...')) oeb2lit(opts, opf)
def run(self, htmlfile): from calibre.ptempfile import TemporaryDirectory from calibre.gui2.convert.gui_conversion import gui_convert from calibre.customize.conversion import OptionRecommendation from calibre.ebooks.epub import initialize_container with TemporaryDirectory('_plugin_html2zip') as tdir: recs = [('debug_pipeline', tdir, OptionRecommendation.HIGH)] recs.append(['keep_ligatures', True, OptionRecommendation.HIGH]) if self.site_customization and self.site_customization.strip(): sc = self.site_customization.strip() enc, _, bf = sc.partition('|') if enc: recs.append( ['input_encoding', enc, OptionRecommendation.HIGH]) if bf == 'bf': recs.append( ['breadth_first', True, OptionRecommendation.HIGH]) gui_convert(htmlfile, tdir, recs, abort_after_input_dump=True) of = self.temporary_file('_plugin_html2zip.zip') tdir = os.path.join(tdir, 'input') opf = glob.glob(os.path.join(tdir, '*.opf'))[0] ncx = glob.glob(os.path.join(tdir, '*.ncx')) if ncx: os.remove(ncx[0]) epub = initialize_container(of.name, os.path.basename(opf)) epub.add_dir(tdir) epub.close() return of.name
def profile(): from calibre.ptempfile import TemporaryDirectory path = sys.argv[-1] with TemporaryDirectory() as tdir, Profiler(): return render( path, tdir, serialize_metadata=True, extract_annotations=True, virtualize_resources=False, max_workers=1 )
def test_export_import(self): from calibre.db.cache import import_library from calibre.utils.exim import Exporter, Importer cache = self.init_cache() for part_size in (1 << 30, 100, 1): with TemporaryDirectory('export_lib') as tdir, TemporaryDirectory('import_lib') as idir: exporter = Exporter(tdir, part_size=part_size) cache.export_library('l', exporter) exporter.commit() importer = Importer(tdir) ic = import_library('l', importer, idir) self.assertEqual(cache.all_book_ids(), ic.all_book_ids()) for book_id in cache.all_book_ids(): self.assertEqual(cache.cover(book_id), ic.cover(book_id), 'Covers not identical for book: %d' % book_id) for fmt in cache.formats(book_id): self.assertEqual(cache.format(book_id, fmt), ic.format(book_id, fmt)) self.assertEqual(cache.format_metadata(book_id, fmt)['mtime'], cache.format_metadata(book_id, fmt)['mtime'])
def _compile_website_translations(self, name='website', threshold=50): from calibre.utils.zipfile import ZipFile, ZipInfo, ZIP_STORED from calibre.ptempfile import TemporaryDirectory from calibre.utils.localization import get_iso639_translator, get_language, get_iso_language self.info('Compiling', name, 'translations...') srcbase = self.j(self.d(self.SRC), 'translations', name) if not os.path.exists(srcbase): os.makedirs(srcbase) fmap = {} files = [] stats = {} done = [] def handle_stats(src, nums): locale = fmap[src] trans = nums[0] total = trans if len(nums) == 1 else (trans + nums[1]) stats[locale] = int(round(100 * trans / total)) with TemporaryDirectory() as tdir, ZipFile( self.j(srcbase, 'locales.zip'), 'w', ZIP_STORED) as zf: for f in os.listdir(srcbase): if f.endswith('.po'): l = f.partition('.')[0] pf = l.split('_')[0] if pf in {'en'}: continue d = os.path.join(tdir, l + '.mo') f = os.path.join(srcbase, f) fmap[f] = l files.append((f, d)) self.compile_group(files, handle_stats=handle_stats) for locale, translated in iteritems(stats): if translated >= 50: with open(os.path.join(tdir, locale + '.mo'), 'rb') as f: raw = f.read() zi = ZipInfo(os.path.basename(f.name)) zi.compress_type = ZIP_STORED zf.writestr(zi, raw) done.append(locale) dl = done + ['en'] lang_names = {} for l in dl: if l == 'en': t = get_language else: t = getattr(get_iso639_translator(l), 'gettext' if ispy3 else 'ugettext') t = partial(get_iso_language, t) lang_names[l] = {x: t(x) for x in dl} zi = ZipInfo('lang-names.json') zi.compress_type = ZIP_STORED zf.writestr( zi, json.dumps(lang_names, ensure_ascii=False).encode('utf-8')) return done