mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Make path length restriction global for all OSes and port to 505 driver
This commit is contained in:
parent
284c3b1797
commit
91cfafdeb9
@ -7,7 +7,6 @@ Device driver for the SONY PRS-505
|
||||
import os, re, time
|
||||
from itertools import cycle
|
||||
|
||||
from calibre import sanitize_file_name as sanitize
|
||||
from calibre.devices.usbms.cli import CLI
|
||||
from calibre.devices.usbms.device import Device
|
||||
from calibre.devices.errors import DeviceError, FreeSpaceError
|
||||
@ -145,29 +144,9 @@ class PRS505(CLI, Device):
|
||||
infile, close = open(infile, 'rb'), True
|
||||
infile.seek(0)
|
||||
|
||||
newpath = path
|
||||
mdata = metadata.next()
|
||||
mdata, fname = metadata.next(), names.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)
|
||||
|
||||
self.put_file(infile, paths[-1], replace_file=True)
|
||||
|
@ -14,7 +14,6 @@ from math import ceil
|
||||
from itertools import cycle
|
||||
|
||||
from calibre import sanitize_file_name as sanitize
|
||||
from calibre.constants import iswindows
|
||||
from calibre.ebooks.metadata import authors_to_string
|
||||
from calibre.devices.usbms.cli import CLI
|
||||
from calibre.devices.usbms.device import Device
|
||||
@ -128,11 +127,32 @@ class USBMS(CLI, Device):
|
||||
metadata = iter(metadata)
|
||||
|
||||
for i, infile in enumerate(files):
|
||||
newpath = path
|
||||
resizable = []
|
||||
mdata, fname = metadata.next(), names.next()
|
||||
filepath = self.create_upload_path(path, mdata, fname)
|
||||
|
||||
paths.append(filepath)
|
||||
|
||||
if hasattr(infile, 'read'):
|
||||
infile.seek(0)
|
||||
|
||||
dest = open(filepath, 'wb')
|
||||
shutil.copyfileobj(infile, dest, 10*1024*1024)
|
||||
|
||||
dest.flush()
|
||||
dest.close()
|
||||
else:
|
||||
shutil.copy2(infile, filepath)
|
||||
|
||||
self.report_progress((i+1) / float(len(files)), _('Transferring books to device...'))
|
||||
|
||||
self.report_progress(1.0, _('Transferring books to device...'))
|
||||
|
||||
return zip(paths, cycle([on_card]))
|
||||
|
||||
def create_upload_path(self, path, mdata, fname):
|
||||
resizable = []
|
||||
newpath = path
|
||||
if self.SUPPORTS_SUB_DIRS:
|
||||
mdata = metadata.next()
|
||||
|
||||
if 'tags' in mdata.keys():
|
||||
for tag in mdata['tags']:
|
||||
@ -166,11 +186,11 @@ class USBMS(CLI, Device):
|
||||
resizable.append(c)
|
||||
|
||||
newpath = os.path.abspath(newpath)
|
||||
fname = sanitize(names.next())
|
||||
fname = sanitize(fname)
|
||||
resizable.append(fname)
|
||||
filepath = os.path.join(newpath, fname)
|
||||
|
||||
if iswindows and len(filepath) > 250:
|
||||
if len(filepath) > 250:
|
||||
extra = len(filepath) - 250
|
||||
delta = int(ceil(extra/float(len(resizable))))
|
||||
for x in resizable:
|
||||
@ -186,24 +206,8 @@ class USBMS(CLI, Device):
|
||||
if not os.path.exists(newpath):
|
||||
os.makedirs(newpath)
|
||||
|
||||
paths.append(filepath)
|
||||
return filepath
|
||||
|
||||
if hasattr(infile, 'read'):
|
||||
infile.seek(0)
|
||||
|
||||
dest = open(filepath, 'wb')
|
||||
shutil.copyfileobj(infile, dest, 10*1024*1024)
|
||||
|
||||
dest.flush()
|
||||
dest.close()
|
||||
else:
|
||||
shutil.copy2(infile, filepath)
|
||||
|
||||
self.report_progress((i+1) / float(len(files)), _('Transferring books to device...'))
|
||||
|
||||
self.report_progress(1.0, _('Transferring books to device...'))
|
||||
|
||||
return zip(paths, cycle([on_card]))
|
||||
|
||||
def add_books_to_metadata(self, locations, metadata, booklists):
|
||||
for i, location in enumerate(locations):
|
||||
|
Loading…
x
Reference in New Issue
Block a user