Browse files

ENH: numpydoc: Python 2 & 3 in single codebase, restructure as a package

  • Loading branch information...
1 parent a4cd4ff commit 7f2da39dea8f869ea4d5256786f55eacbd5722e6 @pv pv committed Feb 16, 2013
View
2 doc/sphinxext/MANIFEST.in
@@ -1,2 +1,2 @@
-recursive-include tests *.py
+recursive-include numpydoc/tests *.py
include *.txt
View
1 doc/sphinxext/__init__.py
@@ -1 +0,0 @@
-from numpydoc import setup
View
1 doc/sphinxext/numpydoc/__init__.py
@@ -0,0 +1 @@
+from .numpydoc import setup
View
15 doc/sphinxext/comment_eater.py → doc/sphinxext/numpydoc/comment_eater.py
@@ -1,10 +1,15 @@
-from cStringIO import StringIO
+import sys
+if sys.version_info[0] >= 3:
+ from io import StringIO
+else:
+ from cStringIO import StringIO
+
import compiler
import inspect
import textwrap
import tokenize
-from compiler_unparse import unparse
+from .compiler_unparse import unparse
class Comment(object):
@@ -68,7 +73,11 @@ def __init__(self):
def process_file(self, file):
""" Process a file object.
"""
- for token in tokenize.generate_tokens(file.next):
+ if sys.version_info[0] >= 3:
+ nxt = file.__next__
+ else:
+ nxt = file.next
+ for token in tokenize.generate_tokens(nxt):
self.process_token(*token)
self.make_index()
View
8 doc/sphinxext/compiler_unparse.py → doc/sphinxext/numpydoc/compiler_unparse.py
@@ -12,11 +12,15 @@
"""
import sys
-import cStringIO
from compiler.ast import Const, Name, Tuple, Div, Mul, Sub, Add
+if sys.version_info[0] >= 3:
+ from io import StringIO
+else:
+ from cStringIO import StringIO
+
def unparse(ast, single_line_functions=False):
- s = cStringIO.StringIO()
+ s = StringIO()
UnparseCompilerAst(ast, s, single_line_functions)
return s.getvalue().lstrip()
View
17 doc/sphinxext/docscrape.py → doc/sphinxext/numpydoc/docscrape.py
@@ -2,13 +2,18 @@
"""
+import sys
import inspect
import textwrap
import re
import pydoc
-from StringIO import StringIO
from warnings import warn
+if sys.version_info[0] >= 3:
+ from io import StringIO
+else:
+ from cStringIO import StringIO
+
class Reader(object):
"""A line-based string reader.
@@ -113,7 +118,7 @@ def __getitem__(self,key):
return self._parsed_data[key]
def __setitem__(self,key,val):
- if not self._parsed_data.has_key(key):
+ if key not in self._parsed_data:
warn("Unknown section %s" % key)
else:
self._parsed_data[key] = val
@@ -374,7 +379,7 @@ def _str_index(self):
idx = self['index']
out = []
out += ['.. index:: %s' % idx.get('default','')]
- for section, references in idx.iteritems():
+ for section, references in idx.items():
if section == 'default':
continue
out += [' :%s: %s' % (section, ', '.join(references))]
@@ -432,7 +437,7 @@ def __init__(self, func, role='func', doc=None, config={}):
argspec = inspect.formatargspec(*argspec)
argspec = argspec.replace('*','\*')
signature = '%s%s' % (func_name, argspec)
- except TypeError, e:
+ except TypeError as e:
signature = '%s()' % func_name
self['Signature'] = signature
@@ -454,8 +459,8 @@ def __str__(self):
'meth': 'method'}
if self._role:
- if not roles.has_key(self._role):
- print "Warning: invalid role %s" % self._role
+ if self._role not in roles:
+ print("Warning: invalid role %s" % self._role)
out += '.. %s:: %s\n \n\n' % (roles.get(self._role,''),
func_name)
View
4 doc/sphinxext/docscrape_sphinx.py → doc/sphinxext/numpydoc/docscrape_sphinx.py
@@ -1,6 +1,6 @@
import re, inspect, textwrap, pydoc
import sphinx
-from docscrape import NumpyDocString, FunctionDoc, ClassDoc
+from .docscrape import NumpyDocString, FunctionDoc, ClassDoc
class SphinxDocString(NumpyDocString):
def __init__(self, docstring, config={}):
@@ -127,7 +127,7 @@ def _str_index(self):
return out
out += ['.. index:: %s' % idx.get('default','')]
- for section, references in idx.iteritems():
+ for section, references in idx.items():
if section == 'default':
continue
elif section == 'refguide':
View
0 doc/sphinxext/linkcode.py → doc/sphinxext/numpydoc/linkcode.py
File renamed without changes.
View
23 doc/sphinxext/numpydoc.py → doc/sphinxext/numpydoc/numpydoc.py
@@ -21,8 +21,8 @@
if sphinx.__version__ < '1.0.1':
raise RuntimeError("Sphinx 1.0.1 or newer is required")
-import os, re, pydoc
-from docscrape_sphinx import get_doc_object, SphinxDocString
+import os, sys, re, pydoc
+from .docscrape_sphinx import get_doc_object, SphinxDocString
from sphinx.util.compat import Directive
import inspect
@@ -34,28 +34,32 @@ def mangle_docstrings(app, what, name, obj, options, lines,
if what == 'module':
# Strip top title
- title_re = re.compile(ur'^\s*[#*=]{4,}\n[a-z0-9 -]+\n[#*=]{4,}\s*',
+ title_re = re.compile(u'^\\s*[#*=]{4,}\\n[a-z0-9 -]+\\n[#*=]{4,}\\s*',
re.I|re.S)
lines[:] = title_re.sub(u'', u"\n".join(lines)).split(u"\n")
else:
doc = get_doc_object(obj, what, u"\n".join(lines), config=cfg)
- lines[:] = unicode(doc).split(u"\n")
+ if sys.version_info[0] >= 3:
+ doc = str(doc)
+ else:
+ doc = str(doc).decode('utf-8')
+ lines[:] = doc.split(u"\n")
if app.config.numpydoc_edit_link and hasattr(obj, '__name__') and \
obj.__name__:
if hasattr(obj, '__module__'):
v = dict(full_name=u"%s.%s" % (obj.__module__, obj.__name__))
else:
v = dict(full_name=obj.__name__)
- lines += [u'', u'.. htmlonly::', '']
+ lines += [u'', u'.. htmlonly::', u'']
lines += [u' %s' % x for x in
(app.config.numpydoc_edit_link % v).split("\n")]
# replace reference numbers so that there are no duplicates
references = []
for line in lines:
line = line.strip()
- m = re.match(ur'^.. \[([a-z0-9_.-])\]', line, re.I)
+ m = re.match(u'^.. \\[([a-z0-9_.-])\\]', line, re.I)
if m:
references.append(m.group(1))
@@ -64,7 +68,7 @@ def mangle_docstrings(app, what, name, obj, options, lines,
if references:
for i, line in enumerate(lines):
for r in references:
- if re.match(ur'^\d+$', r):
+ if re.match(u'^\\d+$', r):
new_r = u"R%d" % (reference_offset[0] + int(r))
else:
new_r = u"%s%d" % (r, reference_offset[0])
@@ -91,6 +95,9 @@ def mangle_signature(app, what, name, obj, options, sig, retann):
return sig, u''
def setup(app, get_doc_object_=get_doc_object):
+ if not hasattr(app, 'add_config_value'):
+ return # probably called by nose, better bail out
+
global get_doc_object
get_doc_object = get_doc_object_
@@ -120,7 +127,7 @@ def __init__(self, *a, **kw):
self.wrap_mangling_directives()
def wrap_mangling_directives(self):
- for name, objtype in self.directive_mangling_map.items():
+ for name, objtype in list(self.directive_mangling_map.items()):
self.directives[name] = wrap_mangling_directive(
self.directives[name], objtype)
View
7 doc/sphinxext/phantom_import.py → doc/sphinxext/numpydoc/phantom_import.py
@@ -23,7 +23,7 @@ def setup(app):
def initialize(app):
fn = app.config.phantom_import_file
if (fn and os.path.isfile(fn)):
- print "[numpydoc] Phantom importing modules from", fn, "..."
+ print("[numpydoc] Phantom importing modules from", fn, "...")
import_phantom_module(fn)
#------------------------------------------------------------------------------
@@ -129,7 +129,10 @@ def base_cmp(a, b):
doc = "%s%s\n\n%s" % (funcname, argspec, doc)
obj = lambda: 0
obj.__argspec_is_invalid_ = True
- obj.func_name = funcname
+ if sys.version_info[0] >= 3:
+ obj.__name__ = funcname
+ else:
+ obj.func_name = funcname
obj.__name__ = name
obj.__doc__ = doc
if inspect.isclass(object_cache[parent]):
View
25 doc/sphinxext/plot_directive.py → doc/sphinxext/numpydoc/plot_directive.py
@@ -75,9 +75,14 @@
"""
-import sys, os, glob, shutil, imp, warnings, cStringIO, re, textwrap, traceback
+import sys, os, glob, shutil, imp, warnings, re, textwrap, traceback
import sphinx
+if sys.version_info[0] >= 3:
+ from io import StringIO
+else:
+ from cStringIO import StringIO
+
import warnings
warnings.warn("A plot_directive module is also available under "
"matplotlib.sphinxext; expect this numpydoc.plot_directive "
@@ -257,7 +262,7 @@ def run(arguments, content, options, state_machine, state, lineno):
# is it in doctest format?
is_doctest = contains_doctest(code)
- if options.has_key('format'):
+ if 'format' in options:
if options['format'] == 'python':
is_doctest = False
else:
@@ -291,7 +296,7 @@ def run(arguments, content, options, state_machine, state, lineno):
results = makefig(code, source_file_name, build_dir, output_base,
config)
errors = []
- except PlotError, err:
+ except PlotError as err:
reporter = state.memo.reporter
sm = reporter.system_message(
2, "Exception occurred in plotting %s: %s" % (output_base, err),
@@ -314,7 +319,7 @@ def run(arguments, content, options, state_machine, state, lineno):
else:
source_code = ""
- opts = [':%s: %s' % (key, val) for key, val in options.items()
+ opts = [':%s: %s' % (key, val) for key, val in list(options.items())
if key in ('alt', 'height', 'width', 'scale', 'align', 'class')]
only_html = ".. only:: html"
@@ -444,7 +449,7 @@ def run_code(code, code_path, ns=None):
# Redirect stdout
stdout = sys.stdout
- sys.stdout = cStringIO.StringIO()
+ sys.stdout = StringIO()
# Reset sys.argv
old_sys_argv = sys.argv
@@ -456,9 +461,9 @@ def run_code(code, code_path, ns=None):
if ns is None:
ns = {}
if not ns:
- exec setup.config.plot_pre_code in ns
- exec code in ns
- except (Exception, SystemExit), err:
+ exec(setup.config.plot_pre_code, ns)
+ exec(code, ns)
+ except (Exception, SystemExit) as err:
raise PlotError(traceback.format_exc())
finally:
os.chdir(pwd)
@@ -520,7 +525,7 @@ def makefig(code, code_path, output_dir, output_base, config):
all_exists = True
for i, code_piece in enumerate(code_pieces):
images = []
- for j in xrange(1000):
+ for j in range(1000):
img = ImageFile('%s_%02d_%02d' % (output_base, i, j), output_dir)
for format, dpi in formats:
if out_of_date(code_path, img.filename(format)):
@@ -565,7 +570,7 @@ def makefig(code, code_path, output_dir, output_base, config):
for format, dpi in formats:
try:
figman.canvas.figure.savefig(img.filename(format), dpi=dpi)
- except exceptions.BaseException, err:
+ except exceptions.BaseException as err:
raise PlotError(traceback.format_exc())
img.formats.append(format)
View
13 doc/sphinxext/tests/test_docscrape.py → ...phinxext/numpydoc/tests/test_docscrape.py
@@ -1,10 +1,9 @@
# -*- encoding:utf-8 -*-
import sys, os
-sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
-from docscrape import NumpyDocString, FunctionDoc, ClassDoc
-from docscrape_sphinx import SphinxDocString, SphinxClassDoc
+from numpydoc.docscrape import NumpyDocString, FunctionDoc, ClassDoc
+from numpydoc.docscrape_sphinx import SphinxDocString, SphinxClassDoc
from nose.tools import *
doc_txt = '''\
@@ -165,7 +164,7 @@ def test_examples():
def test_index():
assert_equal(doc['index']['default'], 'random')
- print doc['index']
+ print(doc['index'])
assert_equal(len(doc['index']), 2)
assert_equal(len(doc['index']['refguide']), 2)
@@ -556,7 +555,11 @@ def test_unicode():
äää
""")
- assert doc['Summary'][0] == u'öäöäöäöäöåååå'.encode('utf-8')
+ assert isinstance(doc['Summary'][0], str)
+ if sys.version_info[0] >= 3:
+ assert doc['Summary'][0] == u'öäöäöäöäöåååå'
+ else:
+ assert doc['Summary'][0] == u'öäöäöäöäöåååå'.encode('utf-8')
def test_plot_examples():
cfg = dict(use_plots=True)
View
10 doc/sphinxext/traitsdoc.py → doc/sphinxext/numpydoc/traitsdoc.py
@@ -18,13 +18,13 @@
import os
import pydoc
-import docscrape
-import docscrape_sphinx
-from docscrape_sphinx import SphinxClassDoc, SphinxFunctionDoc, SphinxDocString
+from . import docscrape
+from . import docscrape_sphinx
+from .docscrape_sphinx import SphinxClassDoc, SphinxFunctionDoc, SphinxDocString
-import numpydoc
+from . import numpydoc
-import comment_eater
+from . import comment_eater
class SphinxTraitsDoc(SphinxClassDoc):
def __init__(self, cls, modulename='', func_doc=SphinxFunctionDoc):
View
10 doc/sphinxext/setup.py
@@ -1,11 +1,16 @@
+import setuptools
from distutils.core import setup
-version = "0.4"
+import sys
+if sys.version_info[0] >= 3 and sys.version_info[1] < 3 or \
+ sys.version_info[0] <= 2 and sys.version_info[1] < 6:
+ raise RuntimeError("Python version 2.6, 2.7 or >= 3.3 required.")
+
+version = "0.4.dev"
setup(
name="numpydoc",
packages=["numpydoc"],
- package_dir={"numpydoc": "."},
version=version,
description="Sphinx extension to support docstrings in Numpy format",
# classifiers from http://pypi.python.org/pypi?%3Aaction=list_classifiers
@@ -20,4 +25,5 @@
license="BSD",
requires=["sphinx (>= 1.0.1)"],
package_data={'numpydoc': ['tests/test_*.py']},
+ test_suite = 'nose.collector',
)

0 comments on commit 7f2da39

Please sign in to comment.