Exemplo n.º 1
0
 def cover(self, path, as_file=False, as_image=False,
         as_path=False):
     path = os.path.join(self.library_path, path, 'cover.jpg')
     ret = None
     if os.access(path, os.R_OK):
         try:
             f = lopen(path, 'rb')
         except (IOError, OSError):
             time.sleep(0.2)
             f = lopen(path, 'rb')
         with f:
             if as_path:
                 pt = PersistentTemporaryFile('_dbcover.jpg')
                 with pt:
                     shutil.copyfileobj(f, pt)
                 return pt.name
             if as_file:
                 ret = SpooledTemporaryFile(SPOOL_SIZE)
                 shutil.copyfileobj(f, ret)
                 ret.seek(0)
             else:
                 ret = f.read()
                 if as_image:
                     from PyQt4.Qt import QImage
                     i = QImage()
                     i.loadFromData(ret)
                     ret = i
     return ret
Exemplo n.º 2
0
 def cover(self, path, as_file=False, as_image=False, as_path=False):
     path = os.path.join(self.library_path, path, 'cover.jpg')
     ret = None
     if os.access(path, os.R_OK):
         try:
             f = lopen(path, 'rb')
         except (IOError, OSError):
             time.sleep(0.2)
             f = lopen(path, 'rb')
         with f:
             if as_path:
                 pt = PersistentTemporaryFile('_dbcover.jpg')
                 with pt:
                     shutil.copyfileobj(f, pt)
                 return pt.name
             if as_file:
                 ret = SpooledTemporaryFile(SPOOL_SIZE)
                 shutil.copyfileobj(f, ret)
                 ret.seek(0)
             else:
                 ret = f.read()
                 if as_image:
                     from PyQt4.Qt import QImage
                     i = QImage()
                     i.loadFromData(ret)
                     ret = i
     return ret
Exemplo n.º 3
0
    def write_metadata_cache(self, storage, bl):
        from calibre.devices.mtp.books import JSONCodec

        if bl.storage_id != storage.storage_id:
            # Just a sanity check, should never happen
            return

        json_codec = JSONCodec()
        stream = SpooledTemporaryFile(10*(1024**2))
        json_codec.encode_to_file(stream, bl)
        size = stream.tell()
        stream.seek(0)
        self.put_calibre_file(storage, 'metadata', stream, size)
Exemplo n.º 4
0
    def open(self, name, spool_size=5 * 1024 * 1024):
        if isinstance(name, LocalHeader):
            name = name.filename
        offset, header = self._get_file_info(name)
        self.stream.seek(offset)
        dest = SpooledTemporaryFile(max_size=spool_size)

        if header.compression_method == ZIP_STORED:
            copy_stored_file(self.stream, header.compressed_size, dest)
        else:
            copy_compressed_file(self.stream, header.compressed_size, dest)
        dest.seek(0)
        return dest
Exemplo n.º 5
0
 def get_mtp_file(self, f, stream=None, callback=None):
     if f.is_folder:
         raise ValueError('%s if a folder'%(f.full_path,))
     set_name = stream is None
     if stream is None:
         stream = SpooledTemporaryFile(5*1024*1024, '_wpd_receive_file.dat')
     ok, errs = self.dev.get_file(f.object_id, stream, callback)
     if not ok:
         raise DeviceError('Failed to get file: %s with errors: %s'%(
             f.full_path, self.format_errorstack(errs)))
     stream.seek(0)
     if set_name:
         stream.name = f.name
     return stream
Exemplo n.º 6
0
 def get_mtp_file(self, f, stream=None, callback=None):
     if f.is_folder:
         raise ValueError('%s if a folder'%(f.full_path,))
     set_name = stream is None
     if stream is None:
         stream = SpooledTemporaryFile(5*1024*1024, '_wpd_receive_file.dat')
     ok, errs = self.dev.get_file(f.object_id, stream, callback)
     if not ok:
         raise DeviceError('Failed to get file: %s with errors: %s'%(
             f.full_path, self.format_errorstack(errs)))
     stream.seek(0)
     if set_name:
         stream.name = f.name
     return stream
Exemplo n.º 7
0
 def get_mtp_file(self, f, stream=None, callback=None):
     if f.is_folder:
         raise ValueError('%s if a folder'%(f.full_path,))
     set_name = stream is None
     if stream is None:
         stream = SpooledTemporaryFile(5*1024*1024, '_wpd_receive_file.dat')
     try:
         try:
             self.dev.get_file(f.object_id, stream, callback)
         except self.wpd.WPDFileBusy:
             time.sleep(2)
             self.dev.get_file(f.object_id, stream, callback)
     except Exception as e:
         raise DeviceError('Failed to fetch the file %s with error: %s'%
                 (f.full_path, as_unicode(e)))
     stream.seek(0)
     if set_name:
         stream.name = f.name
     return stream
