Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Commit 100cd80

Browse files
committed
Started to use redbaron.
1 parent 1f21307 commit 100cd80

File tree

2 files changed

+83
-38
lines changed

2 files changed

+83
-38
lines changed

requirements/runtime.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
astroid
1+
redbaron

src/pydocstyle/parser.py

Lines changed: 82 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections import defaultdict
66
from itertools import chain, dropwhile
77
from re import compile as re
8-
import astroid
8+
import redbaron
99

1010
try:
1111
from StringIO import StringIO
@@ -508,48 +508,93 @@ def _parse_from_import_names(self, is_future_import):
508508

509509

510510
class Parser(object):
511-
def parse(self, filelike, filename):
512-
# TODO: fix log
513-
self.log = logging.getLogger()
514-
module_node = astroid.parse(filelike.read(), path=filename)
515-
516-
module_children_handler = {
517-
astroid.FunctionDef: self.handle_function,
511+
def handle_node(self, node, *args, **kwargs):
512+
node_handler = {
513+
redbaron.nodes.DefNode: self.handle_function,
518514
#astroid.ClassDef: self.handle_class,
519515
#astroid.ImportFrom: self.handle_from_import,
520516
}
521517

518+
if node.__class__ in node_handler:
519+
return node_handler[node.__class__](node, *args, **kwargs)
520+
return None
521+
522+
def parse(self, filelike, filename):
523+
# TODO: fix log
524+
self.log = logging.getLogger()
525+
self.source = filelike.readlines()
526+
nodes = redbaron.RedBaron(''.join(self.source))
527+
522528
module_children = []
523-
for child in module_node.get_children():
524-
handler = module_children_handler[child.__class__]
525-
module_children.append(handler(module_node, child))
526-
527-
return Module(filename,
528-
module_node.source_code,
529-
module_node.fromlineno,
530-
module_node.tolineno,
531-
[],
532-
module_node.doc,
533-
module_children,
534-
None,
535-
None,
536-
None)
537-
538-
def handle_function(self, parent, node):
539-
source_lines = node.root().source_code.split('\n')[node.lineno:node.tolineno]
540-
tokens = TokenStream(StringIO('\n'.join(
541-
node.root().source_code.split('\n')[
542-
node.lineno - 1:node.tolineno])))
543-
544-
for token in tokens:
545-
if token.value != 'def':
546-
continue
529+
for node in nodes:
530+
result = self.handle_node(node)
531+
if result is not None:
532+
module_children.append(result)
533+
534+
module = Module(filename,
535+
self.source,
536+
0,
537+
len(self.source),
538+
[],
539+
#module_node.doc,
540+
None,
541+
module_children,
542+
None,
543+
None,
544+
None)
545+
546+
for node in module.children:
547+
node.parent = module
548+
549+
return module
550+
551+
def handle_function(self, node, nested=False, method=False):
552+
if nested:
553+
assert not method
554+
cls = NestedFunction
555+
else:
556+
cls = Method if method else Function
557+
558+
docstring = None
559+
560+
skip_nodes = (
561+
redbaron.nodes.EndlNode,
562+
redbaron.nodes.CommentNode,
563+
)
564+
for child in node:
565+
if not isinstance(child, skip_nodes):
566+
break
567+
568+
if isinstance(child, redbaron.nodes.StringNode):
569+
# docstring!
570+
docstring = child.value
571+
572+
children = []
573+
for child in node:
574+
result = self.handle_node(child, nested=True)
575+
if result is not None:
576+
children.append(result)
577+
578+
start = node.absolute_bounding_box.top_left.line
579+
bottom_right = node.absolute_bounding_box.bottom_right
580+
if bottom_right.line == start:
581+
end = start
582+
else:
583+
end = bottom_right.line - 1
584+
585+
function = cls(node.name,
586+
self.source,
587+
start,
588+
end,
589+
[decorator.name for decorator in node.decorators],
590+
docstring,
591+
children,
592+
None)
593+
594+
for child in function.children:
595+
child.parent = function
547596

548-
return Function(node.name,
549-
node.root().source_code[node.fromlineno:node.tolineno],
550-
node.fromlineno, node.tolineno, node.decorators or [],
551-
node.doc,
552-
[], parent)
597+
return function
553598

554599
# TODO: remove
555600
def __call__(self, *args, **kwargs):

0 commit comments

Comments
 (0)