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.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):
self.recipe_model.add_custom_recipe(title, script)
self.reset()
@ -366,7 +380,8 @@ class %(classname)s(%(base_class)s):
skip_dialog_name='replace_recipes'
opml = OPML(self.oldest_article.value(), self.max_articles.value());
scriptmap = {}
add_recipes_map = {}
replace_recipes_map = {}
for opml_file in opml_files:
opml.load(opml_file)
outlines = opml.parse()
@ -394,12 +409,14 @@ class %(classname)s(%(base_class)s):
skip_dialog_name=skip_dialog_name,
skip_dialog_msg=_('Show dialog again?')
):
self._model.replace_by_title(title, profile)
replace_recipes_map.update({ title: profile })
else:
scriptmap.update({ title: profile })
add_recipes_map.update({ title: profile })
nr+=1
if scriptmap:
self.model.add_many(scriptmap)
if add_recipes_map:
self.model.add_many(add_recipes_map)
if replace_recipes_map:
self.model.replace_many_by_title(replace_recipes_map)
self.clear()
# reset the question_dialog

View File

@ -120,11 +120,17 @@ def get_custom_recipe_collection(*args):
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, \
custom_recipe_filename
bdir = os.path.dirname(custom_recipes.file_path)
for id_, title, script in script_ids:
id_ = str(int(id_))
existing = custom_recipes.get(id_, None)
bdir = os.path.dirname(custom_recipes.file_path)
if existing is None:
fname = custom_recipe_filename(id_, title)
@ -149,10 +155,10 @@ def add_custom_recipes(script_map):
keys = tuple(map(int, custom_recipes.iterkeys()))
if keys:
id_ = max(keys)+1
bdir = os.path.dirname(custom_recipes.file_path)
with custom_recipes:
for title, script in script_map.iteritems():
fid = str(id_)
bdir = os.path.dirname(custom_recipes.file_path)
fname = custom_recipe_filename(fid, title)
if isinstance(script, unicode):

View File

@ -17,8 +17,8 @@ from calibre.utils.localization import get_language
from calibre.web.feeds.recipes.collection import \
get_builtin_recipe_collection, get_custom_recipe_collection, \
SchedulerConfig, download_builtin_recipe, update_custom_recipe, \
add_custom_recipe, add_custom_recipes, remove_custom_recipe, \
get_custom_recipe, get_builtin_recipe
update_custom_recipes, add_custom_recipe, add_custom_recipes, \
remove_custom_recipe, get_custom_recipe, get_builtin_recipe
from calibre.utils.search_query_parser import ParseException
class NewsTreeItem(object):
@ -171,8 +171,19 @@ class RecipeModel(QAbstractItemModel, SearchQueryParser):
update_custom_recipe(id_, title, script)
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):
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):
add_custom_recipes(scriptmap)