From 654dfbdf2c9096c3efe295cc6f8a411fa1b6921d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 31 May 2009 16:26:50 -0700 Subject: [PATCH] Use os.path.relpath instead of custom implementation --- src/calibre/__init__.py | 34 +---------------------------- src/calibre/ebooks/metadata/opf2.py | 7 +++--- src/calibre/ebooks/mobi/writer.py | 13 ++++++----- src/calibre/gui2/main.py | 2 +- 4 files changed, 13 insertions(+), 43 deletions(-) diff --git a/src/calibre/__init__.py b/src/calibre/__init__.py index 79dc659f34..2bbf18d9e0 100644 --- a/src/calibre/__init__.py +++ b/src/calibre/__init__.py @@ -284,39 +284,7 @@ def launch(path_or_url): path_or_url = 'file:'+path_or_url QDesktopServices.openUrl(QUrl(path_or_url)) -def relpath(target, base=os.curdir): - """ - Return a relative path to the target from either the current dir or an optional base dir. - Base can be a directory specified either as absolute or relative to current dir. - """ - - #if not os.path.exists(target): - # raise OSError, 'Target does not exist: '+target - if target == base: - raise ValueError('target and base are both: %s'%target) - if not os.path.isdir(base): - raise OSError, 'Base is not a directory or does not exist: '+base - - base_list = (os.path.abspath(base)).split(os.sep) - target_list = (os.path.abspath(target)).split(os.sep) - - # On the windows platform the target may be on a completely different drive from the base. - if iswindows and base_list[0].upper() != target_list[0].upper(): - raise OSError, 'Target is on a different drive to base. Target: '+repr(target)+', base: '+repr(base) - - # Starting from the filepath root, work out how much of the filepath is - # shared by base and target. - for i in range(min(len(base_list), len(target_list))): - if base_list[i] != target_list[i]: break - else: - # If we broke out of the loop, i is pointing to the first differing path elements. - # If we didn't break out of the loop, i is pointing to identical path elements. - # Increment i so that in all cases it points to the first differing path elements. - i+=1 - - rel_list = [os.pardir] * (len(base_list)-i) + target_list[i:] - return os.path.join(*rel_list) - +relpath = os.path.relpath _spat = re.compile(r'^the\s+|^a\s+|^an\s+', re.IGNORECASE) def english_sort(x, y): ''' diff --git a/src/calibre/ebooks/metadata/opf2.py b/src/calibre/ebooks/metadata/opf2.py index 4d1af0ee75..f801dbf65c 100644 --- a/src/calibre/ebooks/metadata/opf2.py +++ b/src/calibre/ebooks/metadata/opf2.py @@ -15,7 +15,6 @@ from lxml import etree from dateutil import parser from calibre.ebooks.chardet import xml_to_unicode -from calibre import relpath from calibre.constants import __appname__, __version__ from calibre.ebooks.metadata.toc import TOC from calibre.ebooks.metadata import MetaInformation, string_to_authors @@ -86,8 +85,8 @@ class Resource(object): if self.path == basedir: return ''+frag try: - rpath = relpath(self.path, basedir) - except OSError: # On windows path and basedir could be on different drives + rpath = os.path.relpath(self.path, basedir) + except ValueError: # On windows path and basedir could be on different drives rpath = self.path if isinstance(rpath, unicode): rpath = rpath.encode('utf-8') @@ -560,7 +559,7 @@ class OPF(object): has_path = True break if not has_path: - href = relpath(path, self.base_dir).replace(os.sep, '/') + href = os.path.relpath(path, self.base_dir).replace(os.sep, '/') item = self.create_manifest_item(href, media_type) manifest = self.manifest_ppath(self.root)[0] manifest.append(item) diff --git a/src/calibre/ebooks/mobi/writer.py b/src/calibre/ebooks/mobi/writer.py index 7d63744e57..f63eaa0940 100644 --- a/src/calibre/ebooks/mobi/writer.py +++ b/src/calibre/ebooks/mobi/writer.py @@ -511,7 +511,7 @@ class MobiWriter(object): indxt, indxt_count, indices, last_name = \ self._generate_indxt(ctoc) if last_name is None: - self._oeb.log.warn('Input document has no TOC. No idex generated.') + self._oeb.log.warn('Input document has no TOC. No index generated.') return indx1 = StringIO() @@ -628,8 +628,8 @@ class MobiWriter(object): self._last_toc_entry = None ctoc = StringIO() - def add_node(node, cls): - t = node.title + def add_node(node, cls, title=None): + t = node.title if title is None else title if t and t.strip(): t = t.strip() if not isinstance(t, unicode): @@ -640,8 +640,10 @@ class MobiWriter(object): self._ctoc_name_map[node] = t ctoc.write(decint(len(t), DECINT_FORWARD)+t) + first = True for child in toc.iter(): - add_node(child, 'chapter') + add_node(child, 'chapter', title='Title Page' if first else None) + first = False return align_block(ctoc.getvalue()) @@ -842,7 +844,8 @@ class MobiWriter(object): if INDEXING: # Write unknown EXTH records as 0s for code, size in [(204,4), (205,4), (206,4), (207,4), (300,40)]: - exth.write(pack('>I', code)+'\0'*size) + exth.write(pack('>II', code, 8+size)+'\0'*size) + nrecs += 1 exth = exth.getvalue() trail = len(exth) % 4 pad = '\0' * (4 - trail) # Always pad w/ at least 1 byte diff --git a/src/calibre/gui2/main.py b/src/calibre/gui2/main.py index 0a77839ce1..bdc4568dce 100644 --- a/src/calibre/gui2/main.py +++ b/src/calibre/gui2/main.py @@ -1670,7 +1670,7 @@ class Main(MainWindow, Ui_MainWindow, DeviceGUI): def update_found(self, version): os = 'windows' if iswindows else 'osx' if isosx else 'linux' url = 'http://%s.kovidgoyal.net/download_%s'%(__appname__, os) - self.latest_version = '
'+_('' + self.latest_version = '
' + _('' 'Latest version: %s')%(url, version) self.vanity.setText(self.vanity_template%\ (dict(version=self.latest_version,