When bulk importing automatically place files into most likely folders

This commit is contained in:
Kovid Goyal 2013-12-24 11:06:19 +05:30
parent 0616422ee3
commit a88812a63c
2 changed files with 47 additions and 25 deletions

View File

@ -7,8 +7,9 @@ __license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
import codecs, shutil
import codecs, shutil, os
from urlparse import urlparse
from collections import Counter, defaultdict
from calibre import sanitize_file_name_unicode
from calibre.ebooks.chardet import strip_encoding_declarations
@ -145,3 +146,25 @@ def replace_file(container, name, path, basename, force_mt=None):
with container.open(nname, 'wb') as dest:
shutil.copyfileobj(src, dest)
def get_recommended_folders(container, names):
' Return the folders that are recommended for the given filenames '
from calibre.ebooks.oeb.polish.container import guess_type, OEB_FONTS
from calibre.ebooks.oeb.base import OEB_DOCS, OEB_STYLES
counts = defaultdict(Counter)
def mt_to_category(mt):
if mt in OEB_DOCS:
category = 'text'
elif mt in OEB_STYLES:
category = 'style'
elif mt in OEB_FONTS:
category = 'font'
else:
category = mt.partition('/')[0]
return category
for name, mt in container.mime_map.iteritems():
folder = name.rpartition('/')[0] if '/' in name else ''
counts[mt_to_category(mt)][folder] += 1
recommendations = {category:counter.most_common(1)[0][0] for category, counter in counts.iteritems()}
return {n:recommendations.get(mt_to_category(guess_type(os.path.basename(n))), '') for n in names}

View File

@ -21,7 +21,7 @@ from calibre.ebooks.oeb.polish.main import SUPPORTED, tweak_polish
from calibre.ebooks.oeb.polish.container import get_container as _gc, clone_container, guess_type, OEB_FONTS
from calibre.ebooks.oeb.polish.cover import mark_as_cover, mark_as_titlepage
from calibre.ebooks.oeb.polish.pretty import fix_all_html, pretty_all
from calibre.ebooks.oeb.polish.replace import rename_files, replace_file
from calibre.ebooks.oeb.polish.replace import rename_files, replace_file, get_recommended_folders
from calibre.ebooks.oeb.polish.split import split, merge, AbortError
from calibre.ebooks.oeb.polish.toc import remove_names_from_toc, find_existing_toc
from calibre.ebooks.oeb.polish.utils import link_stylesheets
@ -34,7 +34,7 @@ from calibre.gui2.tweak_book.save import SaveManager, save_container
from calibre.gui2.tweak_book.preview import parse_worker, font_cache
from calibre.gui2.tweak_book.toc import TOCEditor
from calibre.gui2.tweak_book.editor import editor_from_syntax, syntax_from_mime
from calibre.gui2.tweak_book.editor.insert_resource import get_resource_data, NewBook, ChooseFolder
from calibre.gui2.tweak_book.editor.insert_resource import get_resource_data, NewBook
from calibre.gui2.tweak_book.preferences import Preferences
def get_container(*args, **kwargs):
@ -328,28 +328,27 @@ class Boss(QObject):
files = choose_files(self.gui, 'tweak-book-bulk-import-files', _('Choose files'))
if files:
d = ChooseFolder(parent=self.gui)
if d.exec_() == d.Accepted:
base = d.chosen_folder
files = {x:'/'.join((base, os.path.basename(x))) for x in files}
self.commit_dirty_opf()
self.add_savepoint(_('Add files'))
c = current_container()
for path, name in files.iteritems():
i = 0
while c.exists(name):
i += 1
name, ext = name.rpartition('.')[0::2]
name = '%s_%d.%s' % (name, i, ext)
try:
with open(path, 'rb') as f:
c.add_file(name, f.read())
except:
self.rewind_savepoint()
raise
self.gui.file_list.build(c)
if c.opf_name in editors:
editors[c.opf_name].replace_data(c.raw_data(c.opf_name))
folder_map = get_recommended_folders(current_container(), files)
files = {x:('/'.join((folder, os.path.basename(x))) if folder else os.path.basename(x))
for x, folder in folder_map.iteritems()}
self.commit_dirty_opf()
self.add_savepoint(_('Add files'))
c = current_container()
for path, name in files.iteritems():
i = 0
while c.exists(name):
i += 1
name, ext = name.rpartition('.')[0::2]
name = '%s_%d.%s' % (name, i, ext)
try:
with open(path, 'rb') as f:
c.add_file(name, f.read())
except:
self.rewind_savepoint()
raise
self.gui.file_list.build(c)
if c.opf_name in editors:
editors[c.opf_name].replace_data(c.raw_data(c.opf_name))
def edit_toc(self):
self.commit_all_editors_to_container()