Make book upload code for PRS505 more robust

This commit is contained in:
Kovid Goyal 2008-05-30 10:02:44 -07:00
parent c1f9987be8
commit 968064b860

View File

@ -330,7 +330,6 @@ class PRS505(Device):
def put_file(self, infile, path, replace_file=False, end_session=True): def put_file(self, infile, path, replace_file=False, end_session=True):
path = self.munge_path(path) path = self.munge_path(path)
path = path.replace('/', os.sep)
if os.path.isdir(path): if os.path.isdir(path):
path = os.path.join(path, infile.name) path = os.path.join(path, infile.name)
if not replace_file and os.path.exists(path): if not replace_file and os.path.exists(path):
@ -353,7 +352,7 @@ class PRS505(Device):
def upload_books(self, files, names, on_card=False, end_session=True): def upload_books(self, files, names, on_card=False, end_session=True):
path = os.path.join(self._card_prefix, self.CARD_PATH_PREFIX) if on_card \ path = os.path.join(self._card_prefix, self.CARD_PATH_PREFIX) if on_card \
else os.path.join(self._main_prefix, 'database/media/books') else os.path.join(self._main_prefix, 'database', 'media', 'books')
infiles = [file if hasattr(file, 'read') else open(file, 'rb') for file in files] infiles = [file if hasattr(file, 'read') else open(file, 'rb') for file in files]
for f in infiles: f.seek(0, 2) for f in infiles: f.seek(0, 2)
sizes = [f.tell() for f in infiles] sizes = [f.tell() for f in infiles]
@ -375,8 +374,8 @@ class PRS505(Device):
infile.seek(0) infile.seek(0)
name = names.next() name = names.next()
paths.append(os.path.join(path, name)) paths.append(os.path.join(path, name))
if on_card and not os.path.exists(os.path.dirname(paths[-1])): if not os.path.exists(os.path.dirname(paths[-1])):
os.mkdir(os.path.dirname(paths[-1])) os.makedirs(os.path.dirname(paths[-1]))
self.put_file(infile, paths[-1], replace_file=True) self.put_file(infile, paths[-1], replace_file=True)
ctimes.append(os.path.getctime(paths[-1])) ctimes.append(os.path.getctime(paths[-1]))
return zip(paths, sizes, ctimes, cycle([on_card])) return zip(paths, sizes, ctimes, cycle([on_card]))