Make path length restriction global for all OSes and port to 505 driver

This commit is contained in:
Kovid Goyal 2009-07-13 21:51:34 -06:00
parent 284c3b1797
commit 91cfafdeb9
2 changed files with 64 additions and 81 deletions

View File

@ -7,7 +7,6 @@ Device driver for the SONY PRS-505
import os, re, time import os, re, time
from itertools import cycle from itertools import cycle
from calibre import sanitize_file_name as sanitize
from calibre.devices.usbms.cli import CLI from calibre.devices.usbms.cli import CLI
from calibre.devices.usbms.device import Device from calibre.devices.usbms.device import Device
from calibre.devices.errors import DeviceError, FreeSpaceError from calibre.devices.errors import DeviceError, FreeSpaceError
@ -145,29 +144,9 @@ class PRS505(CLI, Device):
infile, close = open(infile, 'rb'), True infile, close = open(infile, 'rb'), True
infile.seek(0) infile.seek(0)
newpath = path mdata, fname = metadata.next(), names.next()
mdata = metadata.next() filepath = self.create_upload_path(path, mdata, fname)
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, sanitize(mdata.get('title', '')))
newpath = os.path.join(newpath, sanitize(mdata.get('timestamp', '')))
elif tag.startswith('/'):
newpath = path
newpath += tag
newpath = os.path.normpath(newpath)
break
if newpath == path:
newpath = os.path.join(newpath, sanitize(mdata.get('authors', _('Unknown'))))
newpath = os.path.join(newpath, sanitize(mdata.get('title', _('Unknown'))))
if not os.path.exists(newpath):
os.makedirs(newpath)
filepath = os.path.join(newpath, sanitize(names.next()))
paths.append(filepath) paths.append(filepath)
self.put_file(infile, paths[-1], replace_file=True) self.put_file(infile, paths[-1], replace_file=True)

View File

@ -14,7 +14,6 @@ from math import ceil
from itertools import cycle from itertools import cycle
from calibre import sanitize_file_name as sanitize from calibre import sanitize_file_name as sanitize
from calibre.constants import iswindows
from calibre.ebooks.metadata import authors_to_string from calibre.ebooks.metadata import authors_to_string
from calibre.devices.usbms.cli import CLI from calibre.devices.usbms.cli import CLI
from calibre.devices.usbms.device import Device from calibre.devices.usbms.device import Device
@ -128,63 +127,8 @@ class USBMS(CLI, Device):
metadata = iter(metadata) metadata = iter(metadata)
for i, infile in enumerate(files): for i, infile in enumerate(files):
newpath = path mdata, fname = metadata.next(), names.next()
resizable = [] filepath = self.create_upload_path(path, mdata, fname)
if self.SUPPORTS_SUB_DIRS:
mdata = metadata.next()
if 'tags' in mdata.keys():
for tag in mdata['tags']:
if tag.startswith(_('News')):
newpath = os.path.join(newpath, 'news')
c = sanitize(mdata.get('title', ''))
if c:
newpath = os.path.join(newpath, c)
resizable.append(c)
c = sanitize(mdata.get('timestamp', ''))
if c:
newpath = os.path.join(newpath, c)
resizable.append(c)
break
elif tag.startswith('/'):
for c in tag.split('/'):
c = sanitize(c)
if not c: continue
newpath = os.path.join(newpath, c)
resizable.append(c)
break
if newpath == path:
c = sanitize(mdata.get('authors', _('Unknown')))
if c:
newpath = os.path.join(newpath, c)
resizable.append(c)
c = sanitize(mdata.get('title', _('Unknown')))
if c:
newpath = os.path.join(newpath, c)
resizable.append(c)
newpath = os.path.abspath(newpath)
fname = sanitize(names.next())
resizable.append(fname)
filepath = os.path.join(newpath, fname)
if iswindows and len(filepath) > 250:
extra = len(filepath) - 250
delta = int(ceil(extra/float(len(resizable))))
for x in resizable:
if delta > len(x):
r = ''
else:
r = x[:-delta]
filepath = filepath.replace(os.sep+x+os.sep, os.sep+r+os.sep)
filepath = filepath.replace(os.sep+os.sep, os.sep)
newpath = os.path.dirname(filepath)
if not os.path.exists(newpath):
os.makedirs(newpath)
paths.append(filepath) paths.append(filepath)
@ -205,6 +149,66 @@ class USBMS(CLI, Device):
return zip(paths, cycle([on_card])) return zip(paths, cycle([on_card]))
def create_upload_path(self, path, mdata, fname):
resizable = []
newpath = path
if self.SUPPORTS_SUB_DIRS:
if 'tags' in mdata.keys():
for tag in mdata['tags']:
if tag.startswith(_('News')):
newpath = os.path.join(newpath, 'news')
c = sanitize(mdata.get('title', ''))
if c:
newpath = os.path.join(newpath, c)
resizable.append(c)
c = sanitize(mdata.get('timestamp', ''))
if c:
newpath = os.path.join(newpath, c)
resizable.append(c)
break
elif tag.startswith('/'):
for c in tag.split('/'):
c = sanitize(c)
if not c: continue
newpath = os.path.join(newpath, c)
resizable.append(c)
break
if newpath == path:
c = sanitize(mdata.get('authors', _('Unknown')))
if c:
newpath = os.path.join(newpath, c)
resizable.append(c)
c = sanitize(mdata.get('title', _('Unknown')))
if c:
newpath = os.path.join(newpath, c)
resizable.append(c)
newpath = os.path.abspath(newpath)
fname = sanitize(fname)
resizable.append(fname)
filepath = os.path.join(newpath, fname)
if len(filepath) > 250:
extra = len(filepath) - 250
delta = int(ceil(extra/float(len(resizable))))
for x in resizable:
if delta > len(x):
r = ''
else:
r = x[:-delta]
filepath = filepath.replace(os.sep+x+os.sep, os.sep+r+os.sep)
filepath = filepath.replace(os.sep+os.sep, os.sep)
newpath = os.path.dirname(filepath)
if not os.path.exists(newpath):
os.makedirs(newpath)
return filepath
def add_books_to_metadata(self, locations, metadata, booklists): def add_books_to_metadata(self, locations, metadata, booklists):
for i, location in enumerate(locations): for i, location in enumerate(locations):
self.report_progress((i+1) / float(len(locations)), _('Adding books to device metadata listing...')) self.report_progress((i+1) / float(len(locations)), _('Adding books to device metadata listing...'))