示例#1
0
def render(pathtoebook, output_dir, book_hash=None, serialize_metadata=False, extract_annotations=False, virtualize_resources=True, max_workers=1):
    pathtoebook = os.path.abspath(pathtoebook)
    with RenderManager(max_workers) as render_manager:
        mi = None
        if serialize_metadata:
            from calibre.customize.ui import quick_metadata
            from calibre.ebooks.metadata.meta import get_metadata
            with lopen(pathtoebook, 'rb') as f, quick_metadata:
                mi = get_metadata(f, os.path.splitext(pathtoebook)[1][1:].lower())
        book_fmt, opfpath, input_fmt = extract_book(pathtoebook, output_dir, log=default_log)
        container, bookmark_data = process_exploded_book(
            book_fmt, opfpath, input_fmt, output_dir, render_manager,
            book_hash=book_hash, save_bookmark_data=extract_annotations,
            book_metadata=mi, virtualize_resources=virtualize_resources
        )
        if serialize_metadata:
            from calibre.ebooks.metadata.book.serialize import metadata_as_dict
            d = metadata_as_dict(mi)
            d.pop('cover_data', None)
            serialize_datetimes(d), serialize_datetimes(d.get('user_metadata', {}))
            with lopen(os.path.join(output_dir, 'calibre-book-metadata.json'), 'wb') as f:
                f.write(json_dumps(d))
        if extract_annotations:
            annotations = None
            if bookmark_data:
                annotations = json_dumps(tuple(get_stored_annotations(container, bookmark_data)))
            if annotations:
                with lopen(os.path.join(output_dir, 'calibre-book-annotations.json'), 'wb') as f:
                    f.write(annotations)
示例#2
0
def render(pathtoebook,
           output_dir,
           book_hash=None,
           serialize_metadata=False,
           extract_annotations=False):
    mi = None
    if serialize_metadata:
        from calibre.ebooks.metadata.meta import get_metadata
        from calibre.customize.ui import quick_metadata
        with lopen(pathtoebook, 'rb') as f, quick_metadata:
            mi = get_metadata(f, os.path.splitext(pathtoebook)[1][1:].lower())
    container = Container(pathtoebook,
                          output_dir,
                          book_hash=book_hash,
                          save_bookmark_data=extract_annotations,
                          book_metadata=mi)
    if serialize_metadata:
        from calibre.utils.serialize import json_dumps
        from calibre.ebooks.metadata.book.serialize import metadata_as_dict
        d = metadata_as_dict(mi)
        d.pop('cover_data', None)
        serialize_datetimes(d), serialize_datetimes(d.get('user_metadata', {}))
        with lopen(os.path.join(output_dir, 'calibre-book-metadata.json'),
                   'wb') as f:
            f.write(json_dumps(d))
    if extract_annotations:
        annotations = None
        if container.bookmark_data:
            annotations = json_dumps(tuple(get_stored_annotations(container)))
        if annotations:
            with lopen(
                    os.path.join(output_dir, 'calibre-book-annotations.json'),
                    'wb') as f:
                f.write(annotations)
示例#3
0
def windows_repair(library_path=None):
    import subprocess
    from calibre.utils.serialize import json_dumps, json_loads
    from polyglot.binary import as_hex_unicode, from_hex_bytes
    if library_path:
        library_path = as_hex_unicode(json_dumps(library_path))
        winutil.prepare_for_restart()
        os.environ['CALIBRE_REPAIR_CORRUPTED_DB'] = environ_item(library_path)
        subprocess.Popen([sys.executable])
    else:
        try:
            app = Application([])
            from calibre.gui2.dialogs.restore_library import repair_library_at
            library_path = json_loads(
                from_hex_bytes(os.environ.pop('CALIBRE_REPAIR_CORRUPTED_DB')))
            done = repair_library_at(library_path, wait_time=4)
        except Exception:
            done = False
            error_dialog(
                None,
                _('Failed to repair library'),
                _('Could not repair library. Click "Show details" for more information.'
                  ),
                det_msg=traceback.format_exc(),
                show=True)
        if done:
            subprocess.Popen([sys.executable])
        app.quit()
示例#4
0
def json(ctx, rd, endpoint, output):
    rd.outheaders.set('Content-Type', 'application/json; charset=UTF-8', replace_all=True)
    if isinstance(output, bytes) or hasattr(output, 'fileno'):
        ans = output  # Assume output is already UTF-8 encoded json
    else:
        ans = json_dumps(output)
    return ans
示例#5
0
def json(ctx, rd, endpoint, output):
    rd.outheaders.set('Content-Type', 'application/json; charset=UTF-8', replace_all=True)
    if isinstance(output, bytes) or hasattr(output, 'fileno'):
        ans = output  # Assume output is already UTF-8 encoded json
    else:
        ans = json_dumps(output)
    return ans
示例#6
0
def serialize_annotations(annots_map):
    ans = []
    for atype, annots in iteritems(annots_map):
        for annot in annots:
            annot = serialize_annotation(annot.copy())
            annot['type'] = atype
            ans.append(annot)
    return json_dumps(ans)
