'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)
:param mode: This is ignored (present for legacy compatibility)'

def format(self, index, format, index_is_id=False, as_file=False, mode='r+b', as_path=False, preserve_filename=False):

    path = self.format_abspath(index, format, index_is_id=index_is_id)
    if (path is not None):
        with lopen(path, mode) as f:
            if as_path:
                if preserve_filename:
                    bd = base_dir()
                    d = os.path.join(bd, 'format_abspath')
                    try:
                        os.makedirs(d)
                    except:
                        pass
                    fname = os.path.basename(path)
                    ret = os.path.join(d, fname)
                    with lopen(ret, 'wb') as f2:
                        shutil.copyfileobj(f, f2)
                else:
                    with PersistentTemporaryFile(('.' + format.lower())) as pt:
                        shutil.copyfileobj(f, pt)
                        ret = pt.name
            elif as_file:
                ret = SpooledTemporaryFile(SPOOL_SIZE)
                shutil.copyfileobj(f, ret)
                ret.seek(0)
                ret.name = f.name
            else:
                ret = f.read()
        return ret
