src/eric7/Utilities/ClassBrowsers/__init__.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
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 32
33 SUPPORTED_TYPES = [PY_SOURCE, PTL_SOURCE, RB_SOURCE, IDL_SOURCE, JS_SOURCE, 33 SUPPORTED_TYPES = [
34 PROTO_SOURCE] 34 PY_SOURCE,
35 PTL_SOURCE,
36 RB_SOURCE,
37 IDL_SOURCE,
38 JS_SOURCE,
39 PROTO_SOURCE,
40 ]
35 41
36 __extensions = { 42 __extensions = {
37 "IDL": [".idl"], 43 "IDL": [".idl"],
38 "Python": [".py", ".pyw", ".ptl"], # currently not used 44 "Python": [".py", ".pyw", ".ptl"], # currently not used
39 "Ruby": [".rb"], 45 "Ruby": [".rb"],
44 50
45 def readmodule(module, path=None, isPyFile=False): 51 def readmodule(module, path=None, isPyFile=False):
46 """ 52 """
47 Read a source file and return a dictionary of classes, functions, modules, 53 Read a source file and return a dictionary of classes, functions, modules,
48 etc. . 54 etc. .
49 55
50 The real work of parsing the source file is delegated to the individual 56 The real work of parsing the source file is delegated to the individual
51 file parsers. 57 file parsers.
52 58
53 @param module name of the source file 59 @param module name of the source file
54 @type str 60 @type str
59 @return the resulting dictionary 65 @return the resulting dictionary
60 @rtype dict 66 @rtype dict
61 """ 67 """
62 ext = os.path.splitext(module)[1].lower() 68 ext = os.path.splitext(module)[1].lower()
63 path = [] if path is None else path[:] 69 path = [] if path is None else path[:]
64 70
65 if ext in __extensions["IDL"]: 71 if ext in __extensions["IDL"]:
66 from . import idlclbr 72 from . import idlclbr
73
67 dictionary = idlclbr.readmodule_ex(module, path) 74 dictionary = idlclbr.readmodule_ex(module, path)
68 idlclbr._modules.clear() 75 idlclbr._modules.clear()
69 elif ext in __extensions["ProtoBuf"]: 76 elif ext in __extensions["ProtoBuf"]:
70 from . import protoclbr 77 from . import protoclbr
78
71 dictionary = protoclbr.readmodule_ex(module, path) 79 dictionary = protoclbr.readmodule_ex(module, path)
72 protoclbr._modules.clear() 80 protoclbr._modules.clear()
73 elif ext in __extensions["Ruby"]: 81 elif ext in __extensions["Ruby"]:
74 from . import rbclbr 82 from . import rbclbr
83
75 dictionary = rbclbr.readmodule_ex(module, path) 84 dictionary = rbclbr.readmodule_ex(module, path)
76 rbclbr._modules.clear() 85 rbclbr._modules.clear()
77 elif ext in __extensions["JavaScript"]: 86 elif ext in __extensions["JavaScript"]:
78 from . import jsclbr 87 from . import jsclbr
88
79 dictionary = jsclbr.readmodule_ex(module, path) 89 dictionary = jsclbr.readmodule_ex(module, path)
80 jsclbr._modules.clear() 90 jsclbr._modules.clear()
81 elif ( 91 elif ext in Preferences.getPython("Python3Extensions") or isPyFile:
82 ext in Preferences.getPython("Python3Extensions") or
83 isPyFile
84 ):
85 from . import pyclbr 92 from . import pyclbr
93
86 dictionary = pyclbr.readmodule_ex(module, path, isPyFile=isPyFile) 94 dictionary = pyclbr.readmodule_ex(module, path, isPyFile=isPyFile)
87 pyclbr._modules.clear() 95 pyclbr._modules.clear()
88 else: 96 else:
89 # try Python if it is without extension 97 # try Python if it is without extension
90 from . import pyclbr 98 from . import pyclbr
99
91 dictionary = pyclbr.readmodule_ex(module, path) 100 dictionary = pyclbr.readmodule_ex(module, path)
92 pyclbr._modules.clear() 101 pyclbr._modules.clear()
93 102
94 return dictionary 103 return dictionary
95 104
96 105
97 def find_module(name, path, isPyFile=False): 106 def find_module(name, path, isPyFile=False):
98 """ 107 """
99 Module function to extend the Python module finding mechanism. 108 Module function to extend the Python module finding mechanism.
100 109
101 This function searches for files in the given list of paths. If the 110 This function searches for files in the given list of paths. If the
102 file name doesn't have an extension or an extension of .py, the normal 111 file name doesn't have an extension or an extension of .py, the normal
103 Python search implemented in the imp module is used. For all other 112 Python search implemented in the imp module is used. For all other
104 supported files only the paths list is searched. 113 supported files only the paths list is searched.
105 114
106 @param name file name or module name to search for 115 @param name file name or module name to search for
107 @type str 116 @type str
108 @param path search paths 117 @param path search paths
109 @type list of str 118 @type list of str
110 @param isPyFile flag indicating a Python file 119 @param isPyFile flag indicating a Python file
113 is a tuple of file suffix, file mode and file type) 122 is a tuple of file suffix, file mode and file type)
114 @rtype tuple 123 @rtype tuple
115 @exception ImportError The file or module wasn't found. 124 @exception ImportError The file or module wasn't found.
116 """ 125 """
117 ext = os.path.splitext(name)[1].lower() 126 ext = os.path.splitext(name)[1].lower()
118 127
119 if ext in __extensions["Ruby"]: 128 if ext in __extensions["Ruby"]:
120 for p in path: # only search in path 129 for p in path: # only search in path
121 pathname = os.path.join(p, name) 130 pathname = os.path.join(p, name)
122 if os.path.exists(pathname): 131 if os.path.exists(pathname):
123 return (open(pathname), pathname, (ext, 'r', RB_SOURCE)) 132 return (open(pathname), pathname, (ext, "r", RB_SOURCE))
124 # __IGNORE_WARNING_Y115__ 133 # __IGNORE_WARNING_Y115__
125 raise ImportError 134 raise ImportError
126 135
127 elif ext in __extensions["IDL"]: 136 elif ext in __extensions["IDL"]:
128 for p in path: # only search in path 137 for p in path: # only search in path
129 pathname = os.path.join(p, name) 138 pathname = os.path.join(p, name)
130 if os.path.exists(pathname): 139 if os.path.exists(pathname):
131 return (open(pathname), pathname, (ext, 'r', IDL_SOURCE)) 140 return (open(pathname), pathname, (ext, "r", IDL_SOURCE))
132 # __IGNORE_WARNING_Y115__ 141 # __IGNORE_WARNING_Y115__
133 raise ImportError 142 raise ImportError
134 143
135 elif ext in __extensions["ProtoBuf"]: 144 elif ext in __extensions["ProtoBuf"]:
136 for p in path: # only search in path 145 for p in path: # only search in path
137 pathname = os.path.join(p, name) 146 pathname = os.path.join(p, name)
138 if os.path.exists(pathname): 147 if os.path.exists(pathname):
139 return (open(pathname), pathname, (ext, 'r', PROTO_SOURCE)) 148 return (open(pathname), pathname, (ext, "r", PROTO_SOURCE))
140 # __IGNORE_WARNING_Y115__ 149 # __IGNORE_WARNING_Y115__
141 raise ImportError 150 raise ImportError
142 151
143 elif ext in __extensions["JavaScript"]: 152 elif ext in __extensions["JavaScript"]:
144 for p in path: # only search in path 153 for p in path: # only search in path
145 pathname = os.path.join(p, name) 154 pathname = os.path.join(p, name)
146 if os.path.exists(pathname): 155 if os.path.exists(pathname):
147 return (open(pathname), pathname, (ext, 'r', JS_SOURCE)) 156 return (open(pathname), pathname, (ext, "r", JS_SOURCE))
148 # __IGNORE_WARNING_Y115__ 157 # __IGNORE_WARNING_Y115__
149 raise ImportError 158 raise ImportError
150 159
151 elif ext == '.ptl': 160 elif ext == ".ptl":
152 for p in path: # only search in path 161 for p in path: # only search in path
153 pathname = os.path.join(p, name) 162 pathname = os.path.join(p, name)
154 if os.path.exists(pathname): 163 if os.path.exists(pathname):
155 return (open(pathname), pathname, (ext, 'r', PTL_SOURCE)) 164 return (open(pathname), pathname, (ext, "r", PTL_SOURCE))
156 # __IGNORE_WARNING_Y115__ 165 # __IGNORE_WARNING_Y115__
157 raise ImportError 166 raise ImportError
158 167
159 elif ( 168 elif (
160 name.lower().endswith( 169 name.lower().endswith(tuple(Preferences.getPython("Python3Extensions")))
161 tuple(Preferences.getPython("Python3Extensions"))) or 170 or isPyFile
162 isPyFile
163 ): 171 ):
164 for p in path: # search in path 172 for p in path: # search in path
165 pathname = os.path.join(p, name) 173 pathname = os.path.join(p, name)
166 if os.path.exists(pathname): 174 if os.path.exists(pathname):
167 return (open(pathname), pathname, (ext, 'r', PY_SOURCE)) 175 return (open(pathname), pathname, (ext, "r", PY_SOURCE))
168 # __IGNORE_WARNING_Y115__ 176 # __IGNORE_WARNING_Y115__
169 raise ImportError 177 raise ImportError
170 178
171 # standard Python module file 179 # standard Python module file
172 if name.lower().endswith('.py'): 180 if name.lower().endswith(".py"):
173 name = name[:-3] 181 name = name[:-3]
174 182
175 spec = importlib.machinery.PathFinder.find_spec(name, path) 183 spec = importlib.machinery.PathFinder.find_spec(name, path)
176 if spec is None: 184 if spec is None:
177 raise ImportError 185 raise ImportError
178 if isinstance(spec.loader, importlib.machinery.SourceFileLoader): 186 if isinstance(spec.loader, importlib.machinery.SourceFileLoader):
179 ext = os.path.splitext(spec.origin)[-1] 187 ext = os.path.splitext(spec.origin)[-1]
180 return (open(spec.origin), spec.origin, (ext, 'r', PY_SOURCE)) 188 return (open(spec.origin), spec.origin, (ext, "r", PY_SOURCE))
181 # __IGNORE_WARNING_Y115__ 189 # __IGNORE_WARNING_Y115__
182 190
183 raise ImportError 191 raise ImportError

eric ide

mercurial