home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- __license__ = 'GPL v3'
- __copyright__ = '2009, John Schember <john at nachtimwald.com>'
- __docformat__ = 'restructuredtext en'
- import os
- import re
- import time
- import json
- from itertools import cycle
- from calibre import prints, isbytestring
- from calibre.constants import filesystem_encoding, DEBUG
- from calibre.devices.usbms.cli import CLI
- from calibre.devices.usbms.device import Device
- from calibre.devices.usbms.books import BookList, Book
- BASE_TIME = None
-
- def debug_print(*args):
- global BASE_TIME
- if BASE_TIME is None:
- BASE_TIME = time.time()
-
- if DEBUG:
- prints('DEBUG: %6.1f' % (time.time() - BASE_TIME), *args)
-
-
-
- class USBMS(CLI, Device):
- description = _('Communicate with an eBook reader.')
- author = _('John Schember')
- supported_platforms = [
- 'windows',
- 'osx',
- 'linux']
- booklist_class = BookList
- book_class = Book
- FORMATS = []
- CAN_SET_METADATA = False
- METADATA_CACHE = 'metadata.calibre'
-
- def get_device_information(self, end_session = True):
- self.report_progress(1, _('Get device information...'))
- return (self.get_gui_name(), '', '', '')
-
-
- def books(self, oncard = None, end_session = True):
- path_to_ext = path_to_ext
- import calibre.ebooks.metadata.meta
- debug_print('USBMS: Fetching list of books from device. oncard=', oncard)
- dummy_bl = self.booklist_class(None, None, None)
- if oncard == 'carda' and not (self._card_a_prefix):
- self.report_progress(1, _('Getting list of books on device...'))
- return dummy_bl
- if oncard == 'cardb' and not (self._card_b_prefix):
- self.report_progress(1, _('Getting list of books on device...'))
- return dummy_bl
- if oncard and oncard != 'carda' and oncard != 'cardb':
- self.report_progress(1, _('Getting list of books on device...'))
- return dummy_bl
- if oncard == 'carda':
- pass
- elif oncard == 'cardb':
- pass
-
- prefix = self._main_prefix
- if oncard == 'carda':
- pass
- elif oncard == 'cardb':
- pass
-
- ebook_dirs = self.get_main_ebook_dir()
- debug_print('USBMS: dirs are:', prefix, ebook_dirs)
- bl = self.booklist_class(oncard, prefix, self.settings)
- need_sync = self.parse_metadata_cache(bl, prefix, self.METADATA_CACHE)
- bl_cache = { }
- for idx, b in enumerate(bl):
- bl_cache[b.lpath] = idx
-
-
- def update_booklist(filename, path, prefix):
- changed = False
- if path_to_ext(filename) in self.FORMATS:
-
- try:
- lpath = os.path.join(path, filename).partition(self.normalize_path(prefix))[2]
- if lpath.startswith(os.sep):
- lpath = lpath[len(os.sep):]
-
- lpath = lpath.replace('\\', '/')
- idx = bl_cache.get(lpath, None)
- if idx is not None:
- bl_cache[lpath] = None
- if self.update_metadata_item(bl[idx]):
- changed = True
-
- elif bl.add_book(self.book_from_path(prefix, lpath), replace_metadata = False):
- changed = True
- import traceback
- traceback.print_exc()
-
-
- return changed
-
- if isinstance(ebook_dirs, basestring):
- ebook_dirs = [
- ebook_dirs]
-
- for ebook_dir in ebook_dirs:
- ebook_dir = self.path_to_unicode(ebook_dir)
- ebook_dir = None(self.normalize_path if ebook_dir else prefix)
- if not os.path.exists(ebook_dir):
- continue
-
- if self.SUPPORTS_SUB_DIRS:
- flist = []
- for path, dirs, files in os.walk(ebook_dir):
- for filename in files:
- if filename != self.METADATA_CACHE:
- flist.append({
- 'filename': self.path_to_unicode(filename),
- 'path': self.path_to_unicode(path) })
- continue
-
-
- for i, f in enumerate(flist):
- self.report_progress(i / float(len(flist)), _('Getting list of books on device...'))
- changed = update_booklist(f['filename'], f['path'], prefix)
- if changed:
- need_sync = True
- continue
-
- paths = os.listdir(ebook_dir)
- for i, filename in enumerate(paths):
- self.report_progress((i + 1) / float(len(paths)), _('Getting list of books on device...'))
- changed = update_booklist(self.path_to_unicode(filename), ebook_dir, prefix)
- if changed:
- need_sync = True
- continue
-
-
- for idx in sorted(bl_cache.itervalues(), reverse = True):
- if idx is not None:
- need_sync = True
- del bl[idx]
- continue
-
- debug_print('USBMS: count found in cache: %d, count of files in metadata: %d, need_sync: %s' % (len(bl_cache), len(bl), need_sync))
- if need_sync:
- if oncard == 'cardb':
- self.sync_booklists((None, None, bl))
- elif oncard == 'carda':
- self.sync_booklists((None, bl, None))
- else:
- self.sync_booklists((bl, None, None))
-
- self.report_progress(1, _('Getting list of books on device...'))
- debug_print('USBMS: Finished fetching list of books from device. oncard=', oncard)
- return bl
-
-
- def upload_books(self, files, names, on_card = None, end_session = True, metadata = None):
- debug_print('USBMS: uploading %d books' % len(files))
- path = self._sanity_check(on_card, files)
- paths = []
- names = iter(names)
- metadata = iter(metadata)
- for i, infile in enumerate(files):
- mdata = metadata.next()
- fname = names.next()
- filepath = self.normalize_path(self.create_upload_path(path, mdata, fname))
- paths.append(filepath)
- if not hasattr(infile, 'read'):
- infile = self.normalize_path(infile)
-
- self.put_file(infile, filepath, replace_file = True)
-
- try:
- self.upload_cover(os.path.dirname(filepath), os.path.splitext(os.path.basename(filepath))[0], mdata)
- except:
- import traceback
- traceback.print_exc()
-
- self.report_progress((i + 1) / float(len(files)), _('Transferring books to device...'))
-
- self.report_progress(1, _('Transferring books to device...'))
- debug_print('USBMS: finished uploading %d books' % len(files))
- return zip(paths, cycle([
- on_card]))
-
-
- def upload_cover(self, path, filename, metadata):
- pass
-
-
- def add_books_to_metadata(self, locations, metadata, booklists):
- debug_print('USBMS: adding metadata for %d books' % len(metadata))
- metadata = iter(metadata)
- for i, location in enumerate(locations):
- self.report_progress((i + 1) / float(len(locations)), _('Adding books to device metadata listing...'))
- info = metadata.next()
- if location[1] == 'cardb':
- pass
- elif location[1] == 'carda':
- pass
-
- blist = 0
- path = self.normalize_path(location[0])
- if self._main_prefix:
- prefix = None if path.startswith(self.normalize_path(self._main_prefix)) else None
-
- if not prefix and self._card_a_prefix:
- prefix = None if path.startswith(self.normalize_path(self._card_a_prefix)) else None
-
- if not prefix and self._card_b_prefix:
- prefix = None if path.startswith(self.normalize_path(self._card_b_prefix)) else None
-
- if prefix is None:
- prints('in add_books_to_metadata. Prefix is None!', path, self._main_prefix)
- continue
-
- lpath = path.partition(prefix)[2]
- if lpath.startswith('/') or lpath.startswith('\\'):
- lpath = lpath[1:]
-
- book = self.book_class(prefix, lpath, other = info)
- if book.size is None:
- book.size = os.stat(self.normalize_path(path)).st_size
-
- book._new_book = True
- booklists[blist].add_book(book, replace_metadata = True)
-
- self.report_progress(1, _('Adding books to device metadata listing...'))
- debug_print('USBMS: finished adding metadata')
-
-
- def delete_books(self, paths, end_session = True):
- debug_print('USBMS: deleting %d books' % len(paths))
- for i, path in enumerate(paths):
- self.report_progress((i + 1) / float(len(paths)), _('Removing books from device...'))
- path = self.normalize_path(path)
- if os.path.exists(path):
- os.unlink(path)
- filepath = os.path.splitext(path)[0]
- for ext in self.DELETE_EXTS:
- if os.path.exists(filepath + ext):
- os.unlink(filepath + ext)
-
- if os.path.exists(path + ext):
- os.unlink(path + ext)
- continue
-
- if self.SUPPORTS_SUB_DIRS:
-
- try:
- os.removedirs(os.path.dirname(path))
-
-
- self.SUPPORTS_SUB_DIRS
-
- self.report_progress(1, _('Removing books from device...'))
- debug_print('USBMS: finished deleting %d books' % len(paths))
-
-
- def remove_books_from_metadata(self, paths, booklists):
- debug_print('USBMS: removing metadata for %d books' % len(paths))
- for i, path in enumerate(paths):
- self.report_progress((i + 1) / float(len(paths)), _('Removing books from device metadata listing...'))
- for bl in booklists:
- for book in bl:
- if path.endswith(book.path):
- bl.remove_book(book)
- continue
-
-
-
- self.report_progress(1, _('Removing books from device metadata listing...'))
- debug_print('USBMS: finished removing metadata for %d books' % len(paths))
-
-
- def sync_booklists(self, booklists, end_session = True):
- debug_print('USBMS: starting sync_booklists')
- if not os.path.exists(self.normalize_path(self._main_prefix)):
- os.makedirs(self.normalize_path(self._main_prefix))
-
-
- def write_prefix(prefix, listid):
- pass
-
- write_prefix(self._main_prefix, 0)
- write_prefix(self._card_a_prefix, 1)
- write_prefix(self._card_b_prefix, 2)
- for blist in booklists:
- if blist is not None:
- for book in blist:
- book._new_book = False
-
- (None, None)
-
- self.report_progress(1, _('Sending metadata to device...'))
- debug_print('USBMS: finished sync_booklists')
-
-
- def build_template_regexp(cls):
-
- def replfunc(match):
- if match.group(1) in ('title', 'series', 'series_index', 'isbn'):
- return '(?P<' + match.group(1) + '>.+?)'
- if match.group(1) in ('authors', 'author_sort'):
- return '(?P<author>.+?)'
- return '(.+?)'
-
- template = cls.save_template().rpartition('/')[2]
- return re.compile(re.sub('{([^}]*)}', replfunc, template) + '([_\\d]*$)')
-
- build_template_regexp = classmethod(build_template_regexp)
-
- def path_to_unicode(cls, path):
- if isbytestring(path):
- path = path.decode(filesystem_encoding)
-
- return path
-
- path_to_unicode = classmethod(path_to_unicode)
-
- def normalize_path(cls, path):
- if path is None:
- return None
- if os.sep == '\\':
- path = path.replace('/', '\\')
- else:
- path = path.replace('\\', '/')
- return cls.path_to_unicode(path)
-
- normalize_path = classmethod(normalize_path)
-
- def parse_metadata_cache(cls, bl, prefix, name):
- js = []
- need_sync = False
- cache_file = cls.normalize_path(os.path.join(prefix, name))
- if os.access(cache_file, os.R_OK):
-
- try:
-
- try:
- f = _[1]
- js = json.load(f, encoding = 'utf-8')
- finally:
- pass
-
- for item in js:
- book = cls.book_class(prefix, item.get('lpath', None))
- for key in item.keys():
- setattr(book, key, item[key])
-
- bl.append(book)
- import traceback
- traceback.print_exc()
- bl = []
- need_sync = True
-
- else:
- need_sync = True
- return need_sync
-
- parse_metadata_cache = classmethod(parse_metadata_cache)
-
- def update_metadata_item(cls, book):
- changed = False
- size = os.stat(cls.normalize_path(book.path)).st_size
- if size != book.size:
- changed = True
- mi = cls.metadata_from_path(book.path)
- book.smart_update(mi)
- book.size = size
-
- return changed
-
- update_metadata_item = classmethod(update_metadata_item)
-
- def metadata_from_path(cls, path):
- return cls.metadata_from_formats([
- path])
-
- metadata_from_path = classmethod(metadata_from_path)
-
- def metadata_from_formats(cls, fmts):
- metadata_from_formats = metadata_from_formats
- import calibre.ebooks.metadata.meta
- quick_metadata = quick_metadata
- import calibre.customize.ui
- quick_metadata.__enter__()
-
- try:
- return metadata_from_formats(fmts, force_read_metadata = True, pattern = cls.build_template_regexp())
- finally:
- pass
-
-
- metadata_from_formats = classmethod(metadata_from_formats)
-
- def book_from_path(cls, prefix, lpath):
- MetaInformation = MetaInformation
- import calibre.ebooks.metadata
- if cls.settings().read_metadata or cls.MUST_READ_METADATA:
- mi = cls.metadata_from_path(cls.normalize_path(os.path.join(prefix, lpath)))
- else:
- metadata_from_filename = metadata_from_filename
- import calibre.ebooks.metadata.meta
- mi = metadata_from_filename(cls.normalize_path(os.path.basename(lpath)), cls.build_template_regexp())
- if mi is None:
- mi = MetaInformation(os.path.splitext(os.path.basename(lpath))[0], [
- _('Unknown')])
-
- size = os.stat(cls.normalize_path(os.path.join(prefix, lpath))).st_size
- book = cls.book_class(prefix, lpath, other = mi, size = size)
- return book
-
- book_from_path = classmethod(book_from_path)
-
-