eric6/Utilities/ModuleParser.py

changeset 7400
e5d62581d002
parent 7360
9190402e4505
child 7585
749c6871f4ef
equal deleted inserted replaced
7399:7088860e4a00 7400:e5d62581d002
17 """ 17 """
18 18
19 19
20 import sys 20 import sys
21 import os 21 import os
22 import imp 22 import importlib
23 import re 23 import re
24 24
25 import Utilities 25 import Utilities
26 from functools import reduce 26 from functools import reduce
27 import Preferences 27 import Preferences
29 __all__ = ["Module", "Class", "Function", "Attribute", "RbModule", 29 __all__ = ["Module", "Class", "Function", "Attribute", "RbModule",
30 "readModule", "getTypeFromTypeName"] 30 "readModule", "getTypeFromTypeName"]
31 31
32 TABWIDTH = 4 32 TABWIDTH = 4
33 33
34 SEARCH_ERROR = 0
35 PY_SOURCE = 1
34 PTL_SOURCE = 128 36 PTL_SOURCE = 128
35 RB_SOURCE = 129 37 RB_SOURCE = 129
36 38
37 SUPPORTED_TYPES = [imp.PY_SOURCE, PTL_SOURCE, RB_SOURCE] 39 SUPPORTED_TYPES = [PY_SOURCE, PTL_SOURCE, RB_SOURCE]
38 TYPE_MAPPING = { 40 TYPE_MAPPING = {
39 "Python": imp.PY_SOURCE, 41 "Python": PY_SOURCE,
40 "Python2": imp.PY_SOURCE, 42 "Python2": PY_SOURCE,
41 "Python3": imp.PY_SOURCE, 43 "Python3": PY_SOURCE,
42 "MicroPython": imp.PY_SOURCE, 44 "MicroPython": PY_SOURCE,
43 "Ruby": RB_SOURCE, 45 "Ruby": RB_SOURCE,
44 } 46 }
45 47
46 48
47 def getTypeFromTypeName(name): 49 def getTypeFromTypeName(name):
421 self.globals = {} 423 self.globals = {}
422 self.imports = [] 424 self.imports = []
423 self.from_imports = {} 425 self.from_imports = {}
424 self.package = '.'.join(name.split('.')[:-1]) 426 self.package = '.'.join(name.split('.')[:-1])
425 self.type = moduleType 427 self.type = moduleType
426 if moduleType in [imp.PY_SOURCE, PTL_SOURCE]: 428 if moduleType in [PY_SOURCE, PTL_SOURCE]:
427 self._getnext = _py_getnext 429 self._getnext = _py_getnext
428 elif moduleType == RB_SOURCE: 430 elif moduleType == RB_SOURCE:
429 self._getnext = _rb_getnext 431 self._getnext = _rb_getnext
430 else: 432 else:
431 self._getnext = None 433 self._getnext = None
497 Public method to scan the source text and retrieve the relevant 499 Public method to scan the source text and retrieve the relevant
498 information. 500 information.
499 501
500 @param src the source text to be scanned (string) 502 @param src the source text to be scanned (string)
501 """ 503 """
502 if self.type in [imp.PY_SOURCE, PTL_SOURCE]: 504 if self.type in [PY_SOURCE, PTL_SOURCE]:
503 self.__py_scan(src) 505 self.__py_scan(src)
504 elif self.type == RB_SOURCE: 506 elif self.type == RB_SOURCE:
505 self.__rb_scan(src) 507 self.__rb_scan(src)
506 508
507 def __py_setVisibility(self, objectRef): 509 def __py_setVisibility(self, objectRef):
1275 """ 1277 """
1276 Public method to get the type of the module's source. 1278 Public method to get the type of the module's source.
1277 1279
1278 @return type of the modules's source (string) 1280 @return type of the modules's source (string)
1279 """ 1281 """
1280 if self.type in [imp.PY_SOURCE, PTL_SOURCE]: 1282 if self.type in [PY_SOURCE, PTL_SOURCE]:
1281 py3ExtList = Preferences.getDebugger("Python3Extensions").split() 1283 py3ExtList = Preferences.getDebugger("Python3Extensions").split()
1282 if self.file.endswith(tuple(py3ExtList)): 1284 if self.file.endswith(tuple(py3ExtList)):
1283 moduleType = "Python3" 1285 moduleType = "Python3"
1284 else: 1286 else:
1285 moduleType = "Python2" 1287 moduleType = "Python2"
1648 # Ruby source file 1650 # Ruby source file
1649 return (open(pathname), pathname, 1651 return (open(pathname), pathname,
1650 ('.rb', 'r', RB_SOURCE)) 1652 ('.rb', 'r', RB_SOURCE))
1651 else: 1653 else:
1652 return (open(pathname), pathname, 1654 return (open(pathname), pathname,
1653 (ext, 'r', imp.PY_SOURCE)) 1655 (ext, 'r', PY_SOURCE))
1654 raise ImportError 1656 raise ImportError
1655 1657
1656 # standard Python module file 1658 # standard Python module file
1657 if name.lower().endswith('.py'): 1659 if name.lower().endswith('.py'):
1658 name = name[:-3] 1660 name = name[:-3]
1659 1661
1660 return imp.find_module(name, path) 1662 spec = importlib.machinery.PathFinder.find_spec(name, path)
1663 if spec is None:
1664 raise ImportError
1665 if isinstance(spec.loader, importlib.machinery.SourceFileLoader):
1666 ext = os.path.splitext(spec.origin)[-1]
1667 return (open(spec.origin), spec.origin, (ext, 'r', PY_SOURCE))
1668
1669 raise ImportError
1661 1670
1662 1671
1663 def resetParsedModules(): 1672 def resetParsedModules():
1664 """ 1673 """
1665 Module function to reset the list of modules already parsed. 1674 Module function to reset the list of modules already parsed.

eric ide

mercurial