def sync_booklists(self, booklists, end_session=True): debug_print('USBMS: starting sync_booklists') json_codec = JsonCodec() if not os.path.exists(self.normalize_path(self._main_prefix)): os.makedirs(self.normalize_path(self._main_prefix)) def write_prefix(prefix, listid): if (prefix is not None and len(booklists) > listid and isinstance(booklists[listid], self.booklist_class)): if not os.path.exists(prefix): os.makedirs(self.normalize_path(prefix)) with lopen( self.normalize_path( os.path.join(prefix, self.METADATA_CACHE)), 'wb') as f: json_codec.encode_to_file(f, booklists[listid]) fsync(f) write_prefix(self._main_prefix, 0) write_prefix(self._card_a_prefix, 1) write_prefix(self._card_b_prefix, 2) # Clear the _new_book indication, as we are supposed to be done with # adding books at this point for blist in booklists: if blist is not None: for book in blist: book._new_book = False self.report_progress(1.0, _('Sending metadata to device...')) debug_print('USBMS: finished sync_booklists')
def parse_metadata_cache(cls, bl, prefix, name): json_codec = JsonCodec() need_sync = False cache_file = cls.normalize_path(os.path.join(prefix, name)) if os.access(cache_file, os.R_OK): try: with lopen(cache_file, 'rb') as f: json_codec.decode_from_file(f, bl, cls.book_class, prefix) except: import traceback traceback.print_exc() bl = [] need_sync = True else: need_sync = True return need_sync
def parse_metadata_cache(self, bl): need_sync = True if not self.bambook: return need_sync # Get the metadata virtual book from Bambook with TemporaryDirectory() as tdir: if self.bambook.GetFile(self.METADATA_FILE_GUID, tdir): cache_name = os.path.join(tdir, self.METADATA_CACHE) if self.bambook.ExtractSNBContent( os.path.join(tdir, self.METADATA_FILE_GUID), 'snbc/' + self.METADATA_CACHE, cache_name): json_codec = JsonCodec() if os.access(cache_name, os.R_OK): try: with open(cache_name, 'rb') as f: json_codec.decode_from_file( f, bl, self.book_class, '') need_sync = False except: import traceback traceback.print_exc() bl = [] return need_sync
def __init__(self): self.ajax_json_codec = JsonCodec()
def book_to_json(ctx, rd, db, book_id, get_category_urls=True, device_compatible=False, device_for_template=None): mi = db.get_metadata(book_id, get_cover=False) codec = JsonCodec(db.field_metadata) if not device_compatible: try: mi.rating = mi.rating/2. except Exception: mi.rating = 0.0 data = codec.encode_book_metadata(mi) for x in ('publication_type', 'size', 'db_id', 'lpath', 'mime', 'rights', 'book_producer'): data.pop(x, None) get = partial(ctx.url_for, get_content, book_id=book_id, library_id=db.server_library_id) data['cover'] = get(what='cover') data['thumbnail'] = get(what='thumb') if not device_compatible: mi.format_metadata = {k.lower():dict(v) for k, v in mi.format_metadata.iteritems()} for v in mi.format_metadata.itervalues(): mtime = v.get('mtime', None) if mtime is not None: v['mtime'] = isoformat(mtime, as_utc=True) data['format_metadata'] = mi.format_metadata fmts = set(x.lower() for x in mi.format_metadata.iterkeys()) pf = prefs['output_format'].lower() other_fmts = list(fmts) try: fmt = pf if pf in fmts else other_fmts[0] except: fmt = None if fmts and fmt: other_fmts = [x for x in fmts if x != fmt] data['formats'] = sorted(fmts) if fmt: data['main_format'] = {fmt:get(what=fmt)} else: data['main_format'] = None data['other_formats'] = {fmt:get(what=fmt) for fmt in other_fmts} if get_category_urls: category_urls = data['category_urls'] = {} all_cats = ctx.get_categories(rd, db) for key in mi.all_field_keys(): fm = mi.metadata_for_field(key) if (fm and fm['is_category'] and not fm['is_csp'] and key != 'formats' and fm['datatype'] != 'rating'): categories = mi.get(key) or [] if isinstance(categories, basestring): categories = [categories] category_urls[key] = dbtags = {} for category in categories: for tag in all_cats.get(key, ()): if tag.original_name == category: dbtags[category] = ctx.url_for( books_in, encoded_category=encode_name(tag.category if tag.category else key), encoded_item=encode_name(tag.original_name if tag.id is None else unicode(tag.id)), library_id=db.server_library_id ) break else: series = data.get('series', None) or '' if series: tsorder = tweaks['save_template_title_series_sorting'] series = title_sort(series, order=tsorder) data['_series_sort_'] = series if device_for_template: import posixpath from calibre.devices.utils import create_upload_path from calibre.utils.filenames import ascii_filename as sanitize from calibre.customize.ui import device_plugins for device_class in device_plugins(): if device_class.__class__.__name__ == device_for_template: template = device_class.save_template() data['_filename_'] = create_upload_path(mi, book_id, template, sanitize, path_type=posixpath) break return data, mi.last_modified
def sync_booklists(self, booklists, end_session=True): ''' Update metadata on device. :param booklists: A tuple containing the result of calls to (:meth:`books(oncard=None)`, :meth:`books(oncard='carda')`, :meth`books(oncard='cardb')`). ''' if not self.bambook: return json_codec = JsonCodec() # Create stub virtual book for sync info with TemporaryDirectory() as tdir: snbcdir = os.path.join(tdir, 'snbc') snbfdir = os.path.join(tdir, 'snbf') os.mkdir(snbcdir) os.mkdir(snbfdir) f = open(os.path.join(snbfdir, 'book.snbf'), 'wb') f.write('''<book-snbf version="1.0"> <head> <name>calibre同步信息</name> <author>calibre</author> <language>ZH-CN</language> <rights/> <publisher>calibre</publisher> <generator>''' + __appname__ + ' ' + __version__ + '''</generator> <created/> <abstract></abstract> <cover/> </head> </book-snbf> ''') f.close() f = open(os.path.join(snbfdir, 'toc.snbf'), 'wb') f.write('''<toc-snbf> <head> <chapters>0</chapters> </head> <body> </body> </toc-snbf> ''') f.close() cache_name = os.path.join(snbcdir, self.METADATA_CACHE) with open(cache_name, 'wb') as f: json_codec.encode_to_file(f, booklists[0]) with TemporaryFile('.snb') as f: if self.bambook.PackageSNB(f, tdir): if not self.bambook.SendFile(f, self.METADATA_FILE_GUID): print "Upload failed" else: print "Package failed" # Clear the _new_book indication, as we are supposed to be done with # adding books at this point for blist in booklists: if blist is not None: for book in blist: book._new_book = False self.report_progress(1.0, _('Sending metadata to device...'))