当前位置: 首页>>代码示例>>Python>>正文


Python SpooledTemporaryFile.seek方法代码示例

本文整理汇总了Python中calibre.ptempfile.SpooledTemporaryFile.seek方法的典型用法代码示例。如果您正苦于以下问题:Python SpooledTemporaryFile.seek方法的具体用法?Python SpooledTemporaryFile.seek怎么用?Python SpooledTemporaryFile.seek使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在calibre.ptempfile.SpooledTemporaryFile的用法示例。


在下文中一共展示了SpooledTemporaryFile.seek方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: cover

# 需要导入模块: from calibre.ptempfile import SpooledTemporaryFile [as 别名]
# 或者: from calibre.ptempfile.SpooledTemporaryFile import seek [as 别名]
 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
开发者ID:Eksmo,项目名称:calibre,代码行数:30,代码来源:backend.py

示例2: write_metadata_cache

# 需要导入模块: from calibre.ptempfile import SpooledTemporaryFile [as 别名]
# 或者: from calibre.ptempfile.SpooledTemporaryFile import seek [as 别名]
    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)
开发者ID:NiLuJe,项目名称:calibre,代码行数:15,代码来源:driver.py

示例3: get_mtp_file

# 需要导入模块: from calibre.ptempfile import SpooledTemporaryFile [as 别名]
# 或者: from calibre.ptempfile.SpooledTemporaryFile import seek [as 别名]
 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
开发者ID:bwhitenb5e,项目名称:calibre,代码行数:16,代码来源:driver.py

示例4: get_mtp_file

# 需要导入模块: from calibre.ptempfile import SpooledTemporaryFile [as 别名]
# 或者: from calibre.ptempfile.SpooledTemporaryFile import seek [as 别名]
 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
开发者ID:BobPyron,项目名称:calibre,代码行数:21,代码来源:driver.py

示例5: cover

# 需要导入模块: from calibre.ptempfile import SpooledTemporaryFile [as 别名]
# 或者: from calibre.ptempfile.SpooledTemporaryFile import seek [as 别名]
    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
开发者ID:KoWiki,项目名称:calibre,代码行数:39,代码来源:cache.py

示例6: find_plugboard

# 需要导入模块: from calibre.ptempfile import SpooledTemporaryFile [as 别名]
# 或者: from calibre.ptempfile.SpooledTemporaryFile import seek [as 别名]
            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)
开发者ID:AtulKumar2,项目名称:calibre,代码行数:33,代码来源:save_to_disk.py

示例7: do_save_book_to_disk

# 需要导入模块: from calibre.ptempfile import SpooledTemporaryFile [as 别名]
# 或者: from calibre.ptempfile.SpooledTemporaryFile import seek [as 别名]
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
开发者ID:aliloac,项目名称:calibre,代码行数:66,代码来源:save_to_disk.py

示例8: format

# 需要导入模块: from calibre.ptempfile import SpooledTemporaryFile [as 别名]
# 或者: from calibre.ptempfile.SpooledTemporaryFile import seek [as 别名]
    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
开发者ID:KoWiki,项目名称:calibre,代码行数:73,代码来源:cache.py


注:本文中的calibre.ptempfile.SpooledTemporaryFile.seek方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。