eric6/Utilities/ModuleParser.py

changeset 8207
d359172d11be
parent 8205
4a0f1f896341
child 8240
93b8a353c4bf
equal deleted inserted replaced
8206:adf11836cfce 8207:d359172d11be
347 _commentsub = re.compile(r"""#[^\n]*\n|#[^\n]*$""").sub 347 _commentsub = re.compile(r"""#[^\n]*\n|#[^\n]*$""").sub
348 348
349 _modules = {} # cache of modules we've seen 349 _modules = {} # cache of modules we've seen
350 350
351 351
352 class VisibilityBase(object): 352 class VisibilityBase:
353 """ 353 """
354 Class implementing the visibility aspect of all objects. 354 Class implementing the visibility aspect of all objects.
355 """ 355 """
356 def isPrivate(self): 356 def isPrivate(self):
357 """ 357 """
394 Public method to set the visibility to Public. 394 Public method to set the visibility to Public.
395 """ 395 """
396 self.visibility = 2 396 self.visibility = 2
397 397
398 398
399 class Module(object): 399 class Module:
400 """ 400 """
401 Class to represent a Python module. 401 Class to represent a Python module.
402 """ 402 """
403 def __init__(self, name, file=None, moduleType=None): 403 def __init__(self, name, file=None, moduleType=None):
404 """ 404 """
591 .split('name')[0] 591 .split('name')[0]
592 .strip("\"', \t") 592 .strip("\"', \t")
593 ) 593 )
594 else: 594 else:
595 meth_pyqtSig = None 595 meth_pyqtSig = None
596 lineno = lineno + src.count('\n', last_lineno_pos, start) 596 lineno += src.count('\n', last_lineno_pos, start)
597 last_lineno_pos = start 597 last_lineno_pos = start
598 if modifierType and modifierIndent == thisindent: 598 if modifierType and modifierIndent == thisindent:
599 if modifierType == "@staticmethod": 599 if modifierType == "@staticmethod":
600 modifier = Function.Static 600 modifier = Function.Static
601 elif modifierType == "@classmethod": 601 elif modifierType == "@classmethod":
706 cur_obj.addDescription(contents) 706 cur_obj.addDescription(contents)
707 707
708 elif m.start("Class") >= 0: 708 elif m.start("Class") >= 0:
709 # we found a class definition 709 # we found a class definition
710 thisindent = _indent(m.group("ClassIndent")) 710 thisindent = _indent(m.group("ClassIndent"))
711 lineno = lineno + src.count('\n', last_lineno_pos, start) 711 lineno += src.count('\n', last_lineno_pos, start)
712 last_lineno_pos = start 712 last_lineno_pos = start
713 # close all classes indented at least as much 713 # close all classes indented at least as much
714 while classstack and classstack[-1][1] >= thisindent: 714 while classstack and classstack[-1][1] >= thisindent:
715 del classstack[-1] 715 del classstack[-1]
716 class_name = m.group("ClassName") 716 class_name = m.group("ClassName")
770 self.addClass(class_name, cur_class) 770 self.addClass(class_name, cur_class)
771 # add nested classes to the module 771 # add nested classes to the module
772 classstack.append((cur_class, thisindent)) 772 classstack.append((cur_class, thisindent))
773 773
774 elif m.start("Attribute") >= 0: 774 elif m.start("Attribute") >= 0:
775 lineno = lineno + src.count('\n', last_lineno_pos, start) 775 lineno += src.count('\n', last_lineno_pos, start)
776 last_lineno_pos = start 776 last_lineno_pos = start
777 index = -1 777 index = -1
778 while index >= -len(classstack): 778 while index >= -len(classstack):
779 if classstack[index][0] is not None: 779 if classstack[index][0] is not None:
780 attrName = m.group("AttributeName") 780 attrName = m.group("AttributeName")
786 else: 786 else:
787 index -= 1 787 index -= 1
788 788
789 elif m.start("Main") >= 0: 789 elif m.start("Main") >= 0:
790 # 'main' part of the script, reset class stack 790 # 'main' part of the script, reset class stack
791 lineno = lineno + src.count('\n', last_lineno_pos, start) 791 lineno += src.count('\n', last_lineno_pos, start)
792 last_lineno_pos = start 792 last_lineno_pos = start
793 classstack = [] 793 classstack = []
794 794
795 elif m.start("Variable") >= 0: 795 elif m.start("Variable") >= 0:
796 thisindent = _indent(m.group("VariableIndent")) 796 thisindent = _indent(m.group("VariableIndent"))
797 variable_name = m.group("VariableName") 797 variable_name = m.group("VariableName")
798 isSignal = m.group("VariableSignal") != "" 798 isSignal = m.group("VariableSignal") != ""
799 lineno = lineno + src.count('\n', last_lineno_pos, start) 799 lineno += src.count('\n', last_lineno_pos, start)
800 last_lineno_pos = start 800 last_lineno_pos = start
801 if thisindent == 0: 801 if thisindent == 0:
802 # global variable 802 # global variable
803 attr = Attribute( 803 attr = Attribute(
804 self.name, variable_name, self.file, lineno, 804 self.name, variable_name, self.file, lineno,
861 if deltastack: 861 if deltastack:
862 del deltastack[-1] 862 del deltastack[-1]
863 conditionalsstack.append(thisindent) 863 conditionalsstack.append(thisindent)
864 deltaindentcalculated = 0 864 deltaindentcalculated = 0
865 865
866 elif m.start("Comment") >= 0: 866 elif m.start("Comment") >= 0 and modulelevel:
867 if modulelevel: 867 continue
868 continue
869 868
870 modulelevel = False 869 modulelevel = False
871 870
872 def __rb_scan(self, src): 871 def __rb_scan(self, src):
873 """ 872 """
899 m.group("MethodName2") or 898 m.group("MethodName2") or
900 m.group("MethodName3") 899 m.group("MethodName3")
901 ) 900 )
902 meth_sig = m.group("MethodSignature") 901 meth_sig = m.group("MethodSignature")
903 meth_sig = meth_sig and meth_sig.replace('\\\n', '') or '' 902 meth_sig = meth_sig and meth_sig.replace('\\\n', '') or ''
904 lineno = lineno + src.count('\n', last_lineno_pos, start) 903 lineno += src.count('\n', last_lineno_pos, start)
905 last_lineno_pos = start 904 last_lineno_pos = start
906 if meth_name.startswith('self.'): 905 if meth_name.startswith('self.'):
907 meth_name = meth_name[5:] 906 meth_name = meth_name[5:]
908 elif meth_name.startswith('self::'): 907 elif meth_name.startswith('self::'):
909 meth_name = meth_name[6:] 908 meth_name = meth_name[6:]
967 if contents is not None: 966 if contents is not None:
968 contents = _hashsub(r"\1", contents) 967 contents = _hashsub(r"\1", contents)
969 if cur_obj: 968 if cur_obj:
970 cur_obj.addDescription(contents) 969 cur_obj.addDescription(contents)
971 970
972 elif m.start("String") >= 0:
973 pass
974
975 elif m.start("Comment") >= 0:
976 pass
977
978 elif m.start("ClassIgnored") >= 0:
979 pass
980
981 elif m.start("Class") >= 0: 971 elif m.start("Class") >= 0:
982 # we found a class definition 972 # we found a class definition
983 thisindent = indent 973 thisindent = indent
984 indent += 1 974 indent += 1
985 lineno = lineno + src.count('\n', last_lineno_pos, start) 975 lineno += src.count('\n', last_lineno_pos, start)
986 last_lineno_pos = start 976 last_lineno_pos = start
987 # close all classes/modules indented at least as much 977 # close all classes/modules indented at least as much
988 while classstack and classstack[-1][1] >= thisindent: 978 while classstack and classstack[-1][1] >= thisindent:
989 if ( 979 if (
990 classstack[-1][0] is not None and 980 classstack[-1][0] is not None and
1031 1021
1032 elif m.start("Module") >= 0: 1022 elif m.start("Module") >= 0:
1033 # we found a module definition 1023 # we found a module definition
1034 thisindent = indent 1024 thisindent = indent
1035 indent += 1 1025 indent += 1
1036 lineno = lineno + src.count('\n', last_lineno_pos, start) 1026 lineno += src.count('\n', last_lineno_pos, start)
1037 last_lineno_pos = start 1027 last_lineno_pos = start
1038 # close all classes/modules indented at least as much 1028 # close all classes/modules indented at least as much
1039 while classstack and classstack[-1][1] >= thisindent: 1029 while classstack and classstack[-1][1] >= thisindent:
1040 if ( 1030 if (
1041 classstack[-1][0] is not None and 1031 classstack[-1][0] is not None and
1108 break 1098 break
1109 else: 1099 else:
1110 index -= 1 1100 index -= 1
1111 1101
1112 elif m.start("Attribute") >= 0: 1102 elif m.start("Attribute") >= 0:
1113 lineno = lineno + src.count('\n', last_lineno_pos, start) 1103 lineno += src.count('\n', last_lineno_pos, start)
1114 last_lineno_pos = start 1104 last_lineno_pos = start
1115 index = -1 1105 index = -1
1116 while index >= -len(classstack): 1106 while index >= -len(classstack):
1117 if ( 1107 if (
1118 classstack[index][0] is not None and 1108 classstack[index][0] is not None and
1138 if lastGlobalEntry: 1128 if lastGlobalEntry:
1139 lastGlobalEntry.setEndLine(lineno - 1) 1129 lastGlobalEntry.setEndLine(lineno - 1)
1140 lastGlobalEntry = None 1130 lastGlobalEntry = None
1141 1131
1142 elif m.start("Attr") >= 0: 1132 elif m.start("Attr") >= 0:
1143 lineno = lineno + src.count('\n', last_lineno_pos, start) 1133 lineno += src.count('\n', last_lineno_pos, start)
1144 last_lineno_pos = start 1134 last_lineno_pos = start
1145 index = -1 1135 index = -1
1146 while index >= -len(classstack): 1136 while index >= -len(classstack):
1147 if ( 1137 if (
1148 classstack[index][0] is not None and 1138 classstack[index][0] is not None and
1204 # it's a class/module method 1194 # it's a class/module method
1205 indent = classstack[-1][1] 1195 indent = classstack[-1][1]
1206 else: 1196 else:
1207 indent = 0 1197 indent = 0
1208 1198
1209 elif m.start("BeginEnd") >= 0: 1199 elif (
1200 m.start("String") >= 0 or
1201 m.start("Comment") >= 0 or
1202 m.start("ClassIgnored") >= 0 or
1203 m.start("BeginEnd") >= 0
1204 ):
1210 pass 1205 pass
1211 1206
1212 def createHierarchy(self): 1207 def createHierarchy(self):
1213 """ 1208 """
1214 Public method to build the inheritance hierarchy for all classes of 1209 Public method to build the inheritance hierarchy for all classes of
1654 for p in path: # only search in path 1649 for p in path: # only search in path
1655 if os.path.exists(os.path.join(p, name)): 1650 if os.path.exists(os.path.join(p, name)):
1656 pathname = os.path.join(p, name) 1651 pathname = os.path.join(p, name)
1657 if ext == '.ptl': 1652 if ext == '.ptl':
1658 # Quixote page template 1653 # Quixote page template
1659 return (open(pathname), pathname, 1654 return (
1660 # __IGNORE_WARNING_Y115__ 1655 open(pathname), pathname,
1661 ('.ptl', 'r', PTL_SOURCE)) 1656 # __IGNORE_WARNING_Y115__
1657 ('.ptl', 'r', PTL_SOURCE)
1658 )
1662 elif ext == '.rb': 1659 elif ext == '.rb':
1663 # Ruby source file 1660 # Ruby source file
1664 return (open(pathname), pathname, 1661 return (
1665 # __IGNORE_WARNING_Y115__ 1662 open(pathname), pathname,
1666 ('.rb', 'r', RB_SOURCE)) 1663 # __IGNORE_WARNING_Y115__
1664 ('.rb', 'r', RB_SOURCE)
1665 )
1667 else: 1666 else:
1668 return (open(pathname), pathname, 1667 return (
1669 # __IGNORE_WARNING_Y115__ 1668 open(pathname), pathname,
1670 (ext, 'r', PY_SOURCE)) 1669 # __IGNORE_WARNING_Y115__
1670 (ext, 'r', PY_SOURCE)
1671 )
1671 raise ImportError 1672 raise ImportError
1672 1673
1673 # standard Python module file 1674 # standard Python module file
1674 if name.lower().endswith('.py'): 1675 if name.lower().endswith('.py'):
1675 name = name[:-3] 1676 name = name[:-3]

eric ide

mercurial