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

View File

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

View File

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

View File

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