|
5 | 5 | from collections import defaultdict
|
6 | 6 | from itertools import chain, dropwhile
|
7 | 7 | from re import compile as re
|
8 |
| -import astroid |
| 8 | +import redbaron |
9 | 9 |
|
10 | 10 | try:
|
11 | 11 | from StringIO import StringIO
|
@@ -508,48 +508,93 @@ def _parse_from_import_names(self, is_future_import):
|
508 | 508 |
|
509 | 509 |
|
510 | 510 | 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, |
518 | 514 | #astroid.ClassDef: self.handle_class,
|
519 | 515 | #astroid.ImportFrom: self.handle_from_import,
|
520 | 516 | }
|
521 | 517 |
|
| 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 | + |
522 | 528 | 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 |
547 | 596 |
|
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 |
553 | 598 |
|
554 | 599 | # TODO: remove
|
555 | 600 | def __call__(self, *args, **kwargs):
|
|
0 commit comments