This commit is contained in:
Kovid Goyal 2007-11-27 17:42:03 +00:00
parent 1fcdada213
commit fea6571d32
2 changed files with 14 additions and 5 deletions

View File

@ -18,7 +18,7 @@ __docformat__ = "epytext"
__author__ = "Kovid Goyal <kovid@kovidgoyal.net>" __author__ = "Kovid Goyal <kovid@kovidgoyal.net>"
__appname__ = 'libprs500' __appname__ = 'libprs500'
import sys, os, logging, mechanize, locale, cStringIO import sys, os, logging, mechanize, locale, cStringIO, re
from gettext import GNUTranslations from gettext import GNUTranslations
from math import floor from math import floor
@ -153,3 +153,11 @@ def get_font_families():
family = describe.shortName(font)[1].strip() family = describe.shortName(font)[1].strip()
zlist.append((family, ff)) zlist.append((family, ff))
return dict(zlist) return dict(zlist)
def sanitize_file_name(name):
'''
Remove characters that are illegal in filenames from name.
Also remove path separators. ALl illegal characters are replaced by
underscores.
'''
return re.sub(r'[:\?\\\/]|^-', '_', name.strip())

View File

@ -19,6 +19,7 @@ import sqlite3 as sqlite
import datetime, re, os, cPickle, traceback import datetime, re, os, cPickle, traceback
from zlib import compress, decompress from zlib import compress, decompress
from libprs500 import sanitize_file_name
from libprs500.ebooks.metadata.meta import set_metadata from libprs500.ebooks.metadata.meta import set_metadata
from libprs500.ebooks.metadata import MetaInformation from libprs500.ebooks.metadata import MetaInformation
@ -1103,12 +1104,12 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
by_author[au] = [] by_author[au] = []
by_author[au].append(index) by_author[au].append(index)
for au in by_author.keys(): for au in by_author.keys():
apath = os.path.join(dir, au.replace(os.sep, '_').strip()) apath = os.path.join(dir, sanitize_file_name(au))
if not os.path.exists(apath): if not os.path.exists(apath):
os.mkdir(apath) os.mkdir(apath)
for idx in by_author[au]: for idx in by_author[au]:
title = re.sub(r'\s', ' ', self.title(idx)) title = re.sub(r'\s', ' ', self.title(idx))
tpath = os.path.join(apath, title.replace(os.sep, '_').strip()) tpath = os.path.join(apath, sanitize_file_name(title))
id = str(self.id(idx)) id = str(self.id(idx))
if not os.path.exists(tpath): if not os.path.exists(tpath):
os.mkdir(tpath) os.mkdir(tpath)
@ -1116,7 +1117,7 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
data = self.format(idx, fmt) data = self.format(idx, fmt)
name = au + ' - ' + title if byauthor else title + ' - ' + au name = au + ' - ' + title if byauthor else title + ' - ' + au
fname = name +'_'+id+'.'+fmt.lower() fname = name +'_'+id+'.'+fmt.lower()
f = open(os.path.join(tpath, fname.replace(os.sep, '_').strip()), 'w+b') f = open(os.path.join(tpath, sanitize_file_name(fname)), 'w+b')
f.write(data) f.write(data)
f.flush() f.flush()
aum = self.authors(idx) aum = self.authors(idx)