Use os.path.relpath instead of custom implementation

This commit is contained in:
Kovid Goyal 2009-05-31 16:26:50 -07:00
parent bae243edce
commit 654dfbdf2c
4 changed files with 13 additions and 43 deletions

View File

@ -284,39 +284,7 @@ def launch(path_or_url):
path_or_url = 'file:'+path_or_url path_or_url = 'file:'+path_or_url
QDesktopServices.openUrl(QUrl(path_or_url)) QDesktopServices.openUrl(QUrl(path_or_url))
def relpath(target, base=os.curdir): relpath = os.path.relpath
"""
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)
_spat = re.compile(r'^the\s+|^a\s+|^an\s+', re.IGNORECASE) _spat = re.compile(r'^the\s+|^a\s+|^an\s+', re.IGNORECASE)
def english_sort(x, y): def english_sort(x, y):
''' '''

View File

@ -15,7 +15,6 @@ from lxml import etree
from dateutil import parser from dateutil import parser
from calibre.ebooks.chardet import xml_to_unicode from calibre.ebooks.chardet import xml_to_unicode
from calibre import relpath
from calibre.constants import __appname__, __version__ from calibre.constants import __appname__, __version__
from calibre.ebooks.metadata.toc import TOC from calibre.ebooks.metadata.toc import TOC
from calibre.ebooks.metadata import MetaInformation, string_to_authors from calibre.ebooks.metadata import MetaInformation, string_to_authors
@ -86,8 +85,8 @@ class Resource(object):
if self.path == basedir: if self.path == basedir:
return ''+frag return ''+frag
try: try:
rpath = relpath(self.path, basedir) rpath = os.path.relpath(self.path, basedir)
except OSError: # On windows path and basedir could be on different drives except ValueError: # On windows path and basedir could be on different drives
rpath = self.path rpath = self.path
if isinstance(rpath, unicode): if isinstance(rpath, unicode):
rpath = rpath.encode('utf-8') rpath = rpath.encode('utf-8')
@ -560,7 +559,7 @@ class OPF(object):
has_path = True has_path = True
break break
if not has_path: 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) item = self.create_manifest_item(href, media_type)
manifest = self.manifest_ppath(self.root)[0] manifest = self.manifest_ppath(self.root)[0]
manifest.append(item) manifest.append(item)

View File

@ -511,7 +511,7 @@ class MobiWriter(object):
indxt, indxt_count, indices, last_name = \ indxt, indxt_count, indices, last_name = \
self._generate_indxt(ctoc) self._generate_indxt(ctoc)
if last_name is None: 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 return
indx1 = StringIO() indx1 = StringIO()
@ -628,8 +628,8 @@ class MobiWriter(object):
self._last_toc_entry = None self._last_toc_entry = None
ctoc = StringIO() ctoc = StringIO()
def add_node(node, cls): def add_node(node, cls, title=None):
t = node.title t = node.title if title is None else title
if t and t.strip(): if t and t.strip():
t = t.strip() t = t.strip()
if not isinstance(t, unicode): if not isinstance(t, unicode):
@ -640,8 +640,10 @@ class MobiWriter(object):
self._ctoc_name_map[node] = t self._ctoc_name_map[node] = t
ctoc.write(decint(len(t), DECINT_FORWARD)+t) ctoc.write(decint(len(t), DECINT_FORWARD)+t)
first = True
for child in toc.iter(): 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()) return align_block(ctoc.getvalue())
@ -842,7 +844,8 @@ class MobiWriter(object):
if INDEXING: if INDEXING:
# Write unknown EXTH records as 0s # Write unknown EXTH records as 0s
for code, size in [(204,4), (205,4), (206,4), (207,4), (300,40)]: 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() exth = exth.getvalue()
trail = len(exth) % 4 trail = len(exth) % 4
pad = '\0' * (4 - trail) # Always pad w/ at least 1 byte pad = '\0' * (4 - trail) # Always pad w/ at least 1 byte