Pull from driver-dev

This commit is contained in:
Kovid Goyal 2009-04-18 01:18:48 -07:00
commit 67d441e45b
10 changed files with 82 additions and 40 deletions

View File

@ -246,6 +246,23 @@ class CurrentDir(object):
os.chdir(self.cwd) os.chdir(self.cwd)
class FileWrapper(object):
'''
Used primarily with pyPdf to ensure the stream is properly closed.
'''
def __init__(self, stream):
for x in ('read', 'seek', 'tell'):
setattr(self, x, getattr(stream, x))
def __exit__(self, *args):
for x in ('read', 'seek', 'tell'):
setattr(self, x, None)
def __enter__(self):
return self
def detect_ncpus(): def detect_ncpus():
"""Detects the number of effective CPUs in the system""" """Detects the number of effective CPUs in the system"""
try: try:

View File

@ -7,7 +7,6 @@ Device driver for Bookeen's Cybook Gen 3
import os, shutil import os, shutil
from itertools import cycle from itertools import cycle
from calibre.ebooks.metadata import authors_to_string
from calibre.devices.errors import DeviceError, FreeSpaceError from calibre.devices.errors import DeviceError, FreeSpaceError
from calibre.devices.usbms.driver import USBMS from calibre.devices.usbms.driver import USBMS
import calibre.devices.cybookg3.t2b as t2b import calibre.devices.cybookg3.t2b as t2b
@ -78,22 +77,21 @@ class CYBOOKG3(USBMS):
newpath = path newpath = path
mdata = metadata.next() mdata = metadata.next()
if self.SUPPORTS_SUB_DIRS: if 'tags' in mdata.keys():
if 'tags' in mdata.keys(): for tag in mdata['tags']:
for tag in mdata['tags']: if tag.startswith(_('News')):
if tag.startswith(_('News')): newpath = os.path.join(newpath, 'news')
newpath = os.path.join(newpath, 'news') newpath = os.path.join(newpath, mdata.get('title', ''))
newpath = os.path.join(newpath, mdata.get('title', '')) newpath = os.path.join(newpath, mdata.get('timestamp', ''))
newpath = os.path.join(newpath, mdata.get('timestamp', '')) elif tag.startswith('/'):
elif tag.startswith('/'): newpath = path
newpath = path newpath += tag
newpath += tag newpath = os.path.normpath(newpath)
newpath = os.path.normpath(newpath) break
break
if newpath == path: if newpath == path:
newpath = os.path.join(newpath, authors_to_string(mdata.get('authors', ''))) newpath = os.path.join(newpath, mdata.get('authors', _('Unknown')))
newpath = os.path.join(newpath, mdata.get('title', '')) newpath = os.path.join(newpath, mdata.get('title', _('Unknown')))
if not os.path.exists(newpath): if not os.path.exists(newpath):
os.makedirs(newpath) os.makedirs(newpath)

View File

@ -119,19 +119,44 @@ class PRS505(CLI, Device):
paths, ctimes = [], [] paths, ctimes = [], []
names = iter(names) names = iter(names)
metadata = iter(metadata)
for infile in files: for infile in files:
close = False close = False
if not hasattr(infile, 'read'): if not hasattr(infile, 'read'):
infile, close = open(infile, 'rb'), True infile, close = open(infile, 'rb'), True
infile.seek(0) infile.seek(0)
name = names.next()
paths.append(os.path.join(path, name)) newpath = path
if not os.path.exists(os.path.dirname(paths[-1])): mdata = metadata.next()
os.makedirs(os.path.dirname(paths[-1]))
if 'tags' in mdata.keys():
for tag in mdata['tags']:
if tag.startswith(_('News')):
newpath = os.path.join(newpath, 'news')
newpath = os.path.join(newpath, mdata.get('title', ''))
newpath = os.path.join(newpath, mdata.get('timestamp', ''))
elif tag.startswith('/'):
newpath = path
newpath += tag
newpath = os.path.normpath(newpath)
break
if newpath == path:
newpath = os.path.join(newpath, mdata.get('authors', _('Unknown')))
newpath = os.path.join(newpath, mdata.get('title', _('Unknown')))
if not os.path.exists(newpath):
os.makedirs(newpath)
filepath = os.path.join(newpath, names.next())
paths.append(filepath)
self.put_file(infile, paths[-1], replace_file=True) self.put_file(infile, paths[-1], replace_file=True)
if close: if close:
infile.close() infile.close()
ctimes.append(os.path.getctime(paths[-1])) ctimes.append(os.path.getctime(paths[-1]))
return zip(paths, sizes, ctimes, cycle([on_card])) return zip(paths, sizes, ctimes, cycle([on_card]))
@classmethod @classmethod

View File

@ -124,8 +124,8 @@ class USBMS(CLI, Device):
break break
if newpath == path: if newpath == path:
newpath = os.path.join(newpath, authors_to_string(mdata.get('authors', ''))) newpath = os.path.join(newpath, mdata.get('authors', _('Unknown')))
newpath = os.path.join(newpath, mdata.get('title', '')) newpath = os.path.join(newpath, mdata.get('title', _('Unknown')))
if not os.path.exists(newpath): if not os.path.exists(newpath):
os.makedirs(newpath) os.makedirs(newpath)

View File

