From bb97bd6cabe025c7ba0cd8d6feb19e926e33412d Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Thu, 30 Sep 2010 09:45:18 +0100 Subject: [PATCH] 1) fix problem in rtf metadata writer -- categories isn't a standard option. 2) fix book/base.py to always return a default. That is what python is supposed to do. 3) add ability to code 'key in mi' (the __iter__ function) (base.py) 4) add has_key() to base.py 5) have rename refuse to allow & characters in author names --- src/calibre/ebooks/metadata/book/base.py | 16 ++++++++++------ src/calibre/ebooks/metadata/rtf.py | 4 ++-- src/calibre/gui2/dialogs/edit_authors_dialog.py | 7 +++++++ src/calibre/gui2/tag_view.py | 7 ++++++- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/calibre/ebooks/metadata/book/base.py b/src/calibre/ebooks/metadata/book/base.py index be597b2327..82da530535 100644 --- a/src/calibre/ebooks/metadata/book/base.py +++ b/src/calibre/ebooks/metadata/book/base.py @@ -133,6 +133,12 @@ class Metadata(object): # Don't abuse this privilege self.__dict__[field] = val + def __iter__(self): + return object.__getattribute__(self, '_data').iterkeys() + + def has_key(self, key): + return key in object.__getattribute__(self, '_data') + def deepcopy(self): m = Metadata(None) m.__dict__ = copy.deepcopy(self.__dict__) @@ -140,12 +146,10 @@ class Metadata(object): return m def get(self, field, default=None): - if default is not None: - try: - return self.__getattribute__(field) - except AttributeError: - return default - return self.__getattribute__(field) + try: + return self.__getattribute__(field) + except AttributeError: + return default def get_extra(self, field): _data = object.__getattribute__(self, '_data') diff --git a/src/calibre/ebooks/metadata/rtf.py b/src/calibre/ebooks/metadata/rtf.py index d116ec30fb..ad41125575 100644 --- a/src/calibre/ebooks/metadata/rtf.py +++ b/src/calibre/ebooks/metadata/rtf.py @@ -125,7 +125,7 @@ def create_metadata(stream, options): au = u', '.join(au) author = au.encode('ascii', 'ignore') md += r'{\author %s}'%(author,) - if options.category: + if options.get('category', None): category = options.category.encode('ascii', 'ignore') md += r'{\category %s}'%(category,) comp = options.comment if hasattr(options, 'comment') else options.comments @@ -180,7 +180,7 @@ def set_metadata(stream, options): src = pat.sub(r'{\\author ' + author + r'}', src) else: src = add_metadata_item(src, 'author', author) - category = options.category + category = options.get('category', None) if category != None: category = category.encode('ascii', 'replace') pat = re.compile(base_pat.replace('name', 'category'), re.DOTALL) diff --git a/src/calibre/gui2/dialogs/edit_authors_dialog.py b/src/calibre/gui2/dialogs/edit_authors_dialog.py index 7fe50181a3..2fdb8e28cc 100644 --- a/src/calibre/gui2/dialogs/edit_authors_dialog.py +++ b/src/calibre/gui2/dialogs/edit_authors_dialog.py @@ -6,6 +6,7 @@ __license__ = 'GPL v3' from PyQt4.Qt import Qt, QDialog, QTableWidgetItem, QAbstractItemView from calibre.ebooks.metadata import author_to_author_sort +from calibre.gui2 import error_dialog from calibre.gui2.dialogs.edit_authors_dialog_ui import Ui_EditAuthorsDialog class tableItem(QTableWidgetItem): @@ -109,6 +110,12 @@ class EditAuthorsDialog(QDialog, Ui_EditAuthorsDialog): if col == 0: item = self.table.item(row, 0) aut = unicode(item.text()).strip() + amper = aut.find('&') + if amper >= 0: + error_dialog(self.parent(), _('Invalid author name'), + _('Author names cannot contain & characters.')).exec_() + aut = aut.replace('&', '%') + self.table.item(row, 0).setText(aut) c = self.table.item(row, 1) c.setText(author_to_author_sort(aut)) item = c diff --git a/src/calibre/gui2/tag_view.py b/src/calibre/gui2/tag_view.py index 6c50a71b92..68b7645d36 100644 --- a/src/calibre/gui2/tag_view.py +++ b/src/calibre/gui2/tag_view.py @@ -505,7 +505,12 @@ class TagsModel(QAbstractItemModel): # {{{ key = item.parent.category_key # make certain we know about the item's category if key not in self.db.field_metadata: - return + return False + if key == 'authors': + if val.find('&') >= 0: + error_dialog(self.tags_view, _('Invalid author name'), + _('Author names cannot contain & characters.')).exec_() + return False if key == 'search': if val in saved_searches().names(): error_dialog(self.tags_view, _('Duplicate search name'),