Exemplo n.º 8
0
 def get_mtp_file(self, f, stream=None, callback=None):
     if f.is_folder:
         raise ValueError('%s if a folder'%(f.full_path,))
     set_name = stream is None
     if stream is None:
         stream = SpooledTemporaryFile(5*1024*1024, '_wpd_receive_file.dat')
     try:
         try:
             self.dev.get_file(f.object_id, stream, callback)
         except self.wpd.WPDFileBusy:
             time.sleep(2)
             self.dev.get_file(f.object_id, stream, callback)
     except Exception as e:
         raise DeviceError('Failed to fetch the file %s with error: %s'%
                 (f.full_path, as_unicode(e)))
     stream.seek(0)
     if set_name:
         stream.name = f.name
     return stream
Exemplo n.º 9
0
    def cover(self, book_id,
            as_file=False, as_image=False, as_path=False):
        '''
        Return the cover image or None. By default, returns the cover as a
        bytestring.

        WARNING: Using as_path will copy the cover to a temp file and return
        the path to the temp file. You should delete the temp file when you are
        done with it.

        :param as_file: If True return the image as an open file object (a SpooledTemporaryFile)
        :param as_image: If True return the image as a QImage object
        :param as_path: If True return the image as a path pointing to a
                        temporary file
        '''
        if as_file:
            ret = SpooledTemporaryFile(SPOOL_SIZE)
            if not self.copy_cover_to(book_id, ret):
                return
            ret.seek(0)
        elif as_path:
            pt = PersistentTemporaryFile('_dbcover.jpg')
            with pt:
                if not self.copy_cover_to(book_id, pt):
                    return
            ret = pt.name
        else:
            buf = BytesIO()
            if not self.copy_cover_to(book_id, buf):
                return
            ret = buf.getvalue()
            if as_image:
                from PyQt4.Qt import QImage
                i = QImage()
                i.loadFromData(ret)
                ret = i
        return ret
Exemplo n.º 10
0
    def cover(self, book_id, as_file=False, as_image=False, as_path=False):
        '''
        Return the cover image or None. By default, returns the cover as a
        bytestring.

        WARNING: Using as_path will copy the cover to a temp file and return
        the path to the temp file. You should delete the temp file when you are
        done with it.

        :param as_file: If True return the image as an open file object (a SpooledTemporaryFile)
        :param as_image: If True return the image as a QImage object
        :param as_path: If True return the image as a path pointing to a
                        temporary file
        '''
        if as_file:
            ret = SpooledTemporaryFile(SPOOL_SIZE)
            if not self.copy_cover_to(book_id, ret):
                return
            ret.seek(0)
        elif as_path:
            pt = PersistentTemporaryFile('_dbcover.jpg')
            with pt:
                if not self.copy_cover_to(book_id, pt):
                    return
            ret = pt.name
        else:
            buf = BytesIO()
            if not self.copy_cover_to(book_id, buf):
                return
            ret = buf.getvalue()
            if as_image:
                from PyQt4.Qt import QImage
                i = QImage()
                i.loadFromData(ret)
                ret = i
        return ret
Exemplo n.º 11
0
            f.write(opf)

    mi.cover = ocover

    written = False
    for fmt in formats:
        global plugboard_save_to_disk_value, plugboard_any_format_value
        cpb = find_plugboard(plugboard_save_to_disk_value, fmt, plugboards)
        fp = format_map.get(fmt, None)
        if fp is None:
            continue
        stream = SpooledTemporaryFile(20*1024*1024, '_save_to_disk.'+(fmt or
            'tmp'))
        with open(fp, 'rb') as f:
            shutil.copyfileobj(f, stream)
        stream.seek(0)
        written = True
        if opts.update_metadata:
            try:
                if cpb:
                    newmi = mi.deepcopy_metadata()
                    newmi.template_to_attribute(mi, cpb)
                else:
                    newmi = mi
                if cover:
                    newmi.cover_data = ('jpg', cover)
                set_metadata(stream, newmi, fmt)
            except:
                if DEBUG:
                    traceback.print_exc()
            stream.seek(0)
