src/eric7/Utilities/ModuleParser.py

branch
server
changeset 10583
2114cc7275e8
parent 10482
72d9b5ea39b4
child 10585
83e5a9a64543
equal deleted inserted replaced
10582:8264ea1f42e5 10583:2114cc7275e8
24 from functools import reduce 24 from functools import reduce
25 25
26 from PyQt6.QtCore import QRegularExpression 26 from PyQt6.QtCore import QRegularExpression
27 27
28 from eric7 import Utilities 28 from eric7 import Utilities
29 from eric7.EricWidgets.EricApplication import ericApp
30 from eric7.SystemUtilities import FileSystemUtilities
29 31
30 __all__ = [ 32 __all__ = [
31 "Attribute", 33 "Attribute",
32 "Class", 34 "Class",
33 "Function", 35 "Function",
1681 global _modules 1683 global _modules
1682 1684
1683 _extensions = ( 1685 _extensions = (
1684 [".py", ".pyw", ".pyi", ".ptl", ".rb"] if extensions is None else extensions[:] 1686 [".py", ".pyw", ".pyi", ".ptl", ".rb"] if extensions is None else extensions[:]
1685 ) 1687 )
1686 with contextlib.suppress(ValueError):
1687 _extensions.remove(".py")
1688
1689 modname = module 1688 modname = module
1690 1689 isRemoteFileName = FileSystemUtilities.isRemoteFileName(module)
1691 if os.path.exists(module): 1690
1692 path = [os.path.dirname(module)] 1691 if isRemoteFileName:
1693 if module.lower().endswith(".py"): 1692 module, extension = os.path.splitext(os.path.basename(module))
1694 module = module[:-3] 1693 else:
1695 if ( 1694 with contextlib.suppress(ValueError):
1696 os.path.exists(os.path.join(path[0], "__init__.py")) 1695 _extensions.remove(".py")
1697 or os.path.exists(os.path.join(path[0], "__init__.pyi")) 1696
1698 or os.path.exists(os.path.join(path[0], "__init__.rb")) 1697 if os.path.exists(module):
1699 or inpackage 1698 path = [os.path.dirname(module)]
1700 ): 1699 if module.lower().endswith(".py"):
1701 if basename: 1700 module = module[:-3]
1702 module = module.replace(basename, "") 1701 if (
1703 if os.path.isabs(module): 1702 os.path.exists(os.path.join(path[0], "__init__.py"))
1704 modname = os.path.splitdrive(module)[1][len(os.sep) :] 1703 or os.path.exists(os.path.join(path[0], "__init__.pyi"))
1704 or os.path.exists(os.path.join(path[0], "__init__.rb"))
1705 or inpackage
1706 ):
1707 if basename:
1708 module = module.replace(basename, "")
1709 if os.path.isabs(module):
1710 modname = os.path.splitdrive(module)[1][len(os.sep) :]
1711 else:
1712 modname = module
1713 modname = modname.replace(os.sep, ".")
1714 inpackage = True
1705 else: 1715 else:
1706 modname = module 1716 modname = os.path.basename(module)
1707 modname = modname.replace(os.sep, ".") 1717 for ext in _extensions:
1708 inpackage = True 1718 if modname.lower().endswith(ext):
1709 else: 1719 modname = modname[: -len(ext)]
1710 modname = os.path.basename(module) 1720 break
1711 for ext in _extensions: 1721 module = os.path.basename(module)
1712 if modname.lower().endswith(ext):
1713 modname = modname[: -len(ext)]
1714 break
1715 module = os.path.basename(module)
1716 1722
1717 if caching and modname in _modules: 1723 if caching and modname in _modules:
1718 # we've seen this module before... 1724 # we've seen this module before...
1719 return _modules[modname] 1725 return _modules[modname]
1720 1726
1723 mod = Module(modname, None, None) 1729 mod = Module(modname, None, None)
1724 if caching: 1730 if caching:
1725 _modules[modname] = mod 1731 _modules[modname] = mod
1726 return mod 1732 return mod
1727 1733
1728 # search the path for the module 1734 if isRemoteFileName:
1729 path = [] if path is None else path[:] 1735 if (
1730 f = None 1736 not ericApp()
1731 if inpackage: 1737 .getObject("EricServer")
1732 try: 1738 .getServiceInterface("FileSystem")
1733 f, file, (suff, mode, moduleType) = find_module(module, path, _extensions) 1739 .exists(modname)
1734 except ImportError: 1740 ):
1735 f = None 1741 raise ImportError
1736 if f is None: 1742 if extension == ".ptl":
1737 fullpath = path[:] + sys.path[:] 1743 moduleType = PTL_SOURCE
1738 f, file, (suff, mode, moduleType) = find_module(module, fullpath, _extensions) 1744 elif extension == ".rb":
1739 if f: 1745 moduleType = RB_SOURCE
1740 f.close() 1746 elif extension in _extensions:
1747 moduleType = PY_SOURCE
1748 else:
1749 raise ImportError
1750
1751 file = modname
1752
1753 modname = FileSystemUtilities.plainFileName(modname)
1754 if modname.startswith(("/", "\\")):
1755 modname = modname[1:]
1756 modname = os.path.splitext(modname)[0].replace("/", ".").replace("\\", ".")
1757 else:
1758 # search the path for the module
1759 path = [] if path is None else path[:]
1760 f = None
1761 if inpackage:
1762 try:
1763 f, file, (suff, mode, moduleType) = find_module(
1764 module, path, _extensions
1765 )
1766 except ImportError:
1767 f = None
1768 if f is None:
1769 fullpath = path[:] + sys.path[:]
1770 f, file, (suff, mode, moduleType) = find_module(
1771 module, fullpath, _extensions
1772 )
1773 if f:
1774 f.close()
1775
1741 if moduleType not in SUPPORTED_TYPES: 1776 if moduleType not in SUPPORTED_TYPES:
1742 # not supported source, can't do anything with this module 1777 # not supported source, can't do anything with this module
1743 _modules[modname] = Module(modname, None, None) 1778 _modules[modname] = Module(modname, None, None)
1744 return _modules[modname] 1779 return _modules[modname]
1745 1780
1746 mod = Module(modname, file, moduleType) 1781 mod = Module(modname, file, moduleType)
1747 with contextlib.suppress(UnicodeError, OSError): 1782 with contextlib.suppress(UnicodeError, OSError):
1748 src = Utilities.readEncodedFile(file)[0] 1783 if isRemoteFileName:
1784 bSource = (
1785 ericApp()
1786 .getObject("EricServer")
1787 .getServiceInterface("FileSystem")
1788 .readFile(file)
1789 )
1790 src = Utilities.decode(bSource)[0]
1791 else:
1792 src = Utilities.readEncodedFile(file)[0]
1749 mod.scan(src) 1793 mod.scan(src)
1750 if caching: 1794 if caching:
1751 _modules[modname] = mod 1795 _modules[modname] = mod
1752 return mod 1796 return mod
1753 1797

eric ide

mercurial