示例#7
0
 def test_serialize_metadata(self):  # {{{
     from calibre.utils.serialize import json_dumps, json_loads, msgpack_dumps, msgpack_loads
     from calibre.library.field_metadata import fm_as_dict
     cache = self.init_cache(self.library_path)
     fm = cache.field_metadata
     for d, l in ((json_dumps, json_loads), (msgpack_dumps, msgpack_loads)):
         fm2 = l(d(fm))
         self.assertEqual(fm_as_dict(fm), fm_as_dict(fm2))
     for i in range(1, 4):
         mi = cache.get_metadata(i, get_cover=True, cover_as_data=True)
         rmi = msgpack_loads(msgpack_dumps(mi))
         self.compare_metadata(mi, rmi, exclude='format_metadata has_cover formats id'.split())
         rmi = json_loads(json_dumps(mi))
         self.compare_metadata(mi, rmi, exclude='format_metadata has_cover formats id'.split())
示例#8
0
 def test_serialize_metadata(self):  # {{{
     from calibre.utils.serialize import json_dumps, json_loads, msgpack_dumps, msgpack_loads
     from calibre.library.field_metadata import fm_as_dict
     cache = self.init_cache(self.library_path)
     fm = cache.field_metadata
     for d, l in ((json_dumps, json_loads), (msgpack_dumps, msgpack_loads)):
         fm2 = l(d(fm))
         self.assertEqual(fm_as_dict(fm), fm_as_dict(fm2))
     for i in range(1, 4):
         mi = cache.get_metadata(i, get_cover=True, cover_as_data=True)
         rmi = msgpack_loads(msgpack_dumps(mi))
         self.compare_metadata(mi, rmi, exclude='format_metadata has_cover formats id'.split())
         rmi = json_loads(json_dumps(mi))
         self.compare_metadata(mi, rmi, exclude='format_metadata has_cover formats id'.split())
示例#9
0
def tag_browser(ctx, rd):
    '''
    Get the Tag Browser serialized as JSON
    Optional: ?library_id=<default library>&sort_tags_by=name&partition_method=first letter
              &collapse_at=25&dont_collapse=&hide_empty_categories=&vl=''
    '''
    db, library_id = get_library_data(ctx, rd)[:2]
    opts = categories_settings(rd.query, db, gst_container=tuple)
    vl = rd.query.get('vl') or ''
    etag = json_dumps([db.last_modified().isoformat(), rd.username, library_id, vl, list(opts)])
    etag = hashlib.sha1(etag).hexdigest()

    def generate():
        return json(ctx, rd, tag_browser, categories_as_json(ctx, rd, db, opts, vl))

    return rd.etagged_dynamic_response(etag, generate)
示例#10
0
文件: code.py 项目: JimmXinu/calibre
def tag_browser(ctx, rd):
    '''
    Get the Tag Browser serialized as JSON
    Optional: ?library_id=<default library>&sort_tags_by=name&partition_method=first letter
              &collapse_at=25&dont_collapse=&hide_empty_categories=&vl=''
    '''
    db, library_id = get_library_data(ctx, rd)[:2]
    opts = categories_settings(rd.query, db, gst_container=tuple)
    vl = rd.query.get('vl') or ''
    etag = json_dumps([db.last_modified().isoformat(), rd.username, library_id, vl, list(opts)])
    etag = hashlib.sha1(etag).hexdigest()

    def generate():
        return json(ctx, rd, tag_browser, categories_as_json(ctx, rd, db, opts, vl))

    return rd.etagged_dynamic_response(etag, generate)
示例#11
0
文件: main.py 项目: JimmXinu/calibre
def windows_repair(library_path=None):
    import subprocess
    from calibre.utils.serialize import json_dumps, json_loads
    from polyglot.binary import as_hex_unicode, from_hex_bytes
    if library_path:
        library_path = as_hex_unicode(json_dumps(library_path))
        winutil.prepare_for_restart()
        os.environ['CALIBRE_REPAIR_CORRUPTED_DB'] = environ_item(library_path)
        subprocess.Popen([sys.executable])
    else:
        try:
            app = Application([])
            from calibre.gui2.dialogs.restore_library import repair_library_at
            library_path = json_loads(from_hex_bytes(os.environ.pop('CALIBRE_REPAIR_CORRUPTED_DB')))
            done = repair_library_at(library_path, wait_time=4)
        except Exception:
            done = False
            error_dialog(None, _('Failed to repair library'), _(
                'Could not repair library. Click "Show details" for more information.'), det_msg=traceback.format_exc(), show=True)
        if done:
            subprocess.Popen([sys.executable])
        app.quit()
示例#12
0
def annot_list_as_bytes(annots):
    return json_dumps(tuple(annot for annot, seconds in annots))
示例#13
0
def action(main, **keys):
    keys['type'] = main
    return 'action:' + as_hex_unicode(json_dumps(keys))
示例#14
0
文件: books.py 项目: cbhaley/calibre
def book_hash(library_uuid, book_id, fmt, size, mtime):
    raw = json_dumps((library_uuid, book_id, fmt.upper(), size, mtime, RENDER_VERSION))
    return as_unicode(sha1(raw).hexdigest())
示例#15
0
文件: books.py 项目: zyhong/calibre
def book_hash(library_uuid, book_id, fmt, size, mtime):
    raw = json_dumps(
        (library_uuid, book_id, fmt.upper(), size, mtime, RENDER_VERSION))
    return as_unicode(sha1(raw).hexdigest())
示例#16
0
def item_data(field_name, value, book_id):
    return as_hex_unicode(json_dumps((field_name, value, book_id)))
示例#17
0
 def serialized_prefs(self) -> bytes:
     from calibre.utils.serialize import json_dumps
     c = dict(deepcopy(self.original_prefs))
     c.update(self.current_prefs)
     return json_dumps(c, indent=2)
示例#18
0
def item_data(field_name, value, book_id):
    return hexlify(json_dumps((field_name, value, book_id)))