@ -73,7 +73,7 @@ class HTMLPreProcessor(object):
(re.compile(r'<br.*?>'), lambda match : '<p>'), (re.compile(r'<br.*?>'), lambda match : '<p>'),
# Un wrap lines # Un wrap lines
(re.compile(r'(?<=\w)\s*</i>\s*<p.*?>\s*<i>\s*(?=\w)'), lambda match: ' '), (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'(?<=\w)\s*<p.*?>\s*(?=\w)', re.UNICODE), lambda match: ' '),
# Clean up spaces # Clean up spaces
(re.compile(u'(?<=\.|,|:|;|\?|!|”|"|\')[\s^ ]*(?=<)'), lambda match: ' '), (re.compile(u'(?<=\.|,|:|;|\?|!|”|"|\')[\s^ ]*(?=<)'), lambda match: ' '),

View File

@ -7,6 +7,7 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
import sys, os, cStringIO import sys, os, cStringIO
from threading import Thread from threading import Thread
from calibre import FileWrapper
from calibre.ebooks.metadata import MetaInformation, authors_to_string from calibre.ebooks.metadata import MetaInformation, authors_to_string
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
from pyPdf import PdfFileReader, PdfFileWriter from pyPdf import PdfFileReader, PdfFileWriter
@ -34,18 +35,19 @@ def get_metadata(stream, extract_cover=True):
traceback.print_exc() traceback.print_exc()
try: try:
info = PdfFileReader(stream).getDocumentInfo() with FileWrapper(stream) as stream:
if info.title: info = PdfFileReader(stream).getDocumentInfo()
mi.title = info.title if info.title:
if info.author: mi.title = info.title
src = info.author.split('&') if info.author:
authors = [] src = info.author.split('&')
for au in src: authors = []
authors += au.split(',') for au in src:
mi.authors = authors authors += au.split(',')
mi.author = info.author mi.authors = authors
if info.subject: mi.author = info.author
mi.category = info.subject if info.subject:
mi.category = info.subject
except Exception, err: except Exception, err:
msg = u'Couldn\'t read metadata from pdf: %s with error %s'%(mi.title, unicode(err)) msg = u'Couldn\'t read metadata from pdf: %s with error %s'%(mi.title, unicode(err))
print >>sys.stderr, msg.encode('utf8') print >>sys.stderr, msg.encode('utf8')

View File

@ -9,7 +9,7 @@ import os
from calibre.customize.conversion import InputFormatPlugin from calibre.customize.conversion import InputFormatPlugin
from calibre.ebooks.pdf.pdftohtml import pdftohtml from calibre.ebooks.pdf.pdftohtml import pdftohtml
from calibre.ebooks.metadata.opf import OPFCreator from calibre.ebooks.metadata.opf2 import OPFCreator
class PDFInput(InputFormatPlugin): class PDFInput(InputFormatPlugin):

View File

@ -9,7 +9,7 @@ import os
from calibre.customize.conversion import InputFormatPlugin from calibre.customize.conversion import InputFormatPlugin
from calibre.ebooks.markdown import markdown from calibre.ebooks.markdown import markdown
from calibre.ebooks.metadata.opf import OPFCreator from calibre.ebooks.metadata.opf2 import OPFCreator
class TXTInput(InputFormatPlugin): class TXTInput(InputFormatPlugin):

View File

@ -18,14 +18,14 @@ class TXTOutput(OutputFormatPlugin):
options = set([ options = set([
OptionRecommendation(name='newline', recommended_value='system', OptionRecommendation(name='newline', recommended_value='system',
level=OptionRecommendation.LOW, long_switch='newline', level=OptionRecommendation.LOW,
short_switch='n', choices=TxtNewlines.NEWLINE_TYPES.keys(), short_switch='n', choices=TxtNewlines.NEWLINE_TYPES.keys(),
help=_('Type of newline to use. Options are %s. Default is \'system\'. ' help=_('Type of newline to use. Options are %s. Default is \'system\'. '
'Use \'old_mac\' for compatibility with Mac OS 9 and earlier. ' 'Use \'old_mac\' for compatibility with Mac OS 9 and earlier. '
'For Mac OS X use \'unix\'. \'system\' will default to the newline ' 'For Mac OS X use \'unix\'. \'system\' will default to the newline '
'type used by this OS.' % sorted(TxtNewlines.NEWLINE_TYPES.keys()))), 'type used by this OS.' % sorted(TxtNewlines.NEWLINE_TYPES.keys()))),
OptionRecommendation(name='prepend_metadata', recommended_value='false', OptionRecommendation(name='prepend_metadata', recommended_value='false',
level=OptionRecommendation.LOW, long_switch='prepend_metadata', level=OptionRecommendation.LOW,
choices=['true', 'false'], choices=['true', 'false'],
help=_('Write the title and author to the beginning of the file. ' help=_('Write the title and author to the beginning of the file. '
'Default is \'true\'. Use \'false\' to disable.')), 'Default is \'true\'. Use \'false\' to disable.')),

View File

@ -76,7 +76,7 @@ class TxtWriter(object):
text = re.sub('(?imu)</[ ]*%s[ ]*>' % tag, '\n\n', text) text = re.sub('(?imu)</[ ]*%s[ ]*>' % tag, '\n\n', text)
for tag in ['hr', 'br']: for tag in ['hr', 'br']:
text = re.sub('(?imu)<[ ]*%s[ ]*/*?>' % tag, '\n\n', text) text = re.sub('(?imu)<[ ]*%s.*?>' % tag, '\n\n', text)
# Remove any tags that do not need special processing. # Remove any tags that do not need special processing.
text = re.sub('<.*?>', '', text) text = re.sub('<.*?>', '', text)