src/eric7/Utilities/ClassBrowsers/pyclbr.py

branch
eric7
changeset 10216
c07a1ef5c5d3
parent 10050
3750abc45d5e
child 10433
328f3ec4b77a
equal deleted inserted replaced
10215:d476667171a1 10216:c07a1ef5c5d3
8 8
9 Parse enough of a Python file to recognize class and method definitions and 9 Parse enough of a Python file to recognize class and method definitions and
10 to find out the superclasses of a class as well as its attributes. 10 to find out the superclasses of a class as well as its attributes.
11 """ 11 """
12 12
13 import keyword
13 import re 14 import re
14 import sys 15 import sys
15 16
16 from dataclasses import dataclass 17 from dataclasses import dataclass
17 from functools import reduce 18 from functools import reduce
69 | (?P<Method> 70 | (?P<Method>
70 ^ 71 ^
71 (?P<MethodIndent> [ \t]* ) 72 (?P<MethodIndent> [ \t]* )
72 (?: async [ \t]+ )? (?: cdef | cpdef | def) [ \t]+ 73 (?: async [ \t]+ )? (?: cdef | cpdef | def) [ \t]+
73 (?P<MethodName> \w+ ) 74 (?P<MethodName> \w+ )
74 (?: [ \t]* \[ (?: plain | html ) \] )? 75 (?: [ \t]* \[ [^\]]+ \] )?
75 [ \t]* \( 76 [ \t]* \(
76 (?P<MethodSignature> (?: [^)] | \)[ \t]*,? )*? ) 77 (?P<MethodSignature> (?: [^)] | \)[ \t]*,? )*? )
77 \) [ \t]* 78 \) [ \t]*
78 (?P<MethodReturnAnnotation> (?: -> [ \t]* [^:]+ )? ) 79 (?P<MethodReturnAnnotation> (?: -> [ \t]* [^:]+ )? )
79 [ \t]* : 80 [ \t]* :
83 ^ 84 ^
84 (?P<ClassIndent> [ \t]* ) 85 (?P<ClassIndent> [ \t]* )
85 (?: cdef [ \t]+ )? 86 (?: cdef [ \t]+ )?
86 class [ \t]+ 87 class [ \t]+
87 (?P<ClassName> \w+ ) 88 (?P<ClassName> \w+ )
89 (?: [ \t]* \[ [^\]]+ \] )?
88 [ \t]* 90 [ \t]*
89 (?P<ClassSupers> \( [^)]* \) )? 91 (?P<ClassSupers> \( [^)]* \) )?
90 [ \t]* : 92 [ \t]* :
91 ) 93 )
92 94
93 | (?P<Attribute> 95 | (?P<Attribute>
94 ^ 96 ^
95 (?P<AttributeIndent> [ \t]* ) 97 (?P<AttributeIndent> [ \t]* )
96 self [ \t]* \. [ \t]* 98 self [ \t]* \. [ \t]*
97 (?P<AttributeName> \w+ ) 99 (?P<AttributeName> \w+ )
100 (?: [ \t]* : [^=\n]+ )?
98 [ \t]* = 101 [ \t]* =
99 ) 102 )
100 103
101 | (?P<Variable> 104 | (?P<Variable>
102 ^ 105 ^
103 (?P<VariableIndent> [ \t]* ) 106 (?P<VariableIndent> [ \t]* )
104 (?P<VariableName> \w+ ) 107 (?P<VariableName> \w+ )
105 [ \t]* = 108 [ \t]* =
109 )
110
111 | (?P<TypedVariable>
112 ^
113 (?P<TypedVariableIndent> [ \t]* )
114 (?P<TypedVariableName> \w+ )
115 [ \t]* :
106 ) 116 )
107 117
108 | (?P<Main> 118 | (?P<Main>
109 ^ 119 ^
110 if \s+ __name__ \s* == \s* [^:]+ : $ 120 if \s+ __name__ \s* == \s* [^:]+ : $
652 classstack[index][0]._addglobal( 662 classstack[index][0]._addglobal(
653 Attribute(module, variable_name, file, lineno) 663 Attribute(module, variable_name, file, lineno)
654 ) 664 )
655 break 665 break
656 666
667 elif m.captured("TypedVariable"):
668 thisindent = _indent(m.captured("TypedVariableIndent"))
669 variable_name = m.captured("TypedVariableName")
670 if not keyword.iskeyword(variable_name):
671 # only if the determined name is not a keyword (e.g. else, except)
672 lineno += src.count("\n", last_lineno_pos, start)
673 last_lineno_pos = start
674 if thisindent == 0 or not classstack:
675 # global variable, reset class stack first
676 classstack = []
677
678 if "@@Globals@@" not in dictionary:
679 dictionary["@@Globals@@"] = ClbrBaseClasses.ClbrBase(
680 module, "Globals", file, lineno
681 )
682 dictionary["@@Globals@@"]._addglobal(
683 Attribute(module, variable_name, file, lineno)
684 )
685 else:
686 index = -1
687 while index >= -len(classstack):
688 if classstack[index][1] >= thisindent:
689 index -= 1
690 else:
691 if isinstance(classstack[index][0], Class):
692 classstack[index][0]._addglobal(
693 Attribute(module, variable_name, file, lineno)
694 )
695 break
696
657 elif m.captured("Publics"): 697 elif m.captured("Publics"):
658 idents = m.captured("Identifiers") 698 idents = m.captured("Identifiers")
659 lineno += src.count("\n", last_lineno_pos, start) 699 lineno += src.count("\n", last_lineno_pos, start)
660 last_lineno_pos = start 700 last_lineno_pos = start
661 pubs = Publics( 701 pubs = Publics(

eric ide

mercurial