From 91cfafdeb9a66a96e60f21112fe212c6adb59516 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 13 Jul 2009 21:51:34 -0600 Subject: [PATCH] Make path length restriction global for all OSes and port to 505 driver --- src/calibre/devices/prs505/driver.py | 25 +----- src/calibre/devices/usbms/driver.py | 120 ++++++++++++++------------- 2 files changed, 64 insertions(+), 81 deletions(-) diff --git a/src/calibre/devices/prs505/driver.py b/src/calibre/devices/prs505/driver.py index 3e46e46cde..9c9aaca97e 100644 --- a/src/calibre/devices/prs505/driver.py +++ b/src/calibre/devices/prs505/driver.py @@ -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) diff --git a/src/calibre/devices/usbms/driver.py b/src/calibre/devices/usbms/driver.py index 5c5a7fb1da..065c144662 100644 --- a/src/calibre/devices/usbms/driver.py +++ b/src/calibre/devices/usbms/driver.py @@ -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,63 +127,8 @@ class USBMS(CLI, Device): metadata = iter(metadata) for i, infile in enumerate(files): - newpath = path - resizable = [] - - 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) + mdata, fname = metadata.next(), names.next() + filepath = self.create_upload_path(path, mdata, fname) paths.append(filepath) @@ -205,6 +149,66 @@ class USBMS(CLI, Device): 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): for i, location in enumerate(locations): self.report_progress((i+1) / float(len(locations)), _('Adding books to device metadata listing...'))