diff --git a/src/calibre/gui2/dialogs/user_profiles.py b/src/calibre/gui2/dialogs/user_profiles.py index 45ed14427a..5c49f1cfbd 100644 --- a/src/calibre/gui2/dialogs/user_profiles.py +++ b/src/calibre/gui2/dialogs/user_profiles.py @@ -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 diff --git a/src/calibre/web/feeds/recipes/collection.py b/src/calibre/web/feeds/recipes/collection.py index 5cfbb7899c..5092e3ee4c 100644 --- a/src/calibre/web/feeds/recipes/collection.py +++ b/src/calibre/web/feeds/recipes/collection.py @@ -120,23 +120,29 @@ 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 - id_ = str(int(id_)) - existing = custom_recipes.get(id_, None) + bdir = os.path.dirname(custom_recipes.file_path) + for id_, title, script in script_ids: - if existing is None: - fname = custom_recipe_filename(id_, title) - else: - fname = existing[1] - if isinstance(script, unicode): - script = script.encode('utf-8') + id_ = str(int(id_)) + existing = custom_recipes.get(id_, None) - 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: - f.write(script) + custom_recipes[id_] = (title, fname) + + with open(os.path.join(bdir, fname), 'wb') as f: + f.write(script) def add_custom_recipe(title, script): @@ -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): diff --git a/src/calibre/web/feeds/recipes/model.py b/src/calibre/web/feeds/recipes/model.py index 327e04e09d..dc10def57b 100644 --- a/src/calibre/web/feeds/recipes/model.py +++ b/src/calibre/web/feeds/recipes/model.py @@ -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)