summaryrefslogtreecommitdiff
path: root/sphinx/util
diff options
context:
space:
mode:
authorshimizukawa <shimizukawa@gmail.com>2012-12-10 10:18:32 +0900
committershimizukawa <shimizukawa@gmail.com>2012-12-10 10:18:32 +0900
commitd45dcfbd7e5add4495a7e31ee53e672ee180d534 (patch)
treee3ccbee28f06bfccb66ab2c54cb3b1648409e704 /sphinx/util
parent207e12471f451fee53a95ca6918c93abfef3a632 (diff)
parent5818df7941b73abecdb1b399fee7956ea283d698 (diff)
downloadsphinx-d45dcfbd7e5add4495a7e31ee53e672ee180d534.tar.gz
merge heads
Diffstat (limited to 'sphinx/util')
-rw-r--r--sphinx/util/__init__.py13
-rw-r--r--sphinx/util/inspect.py5
-rw-r--r--sphinx/util/nodes.py9
-rw-r--r--sphinx/util/osutil.py15
-rw-r--r--sphinx/util/pycompat.py24
5 files changed, 58 insertions, 8 deletions
diff --git a/sphinx/util/__init__.py b/sphinx/util/__init__.py
index 6cb83aec..5d0c2811 100644
--- a/sphinx/util/__init__.py
+++ b/sphinx/util/__init__.py
@@ -197,13 +197,18 @@ def get_module_source(modname):
except Exception, err:
raise PycodeError('error importing %r' % modname, err)
mod = sys.modules[modname]
- if hasattr(mod, '__loader__'):
+ filename = getattr(mod, '__file__', None)
+ loader = getattr(mod, '__loader__', None)
+ if loader and getattr(loader, 'get_filename', None):
+ try:
+ filename = loader.get_filename(modname)
+ except Exception, err:
+ raise PycodeError('error getting filename for %r' % filename, err)
+ if filename is None and loader:
try:
- source = mod.__loader__.get_source(modname)
+ return 'string', loader.get_source(modname)
except Exception, err:
raise PycodeError('error getting source for %r' % modname, err)
- return 'string', source
- filename = getattr(mod, '__file__', None)
if filename is None:
raise PycodeError('no source found for module %r' % modname)
filename = path.normpath(path.abspath(filename))
diff --git a/sphinx/util/inspect.py b/sphinx/util/inspect.py
index b5c3db59..ba3fa968 100644
--- a/sphinx/util/inspect.py
+++ b/sphinx/util/inspect.py
@@ -45,7 +45,10 @@ if sys.version_info >= (2, 5):
del func_defaults[i]
except IndexError:
pass
- return inspect.ArgSpec(args, varargs, varkw, func_defaults)
+ if sys.version_info >= (2, 6):
+ return inspect.ArgSpec(args, varargs, varkw, func_defaults)
+ else:
+ return (args, varargs, varkw, func_defaults)
else:
getargspec = inspect.getargspec
diff --git a/sphinx/util/nodes.py b/sphinx/util/nodes.py
index dbedb7f2..62796bec 100644
--- a/sphinx/util/nodes.py
+++ b/sphinx/util/nodes.py
@@ -43,6 +43,15 @@ IGNORED_NODES = (
def extract_messages(doctree):
"""Extract translatable messages from a document tree."""
for node in doctree.traverse(nodes.TextElement):
+ # workaround: nodes.term doesn't have source, line and rawsource
+ # (fixed in Docutils r7495)
+ if isinstance(node, nodes.term) and not node.source:
+ definition_list_item = node.parent
+ if definition_list_item.line is not None:
+ node.source = definition_list_item.source
+ node.line = definition_list_item.line - 1
+ node.rawsource = definition_list_item.\
+ rawsource.split("\n", 2)[0]
if not node.source:
continue # built-in message
if isinstance(node, IGNORED_NODES):
diff --git a/sphinx/util/osutil.py b/sphinx/util/osutil.py
index 8dc3b9d3..17619ee1 100644
--- a/sphinx/util/osutil.py
+++ b/sphinx/util/osutil.py
@@ -40,12 +40,20 @@ def relative_uri(base, to):
return to
b2 = base.split(SEP)
t2 = to.split(SEP)
- # remove common segments
- for x, y in zip(b2, t2):
+ # remove common segments (except the last segment)
+ for x, y in zip(b2[:-1], t2[:-1]):
if x != y:
break
b2.pop(0)
t2.pop(0)
+ if b2 == t2:
+ # Special case: relative_uri('f/index.html','f/index.html')
+ # returns '', not 'index.html'
+ return ''
+ if len(b2) == 1 and t2 == ['']:
+ # Special case: relative_uri('f/index.html','f/') should
+ # return './', not ''
+ return '.' + SEP
return ('..' + SEP) * (len(b2)-1) + SEP.join(t2)
@@ -136,8 +144,9 @@ else:
def safe_relpath(path, start=None):
+ from sphinx.util.pycompat import relpath
try:
- return os.path.relpath(path, start)
+ return relpath(path, start)
except ValueError:
return path
diff --git a/sphinx/util/pycompat.py b/sphinx/util/pycompat.py
index 9e081b02..c2c6fe8b 100644
--- a/sphinx/util/pycompat.py
+++ b/sphinx/util/pycompat.py
@@ -82,6 +82,10 @@ if sys.version_info >= (2, 6):
except ImportError:
from itertools import izip_longest as zip_longest
+ import os
+ relpath = os.path.relpath
+ del os
+
else:
# Python < 2.6
from itertools import izip, repeat, chain
@@ -114,6 +118,26 @@ else:
except IndexError:
pass
+ from os.path import curdir
+ def relpath(path, start=curdir):
+ """Return a relative version of a path"""
+ from os.path import sep, abspath, commonprefix, join, pardir
+
+ if not path:
+ raise ValueError("no path specified")
+
+ start_list = abspath(start).split(sep)
+ path_list = abspath(path).split(sep)
+
+ # Work out how much of the filepath is shared by start and path.
+ i = len(commonprefix([start_list, path_list]))
+
+ rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
+ if not rel_list:
+ return curdir
+ return join(*rel_list)
+ del curdir
+
# ------------------------------------------------------------------------------
# Missing builtins and codecs in Python < 2.5