mirror of
https://github.com/gryf/ebook-converter.git
synced 2026-01-06 03:04:11 +01:00
Convert calibre modules to ebook_converter.
Here is the first batch of modules, which are needed for converting several formats to LRF. Some of the logic has been change, more cleanups will follow.
This commit is contained in:
@@ -7,9 +7,9 @@ __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||
|
||||
import os, sys
|
||||
|
||||
from polyglot.builtins import reraise
|
||||
from ebook_converter.polyglot.builtins import reraise
|
||||
|
||||
from calibre.constants import iswindows, plugins, ispy3
|
||||
from ebook_converter.constants import iswindows, plugins, ispy3
|
||||
|
||||
'''
|
||||
This module defines a share_open() function which is a replacement for
|
||||
@@ -28,10 +28,10 @@ directory until all file handles are closed. To get around this, rename the
|
||||
file before deleting it.
|
||||
'''
|
||||
|
||||
speedup, err = plugins['speedup']
|
||||
# speedup, err = plugins['speedup']
|
||||
|
||||
if not speedup:
|
||||
raise RuntimeError('Failed to load the speedup plugin with error: %s' % err)
|
||||
# if not speedup:
|
||||
# raise RuntimeError('Failed to load the speedup plugin with error: %s' % err)
|
||||
|
||||
valid_modes = {'a', 'a+', 'a+b', 'ab', 'r', 'rb', 'r+', 'r+b', 'w', 'wb', 'w+', 'w+b'}
|
||||
|
||||
@@ -81,117 +81,12 @@ def flags_from_mode(mode):
|
||||
return flags
|
||||
|
||||
|
||||
if iswindows:
|
||||
from numbers import Integral
|
||||
import msvcrt
|
||||
import win32file, pywintypes
|
||||
CREATE_NEW = win32file.CREATE_NEW
|
||||
CREATE_ALWAYS = win32file.CREATE_ALWAYS
|
||||
OPEN_EXISTING = win32file.OPEN_EXISTING
|
||||
OPEN_ALWAYS = win32file.OPEN_ALWAYS
|
||||
TRUNCATE_EXISTING = win32file.TRUNCATE_EXISTING
|
||||
FILE_SHARE_READ = win32file.FILE_SHARE_READ
|
||||
FILE_SHARE_WRITE = win32file.FILE_SHARE_WRITE
|
||||
FILE_SHARE_DELETE = win32file.FILE_SHARE_DELETE
|
||||
FILE_SHARE_VALID_FLAGS = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE
|
||||
FILE_ATTRIBUTE_READONLY = win32file.FILE_ATTRIBUTE_READONLY
|
||||
FILE_ATTRIBUTE_NORMAL = win32file.FILE_ATTRIBUTE_NORMAL
|
||||
FILE_ATTRIBUTE_TEMPORARY = win32file.FILE_ATTRIBUTE_TEMPORARY
|
||||
FILE_FLAG_DELETE_ON_CLOSE = win32file.FILE_FLAG_DELETE_ON_CLOSE
|
||||
FILE_FLAG_SEQUENTIAL_SCAN = win32file.FILE_FLAG_SEQUENTIAL_SCAN
|
||||
FILE_FLAG_RANDOM_ACCESS = win32file.FILE_FLAG_RANDOM_ACCESS
|
||||
GENERIC_READ = win32file.GENERIC_READ & 0xffffffff
|
||||
GENERIC_WRITE = win32file.GENERIC_WRITE & 0xffffffff
|
||||
DELETE = 0x00010000
|
||||
|
||||
_ACCESS_MASK = os.O_RDONLY | os.O_WRONLY | os.O_RDWR
|
||||
_ACCESS_MAP = {
|
||||
os.O_RDONLY : GENERIC_READ,
|
||||
os.O_WRONLY : GENERIC_WRITE,
|
||||
os.O_RDWR : GENERIC_READ | GENERIC_WRITE
|
||||
}
|
||||
|
||||
_CREATE_MASK = os.O_CREAT | os.O_EXCL | os.O_TRUNC
|
||||
_CREATE_MAP = {
|
||||
0 : OPEN_EXISTING,
|
||||
os.O_EXCL : OPEN_EXISTING,
|
||||
os.O_CREAT : OPEN_ALWAYS,
|
||||
os.O_CREAT | os.O_EXCL : CREATE_NEW,
|
||||
os.O_CREAT | os.O_TRUNC | os.O_EXCL : CREATE_NEW,
|
||||
os.O_TRUNC : TRUNCATE_EXISTING,
|
||||
os.O_TRUNC | os.O_EXCL : TRUNCATE_EXISTING,
|
||||
os.O_CREAT | os.O_TRUNC : CREATE_ALWAYS
|
||||
}
|
||||
|
||||
def raise_winerror(pywinerr):
|
||||
reraise(
|
||||
WindowsError,
|
||||
WindowsError(pywinerr.winerror,
|
||||
(pywinerr.funcname or '') + b': ' + (pywinerr.strerror or '')),
|
||||
sys.exc_info()[2])
|
||||
|
||||
def os_open(path, flags, mode=0o777, share_flags=FILE_SHARE_VALID_FLAGS):
|
||||
'''
|
||||
Replacement for os.open() allowing moving or unlinking before closing
|
||||
'''
|
||||
if not isinstance(flags, Integral):
|
||||
raise TypeError('flags must be an integer')
|
||||
if not isinstance(mode, Integral):
|
||||
raise TypeError('mode must be an integer')
|
||||
|
||||
if share_flags & ~FILE_SHARE_VALID_FLAGS:
|
||||
raise ValueError('bad share_flags: %r' % share_flags)
|
||||
|
||||
access_flags = _ACCESS_MAP[flags & _ACCESS_MASK]
|
||||
create_flags = _CREATE_MAP[flags & _CREATE_MASK]
|
||||
attrib_flags = FILE_ATTRIBUTE_NORMAL
|
||||
|
||||
if flags & os.O_CREAT and mode & ~0o444 == 0:
|
||||
attrib_flags = FILE_ATTRIBUTE_READONLY
|
||||
|
||||
if flags & os.O_TEMPORARY:
|
||||
share_flags |= FILE_SHARE_DELETE
|
||||
attrib_flags |= FILE_FLAG_DELETE_ON_CLOSE
|
||||
access_flags |= DELETE
|
||||
|
||||
if flags & os.O_SHORT_LIVED:
|
||||
attrib_flags |= FILE_ATTRIBUTE_TEMPORARY
|
||||
|
||||
if flags & os.O_SEQUENTIAL:
|
||||
attrib_flags |= FILE_FLAG_SEQUENTIAL_SCAN
|
||||
|
||||
if flags & os.O_RANDOM:
|
||||
attrib_flags |= FILE_FLAG_RANDOM_ACCESS
|
||||
|
||||
try:
|
||||
h = win32file.CreateFileW(
|
||||
path, access_flags, share_flags, None, create_flags, attrib_flags, None)
|
||||
except pywintypes.error as e:
|
||||
raise_winerror(e)
|
||||
ans = msvcrt.open_osfhandle(h, flags | os.O_NOINHERIT)
|
||||
h.Detach() # We dont want the handle to be automatically closed when h is deleted
|
||||
return ans
|
||||
|
||||
def share_open(path, mode='r', buffering=-1):
|
||||
flags = flags_from_mode(mode)
|
||||
return speedup.fdopen(os_open(path, flags), path, mode, buffering)
|
||||
|
||||
else:
|
||||
if ispy3:
|
||||
# See PEP 446
|
||||
share_open = open
|
||||
else:
|
||||
def share_open(path, mode='r', buffering=-1):
|
||||
flags = flags_from_mode(mode) | speedup.O_CLOEXEC
|
||||
return speedup.fdopen(os.open(path, flags), path, mode, buffering)
|
||||
|
||||
def raise_winerror(x):
|
||||
reraise(NotImplementedError, None, sys.exc_info()[2])
|
||||
share_open = open
|
||||
|
||||
|
||||
def find_tests():
|
||||
import unittest
|
||||
from calibre.ptempfile import TemporaryDirectory
|
||||
from ebook_converter.ptempfile import TemporaryDirectory
|
||||
|
||||
class SharedFileTest(unittest.TestCase):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user