Exemplo n.º 12
0
def do_save_book_to_disk(id_, mi, cover, plugboards,
        format_map, root, opts, length):
    available_formats = [x.lower().strip() for x in format_map.keys()]
    if mi.pubdate:
        mi.pubdate = as_local_time(mi.pubdate)
    if mi.timestamp:
        mi.timestamp = as_local_time(mi.timestamp)

    if opts.formats == 'all':
        asked_formats = available_formats
    else:
        asked_formats = [x.lower().strip() for x in opts.formats.split(',')]
    formats = set(available_formats).intersection(set(asked_formats))
    if not formats:
        return True, id_, mi.title

    components = get_path_components(opts, mi, id_, length)
    base_path = os.path.join(root, *components)
    base_name = os.path.basename(base_path)
    dirpath = os.path.dirname(base_path)
    # Don't test for existence first as the test could fail but
    # another worker process could create the directory before
    # the call to makedirs
    try:
        os.makedirs(dirpath)
    except BaseException:
        if not os.path.exists(dirpath):
            raise

    ocover = mi.cover
    if opts.save_cover and cover:
        with open(base_path+'.jpg', 'wb') as f:
            f.write(cover)
        mi.cover = base_name+'.jpg'
    else:
        mi.cover = None

    if opts.write_opf:
        from calibre.ebooks.metadata.opf2 import metadata_to_opf
        opf = metadata_to_opf(mi)
        with open(base_path+'.opf', 'wb') as f:
            f.write(opf)

    mi.cover = ocover

    written = False
    for fmt in formats:
        fp = format_map.get(fmt, None)
        if fp is None:
            continue
        stream = SpooledTemporaryFile(20*1024*1024, '_save_to_disk.'+(fmt or
            'tmp'))
        with open(fp, 'rb') as f:
            shutil.copyfileobj(f, stream)
        stream.seek(0)
        written = True
        if opts.update_metadata:
            update_metadata(mi, fmt, stream, plugboards, cover)
            stream.seek(0)
        fmt_path = base_path+'.'+str(fmt)
        with open(fmt_path, 'wb') as f:
            shutil.copyfileobj(stream, f)

    return not written, id_, mi.title
Exemplo n.º 13
0
            f.write(opf)

    mi.cover = ocover

    written = False
    for fmt in formats:
        global plugboard_save_to_disk_value, plugboard_any_format_value
        cpb = find_plugboard(plugboard_save_to_disk_value, fmt, plugboards)
        fp = format_map.get(fmt, None)
        if fp is None:
            continue
        stream = SpooledTemporaryFile(20 * 1024 * 1024,
                                      '_save_to_disk.' + (fmt or 'tmp'))
        with open(fp, 'rb') as f:
            shutil.copyfileobj(f, stream)
        stream.seek(0)
        written = True
        if opts.update_metadata:
            try:
                if cpb:
                    newmi = mi.deepcopy_metadata()
                    newmi.template_to_attribute(mi, cpb)
                else:
                    newmi = mi
                if cover:
                    newmi.cover_data = ('jpg', cover)
                set_metadata(stream, newmi, fmt)
            except:
                if DEBUG:
                    traceback.print_exc()
            stream.seek(0)
Exemplo n.º 14
0
    def format(self,
               book_id,
               fmt,
               as_file=False,
               as_path=False,
               preserve_filename=False):
        '''
        Return the ebook format as a bytestring or `None` if the format doesn't exist,
        or we don't have permission to write to the ebook file.

        :param as_file: If True the ebook format is returned as a file object. Note
                        that the file object is a SpooledTemporaryFile, so if what you want to
                        do is copy the format to another file, use :method:`copy_format_to`
                        instead for performance.
        :param as_path: Copies the format file to a temp file and returns the
                        path to the temp file
        :param preserve_filename: If True and returning a path the filename is
                                  the same as that used in the library. Note that using
                                  this means that repeated calls yield the same
                                  temp file (which is re-created each time)
        '''
        ext = ('.' + fmt.lower()) if fmt else ''
        if as_path:
            if preserve_filename:
                with self.read_lock:
                    try:
                        fname = self.fields['formats'].format_fname(
                            book_id, fmt)
                    except:
                        return None
                    fname += ext

                bd = base_dir()
                d = os.path.join(bd, 'format_abspath')
                try:
                    os.makedirs(d)
                except:
                    pass
                ret = os.path.join(d, fname)
                try:
                    self.copy_format_to(book_id, fmt, ret)
                except NoSuchFormat:
                    return None
            else:
                with PersistentTemporaryFile(ext) as pt:
                    try:
                        self.copy_format_to(book_id, fmt, pt)
                    except NoSuchFormat:
                        return None
                    ret = pt.name
        elif as_file:
            with self.read_lock:
                try:
                    fname = self.fields['formats'].format_fname(book_id, fmt)
                except:
                    return None
                fname += ext

            ret = SpooledTemporaryFile(SPOOL_SIZE)
            try:
                self.copy_format_to(book_id, fmt, ret)
            except NoSuchFormat:
                return None
            ret.seek(0)
            # Various bits of code try to use the name as the default
            # title when reading metadata, so set it
            ret.name = fname
        else:
            buf = BytesIO()
            try:
                self.copy_format_to(book_id, fmt, buf)
            except NoSuchFormat:
                return None

            ret = buf.getvalue()

        return ret
