When replacing multiple recipes, get_custom_recipe_collection is now

only called once.
This commit is contained in:
ingkebil 2014-04-04 22:41:55 +02:00
parent 05fb53ff42
commit b069a6b7d4
3 changed files with 54 additions and 20 deletions

View File

@ -60,6 +60,20 @@ class CustomRecipeModel(QAbstractListModel):
self.recipe_model.update_custom_recipe(urn, title, script) self.recipe_model.update_custom_recipe(urn, title, script)
self.reset() self.reset()
def replace_many_by_title(self, scriptmap):
script_urn_map = {}
for title, script in scriptmap.iteritems():
urn = None
for x in self.recipe_model.custom_recipe_collection:
if x.get('title', False) == title:
urn = x.get('id')
if urn is not None:
script_urn_map.update({ urn: (title, script) })
if script_urn_map:
self.recipe_model.update_custom_recipes(script_urn_map)
self.reset()
def add(self, title, script): def add(self, title, script):
self.recipe_model.add_custom_recipe(title, script) self.recipe_model.add_custom_recipe(title, script)
self.reset() self.reset()
@ -366,7 +380,8 @@ class %(classname)s(%(base_class)s):
skip_dialog_name='replace_recipes' skip_dialog_name='replace_recipes'
opml = OPML(self.oldest_article.value(), self.max_articles.value()); opml = OPML(self.oldest_article.value(), self.max_articles.value());
scriptmap = {} add_recipes_map = {}
replace_recipes_map = {}
for opml_file in opml_files: for opml_file in opml_files:
opml.load(opml_file) opml.load(opml_file)
outlines = opml.parse() outlines = opml.parse()
@ -394,12 +409,14 @@ class %(classname)s(%(base_class)s):
skip_dialog_name=skip_dialog_name, skip_dialog_name=skip_dialog_name,
skip_dialog_msg=_('Show dialog again?') skip_dialog_msg=_('Show dialog again?')
): ):
self._model.replace_by_title(title, profile) replace_recipes_map.update({ title: profile })
else: else:
scriptmap.update({ title: profile }) add_recipes_map.update({ title: profile })
nr+=1 nr+=1
if scriptmap: if add_recipes_map:
self.model.add_many(scriptmap) self.model.add_many(add_recipes_map)
if replace_recipes_map:
self.model.replace_many_by_title(replace_recipes_map)
self.clear() self.clear()
# reset the question_dialog # reset the question_dialog

View File

@ -120,23 +120,29 @@ def get_custom_recipe_collection(*args):
def update_custom_recipe(id_, title, script): def update_custom_recipe(id_, title, script):
update_custom_recipes( [(id_, title, script)] )
def update_custom_recipes(script_ids):
from calibre.web.feeds.recipes import custom_recipes, \ from calibre.web.feeds.recipes import custom_recipes, \
custom_recipe_filename custom_recipe_filename
id_ = str(int(id_))
existing = custom_recipes.get(id_, None)
bdir = os.path.dirname(custom_recipes.file_path) bdir = os.path.dirname(custom_recipes.file_path)
for id_, title, script in script_ids:
if existing is None: id_ = str(int(id_))
fname = custom_recipe_filename(id_, title) existing = custom_recipes.get(id_, None)
else:
fname = existing[1]
if isinstance(script, unicode):
script = script.encode('utf-8')
custom_recipes[id_] = (title, fname) if existing is None:
fname = custom_recipe_filename(id_, title)
else:
fname = existing[1]
if isinstance(script, unicode):
script = script.encode('utf-8')
with open(os.path.join(bdir, fname), 'wb') as f: custom_recipes[id_] = (title, fname)
f.write(script)
with open(os.path.join(bdir, fname), 'wb') as f:
f.write(script)
def add_custom_recipe(title, script): def add_custom_recipe(title, script):
@ -149,10 +155,10 @@ def add_custom_recipes(script_map):
keys = tuple(map(int, custom_recipes.iterkeys())) keys = tuple(map(int, custom_recipes.iterkeys()))
if keys: if keys:
id_ = max(keys)+1 id_ = max(keys)+1
bdir = os.path.dirname(custom_recipes.file_path)
with custom_recipes: with custom_recipes:
for title, script in script_map.iteritems(): for title, script in script_map.iteritems():
fid = str(id_) fid = str(id_)
bdir = os.path.dirname(custom_recipes.file_path)
fname = custom_recipe_filename(fid, title) fname = custom_recipe_filename(fid, title)
if isinstance(script, unicode): if isinstance(script, unicode):

View File

@ -17,8 +17,8 @@ from calibre.utils.localization import get_language
from calibre.web.feeds.recipes.collection import \ from calibre.web.feeds.recipes.collection import \
get_builtin_recipe_collection, get_custom_recipe_collection, \ get_builtin_recipe_collection, get_custom_recipe_collection, \
SchedulerConfig, download_builtin_recipe, update_custom_recipe, \ SchedulerConfig, download_builtin_recipe, update_custom_recipe, \
add_custom_recipe, add_custom_recipes, remove_custom_recipe, \ update_custom_recipes, add_custom_recipe, add_custom_recipes, \
get_custom_recipe, get_builtin_recipe remove_custom_recipe, get_custom_recipe, get_builtin_recipe
from calibre.utils.search_query_parser import ParseException from calibre.utils.search_query_parser import ParseException
class NewsTreeItem(object): class NewsTreeItem(object):
@ -171,8 +171,19 @@ class RecipeModel(QAbstractItemModel, SearchQueryParser):
update_custom_recipe(id_, title, script) update_custom_recipe(id_, title, script)
self.custom_recipe_collection = get_custom_recipe_collection() self.custom_recipe_collection = get_custom_recipe_collection()
def update_custom_recipes(self, script_urn_map):
script_ids = []
for urn, title_script in script_urn_map.iteritems():
id_ = int(urn[len('custom:'):])
(title, script) = title_script
script_ids.append((id_, title, script))
update_custom_recipes(script_ids)
self.custom_recipe_collection = get_custom_recipe_collection()
def add_custom_recipe(self, title, script): def add_custom_recipe(self, title, script):
self.add_custom_recipes({ title: script }) self.add_custom_recipe(title, script)
self.custom_recipe_collection = get_custom_recipe_collection()
def add_custom_recipes(self, scriptmap): def add_custom_recipes(self, scriptmap):
add_custom_recipes(scriptmap) add_custom_recipes(scriptmap)