Utilities/ModuleParser.py

branch
Py2 comp.
changeset 3057
10516539f238
parent 3056
9986ec0e559a
parent 2997
7f0ef975da9e
child 3058
0a02c433f52d
equal deleted inserted replaced
3056:9986ec0e559a 3057:10516539f238
25 25
26 import Utilities 26 import Utilities
27 from functools import reduce 27 from functools import reduce
28 import Preferences 28 import Preferences
29 29
30 __all__ = ["Module", "Class", "Function", "Attribute", "RbModule", "readModule", 30 __all__ = ["Module", "Class", "Function", "Attribute", "RbModule",
31 "getTypeFromTypeName"] 31 "readModule", "getTypeFromTypeName"]
32 32
33 TABWIDTH = 4 33 TABWIDTH = 4
34 34
35 PTL_SOURCE = 128 35 PTL_SOURCE = 128
36 RB_SOURCE = 129 36 RB_SOURCE = 129
123 (?P<MethodModifierIndent> [ \t]* ) 123 (?P<MethodModifierIndent> [ \t]* )
124 (?P<MethodModifierType> @classmethod | @staticmethod ) 124 (?P<MethodModifierType> @classmethod | @staticmethod )
125 ) 125 )
126 126
127 | (?P<Method> 127 | (?P<Method>
128 (^ [ \t]* @ (?: PyQt4 \. )? (?: QtCore \. )? (?: pyqtSignature | pyqtSlot ) 128 (^ [ \t]* @ (?: PyQt4 \. )? (?: QtCore \. )?
129 (?: pyqtSignature | pyqtSlot )
129 [ \t]* \( 130 [ \t]* \(
130 (?P<MethodPyQtSignature> [^)]* ) 131 (?P<MethodPyQtSignature> [^)]* )
131 \) \s* 132 \) \s*
132 )? 133 )?
133 ^ 134 ^
215 | (?P<Method> 216 | (?P<Method>
216 ^ 217 ^
217 (?P<MethodIndent> [ \t]* ) 218 (?P<MethodIndent> [ \t]* )
218 def [ \t]+ 219 def [ \t]+
219 (?: 220 (?:
220 (?P<MethodName2> [a-zA-Z0-9_]+ (?: \. | :: ) [a-zA-Z_] [a-zA-Z0-9_?!=]* ) 221 (?P<MethodName2> [a-zA-Z0-9_]+ (?: \. | :: )
222 [a-zA-Z_] [a-zA-Z0-9_?!=]* )
221 | 223 |
222 (?P<MethodName> [a-zA-Z_] [a-zA-Z0-9_?!=]* ) 224 (?P<MethodName> [a-zA-Z_] [a-zA-Z0-9_?!=]* )
223 | 225 |
224 (?P<MethodName3> [^( \t]{1,3} ) 226 (?P<MethodName3> [^( \t]{1,3} )
225 ) 227 )
268 ^ 270 ^
269 (?P<AccessControlIndent> [ \t]* ) 271 (?P<AccessControlIndent> [ \t]* )
270 (?: 272 (?:
271 (?P<AccessControlType> private | public | protected ) [^_] 273 (?P<AccessControlType> private | public | protected ) [^_]
272 | 274 |
273 (?P<AccessControlType2> private_class_method | public_class_method ) 275 (?P<AccessControlType2>
276 private_class_method | public_class_method )
274 ) 277 )
275 \(? 278 \(?
276 [ \t]* 279 [ \t]*
277 (?P<AccessControlList> (?: : [a-zA-Z0-9_]+ , \s* )* (?: : [a-zA-Z0-9_]+ )+ )? 280 (?P<AccessControlList> (?: : [a-zA-Z0-9_]+ , \s* )*
281 (?: : [a-zA-Z0-9_]+ )+ )?
278 [ \t]* 282 [ \t]*
279 \)? 283 \)?
280 ) 284 )
281 285
282 | (?P<Attribute> 286 | (?P<Attribute>
291 (?P<AttrIndent> [ \t]* ) 295 (?P<AttrIndent> [ \t]* )
292 attr 296 attr
293 (?P<AttrType> (?: _accessor | _reader | _writer ) )? 297 (?P<AttrType> (?: _accessor | _reader | _writer ) )?
294 \(? 298 \(?
295 [ \t]* 299 [ \t]*
296 (?P<AttrList> (?: : [a-zA-Z0-9_]+ , \s* )* (?: : [a-zA-Z0-9_]+ | true | false )+ ) 300 (?P<AttrList> (?: : [a-zA-Z0-9_]+ , \s* )*
301 (?: : [a-zA-Z0-9_]+ | true | false )+ )
297 [ \t]* 302 [ \t]*
298 \)? 303 \)?
299 ) 304 )
300 305
301 | (?P<Begin> 306 | (?P<Begin>
375 """ 380 """
376 self.visibility = 2 381 self.visibility = 2
377 382
378 383
379 class Module(object): 384 class Module(object):
380 ''' 385 """
381 Class to represent a Python module. 386 Class to represent a Python module.
382 ''' 387 """
383 def __init__(self, name, file=None, type=None): 388 def __init__(self, name, file=None, type=None):
384 """ 389 """
385 Constructor 390 Constructor
386 391
387 @param name name of this module (string) 392 @param name name of this module (string)
471 """ 476 """
472 self.description = description 477 self.description = description
473 478
474 def scan(self, src): 479 def scan(self, src):
475 """ 480 """
476 Public method to scan the source text and retrieve the relevant information. 481 Public method to scan the source text and retrieve the relevant
482 information.
477 483
478 @param src the source text to be scanned (string) 484 @param src the source text to be scanned (string)
479 """ 485 """
480 if self.type in [imp.PY_SOURCE, PTL_SOURCE]: 486 if self.type in [imp.PY_SOURCE, PTL_SOURCE]:
481 self.__py_scan(src) 487 self.__py_scan(src)
495 else: 501 else:
496 object.setPublic() 502 object.setPublic()
497 503
498 def __py_scan(self, src): 504 def __py_scan(self, src):
499 """ 505 """
500 Private method to scan the source text of a Python module and retrieve the 506 Private method to scan the source text of a Python module and retrieve
501 relevant information. 507 the relevant information.
502 508
503 @param src the source text to be scanned (string) 509 @param src the source text to be scanned (string)
504 """ 510 """
505 lineno, last_lineno_pos = 1, 0 511 lineno, last_lineno_pos = 1, 0
506 lastGlobalEntry = None 512 lastGlobalEntry = None
551 modifier = Function.General 557 modifier = Function.General
552 # modify indentation level for conditional defines 558 # modify indentation level for conditional defines
553 if conditionalsstack: 559 if conditionalsstack:
554 if thisindent > conditionalsstack[-1]: 560 if thisindent > conditionalsstack[-1]:
555 if not deltaindentcalculated: 561 if not deltaindentcalculated:
556 deltastack.append(thisindent - conditionalsstack[-1]) 562 deltastack.append(
557 deltaindent = reduce(lambda x, y: x + y, deltastack) 563 thisindent - conditionalsstack[-1])
564 deltaindent = reduce(
565 lambda x, y: x + y, deltastack)
558 deltaindentcalculated = 1 566 deltaindentcalculated = 1
559 thisindent -= deltaindent 567 thisindent -= deltaindent
560 else: 568 else:
561 while conditionalsstack and \ 569 while conditionalsstack and \
562 conditionalsstack[-1] >= thisindent: 570 conditionalsstack[-1] >= thisindent:
581 if cur_class is None: 589 if cur_class is None:
582 continue 590 continue
583 591
584 if isinstance(cur_class, Class): 592 if isinstance(cur_class, Class):
585 # it's a class method 593 # it's a class method
586 f = Function(None, meth_name, None, lineno, 594 f = Function(
587 meth_sig, meth_pyqtSig, modifierType=modifier) 595 None, meth_name, None, lineno,
596 meth_sig, meth_pyqtSig, modifierType=modifier)
588 self.__py_setVisibility(f) 597 self.__py_setVisibility(f)
589 cur_class.addMethod(meth_name, f) 598 cur_class.addMethod(meth_name, f)
590 break 599 break
591 else: 600 else:
592 # it's a nested function of a module function 601 # it's a nested function of a module function
593 f = Function(self.name, meth_name, self.file, lineno, 602 f = Function(
594 meth_sig, meth_pyqtSig, modifierType=modifier) 603 self.name, meth_name, self.file, lineno,
604 meth_sig, meth_pyqtSig, modifierType=modifier)
595 self.__py_setVisibility(f) 605 self.__py_setVisibility(f)
596 self.addFunction(meth_name, f) 606 self.addFunction(meth_name, f)
597 else: 607 else:
598 # it's a module function 608 # it's a module function
599 f = Function(self.name, meth_name, self.file, lineno, 609 f = Function(self.name, meth_name, self.file, lineno,
700 last_lineno_pos = start 710 last_lineno_pos = start
701 index = -1 711 index = -1
702 while index >= -len(classstack): 712 while index >= -len(classstack):
703 if classstack[index][0] is not None: 713 if classstack[index][0] is not None:
704 attrName = m.group("AttributeName") 714 attrName = m.group("AttributeName")
705 attr = Attribute(self.name, attrName, self.file, lineno) 715 attr = Attribute(
716 self.name, attrName, self.file, lineno)
706 self.__py_setVisibility(attr) 717 self.__py_setVisibility(attr)
707 classstack[index][0].addAttribute(attrName, attr) 718 classstack[index][0].addAttribute(attrName, attr)
708 break 719 break
709 else: 720 else:
710 index -= 1 721 index -= 1
715 isSignal = m.group("VariableSignal") != "" 726 isSignal = m.group("VariableSignal") != ""
716 lineno = lineno + src.count('\n', last_lineno_pos, start) 727 lineno = lineno + src.count('\n', last_lineno_pos, start)
717 last_lineno_pos = start 728 last_lineno_pos = start
718 if thisindent == 0: 729 if thisindent == 0:
719 # global variable 730 # global variable
720 attr = Attribute(self.name, variable_name, self.file, lineno, 731 attr = Attribute(
721 isSignal=isSignal) 732 self.name, variable_name, self.file, lineno,
733 isSignal=isSignal)
722 self.__py_setVisibility(attr) 734 self.__py_setVisibility(attr)
723 self.addGlobal(variable_name, attr) 735 self.addGlobal(variable_name, attr)
724 if lastGlobalEntry: 736 if lastGlobalEntry:
725 lastGlobalEntry.setEndLine(lineno - 1) 737 lastGlobalEntry.setEndLine(lineno - 1)
726 lastGlobalEntry = None 738 lastGlobalEntry = None
730 if classstack[index][1] >= thisindent: 742 if classstack[index][1] >= thisindent:
731 index -= 1 743 index -= 1
732 else: 744 else:
733 if classstack[index][0] is not None and \ 745 if classstack[index][0] is not None and \
734 isinstance(classstack[index][0], Class): 746 isinstance(classstack[index][0], Class):
735 attr = Attribute(self.name, variable_name, self.file, 747 attr = Attribute(
736 lineno, isSignal=isSignal) 748 self.name, variable_name, self.file,
749 lineno, isSignal=isSignal)
737 self.__py_setVisibility(attr) 750 self.__py_setVisibility(attr)
738 classstack[index][0].addGlobal(variable_name, attr) 751 classstack[index][0].addGlobal(
752 variable_name, attr)
739 break 753 break
740 754
741 elif m.start("Import") >= 0: 755 elif m.start("Import") >= 0:
742 # import module 756 # import module
743 names = [n.strip() for n in "".join( 757 names = [n.strip() for n in
744 m.group("ImportList").splitlines()).replace("\\", "").split(',')] 758 "".join(m.group("ImportList").splitlines())
759 .replace("\\", "").split(',')]
745 for name in names: 760 for name in names:
746 if not name in self.imports: 761 if not name in self.imports:
747 self.imports.append(name) 762 self.imports.append(name)
748 763
749 elif m.start("ImportFrom") >= 0: 764 elif m.start("ImportFrom") >= 0:
750 # from module import stuff 765 # from module import stuff
751 mod = m.group("ImportFromPath") 766 mod = m.group("ImportFromPath")
752 names = [n.strip() for n in "".join( 767 names = [n.strip() for n in
753 m.group("ImportFromList").splitlines()).replace("\\", "").split(',')] 768 "".join(m.group("ImportFromList").splitlines())
769 .replace("\\", "").split(',')]
754 if mod not in self.from_imports: 770 if mod not in self.from_imports:
755 self.from_imports[mod] = [] 771 self.from_imports[mod] = []
756 self.from_imports[mod].extend(names) 772 self.from_imports[mod].extend(names)
757 773
758 elif m.start("ConditionalDefine") >= 0: 774 elif m.start("ConditionalDefine") >= 0:
771 787
772 modulelevel = 0 788 modulelevel = 0
773 789
774 def __rb_scan(self, src): 790 def __rb_scan(self, src):
775 """ 791 """
776 Private method to scan the source text of a Python module and retrieve the 792 Private method to scan the source text of a Python module and retrieve
777 relevant information. 793 the relevant information.
778 794
779 @param src the source text to be scanned (string) 795 @param src the source text to be scanned (string)
780 """ 796 """
781 lineno, last_lineno_pos = 1, 0 797 lineno, last_lineno_pos = 1, 0
782 classstack = [] # stack of (class, indent) pairs 798 classstack = [] # stack of (class, indent) pairs
808 meth_name = meth_name[6:] 824 meth_name = meth_name[6:]
809 # close all classes/modules indented at least as much 825 # close all classes/modules indented at least as much
810 while classstack and \ 826 while classstack and \
811 classstack[-1][1] >= thisindent: 827 classstack[-1][1] >= thisindent:
812 if classstack[-1][0] is not None and \ 828 if classstack[-1][0] is not None and \
813 isinstance(classstack[-1][0], (Class, Function, RbModule)): 829 isinstance(classstack[-1][0],
830 (Class, Function, RbModule)):
814 # record the end line of this class, function or module 831 # record the end line of this class, function or module
815 classstack[-1][0].setEndLine(lineno - 1) 832 classstack[-1][0].setEndLine(lineno - 1)
816 del classstack[-1] 833 del classstack[-1]
817 while acstack and \ 834 while acstack and \
818 acstack[-1][1] >= thisindent: 835 acstack[-1][1] >= thisindent:
833 None, lineno, meth_sig) 850 None, lineno, meth_sig)
834 cur_class.addMethod(meth_name, f) 851 cur_class.addMethod(meth_name, f)
835 break 852 break
836 else: 853 else:
837 # it's a nested function of a module function 854 # it's a nested function of a module function
838 f = Function(self.name, meth_name, self.file, lineno, meth_sig) 855 f = Function(
856 self.name, meth_name, self.file, lineno, meth_sig)
839 self.addFunction(meth_name, f) 857 self.addFunction(meth_name, f)
840 # set access control 858 # set access control
841 if acstack: 859 if acstack:
842 accesscontrol = acstack[-1][0] 860 accesscontrol = acstack[-1][0]
843 if accesscontrol == "private": 861 if accesscontrol == "private":
846 f.setProtected() 864 f.setProtected()
847 elif accesscontrol == "public": 865 elif accesscontrol == "public":
848 f.setPublic() 866 f.setPublic()
849 else: 867 else:
850 # it's a function 868 # it's a function
851 f = Function(self.name, meth_name, self.file, lineno, meth_sig) 869 f = Function(
870 self.name, meth_name, self.file, lineno, meth_sig)
852 self.addFunction(meth_name, f) 871 self.addFunction(meth_name, f)
853 if not classstack: 872 if not classstack:
854 if lastGlobalEntry: 873 if lastGlobalEntry:
855 lastGlobalEntry.setEndLine(lineno - 1) 874 lastGlobalEntry.setEndLine(lineno - 1)
856 lastGlobalEntry = f 875 lastGlobalEntry = f
883 last_lineno_pos = start 902 last_lineno_pos = start
884 # close all classes/modules indented at least as much 903 # close all classes/modules indented at least as much
885 while classstack and \ 904 while classstack and \
886 classstack[-1][1] >= thisindent: 905 classstack[-1][1] >= thisindent:
887 if classstack[-1][0] is not None and \ 906 if classstack[-1][0] is not None and \
888 isinstance(classstack[-1][0], (Class, Function, RbModule)): 907 isinstance(classstack[-1][0],
908 (Class, Function, RbModule)):
889 # record the end line of this class, function or module 909 # record the end line of this class, function or module
890 classstack[-1][0].setEndLine(lineno - 1) 910 classstack[-1][0].setEndLine(lineno - 1)
891 del classstack[-1] 911 del classstack[-1]
892 class_name = m.group("ClassName") or m.group("ClassName2") 912 class_name = m.group("ClassName") or m.group("ClassName2")
893 inherit = m.group("ClassSupers") 913 inherit = m.group("ClassSupers")
931 last_lineno_pos = start 951 last_lineno_pos = start
932 # close all classes/modules indented at least as much 952 # close all classes/modules indented at least as much
933 while classstack and \ 953 while classstack and \
934 classstack[-1][1] >= thisindent: 954 classstack[-1][1] >= thisindent:
935 if classstack[-1][0] is not None and \ 955 if classstack[-1][0] is not None and \
936 isinstance(classstack[-1][0], (Class, Function, RbModule)): 956 isinstance(classstack[-1][0],
957 (Class, Function, RbModule)):
937 # record the end line of this class, function or module 958 # record the end line of this class, function or module
938 classstack[-1][0].setEndLine(lineno - 1) 959 classstack[-1][0].setEndLine(lineno - 1)
939 del classstack[-1] 960 del classstack[-1]
940 module_name = m.group("ModuleName") 961 module_name = m.group("ModuleName")
941 # remember this class 962 # remember this class
962 aclist = m.group("AccessControlList") 983 aclist = m.group("AccessControlList")
963 if aclist is None: 984 if aclist is None:
964 index = -1 985 index = -1
965 while index >= -len(acstack): 986 while index >= -len(acstack):
966 if acstack[index][1] < indent: 987 if acstack[index][1] < indent:
967 actype = m.group("AccessControlType") or \ 988 actype = \
968 m.group("AccessControlType2").split('_')[0] 989 m.group("AccessControlType") or \
990 m.group("AccessControlType2").split('_')[0]
969 acstack[index][0] = actype.lower() 991 acstack[index][0] = actype.lower()
970 break 992 break
971 else: 993 else:
972 index -= 1 994 index -= 1
973 else: 995 else:
975 while index >= -len(classstack): 997 while index >= -len(classstack):
976 if classstack[index][0] is not None and \ 998 if classstack[index][0] is not None and \
977 not isinstance(classstack[index][0], Function) and \ 999 not isinstance(classstack[index][0], Function) and \
978 not classstack[index][1] >= indent: 1000 not classstack[index][1] >= indent:
979 parent = classstack[index][0] 1001 parent = classstack[index][0]
980 actype = m.group("AccessControlType") or \ 1002 actype = \
981 m.group("AccessControlType2").split('_')[0] 1003 m.group("AccessControlType") or \
1004 m.group("AccessControlType2").split('_')[0]
982 actype = actype.lower() 1005 actype = actype.lower()
983 for name in aclist.split(","): 1006 for name in aclist.split(","):
984 name = name.strip()[1:] # get rid of leading ':' 1007 # get rid of leading ':'
1008 name = name.strip()[1:]
985 acmeth = parent.getMethod(name) 1009 acmeth = parent.getMethod(name)
986 if acmeth is None: 1010 if acmeth is None:
987 continue 1011 continue
988 if actype == "private": 1012 if actype == "private":
989 acmeth.setPrivate() 1013 acmeth.setPrivate()
1002 while index >= -len(classstack): 1026 while index >= -len(classstack):
1003 if classstack[index][0] is not None and \ 1027 if classstack[index][0] is not None and \
1004 not isinstance(classstack[index][0], Function) and \ 1028 not isinstance(classstack[index][0], Function) and \
1005 not classstack[index][1] >= indent: 1029 not classstack[index][1] >= indent:
1006 attrName = m.group("AttributeName") 1030 attrName = m.group("AttributeName")
1007 attr = Attribute(self.name, attrName, self.file, lineno) 1031 attr = Attribute(
1032 self.name, attrName, self.file, lineno)
1008 if attrName.startswith("@@") or attrName[0].isupper(): 1033 if attrName.startswith("@@") or attrName[0].isupper():
1009 classstack[index][0].addGlobal(attrName, attr) 1034 classstack[index][0].addGlobal(attrName, attr)
1010 else: 1035 else:
1011 classstack[index][0].addAttribute(attrName, attr) 1036 classstack[index][0].addAttribute(attrName, attr)
1012 break 1037 break
1013 else: 1038 else:
1014 index -= 1 1039 index -= 1
1015 else: 1040 else:
1016 attrName = m.group("AttributeName") 1041 attrName = m.group("AttributeName")
1017 if attrName[0] != "@": 1042 if attrName[0] != "@":
1018 attr = Attribute(self.name, attrName, self.file, lineno) 1043 attr = Attribute(
1044 self.name, attrName, self.file, lineno)
1019 self.addGlobal(attrName, attr) 1045 self.addGlobal(attrName, attr)
1020 if lastGlobalEntry: 1046 if lastGlobalEntry:
1021 lastGlobalEntry.setEndLine(lineno - 1) 1047 lastGlobalEntry.setEndLine(lineno - 1)
1022 lastGlobalEntry = None 1048 lastGlobalEntry = None
1023 1049
1032 parent = classstack[index][0] 1058 parent = classstack[index][0]
1033 if m.group("AttrType") is None: 1059 if m.group("AttrType") is None:
1034 nv = m.group("AttrList").split(",") 1060 nv = m.group("AttrList").split(",")
1035 if not nv: 1061 if not nv:
1036 break 1062 break
1037 name = nv[0].strip()[1:] # get rid of leading ':' 1063 # get rid of leading ':'
1064 name = nv[0].strip()[1:]
1038 attr = parent.getAttribute("@" + name) or \ 1065 attr = parent.getAttribute("@" + name) or \
1039 parent.getAttribute("@@" + name) or \ 1066 parent.getAttribute("@@" + name) or \
1040 Attribute(self.name, "@" + name, self.file, lineno) 1067 Attribute(
1068 self.name, "@" + name, self.file, lineno)
1041 if len(nv) == 1 or nv[1].strip() == "false": 1069 if len(nv) == 1 or nv[1].strip() == "false":
1042 attr.setProtected() 1070 attr.setProtected()
1043 elif nv[1].strip() == "true": 1071 elif nv[1].strip() == "true":
1044 attr.setPublic() 1072 attr.setPublic()
1045 parent.addAttribute(attr.name, attr) 1073 parent.addAttribute(attr.name, attr)
1046 else: 1074 else:
1047 access = m.group("AttrType") 1075 access = m.group("AttrType")
1048 for name in m.group("AttrList").split(","): 1076 for name in m.group("AttrList").split(","):
1049 name = name.strip()[1:] # get rid of leading ':' 1077 # get rid of leading ':'
1078 name = name.strip()[1:]
1050 attr = parent.getAttribute("@" + name) or \ 1079 attr = parent.getAttribute("@" + name) or \
1051 parent.getAttribute("@@" + name) or \ 1080 parent.getAttribute("@@" + name) or \
1052 Attribute(self.name, "@" + name, self.file, lineno) 1081 Attribute(
1082 self.name, "@" + name, self.file,
1083 lineno)
1053 if access == "_accessor": 1084 if access == "_accessor":
1054 attr.setPublic() 1085 attr.setPublic()
1055 elif access == "_reader" or access == "_writer": 1086 elif access == "_reader" or \
1087 access == "_writer":
1056 if attr.isPrivate(): 1088 if attr.isPrivate():
1057 attr.setProtected() 1089 attr.setProtected()
1058 elif attr.isProtected(): 1090 elif attr.isProtected():
1059 attr.setPublic() 1091 attr.setPublic()
1060 parent.addAttribute(attr.name, attr) 1092 parent.addAttribute(attr.name, attr)
1083 else: 1115 else:
1084 assert 0, "regexp _getnext found something unexpected" 1116 assert 0, "regexp _getnext found something unexpected"
1085 1117
1086 def createHierarchy(self): 1118 def createHierarchy(self):
1087 """ 1119 """
1088 Public method to build the inheritance hierarchy for all classes of this module. 1120 Public method to build the inheritance hierarchy for all classes of
1121 this module.
1089 1122
1090 @return A dictionary with inheritance hierarchies. 1123 @return A dictionary with inheritance hierarchies.
1091 """ 1124 """
1092 hierarchy = {} 1125 hierarchy = {}
1093 for cls in list(list(self.classes.keys())): 1126 for cls in list(list(self.classes.keys())):
1116 for cls in classes[name].super: 1149 for cls in classes[name].super:
1117 if cls not in classes: 1150 if cls not in classes:
1118 rv[cls] = {} 1151 rv[cls] = {}
1119 exhausted = path + [cls] 1152 exhausted = path + [cls]
1120 exhausted.reverse() 1153 exhausted.reverse()
1121 self.addPathToHierarchy(exhausted, result, self.addPathToHierarchy) 1154 self.addPathToHierarchy(
1155 exhausted, result, self.addPathToHierarchy)
1122 else: 1156 else:
1123 rv[cls] = self.assembleHierarchy(cls, 1157 rv[cls] = self.assembleHierarchy(cls,
1124 classes, path + [cls], result) 1158 classes, path + [cls], result)
1125 1159
1126 if len(rv) == 0: 1160 if len(rv) == 0:
1179 type = "" 1213 type = ""
1180 return type 1214 return type
1181 1215
1182 1216
1183 class Class(VisibilityBase): 1217 class Class(VisibilityBase):
1184 ''' 1218 """
1185 Class to represent a Python class. 1219 Class to represent a Python class.
1186 ''' 1220 """
1187 def __init__(self, module, name, super, file, lineno): 1221 def __init__(self, module, name, super, file, lineno):
1188 """ 1222 """
1189 Constructor 1223 Constructor
1190 1224
1191 @param module name of module containing this class (string) 1225 @param module name of module containing this class (string)
1282 """ 1316 """
1283 self.endlineno = endLineNo 1317 self.endlineno = endLineNo
1284 1318
1285 1319
1286 class RbModule(Class): 1320 class RbModule(Class):
1287 ''' 1321 """
1288 Class to represent a Ruby module. 1322 Class to represent a Ruby module.
1289 ''' 1323 """
1290 def __init__(self, module, name, file, lineno): 1324 def __init__(self, module, name, file, lineno):
1291 """ 1325 """
1292 Constructor 1326 Constructor
1293 1327
1294 @param module name of module containing this class (string) 1328 @param module name of module containing this class (string)
1308 """ 1342 """
1309 self.classes[name] = _class 1343 self.classes[name] = _class
1310 1344
1311 1345
1312 class Function(VisibilityBase): 1346 class Function(VisibilityBase):
1313 ''' 1347 """
1314 Class to represent a Python function or method. 1348 Class to represent a Python function or method.
1315 ''' 1349 """
1316 General = 0 1350 General = 0
1317 Static = 1 1351 Static = 1
1318 Class = 2 1352 Class = 2
1319 1353
1320 def __init__(self, module, name, file, lineno, signature='', pyqtSignature=None, 1354 def __init__(self, module, name, file, lineno, signature='',
1321 modifierType=General): 1355 pyqtSignature=None, modifierType=General):
1322 """ 1356 """
1323 Constructor 1357 Constructor
1324 1358
1325 @param module name of module containing this function (string) 1359 @param module name of module containing this function (string)
1326 @param name name of the function (string) 1360 @param name name of the function (string)
1358 """ 1392 """
1359 self.endlineno = endLineNo 1393 self.endlineno = endLineNo
1360 1394
1361 1395
1362 class Attribute(VisibilityBase): 1396 class Attribute(VisibilityBase):
1363 ''' 1397 """
1364 Class to represent a Python function or method. 1398 Class to represent a Python function or method.
1365 ''' 1399 """
1366 def __init__(self, module, name, file, lineno, isSignal=False): 1400 def __init__(self, module, name, file, lineno, isSignal=False):
1367 """ 1401 """
1368 Constructor 1402 Constructor
1369 1403
1370 @param module name of module containing this function (string) 1404 @param module name of module containing this function (string)
1383 1417
1384 def addAssignment(self, lineno): 1418 def addAssignment(self, lineno):
1385 """ 1419 """
1386 Public method to add another assignment line number. 1420 Public method to add another assignment line number.
1387 1421
1388 @param lineno linenumber of the additional attribute assignment (integer) 1422 @param lineno linenumber of the additional attribute assignment
1423 (integer)
1389 """ 1424 """
1390 if lineno not in self.linenos: 1425 if lineno not in self.linenos:
1391 self.linenos.append(lineno) 1426 self.linenos.append(lineno)
1392 1427
1393 1428
1394 def readModule(module, path=[], inpackage=False, basename="", 1429 def readModule(module, path=[], inpackage=False, basename="",
1395 extensions=None, caching=True): 1430 extensions=None, caching=True):
1396 ''' 1431 """
1397 Function to read a module file and parse it. 1432 Function to read a module file and parse it.
1398 1433
1399 The module is searched in path and sys.path, read and parsed. 1434 The module is searched in path and sys.path, read and parsed.
1400 If the module was parsed before, the information is taken 1435 If the module was parsed before, the information is taken
1401 from a cache in order to speed up processing. 1436 from a cache in order to speed up processing.
1410 source file extensions (list of strings) 1445 source file extensions (list of strings)
1411 @param caching flag indicating that the parsed module should be 1446 @param caching flag indicating that the parsed module should be
1412 cached (boolean) 1447 cached (boolean)
1413 @return reference to a Module object containing the parsed 1448 @return reference to a Module object containing the parsed
1414 module information (Module) 1449 module information (Module)
1415 ''' 1450 """
1416 global _modules 1451 global _modules
1417 1452
1418 if extensions is None: 1453 if extensions is None:
1419 _extensions = ['.py', '.pyw', '.ptl', '.rb'] 1454 _extensions = ['.py', '.pyw', '.ptl', '.rb']
1420 else: 1455 else:
1462 1497
1463 # search the path for the module 1498 # search the path for the module
1464 f = None 1499 f = None
1465 if inpackage: 1500 if inpackage:
1466 try: 1501 try:
1467 f, file, (suff, mode, type) = find_module(module, path, _extensions) 1502 f, file, (suff, mode, type) = find_module(
1503 module, path, _extensions)
1468 except ImportError: 1504 except ImportError:
1469 f = None 1505 f = None
1470 if f is None: 1506 if f is None:
1471 fullpath = list(path) + sys.path 1507 fullpath = list(path) + sys.path
1472 f, file, (suff, mode, type) = find_module(module, fullpath, _extensions) 1508 f, file, (suff, mode, type) = find_module(
1509 module, fullpath, _extensions)
1473 if f: 1510 if f:
1474 f.close() 1511 f.close()
1475 if type not in SUPPORTED_TYPES: 1512 if type not in SUPPORTED_TYPES:
1476 # not supported source, can't do anything with this module 1513 # not supported source, can't do anything with this module
1477 _modules[modname] = Module(modname, None, None) 1514 _modules[modname] = Module(modname, None, None)
1520 for p in path: # only search in path 1557 for p in path: # only search in path
1521 if os.path.exists(os.path.join(p, name)): 1558 if os.path.exists(os.path.join(p, name)):
1522 pathname = os.path.join(p, name) 1559 pathname = os.path.join(p, name)
1523 if ext == '.ptl': 1560 if ext == '.ptl':
1524 # Quixote page template 1561 # Quixote page template
1525 return (open(pathname), pathname, ('.ptl', 'r', PTL_SOURCE)) 1562 return (open(pathname), pathname,
1563 ('.ptl', 'r', PTL_SOURCE))
1526 elif ext == '.rb': 1564 elif ext == '.rb':
1527 # Ruby source file 1565 # Ruby source file
1528 return (open(pathname), pathname, ('.rb', 'r', RB_SOURCE)) 1566 return (open(pathname), pathname,
1567 ('.rb', 'r', RB_SOURCE))
1529 else: 1568 else:
1530 return (open(pathname), pathname, (ext, 'r', imp.PY_SOURCE)) 1569 return (open(pathname), pathname,
1570 (ext, 'r', imp.PY_SOURCE))
1531 raise ImportError 1571 raise ImportError
1532 1572
1533 # standard Python module file 1573 # standard Python module file
1534 if name.lower().endswith('.py'): 1574 if name.lower().endswith('.py'):
1535 name = name[:-3] 1575 name = name[:-3]
1562 if basename: 1602 if basename:
1563 module = module.replace(basename, "") 1603 module = module.replace(basename, "")
1564 modname = module.replace(os.sep, '.') 1604 modname = module.replace(os.sep, '.')
1565 else: 1605 else:
1566 modname = os.path.basename(module) 1606 modname = os.path.basename(module)
1567 if modname.lower().endswith(".ptl") or modname.lower().endswith(".pyw"): 1607 if modname.lower().endswith(".ptl") or \
1608 modname.lower().endswith(".pyw"):
1568 modname = modname[:-4] 1609 modname = modname[:-4]
1569 elif modname.lower().endswith(".rb"): 1610 elif modname.lower().endswith(".rb"):
1570 modname = modname[:-3] 1611 modname = modname[:-3]
1571 module = os.path.basename(module) 1612 module = os.path.basename(module)
1572 1613

eric ide

mercurial