Pull from driver-dev

This commit is contained in:
Kovid Goyal 2009-04-19 14:46:20 -07:00
commit 10a95db60a
6 changed files with 61 additions and 32 deletions

View File

@ -246,7 +246,7 @@ class CurrentDir(object):
os.chdir(self.cwd)
class FileWrapper(object):
class StreamReadWrapper(object):
'''
Used primarily with pyPdf to ensure the stream is properly closed.
'''

View File

@ -23,6 +23,14 @@ def sanitize_head(match):
x = _span_pat.sub('', x)
return '<head>\n'+x+'\n</head>'
def chap_head(match):
chap = match.group('chap')
title = match.group('title')
if not title:
return '<h1>'+chap+'</h1><br/>'
else:
return '<h1>'+chap+'<br/>'+title+'</h1><br/>'
class CSSPreProcessor(object):
@ -54,8 +62,9 @@ class HTMLPreProcessor(object):
(re.compile(r'<hr.*?>', re.IGNORECASE), lambda match: '<br />'),
# Remove page numbers
(re.compile(r'\d+<br>', re.IGNORECASE), lambda match: ''),
# Remove <br> and replace <br><br> with <p>
# Replace <br><br> with <p>
(re.compile(r'<br.*?>\s*<br.*?>', re.IGNORECASE), lambda match: '<p>'),
# Remove <br>
(re.compile(r'(.*)<br.*?>', re.IGNORECASE),
lambda match: match.group() if \
re.match('<', match.group(1).lstrip()) or \
@ -69,14 +78,21 @@ class HTMLPreProcessor(object):
# Remove non breaking spaces
(re.compile(ur'\u00a0'), lambda match : ' '),
# Detect Chapters to match default XPATH in GUI
(re.compile(r'(<br[^>]*>)?(</?p[^>]*>)?s*(?P<chap>(Chapter|Epilogue|Prologue|Book|Part)\s*(\d+|\w+)?)(</?p[^>]*>|<br[^>]*>)\n?((?=(<i>)?\s*\w+(\s+\w+)?(</i>)?(<br[^>]*>|</?p[^>]*>))((?P<title>.*)(<br[^>]*>|</?p[^>]*>)))?', re.IGNORECASE), chap_head),
(re.compile(r'(<br[^>]*>)?(</?p[^>]*>)?s*(?P<chap>([A-Z \'"!]{5,})\s*(\d+|\w+)?)(</?p[^>]*>|<br[^>]*>)\n?((?=(<i>)?\s*\w+(\s+\w+)?(</i>)?(<br[^>]*>|</?p[^>]*>))((?P<title>.*)(<br[^>]*>|</?p[^>]*>)))?'), chap_head),
# Have paragraphs show better
(re.compile(r'<br.*?>'), lambda match : '<p>'),
# Un wrap lines
(re.compile(r'(?<=\w)\s*</(i|b|u)>\s*<p.*?>\s*<(i|b|u)>\s*(?=\w)'), lambda match: ' '),
(re.compile(r'(?<=\w)\s*<p.*?>\s*(?=\w)', re.UNICODE), lambda match: ' '),
(re.compile(r'(?<=[^\.^\^?^!^"^”])\s*(</(i|b|u)>)*\s*<p.*?>\s*(<(i|b|u)>)*\s*(?=[a-z0-9I])', re.UNICODE), lambda match: ' '),
# Clean up spaces
(re.compile(u'(?<=\.|,|:|;|\?|!|”|"|\')[\s^ ]*(?=<)'), lambda match: ' '),
(re.compile(u'(?<=[\.,:;\?!”"\'])[\s^ ]*(?=<)'), lambda match: ' '),
# Add space before and after italics
(re.compile(r'(?<!“)<i>'), lambda match: ' <i>'),
(re.compile(r'</i>(?=\w)'), lambda match: '</i> '),
]
# Fix Book Designer markup

View File

@ -6,7 +6,7 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
import sys, os, cStringIO
from threading import Thread
from calibre import FileWrapper
from calibre import StreamReadWrapper
from calibre.ebooks.metadata import MetaInformation, authors_to_string
from calibre.ptempfile import TemporaryDirectory
from pyPdf import PdfFileReader, PdfFileWriter
@ -33,7 +33,7 @@ def get_metadata(stream, extract_cover=True):
traceback.print_exc()
try:
with FileWrapper(stream) as stream:
with StreamReadWrapper(stream) as stream:
info = PdfFileReader(stream).getDocumentInfo()
if info.title:
mi.title = info.title
@ -94,8 +94,8 @@ def set_metadata(stream, mi):
stream.seek(0)
def get_cover(stream):
try:
with StreamReadWrapper(stream) as stream:
pdf = PdfFileReader(stream)
output = PdfFileWriter()
@ -113,10 +113,8 @@ def get_cover(stream):
MagickSetImageFormat(wand, 'JPEG')
MagickWriteImage(wand, '%s.jpg' % cover_path)
return open('%s.jpg' % cover_path, 'rb').read()
except:
import traceback
traceback.print_exc()
return ''

View File

@ -126,7 +126,7 @@ class PDFWriter(QObject):
try:
outPDF = PdfFileWriter(title=self.metadata.title, author=self.metadata.author)
for item in self.combine_queue:
inputPDF = PdfFileReader(file(item, 'rb'))
inputPDF = PdfFileReader(open(item, 'rb'))
for page in inputPDF.pages:
outPDF.addPage(page)
outPDF.write(self.out_stream)

View File

@ -346,10 +346,25 @@ class DeviceMenu(QMenu):
self.action_triggered(action)
break
def enable_device_actions(self, enable):
def enable_device_actions(self, enable, card_prefix=(None, None)):
for action in self.actions:
if action.dest in ('main:', 'carda:0', 'cardb:0'):
action.setEnabled(enable)
if not enable:
action.setEnabled(False)
else:
if action.dest == 'main:':
action.setEnabled(True)
elif action.dest == 'carda:0':
if card_prefix[0] != None:
action.setEnabled(True)
else:
action.setEnabled(False)
elif action.dest == 'cardb:0':
if card_prefix[1] != None:
action.setEnabled(True)
else:
action.setEnabled(False)
class Emailer(Thread):

View File

@ -608,7 +608,7 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI):
self.device_manager.device.__class__.__name__+\
_(' detected.'), 3000)
self.device_connected = True
self._sync_menu.enable_device_actions(True)
self._sync_menu.enable_device_actions(True, self.device_manager.device.card_prefix())
else:
self.device_connected = False
self._sync_menu.enable_device_actions(False)