mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Move ReadOnyFileBuffer into its own module
This commit is contained in:
parent
0f37808458
commit
eb3eb615ec
@ -22,7 +22,8 @@ from calibre.srv.http_request import HTTPRequest, read_headers
|
|||||||
from calibre.srv.sendfile import file_metadata, sendfile_to_socket_async, CannotSendfile, SendfileInterrupted
|
from calibre.srv.sendfile import file_metadata, sendfile_to_socket_async, CannotSendfile, SendfileInterrupted
|
||||||
from calibre.srv.utils import (
|
from calibre.srv.utils import (
|
||||||
MultiDict, http_date, HTTP1, HTTP11, socket_errors_socket_closed,
|
MultiDict, http_date, HTTP1, HTTP11, socket_errors_socket_closed,
|
||||||
sort_q_values, get_translator_for_lang, Cookie, ReadOnlyFileBuffer)
|
sort_q_values, get_translator_for_lang, Cookie)
|
||||||
|
from calibre.utils.speedups import ReadOnlyFileBuffer
|
||||||
from calibre.utils.monotonic import monotonic
|
from calibre.utils.monotonic import monotonic
|
||||||
|
|
||||||
Range = namedtuple('Range', 'start stop size')
|
Range = namedtuple('Range', 'start stop size')
|
||||||
|
@ -434,42 +434,6 @@ class Accumulator(object): # {{{
|
|||||||
return ans
|
return ans
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
class ReadOnlyFileBuffer(object):
|
|
||||||
|
|
||||||
''' A zero copy implementation of a file like object. Uses memoryviews for efficiency. '''
|
|
||||||
|
|
||||||
def __init__(self, raw):
|
|
||||||
self.sz, self.mv = len(raw), (raw if isinstance(raw, memoryview) else memoryview(raw))
|
|
||||||
self.pos = 0
|
|
||||||
|
|
||||||
def tell(self):
|
|
||||||
return self.pos
|
|
||||||
|
|
||||||
def read(self, n=None):
|
|
||||||
if n is None:
|
|
||||||
ans = self.mv[self.pos:]
|
|
||||||
self.pos = self.sz
|
|
||||||
return ans
|
|
||||||
ans = self.mv[self.pos:self.pos+n]
|
|
||||||
self.pos = min(self.pos + n, self.sz)
|
|
||||||
return ans
|
|
||||||
|
|
||||||
def seek(self, pos, whence=os.SEEK_SET):
|
|
||||||
if whence == os.SEEK_SET:
|
|
||||||
self.pos = pos
|
|
||||||
elif whence == os.SEEK_END:
|
|
||||||
self.pos = self.sz + pos
|
|
||||||
else:
|
|
||||||
self.pos += pos
|
|
||||||
self.pos = max(0, min(self.pos, self.sz))
|
|
||||||
return self.pos
|
|
||||||
|
|
||||||
def getvalue(self):
|
|
||||||
return self.mv
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def get_db(ctx, rd, library_id):
|
def get_db(ctx, rd, library_id):
|
||||||
db = ctx.get_library(rd, library_id)
|
db = ctx.get_library(rd, library_id)
|
||||||
if db is None:
|
if db is None:
|
||||||
|
@ -17,7 +17,8 @@ from calibre import as_unicode
|
|||||||
from calibre.constants import plugins
|
from calibre.constants import plugins
|
||||||
from calibre.srv.loop import ServerLoop, HandleInterrupt, WRITE, READ, RDWR, Connection
|
from calibre.srv.loop import ServerLoop, HandleInterrupt, WRITE, READ, RDWR, Connection
|
||||||
from calibre.srv.http_response import HTTPConnection, create_http_handler
|
from calibre.srv.http_response import HTTPConnection, create_http_handler
|
||||||
from calibre.srv.utils import DESIRED_SEND_BUFFER_SIZE, ReadOnlyFileBuffer
|
from calibre.srv.utils import DESIRED_SEND_BUFFER_SIZE
|
||||||
|
from calibre.utils.speedups import ReadOnlyFileBuffer
|
||||||
speedup, err = plugins['speedup']
|
speedup, err = plugins['speedup']
|
||||||
if not speedup:
|
if not speedup:
|
||||||
raise RuntimeError('Failed to load speedup module with error: ' + err)
|
raise RuntimeError('Failed to load speedup module with error: ' + err)
|
||||||
|
@ -437,7 +437,7 @@ def optimize_png(file_path):
|
|||||||
return run_optimizer(file_path, cmd)
|
return run_optimizer(file_path, cmd)
|
||||||
|
|
||||||
def encode_jpeg(file_path, quality=80):
|
def encode_jpeg(file_path, quality=80):
|
||||||
from calibre.srv.utils import ReadOnlyFileBuffer
|
from calibre.utils.speedups import ReadOnlyFileBuffer
|
||||||
quality = max(0, min(100, int(quality)))
|
quality = max(0, min(100, int(quality)))
|
||||||
exe = get_exe_path('cjpeg')
|
exe = get_exe_path('cjpeg')
|
||||||
cmd = [exe] + '-optimize -progressive -maxmemory 100M -quality'.split() + [str(quality)]
|
cmd = [exe] + '-optimize -progressive -maxmemory 100M -quality'.split() + [str(quality)]
|
||||||
|
@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
|
|||||||
print_function)
|
print_function)
|
||||||
from struct import unpack, error
|
from struct import unpack, error
|
||||||
import os
|
import os
|
||||||
from calibre.srv.utils import ReadOnlyFileBuffer
|
from calibre.utils.speedups import ReadOnlyFileBuffer
|
||||||
|
|
||||||
""" Recognize image file formats and sizes based on their first few bytes."""
|
""" Recognize image file formats and sizes based on their first few bytes."""
|
||||||
|
|
||||||
|
46
src/calibre/utils/speedups.py
Normal file
46
src/calibre/utils/speedups.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#!/usr/bin/env python2
|
||||||
|
# vim:fileencoding=utf-8
|
||||||
|
# License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
|
from __future__ import (unicode_literals, division, absolute_import,
|
||||||
|
print_function)
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
class ReadOnlyFileBuffer(object):
|
||||||
|
|
||||||
|
''' A zero copy implementation of a file like object. Uses memoryviews for efficiency. '''
|
||||||
|
|
||||||
|
def __init__(self, raw):
|
||||||
|
self.sz, self.mv = len(raw), (raw if isinstance(raw, memoryview) else memoryview(raw))
|
||||||
|
self.pos = 0
|
||||||
|
|
||||||
|
def tell(self):
|
||||||
|
return self.pos
|
||||||
|
|
||||||
|
def read(self, n=None):
|
||||||
|
if n is None:
|
||||||
|
ans = self.mv[self.pos:]
|
||||||
|
self.pos = self.sz
|
||||||
|
return ans
|
||||||
|
ans = self.mv[self.pos:self.pos+n]
|
||||||
|
self.pos = min(self.pos + n, self.sz)
|
||||||
|
return ans
|
||||||
|
|
||||||
|
def seek(self, pos, whence=os.SEEK_SET):
|
||||||
|
if whence == os.SEEK_SET:
|
||||||
|
self.pos = pos
|
||||||
|
elif whence == os.SEEK_END:
|
||||||
|
self.pos = self.sz + pos
|
||||||
|
else:
|
||||||
|
self.pos += pos
|
||||||
|
self.pos = max(0, min(self.pos, self.sz))
|
||||||
|
return self.pos
|
||||||
|
|
||||||
|
def getvalue(self):
|
||||||
|
return self.mv
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user