eric7/Plugins/CheckerPlugins/CodeStyleChecker/Imports/ImportsChecker.py

branch
eric7
changeset 8801
8fbb21be8579
parent 8789
b165effc3c62
child 8802
129a973fc33e
equal deleted inserted replaced
8800:d0d2fa9dbbb7 8801:8fbb21be8579
6 """ 6 """
7 Module implementing a checker for import statements. 7 Module implementing a checker for import statements.
8 """ 8 """
9 9
10 import copy 10 import copy
11 import sys
11 12
12 13
13 class ImportsChecker: 14 class ImportsChecker:
14 """ 15 """
15 Class implementing a checker for import statements. 16 Class implementing a checker for import statements.
16 """ 17 """
17 Codes = [ 18 Codes = [
19 ## Local imports
20 "I101", "I102", "I103",
18 ] 21 ]
19 22
20 def __init__(self, source, filename, tree, select, ignore, expected, 23 def __init__(self, source, filename, tree, select, ignore, expected,
21 repeat, args): 24 repeat, args):
22 """ 25 """
34 @type list of str 37 @type list of str
35 @param expected list of expected codes 38 @param expected list of expected codes
36 @type list of str 39 @type list of str
37 @param repeat flag indicating to report each occurrence of a code 40 @param repeat flag indicating to report each occurrence of a code
38 @type bool 41 @type bool
39 @param args dictionary of arguments for the miscellaneous checks 42 @param args dictionary of arguments for the various checks
40 @type dict 43 @type dict
41 """ 44 """
42 self.__select = tuple(select) 45 self.__select = tuple(select)
43 self.__ignore = ('',) if select else tuple(ignore) 46 self.__ignore = ("",) if select else tuple(ignore)
44 self.__expected = expected[:] 47 self.__expected = expected[:]
45 self.__repeat = repeat 48 self.__repeat = repeat
46 self.__filename = filename 49 self.__filename = filename
47 self.__source = source[:] 50 self.__source = source[:]
48 self.__tree = copy.deepcopy(tree) 51 self.__tree = copy.deepcopy(tree)
53 56
54 # collection of detected errors 57 # collection of detected errors
55 self.errors = [] 58 self.errors = []
56 59
57 checkersWithCodes = [ 60 checkersWithCodes = [
61 (self.__checkLocalImports, ("I101", "I102", "I103")),
58 ] 62 ]
59 63
60 self.__checkers = [] 64 self.__checkers = []
61 for checker, codes in checkersWithCodes: 65 for checker, codes in checkersWithCodes:
62 if any(not (code and self.__ignoreCode(code)) 66 if any(not (code and self.__ignoreCode(code))
125 # don't do anything, if no codes were selected 129 # don't do anything, if no codes were selected
126 return 130 return
127 131
128 for check in self.__checkers: 132 for check in self.__checkers:
129 check() 133 check()
134
135 def getStandardModules(self):
136 """
137 Public method to get a list of modules of the standard library.
138
139 @return set of builtin modules
140 @rtype set of str
141 """
142 try:
143 return sys.stdlib_module_names
144 except AttributeError:
145 return {
146 "__future__", "__main__", "_dummy_thread", "_thread", "abc",
147 "aifc", "argparse", "array", "ast", "asynchat", "asyncio",
148 "asyncore", "atexit", "audioop", "base64", "bdb", "binascii",
149 "binhex", "bisect", "builtins", "bz2", "calendar", "cgi",
150 "cgitb", "chunk", "cmath", "cmd", "code", "codecs", "codeop",
151 "collections", "colorsys", "compileall", "concurrent",
152 "configparser", "contextlib", "contextvars", "copy", "copyreg",
153 "cProfile", "crypt", "csv", "ctypes", "curses", "dataclasses",
154 "datetime", "dbm", "decimal", "difflib", "dis", "distutils",
155 "doctest", "dummy_threading", "email", "encodings",
156 "ensurepip", "enum", "errno", "faulthandler", "fcntl",
157 "filecmp", "fileinput", "fnmatch", "formatter", "fractions",
158 "ftplib", "functools", "gc", "getopt", "getpass", "gettext",
159 "glob", "grp", "gzip", "hashlib", "heapq", "hmac", "html",
160 "http", "imaplib", "imghdr", "imp", "importlib", "inspect",
161 "io", "ipaddress", "itertools", "json", "keyword", "lib2to3",
162 "linecache", "locale", "logging", "lzma", "mailbox", "mailcap",
163 "marshal", "math", "mimetypes", "mmap", "modulefinder",
164 "msilib", "msvcrt", "multiprocessing", "netrc", "nis",
165 "nntplib", "numbers", "operator", "optparse", "os",
166 "ossaudiodev", "parser", "pathlib", "pdb", "pickle",
167 "pickletools", "pipes", "pkgutil", "platform", "plistlib",
168 "poplib", "posix", "pprint", "profile", "pstats", "pty", "pwd",
169 "py_compile", "pyclbr", "pydoc", "queue", "quopri", "random",
170 "re", "readline", "reprlib", "resource", "rlcompleter",
171 "runpy", "sched", "secrets", "select", "selectors", "shelve",
172 "shlex", "shutil", "signal", "site", "smtpd", "smtplib",
173 "sndhdr", "socket", "socketserver", "spwd", "sqlite3", "ssl",
174 "stat", "statistics", "string", "stringprep", "struct",
175 "subprocess", "sunau", "symbol", "symtable", "sys",
176 "sysconfig", "syslog", "tabnanny", "tarfile", "telnetlib",
177 "tempfile", "termios", "test", "textwrap", "threading", "time",
178 "timeit", "tkinter", "token", "tokenize", "trace", "traceback",
179 "tracemalloc", "tty", "turtle", "turtledemo", "types",
180 "typing", "unicodedata", "unittest", "urllib", "uu", "uuid",
181 "venv", "warnings", "wave", "weakref", "webbrowser", "winreg",
182 "winsound", "wsgiref", "xdrlib", "xml", "xmlrpc", "zipapp",
183 "zipfile", "zipimport", "zlib", "zoneinfo",
184 }
185
186 #######################################################################
187 ## Local imports
188 ##
189 ## adapted from: flake8-local-import v1.0.6
190 #######################################################################
191
192 def __checkLocalImports(self):
193 """
194 Private method to check local imports.
195 """
196 from .LocalImportVisitor import LocalImportVisitor
197
198 visitor = LocalImportVisitor(self.__args, self)
199 visitor.visit(copy.deepcopy(self.__tree))
200 for violation in visitor.violations:
201 node = violation[0]
202 reason = violation[1]
203 self.__error(node.lineno - 1, node.col_offset, reason)

eric ide

mercurial