mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Cleanup; add scoping to ClassAttrVisitor.
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
from os import path
|
from os import path
|
||||||
|
|
||||||
from sphinx.pycode import pytree
|
from sphinx.pycode import pytree
|
||||||
@@ -45,8 +46,10 @@ _eq = pytree.Leaf(token.EQUAL, '=')
|
|||||||
|
|
||||||
|
|
||||||
class ClassAttrVisitor(pytree.NodeVisitor):
|
class ClassAttrVisitor(pytree.NodeVisitor):
|
||||||
def init(self):
|
def init(self, scope):
|
||||||
|
self.scope = scope
|
||||||
self.namespace = []
|
self.namespace = []
|
||||||
|
self.collected = []
|
||||||
|
|
||||||
def visit_classdef(self, node):
|
def visit_classdef(self, node):
|
||||||
self.namespace.append(node[1].value)
|
self.namespace.append(node[1].value)
|
||||||
@@ -54,17 +57,21 @@ class ClassAttrVisitor(pytree.NodeVisitor):
|
|||||||
self.namespace.pop()
|
self.namespace.pop()
|
||||||
|
|
||||||
def visit_expr_stmt(self, node):
|
def visit_expr_stmt(self, node):
|
||||||
if _eq in node.children:
|
if _eq not in node.children:
|
||||||
prefix = node[0].get_prefix()
|
# not an assignment (we don't care for augmented assignments)
|
||||||
if not prefix:
|
return
|
||||||
prev = node[0].get_prev_leaf()
|
prefix = node[0].get_prefix()
|
||||||
if prev and prev.type == token.INDENT:
|
if not prefix:
|
||||||
prefix = prev.prefix
|
# if this assignment is the first thing in a class block,
|
||||||
doc = prepare_commentdoc(prefix)
|
# the comment will be the prefix of the preceding INDENT token
|
||||||
if doc:
|
prev = node[0].get_prev_leaf()
|
||||||
targ = '.'.join(self.namespace + [node[0].compact()])
|
if prev and prev.type == token.INDENT:
|
||||||
print targ
|
prefix = prev.prefix
|
||||||
print doc
|
doc = prepare_commentdoc(prefix)
|
||||||
|
if doc:
|
||||||
|
name = '.'.join(self.namespace + [node[0].compact()])
|
||||||
|
if name.startswith(self.scope):
|
||||||
|
self.collected.append((name, doc))
|
||||||
|
|
||||||
def visit_funcdef(self, node):
|
def visit_funcdef(self, node):
|
||||||
return
|
return
|
||||||
@@ -115,28 +122,16 @@ class ModuleAnalyzer(object):
|
|||||||
return cls.for_file(filename, modname)
|
return cls.for_file(filename, modname)
|
||||||
|
|
||||||
def find_defs(self):
|
def find_defs(self):
|
||||||
attr_visitor = ClassAttrVisitor(number2name)
|
attr_visitor = ClassAttrVisitor(number2name, '')
|
||||||
attr_visitor.namespace = [self.modname]
|
|
||||||
attr_visitor.visit(self.tree)
|
attr_visitor.visit(self.tree)
|
||||||
|
for name, doc in attr_visitor.collected:
|
||||||
class Test:
|
print '>>', name
|
||||||
"""doc"""
|
print doc
|
||||||
|
|
||||||
#: testing...
|
|
||||||
x = 1
|
|
||||||
"""doc"""
|
|
||||||
|
|
||||||
#: testing more...
|
|
||||||
x = 2
|
|
||||||
|
|
||||||
|
|
||||||
#ma = ModuleAnalyzer.for_file(__file__.rstrip('c'))
|
x0 = time.time()
|
||||||
import time
|
ma = ModuleAnalyzer.for_module('sphinx.builders.html')
|
||||||
x0=time.time()
|
x1 = time.time()
|
||||||
ma = ModuleAnalyzer.for_module('sphinx.builders.latex')
|
|
||||||
x1=time.time()
|
|
||||||
ma.find_defs()
|
ma.find_defs()
|
||||||
x2=time.time()
|
x2 = time.time()
|
||||||
print "%.4f %.4f" % (x1-x0, x2-x1)
|
print "parsing %.4f, finding %.4f" % (x1-x0, x2-x1)
|
||||||
|
|
||||||
#print pytree.nice_repr(ma.tree, number2name, True)
|
|
||||||
|
|||||||
@@ -275,11 +275,11 @@ def nice_repr(node, number2name, prefix=False):
|
|||||||
|
|
||||||
|
|
||||||
class NodeVisitor(object):
|
class NodeVisitor(object):
|
||||||
def __init__(self, number2name):
|
def __init__(self, number2name, *args):
|
||||||
self.number2name = number2name
|
self.number2name = number2name
|
||||||
self.init()
|
self.init(*args)
|
||||||
|
|
||||||
def init(self):
|
def init(self, *args):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def visit(self, node):
|
def visit(self, node):
|
||||||
|
|||||||
Reference in New Issue
Block a user