squash squash...chitter...squash

This commit is contained in:
Kovid Goyal 2007-08-07 03:07:54 +00:00
parent 4ea00e817a
commit 6b7af60e4b
5 changed files with 47 additions and 19 deletions

View File

@ -13,7 +13,7 @@
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
''' E-book management software'''
__version__ = "0.3.91"
__version__ = "0.3.92"
__docformat__ = "epytext"
__author__ = "Kovid Goyal <kovid@kovidgoyal.net>"
__appname__ = 'libprs500'

View File

@ -31,7 +31,7 @@ MIME_MAP = { \
}
def sortable_title(title):
return re.sub('^\s*A\s+|^\s*The\s+|^\s*An\s+', '', ' An Crum').rstrip()
return re.sub('^\s*A\s+|^\s*The\s+|^\s*An\s+', '', title).rstrip()
class book_metadata_field(object):
""" Represents metadata stored as an attribute """
@ -113,7 +113,7 @@ class Book(object):
def __str__(self):
""" Return a utf-8 encoded string with title author and path information """
return self.title.encode('utf-8') + " by " + \
self.author.encode('utf-8') + " at " + self.path.encode('utf-8')
self.authors.encode('utf-8') + " at " + self.path.encode('utf-8')
def fix_ids(media, cache):
@ -203,6 +203,16 @@ class BookList(list):
plitems.extend(pl.getElementsByTagName(self.prefix+'item'))
return plitems
def purge_corrupted_files(self):
corrupted = self.root.getElementsByTagName(self.prefix+'corrupted')
paths = []
proot = self.proot if self.proot.endswith('/') else self.proot + '/'
for c in corrupted:
paths.append(proot + c.getAttribute('path'))
c.parentNode.removeChild(c)
c.unlink()
return paths
def purge_empty_playlists(self):
''' Remove all playlist entries that have no children. '''
playlists = self.root.getElementsByTagName(self.prefix+'playlist')
@ -250,12 +260,13 @@ class BookList(list):
sourceid = str(self[0].sourceid) if len(self) else "1"
attrs = {
"title" : info["title"],
'titleSorter' : info['title'],
'titleSorter' : sortable_title(info['title']),
"author" : info["authors"] if info['authors'] else 'Unknown', \
"page":"0", "part":"0", "scale":"0", \
"sourceid":sourceid, "id":str(cid), "date":"", \
"mime":mime, "path":name, "size":str(size)
}
print name
for attr in attrs.keys():
node.setAttributeNode(self.document.createAttribute(attr))
node.setAttribute(attr, attrs[attr])

View File

@ -107,7 +107,8 @@ class PRS500(Device):
FORMATS = ["lrf", "rtf", "pdf", "txt"]
# Height for thumbnails of books/images on the device
THUMBNAIL_HEIGHT = 68
# Directory on card to which books are copied
CARD_PATH_PREFIX = 'libprs500'
_packet_number = 0 #: Keep track of the packet number for packet tracing
def log_packet(self, packet, header, stream=sys.stderr):
@ -799,8 +800,15 @@ class PRS500(Device):
if tfile.tell() == 0:
tfile = None
else:
self.get_file(self.MEDIA_XML, tfile, end_session=False)
return BookList(root=root, sfile=tfile)
self.get_file(self.MEDIA_XML, tfile, end_session=False)
bl = BookList(root=root, sfile=tfile)
paths = bl.purge_corrupted_files()
for path in paths:
try:
self.del_file(path, end_session=False)
except PathError: # Incase this is a refetch without a sync in between
continue
return bl
@safe
def remove_books(self, paths, booklists, end_session=True):
@ -827,7 +835,7 @@ class PRS500(Device):
@safe
def upload_books(self, files, names, on_card=False, end_session=True):
card = self.card(end_session=False)
prefix = card + '/libprs500/' if on_card else '/Data/media/books/'
prefix = card + '/' + self.CARD_PATH_PREFIX +'/' if on_card else '/Data/media/books/'
if on_card and not self._exists(prefix)[0]:
self.mkdir(prefix[:-1], False)
paths, ctimes = [], []
@ -862,8 +870,7 @@ class PRS500(Device):
path = location[0]
on_card = 1 if path[1] == ':' else 0
name = path.rpartition('/')[2]
if not on_card:
name = 'books/' + name
name = (cls.CARD_PATH_PREFIX+'/' if on_card else 'books/') + name
booklists[on_card].add_book(info, name, *location[1:])
fix_ids(*booklists)

View File

@ -12,6 +12,7 @@
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from libprs500 import filename_to_utf8
''''''
import sys, os, subprocess
@ -35,12 +36,18 @@ def generate_html(pathtopdf):
raise ConversionError, 'Cannot read from ' + pathtopdf
pf = PersistentTemporaryFile('.html')
pf.close()
cmd = PDFTOHTML + ' -noframes -p -nomerge "%s" "%s"'%(pathtopdf, pf.name)
p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
ret = p.wait()
if ret != 0:
err = p.stderr.read()
raise ConversionError, err
# This is neccessary as pdftohtml doesn't always (linux) respect absolute paths
cmd = PDFTOHTML + ' -noframes -p -nomerge "%s" "%s"'%(pathtopdf, os.path.basename(pf.name))
cwd = os.getcwd()
try:
os.chdir(os.path.dirname(pf.name))
p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
ret = p.wait()
if ret != 0:
err = p.stderr.read()
raise ConversionError, err
finally:
os.chdir(cwd)
return pf
def option_parser():
@ -61,11 +68,13 @@ def main(args=sys.argv):
pdf = os.path.abspath(os.path.expanduser(args[1]))
htmlfile = generate_html(pdf)
if not options.output:
ext = '.lrs' if options.lrs else '.lrf'
ext = '.lrs' if options.lrs else '.lrf'
options.output = os.path.abspath(os.path.basename(os.path.splitext(args[1])[0]) + ext)
else:
options.output = os.path.abspath(options.output)
options.pdftohtml = True
options.pdftohtml = True
if not options.title:
options.title = filename_to_utf8(os.path.splitext(os.path.basename(options.output))[0])
process_file(htmlfile.name, options)
return 0

View File

@ -415,7 +415,8 @@ class Main(QObject, Ui_MainWindow):
self.status_bar.showMessage('Sending books to device.', 5000)
if bad:
bad = '\n'.join('<li>%s</li>'%(i,) for i in bad)
d = warning_dialog(self.window, 'No suitable formats', 'Could not upload the following books to the device, as no suitable formats were found:<br><ul>%s</ul>'%(bad,))
d = warning_dialog(self.window, 'No suitable formats',
'Could not upload the following books to the device, as no suitable formats were found:<br><ul>%s</ul>'%(bad,))
d.exec_()