src/eric7/Utilities/ClassBrowsers/__init__.py

branch
eric7
changeset 9490
77b8d3a635b7
parent 9482
a2bc06a54d9d
child 9497
8beca4047c53
equal deleted inserted replaced
9489:c03b8323d11a 9490:77b8d3a635b7
27 PTL_SOURCE = 128 27 PTL_SOURCE = 128
28 RB_SOURCE = 129 28 RB_SOURCE = 129
29 IDL_SOURCE = 130 29 IDL_SOURCE = 130
30 JS_SOURCE = 131 30 JS_SOURCE = 131
31 PROTO_SOURCE = 132 31 PROTO_SOURCE = 132
32 UNKNOWN_SOURCE = 255
32 33
33 SUPPORTED_TYPES = [ 34 SUPPORTED_TYPES = [
34 PY_SOURCE, 35 PY_SOURCE,
35 PTL_SOURCE, 36 PTL_SOURCE,
36 RB_SOURCE, 37 RB_SOURCE,
46 "JavaScript": [".js"], 47 "JavaScript": [".js"],
47 "ProtoBuf": [".proto"], 48 "ProtoBuf": [".proto"],
48 } 49 }
49 50
50 51
52 def getClassBrowserModule(moduleType):
53 """
54 Function to import a class browser module.
55
56 @param moduleType type of class browser to load
57 @type str
58 @return reference to the imported class browser module
59 @rtype module
60 """
61 typeMapping = {
62 "idl": "idlclbr",
63 "javascript": "jsclbr",
64 "protobuf": "protoclbr",
65 "python": "pyclbr",
66 "ruby": "rbclbr",
67 }
68
69 if moduleType in typeMapping:
70 mod = importlib.import_module(
71 "eric7.Utilities.ClassBrowsers.{0}".format(typeMapping[moduleType])
72 )
73 return mod
74
75 return None
76
77
51 def readmodule(module, path=None, isPyFile=False): 78 def readmodule(module, path=None, isPyFile=False):
52 """ 79 """
53 Read a source file and return a dictionary of classes, functions, modules, 80 Function to read a source file and return a dictionary of classes, functions,
54 etc. . 81 modules, etc. .
55 82
56 The real work of parsing the source file is delegated to the individual 83 The real work of parsing the source file is delegated to the individual
57 file parsers. 84 file parsers.
58 85
59 @param module name of the source file 86 @param module name of the source file
67 """ 94 """
68 ext = os.path.splitext(module)[1].lower() 95 ext = os.path.splitext(module)[1].lower()
69 path = [] if path is None else path[:] 96 path = [] if path is None else path[:]
70 97
71 if ext in __extensions["IDL"]: 98 if ext in __extensions["IDL"]:
72 from . import idlclbr # __IGNORE_WARNING_I101__ 99 moduleType = "idl"
73
74 dictionary = idlclbr.readmodule_ex(module, path)
75 idlclbr._modules.clear()
76 elif ext in __extensions["ProtoBuf"]: 100 elif ext in __extensions["ProtoBuf"]:
77 from . import protoclbr # __IGNORE_WARNING_I101__ 101 moduleType = "protobuf"
78
79 dictionary = protoclbr.readmodule_ex(module, path)
80 protoclbr._modules.clear()
81 elif ext in __extensions["Ruby"]: 102 elif ext in __extensions["Ruby"]:
82 from . import rbclbr # __IGNORE_WARNING_I101__ 103 moduleType = "ruby"
83
84 dictionary = rbclbr.readmodule_ex(module, path)
85 rbclbr._modules.clear()
86 elif ext in __extensions["JavaScript"]: 104 elif ext in __extensions["JavaScript"]:
87 from . import jsclbr # __IGNORE_WARNING_I101__ 105 moduleType = "javascript"
88
89 dictionary = jsclbr.readmodule_ex(module, path)
90 jsclbr._modules.clear()
91 elif ext in Preferences.getPython("Python3Extensions") or isPyFile: 106 elif ext in Preferences.getPython("Python3Extensions") or isPyFile:
92 from . import pyclbr # __IGNORE_WARNING_I101__ 107 moduleType = "python"
93
94 dictionary = pyclbr.readmodule_ex(module, path, isPyFile=isPyFile)
95 pyclbr._modules.clear()
96 else: 108 else:
97 # try Python if it is without extension 109 # try Python if it is without extension
98 from . import pyclbr # __IGNORE_WARNING_I101__ 110 moduleType = "python"
99 111
100 dictionary = pyclbr.readmodule_ex(module, path) 112 classBrowserModule = getClassBrowserModule(moduleType)
101 pyclbr._modules.clear() 113 if classBrowserModule:
114 dictionary = classBrowserModule.readmodule_ex(module, path, isTypeFile=isPyFile)
115 classBrowserModule.clearModulesCache()
116 else:
117 dictionary = {}
102 118
103 return dictionary 119 return dictionary
104 120
105 121
106 def find_module(name, path, isPyFile=False): 122 def find_module(name, path, isPyFile=False):
107 """ 123 """
108 Module function to extend the Python module finding mechanism. 124 Function to extend the Python module finding mechanism.
109 125
110 This function searches for files in the given list of paths. If the 126 This function searches for files in the given list of paths. If the
111 file name doesn't have an extension or an extension of .py, the normal 127 file name doesn't have an extension or an extension of .py, the normal
112 Python search implemented in the imp module is used. For all other 128 Python search implemented in the imp module is used. For all other
113 supported files only the paths list is searched. 129 supported files only the paths list is searched.
124 @exception ImportError The file or module wasn't found. 140 @exception ImportError The file or module wasn't found.
125 """ 141 """
126 ext = os.path.splitext(name)[1].lower() 142 ext = os.path.splitext(name)[1].lower()
127 143
128 if ext in __extensions["Ruby"]: 144 if ext in __extensions["Ruby"]:
129 for p in path: # only search in path 145 sourceType = RB_SOURCE
130 pathname = os.path.join(p, name)
131 if os.path.exists(pathname):
132 return (open(pathname), pathname, (ext, "r", RB_SOURCE))
133 # __IGNORE_WARNING_Y115__
134 raise ImportError
135
136 elif ext in __extensions["IDL"]: 146 elif ext in __extensions["IDL"]:
137 for p in path: # only search in path 147 sourceType = IDL_SOURCE
138 pathname = os.path.join(p, name)
139 if os.path.exists(pathname):
140 return (open(pathname), pathname, (ext, "r", IDL_SOURCE))
141 # __IGNORE_WARNING_Y115__
142 raise ImportError
143
144 elif ext in __extensions["ProtoBuf"]: 148 elif ext in __extensions["ProtoBuf"]:
145 for p in path: # only search in path 149 sourceType = PROTO_SOURCE
146 pathname = os.path.join(p, name)
147 if os.path.exists(pathname):
148 return (open(pathname), pathname, (ext, "r", PROTO_SOURCE))
149 # __IGNORE_WARNING_Y115__
150 raise ImportError
151
152 elif ext in __extensions["JavaScript"]: 150 elif ext in __extensions["JavaScript"]:
153 for p in path: # only search in path 151 sourceType = JS_SOURCE
154 pathname = os.path.join(p, name)
155 if os.path.exists(pathname):
156 return (open(pathname), pathname, (ext, "r", JS_SOURCE))
157 # __IGNORE_WARNING_Y115__
158 raise ImportError
159
160 elif ext == ".ptl": 152 elif ext == ".ptl":
161 for p in path: # only search in path 153 sourceType = PTL_SOURCE
162 pathname = os.path.join(p, name)
163 if os.path.exists(pathname):
164 return (open(pathname), pathname, (ext, "r", PTL_SOURCE))
165 # __IGNORE_WARNING_Y115__
166 raise ImportError
167
168 elif ( 154 elif (
169 name.lower().endswith(tuple(Preferences.getPython("Python3Extensions"))) 155 name.lower().endswith(tuple(Preferences.getPython("Python3Extensions")))
170 or isPyFile 156 or isPyFile
171 ): 157 ):
158 sourceType = PY_SOURCE
159 else:
160 sourceType = UNKNOWN_SOURCE
161
162 if sourceType != UNKNOWN_SOURCE:
172 for p in path: # search in path 163 for p in path: # search in path
173 pathname = os.path.join(p, name) 164 pathname = os.path.join(p, name)
174 if os.path.exists(pathname): 165 if os.path.exists(pathname):
175 return (open(pathname), pathname, (ext, "r", PY_SOURCE)) 166 return (open(pathname), pathname, (ext, "r", sourceType))
176 # __IGNORE_WARNING_Y115__ 167 # __IGNORE_WARNING_Y115__
177 raise ImportError 168 raise ImportError
169 else:
170 # standard Python module file
171 if name.lower().endswith(".py"):
172 name = name[:-3]
178 173
179 # standard Python module file 174 spec = importlib.machinery.PathFinder.find_spec(name, path)
180 if name.lower().endswith(".py"): 175 if spec is None:
181 name = name[:-3] 176 raise ImportError
182 177 if isinstance(spec.loader, importlib.machinery.SourceFileLoader):
183 spec = importlib.machinery.PathFinder.find_spec(name, path) 178 ext = os.path.splitext(spec.origin)[-1]
184 if spec is None: 179 return (open(spec.origin), spec.origin, (ext, "r", PY_SOURCE))
185 raise ImportError 180 # __IGNORE_WARNING_Y115__
186 if isinstance(spec.loader, importlib.machinery.SourceFileLoader):
187 ext = os.path.splitext(spec.origin)[-1]
188 return (open(spec.origin), spec.origin, (ext, "r", PY_SOURCE))
189 # __IGNORE_WARNING_Y115__
190 181
191 raise ImportError 182 raise ImportError

eric ide

mercurial