Exemplo n.º 15
0
    def format(self, book_id, fmt, as_file=False, as_path=False, preserve_filename=False):
        '''
        Return the ebook format as a bytestring or `None` if the format doesn't exist,
        or we don't have permission to write to the ebook file.

        :param as_file: If True the ebook format is returned as a file object. Note
                        that the file object is a SpooledTemporaryFile, so if what you want to
                        do is copy the format to another file, use :method:`copy_format_to`
                        instead for performance.
        :param as_path: Copies the format file to a temp file and returns the
                        path to the temp file
        :param preserve_filename: If True and returning a path the filename is
                                  the same as that used in the library. Note that using
                                  this means that repeated calls yield the same
                                  temp file (which is re-created each time)
        '''
        ext = ('.'+fmt.lower()) if fmt else ''
        if as_path:
            if preserve_filename:
                with self.read_lock:
                    try:
                        fname = self.fields['formats'].format_fname(book_id, fmt)
                    except:
                        return None
                    fname += ext

                bd = base_dir()
                d = os.path.join(bd, 'format_abspath')
                try:
                    os.makedirs(d)
                except:
                    pass
                ret = os.path.join(d, fname)
                try:
                    self.copy_format_to(book_id, fmt, ret)
                except NoSuchFormat:
                    return None
            else:
                with PersistentTemporaryFile(ext) as pt:
                    try:
                        self.copy_format_to(book_id, fmt, pt)
                    except NoSuchFormat:
                        return None
                    ret = pt.name
        elif as_file:
            with self.read_lock:
                try:
                    fname = self.fields['formats'].format_fname(book_id, fmt)
                except:
                    return None
                fname += ext

            ret = SpooledTemporaryFile(SPOOL_SIZE)
            try:
                self.copy_format_to(book_id, fmt, ret)
            except NoSuchFormat:
                return None
            ret.seek(0)
            # Various bits of code try to use the name as the default
            # title when reading metadata, so set it
            ret.name = fname
        else:
            buf = BytesIO()
            try:
                self.copy_format_to(book_id, fmt, buf)
            except NoSuchFormat:
                return None

            ret = buf.getvalue()

        return ret
Exemplo n.º 16
0
def do_save_book_to_disk(id_, mi, cover, plugboards, format_map, root, opts,
                         length):
    available_formats = [x.lower().strip() for x in format_map.keys()]
    if mi.pubdate:
        mi.pubdate = as_local_time(mi.pubdate)
    if mi.timestamp:
        mi.timestamp = as_local_time(mi.timestamp)

    if opts.formats == 'all':
        asked_formats = available_formats
    else:
        asked_formats = [x.lower().strip() for x in opts.formats.split(',')]
    formats = set(available_formats).intersection(set(asked_formats))
    if not formats:
        return True, id_, mi.title

    components = get_path_components(opts, mi, id_, length)
    base_path = os.path.join(root, *components)
    base_name = os.path.basename(base_path)
    dirpath = os.path.dirname(base_path)
    # Don't test for existence first as the test could fail but
    # another worker process could create the directory before
    # the call to makedirs
    try:
        os.makedirs(dirpath)
    except BaseException:
        if not os.path.exists(dirpath):
            raise

    ocover = mi.cover
    if opts.save_cover and cover:
        with open(base_path + '.jpg', 'wb') as f:
            f.write(cover)
        mi.cover = base_name + '.jpg'
    else:
        mi.cover = None

    if opts.write_opf:
        from calibre.ebooks.metadata.opf2 import metadata_to_opf
        opf = metadata_to_opf(mi)
        with open(base_path + '.opf', 'wb') as f:
            f.write(opf)

    mi.cover = ocover

    written = False
    for fmt in formats:
        fp = format_map.get(fmt, None)
        if fp is None:
            continue
        stream = SpooledTemporaryFile(20 * 1024 * 1024,
                                      '_save_to_disk.' + (fmt or 'tmp'))
        with open(fp, 'rb') as f:
            shutil.copyfileobj(f, stream)
        stream.seek(0)
        written = True
        if opts.update_metadata:
            update_metadata(mi, fmt, stream, plugboards, cover)
            stream.seek(0)
        fmt_path = base_path + '.' + str(fmt)
        with open(fmt_path, 'wb') as f:
            shutil.copyfileobj(stream, f)

    return not written, id_, mi.title