Create sub directories based on metadata when sending books to devices that support sub directories

This commit is contained in:
Kovid Goyal 2009-03-03 17:35:22 -08:00
commit 9883f60ca5
4 changed files with 33 additions and 12 deletions

View File

@ -59,7 +59,7 @@ class CYBOOKG3(USBMS):
if on_card and size > self.free_space()[2] - 1024*1024:
raise FreeSpaceError(_("There is insufficient free space on the storage card"))
if not on_card and size > self.free_space()[0] - 2*1024*1024:
if not on_card and size > self.free_space()[0] - 2*1024*1024:
raise FreeSpaceError(_("There is insufficient free space in main memory"))
paths = []
@ -69,19 +69,28 @@ class CYBOOKG3(USBMS):
for infile in files:
newpath = path
mdata = metadata.next()
if self.SUPPORTS_SUB_DIRS:
if 'tags' in mdata.keys():
for tag in mdata['tags']:
if tag.startswith('/'):
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', ''))
newpath = os.path.join(newpath, mdata.get('title', ''))
if not os.path.exists(newpath):
os.makedirs(newpath)
filepath = os.path.join(newpath, names.next())
filepath = os.path.join(newpath, names.next())
paths.append(filepath)
if hasattr(infile, 'read'):
@ -90,7 +99,7 @@ class CYBOOKG3(USBMS):
dest = open(filepath, 'wb')
shutil.copyfileobj(infile, dest, 10*1024*1024)
dest.flush()
dest.flush()
dest.close()
else:
shutil.copy2(infile, filepath)

View File

@ -37,7 +37,7 @@ class USBMS(Device):
SUPPORTS_SUB_DIRS = False
def __init__(self, key='-1', log_packets=False, report_progress=None):
Device.__init__(self, key=key, log_packets=log_packets,
Device.__init__(self, key=key, log_packets=log_packets,
report_progress=report_progress)
def get_device_information(self, end_session=True):
@ -103,10 +103,20 @@ class USBMS(Device):
if 'tags' in mdata.keys():
for tag in mdata['tags']:
if tag.startswith('/'):
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', ''))
break
elif tag.startswith('/'):
newpath = path
newpath += tag
newpath = os.path.normpath(newpath)
break
if newpath == path:
newpath = os.path.join(newpath, mdata.get('authors', ''))
newpath = os.path.join(newpath, mdata.get('title', ''))
if not os.path.exists(newpath):
os.makedirs(newpath)

View File

@ -67,6 +67,8 @@ def set_metadata(stream, mi):
stream.seek(0)
def get_cover(stream):
data = StringIO.StringIO()
try:
pdf = PdfFileReader(stream)
output = PdfFileWriter()
@ -88,12 +90,12 @@ def get_cover(stream):
img = Image.open('%s.jpg' % cover_path)
data = StringIO.StringIO()
img.save(data, 'JPEG')
return data.getvalue()
except:
import traceback
traceback.print_exc()
return data.getvalue()
def option_parser():
p = get_parser('pdf')

View File

@ -58,10 +58,10 @@ entry_points = {
'calibredb = calibre.library.cli:main',
'calibre-fontconfig = calibre.utils.fontconfig:main',
'calibre-parallel = calibre.parallel:main',
'calibre-customize = calibre.customize.ui:main',
'pdftrim = calibre.ebooks.pdf.pdftrim:main' ,
'calibre-customize = calibre.customize.ui:main',
'pdftrim = calibre.ebooks.pdf.pdftrim:main' ,
'any2pdf = calibre.ebooks.pdf.from_any:main',
],
],
'gui_scripts' : [
__appname__+' = calibre.gui2.main:main',
'lrfviewer = calibre.gui2.lrf_renderer.main:main',