Started implementing a checker for import statements. eric7

Wed, 24 Nov 2021 20:08:25 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 24 Nov 2021 20:08:25 +0100
branch
eric7
changeset 8789
b165effc3c62
parent 8787
779054e59d8e
child 8790
548df4df8256

Started implementing a checker for import statements.

eric7.epj file | annotate | diff | comparison | revisions
eric7/Plugins/CheckerPlugins/CodeStyleChecker/Imports/ImportsChecker.py file | annotate | diff | comparison | revisions
eric7/Plugins/CheckerPlugins/CodeStyleChecker/Imports/__init__.py file | annotate | diff | comparison | revisions
eric7/Plugins/CheckerPlugins/CodeStyleChecker/Imports/translations.py file | annotate | diff | comparison | revisions
--- a/eric7.epj	Wed Nov 24 20:06:52 2021 +0100
+++ b/eric7.epj	Wed Nov 24 20:08:25 2021 +0100
@@ -2296,7 +2296,10 @@
       "eric7/HelpViewer/HelpViewerImplQWE.py",
       "eric7/Plugins/CheckerPlugins/CodeStyleChecker/Annotations/AnnotationsFutureVisitor.py",
       "eric7/DebugClients/Python/coverage/exceptions.py",
-      "eric7/QScintilla/EditorOutlineSizesDialog.py"
+      "eric7/QScintilla/EditorOutlineSizesDialog.py",
+      "eric7/Plugins/CheckerPlugins/CodeStyleChecker/Imports/__init__.py",
+      "eric7/Plugins/CheckerPlugins/CodeStyleChecker/Imports/ImportsChecker.py",
+      "eric7/Plugins/CheckerPlugins/CodeStyleChecker/Imports/translations.py"
     ],
     "SPELLEXCLUDES": "Dictionaries/excludes.dic",
     "SPELLLANGUAGE": "en_US",
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Imports/ImportsChecker.py	Wed Nov 24 20:08:25 2021 +0100
@@ -0,0 +1,129 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a checker for import statements.
+"""
+
+import copy
+
+
+class ImportsChecker:
+    """
+    Class implementing a checker for import statements.
+    """
+    Codes = [
+    ]
+
+    def __init__(self, source, filename, tree, select, ignore, expected,
+                 repeat, args):
+        """
+        Constructor
+        
+        @param source source code to be checked
+        @type list of str
+        @param filename name of the source file
+        @type str
+        @param tree AST tree of the source code
+        @type ast.Module
+        @param select list of selected codes
+        @type list of str
+        @param ignore list of codes to be ignored
+        @type list of str
+        @param expected list of expected codes
+        @type list of str
+        @param repeat flag indicating to report each occurrence of a code
+        @type bool
+        @param args dictionary of arguments for the miscellaneous checks
+        @type dict
+        """
+        self.__select = tuple(select)
+        self.__ignore = ('',) if select else tuple(ignore)
+        self.__expected = expected[:]
+        self.__repeat = repeat
+        self.__filename = filename
+        self.__source = source[:]
+        self.__tree = copy.deepcopy(tree)
+        self.__args = args
+        
+        # statistics counters
+        self.counters = {}
+        
+        # collection of detected errors
+        self.errors = []
+        
+        checkersWithCodes = [
+        ]
+        
+        self.__checkers = []
+        for checker, codes in checkersWithCodes:
+            if any(not (code and self.__ignoreCode(code))
+                    for code in codes):
+                self.__checkers.append(checker)
+    
+    def __ignoreCode(self, code):
+        """
+        Private method to check if the message code should be ignored.
+
+        @param code message code to check for
+        @type str
+        @return flag indicating to ignore the given code
+        @rtype bool
+        """
+        return (code.startswith(self.__ignore) and
+                not code.startswith(self.__select))
+    
+    def __error(self, lineNumber, offset, code, *args):
+        """
+        Private method to record an issue.
+        
+        @param lineNumber line number of the issue
+        @type int
+        @param offset position within line of the issue
+        @type int
+        @param code message code
+        @type str
+        @param args arguments for the message
+        @type list
+        """
+        if self.__ignoreCode(code):
+            return
+        
+        if code in self.counters:
+            self.counters[code] += 1
+        else:
+            self.counters[code] = 1
+        
+        # Don't care about expected codes
+        if code in self.__expected:
+            return
+        
+        if code and (self.counters[code] == 1 or self.__repeat):
+            # record the issue with one based line number
+            self.errors.append(
+                {
+                    "file": self.__filename,
+                    "line": lineNumber + 1,
+                    "offset": offset,
+                    "code": code,
+                    "args": args,
+                }
+            )
+    
+    def run(self):
+        """
+        Public method to check the given source against miscellaneous
+        conditions.
+        """
+        if not self.__filename:
+            # don't do anything, if essential data is missing
+            return
+        
+        if not self.__checkers:
+            # don't do anything, if no codes were selected
+            return
+        
+        for check in self.__checkers:
+            check()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Imports/__init__.py	Wed Nov 24 20:08:25 2021 +0100
@@ -0,0 +1,8 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Package implementing a checker for import statements.
+"""
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Imports/translations.py	Wed Nov 24 20:08:25 2021 +0100
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing message translations for the code style plugin messages
+(import statements part).
+"""
+
+from PyQt6.QtCore import QCoreApplication
+
+_importsMessages = {
+
+}
+
+__importsMessagesSampleArgs = {
+    
+}

eric ide

mercurial