From 3d3dcb39159842c7a9335f7678912477dec83864 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Tue, 1 Feb 2011 16:42:26 +0000 Subject: [PATCH 1/2] Fix for #7883: 'title_sort' field in save to disk template empty in v 7.33 --- src/calibre/ebooks/metadata/opf2.py | 30 ++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/calibre/ebooks/metadata/opf2.py b/src/calibre/ebooks/metadata/opf2.py index 62d57f2251..456bfb0ea6 100644 --- a/src/calibre/ebooks/metadata/opf2.py +++ b/src/calibre/ebooks/metadata/opf2.py @@ -780,22 +780,30 @@ class OPF(object): # {{{ def title_sort(self): def fget(self): - matches = self.title_path(self.metadata) + matches = self.root.xpath('//*[name() = "meta" and starts-with(@name,' + '"calibre:title_sort") and @content]') if matches: - for match in matches: - ans = match.get('{%s}file-as'%self.NAMESPACES['opf'], None) - if not ans: - ans = match.get('file-as', None) - if ans: - return ans + for elem in matches: + return self.get_text(elem) + return None def fset(self, val): + print 'here' + matches = self.root.xpath('//*[name() = "meta" and starts-with(@name,' + '"calibre:title_sort") and @content]') + if matches: + for elem in matches: + elem.getparent().remove(elem) matches = self.title_path(self.metadata) if matches: - for key in matches[0].attrib: - if key.endswith('file-as'): - matches[0].attrib.pop(key) - matches[0].set('{%s}file-as'%self.NAMESPACES['opf'], unicode(val)) + for elem in matches: + parent = elem.getparent() + attrib = {} + attrib['name'] = 'calibre:title_sort' + attrib['content'] = val + e = elem.makeelement('meta', attrib=attrib) + e.tail = '\n'+(' '*8) + parent.append(elem) return property(fget=fget, fset=fset) From 9acd6d6189cdb2f008ea519fb5b0709db93c4601 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Tue, 1 Feb 2011 17:28:55 +0000 Subject: [PATCH 2/2] Put file-as back into OPF title_sort.get as a fall back --- src/calibre/ebooks/metadata/opf2.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/metadata/opf2.py b/src/calibre/ebooks/metadata/opf2.py index 456bfb0ea6..a721c5cb2f 100644 --- a/src/calibre/ebooks/metadata/opf2.py +++ b/src/calibre/ebooks/metadata/opf2.py @@ -780,15 +780,24 @@ class OPF(object): # {{{ def title_sort(self): def fget(self): + #first try the title_sort meta tag matches = self.root.xpath('//*[name() = "meta" and starts-with(@name,' '"calibre:title_sort") and @content]') if matches: for elem in matches: return self.get_text(elem) + # fallback to file-as + matches = self.title_path(self.metadata) + if matches: + for match in matches: + ans = match.get('{%s}file-as'%self.NAMESPACES['opf'], None) + if not ans: + ans = match.get('file-as', None) + if ans: + return ans return None def fset(self, val): - print 'here' matches = self.root.xpath('//*[name() = "meta" and starts-with(@name,' '"calibre:title_sort") and @content]') if matches: