eric6/Plugins/CheckerPlugins/CodeStyleChecker/translations.py

changeset 8168
bdb0258faf42
parent 8166
bd5cd5858503
child 8169
139bd30c52c2
--- a/eric6/Plugins/CheckerPlugins/CodeStyleChecker/translations.py	Sun Mar 21 18:00:30 2021 +0100
+++ b/eric6/Plugins/CheckerPlugins/CodeStyleChecker/translations.py	Mon Mar 22 19:22:11 2021 +0100
@@ -7,6 +7,8 @@
 Module implementing message translations for the code style plugin messages.
 """
 
+import re
+
 from PyQt5.QtCore import QCoreApplication
 
 from .Annotations.translations import (
@@ -27,10 +29,11 @@
     _securityMessages, _securityMessagesSampleArgs
 )
 
-_messages = {
-    ##################################################################
-    ## pycodestyle messages
-    ##################################################################
+##################################################################
+## pycodestyle error messages
+##################################################################
+
+_pycodestyleErrorMessages = {
     "E101": QCoreApplication.translate(
         "pycodestyle",
         "indentation contains mixed spaces and tabs"),
@@ -89,9 +92,6 @@
     "E133": QCoreApplication.translate(
         "pycodestyle",
         "closing bracket is missing indentation"),
-    "W191": QCoreApplication.translate(
-        "pycodestyle",
-        "indentation contains tabs"),
     "E201": QCoreApplication.translate(
         "pycodestyle",
         "whitespace after '{0}'"),
@@ -170,15 +170,6 @@
     "E275": QCoreApplication.translate(
         "pycodestyle",
         "missing whitespace after keyword"),
-    "W291": QCoreApplication.translate(
-        "pycodestyle",
-        "trailing whitespace"),
-    "W292": QCoreApplication.translate(
-        "pycodestyle",
-        "no newline at end of file"),
-    "W293": QCoreApplication.translate(
-        "pycodestyle",
-        "blank line contains whitespace"),
     "E301": QCoreApplication.translate(
         "pycodestyle",
         "expected {0} blank lines, found {1}"),
@@ -204,9 +195,6 @@
     "E308": QCoreApplication.translate(
         "pycodestyle",
         "too many blank lines ({0})"),
-    "W391": QCoreApplication.translate(
-        "pycodestyle",
-        "blank line at end of file"),
     "E401": QCoreApplication.translate(
         "pycodestyle",
         "multiple imports on one line"),
@@ -219,33 +207,6 @@
     "E502": QCoreApplication.translate(
         "pycodestyle",
         "the backslash is redundant between brackets"),
-    "W503": QCoreApplication.translate(
-        "pycodestyle",
-        "line break before binary operator"),
-    "W504": QCoreApplication.translate(
-        "pycodestyle",
-        "line break after binary operator"),
-    "W505": QCoreApplication.translate(
-        "pycodestyle",
-        "doc line too long ({0} > {1} characters)"),
-    "W601": QCoreApplication.translate(
-        "pycodestyle",
-        ".has_key() is deprecated, use 'in'"),
-    "W602": QCoreApplication.translate(
-        "pycodestyle",
-        "deprecated form of raising exception"),
-    "W603": QCoreApplication.translate(
-        "pycodestyle",
-        "'<>' is deprecated, use '!='"),
-    "W604": QCoreApplication.translate(
-        "pycodestyle",
-        "backticks are deprecated, use 'repr()'"),
-    "W605": QCoreApplication.translate(
-        "pycodestyle",
-        "invalid escape sequence '\\{0}'"),
-    "W606": QCoreApplication.translate(
-        "pycodestyle",
-        "'async' and 'await' are reserved keywords starting with Python 3.7"),
     "E701": QCoreApplication.translate(
         "pycodestyle",
         "multiple statements on one line (colon)"),
@@ -294,11 +255,62 @@
     "E902": QCoreApplication.translate(
         "pycodestyle",
         "{0}"),
-    
-    ##################################################################
-    ## CodeStyleFixer messages
-    ##################################################################
-    
+}
+
+##################################################################
+## pycodestyle warning messages
+##################################################################
+
+_pycodestyleWarningMessages = {
+    "W191": QCoreApplication.translate(
+        "pycodestyle",
+        "indentation contains tabs"),
+    "W291": QCoreApplication.translate(
+        "pycodestyle",
+        "trailing whitespace"),
+    "W292": QCoreApplication.translate(
+        "pycodestyle",
+        "no newline at end of file"),
+    "W293": QCoreApplication.translate(
+        "pycodestyle",
+        "blank line contains whitespace"),
+    "W391": QCoreApplication.translate(
+        "pycodestyle",
+        "blank line at end of file"),
+    "W503": QCoreApplication.translate(
+        "pycodestyle",
+        "line break before binary operator"),
+    "W504": QCoreApplication.translate(
+        "pycodestyle",
+        "line break after binary operator"),
+    "W505": QCoreApplication.translate(
+        "pycodestyle",
+        "doc line too long ({0} > {1} characters)"),
+    "W601": QCoreApplication.translate(
+        "pycodestyle",
+        ".has_key() is deprecated, use 'in'"),
+    "W602": QCoreApplication.translate(
+        "pycodestyle",
+        "deprecated form of raising exception"),
+    "W603": QCoreApplication.translate(
+        "pycodestyle",
+        "'<>' is deprecated, use '!='"),
+    "W604": QCoreApplication.translate(
+        "pycodestyle",
+        "backticks are deprecated, use 'repr()'"),
+    "W605": QCoreApplication.translate(
+        "pycodestyle",
+        "invalid escape sequence '\\{0}'"),
+    "W606": QCoreApplication.translate(
+        "pycodestyle",
+        "'async' and 'await' are reserved keywords starting with Python 3.7"),
+}
+
+##################################################################
+## CodeStyleFixer messages
+##################################################################
+
+_fixMessages = {
     "FIXD111": QCoreApplication.translate(
         'CodeStyleFixer',
         "Triple single quotes converted to triple double quotes."),
@@ -448,7 +460,7 @@
         "Could not save the file! Skipping it. Reason: {0}"),
 }
 
-_messages_sample_args = {
+_pycodestyleErrorMessagesSampleArgs = {
     "E201": ["([{"],
     "E202": ["}])"],
     "E203": [",;:"],
@@ -464,7 +476,6 @@
     "E307": [3, 1],
     "E308": [3],
     "E501": [85, 79],
-    "W505": [80, 72],
     "E605": ["A"],
     "E711": ["None", "'if cond is None:'"],
     "E712": ["True", "'if cond is True:' or 'if cond:'"],
@@ -473,33 +484,44 @@
     "E743": ["l"],
     "E901": ["SyntaxError", "Invalid Syntax"],
     "E902": ["OSError"],
-    
+}
+
+_pycodestyleWarningMessagesSampleArgs = {
+    "W505": [80, 72],
+}
+
+_fixMessagesSampleArgs = {
     "FIXWRITE_ERROR": ["OSError"],
 }
 
-messageCatalogs = (
-    _messages,
-    _annotationsMessages,
-    _complexityMessages,
-    _docStyleMessages,
-    _miscellaneousMessages,
-    _namingStyleMessages,
-    _pathlibMessages,
-    _securityMessages,
-)
+messageCatalogs = {
+    "A": _annotationsMessages,
+    "C": _complexityMessages,
+    "D": _docStyleMessages,
+    "E": _pycodestyleErrorMessages,
+    "M": _miscellaneousMessages,
+    "N": _namingStyleMessages,
+    "P": _pathlibMessages,
+    "S": _securityMessages,
+    "W": _pycodestyleWarningMessages,
+    
+    "FIX": _fixMessages,
+}
 
-messageSampleArgsCatalog = (
-    _messages_sample_args,
-    _annotationsMessagesSampleArgs,
-    _complexityMessagesSampleArgs,
-    _docStyleMessagesSampleArgs,
-    _miscellaneousMessagesSampleArgs,
-    _securityMessagesSampleArgs,
-)
+messageSampleArgsCatalog = {
+    "A": _annotationsMessagesSampleArgs,
+    "C": _complexityMessagesSampleArgs,
+    "D": _docStyleMessagesSampleArgs,
+    "E": _pycodestyleErrorMessagesSampleArgs,
+    "M": _miscellaneousMessagesSampleArgs,
+    "S": _securityMessagesSampleArgs,
+    "W": _pycodestyleWarningMessagesSampleArgs,
+    
+    "FIX": _fixMessagesSampleArgs,
+}
 
+messageCategoryRe = re.compile(r"([A-Z]{1,3}).+")
 
-# TODO: change this to use the initial character (= class) of the message code
-#       to select the appropriate catalog (observe FIX as code category)
 def getTranslatedMessage(messageCode, messageArgs, example=False):
     """
     Module function to get a translated and formatted message for a
@@ -516,26 +538,39 @@
     @return translated and formatted message
     @rtype str
     """
-    if example:
-        for argsCatalog in messageSampleArgsCatalog:
-            if messageCode in argsCatalog:
-                args = argsCatalog[messageCode]
-                break
+    match = messageCategoryRe.match(messageCode)
+    if match:
+        # the message code is OK
+        messageCategory = match.group(1)
+        
+        if example:
+            try:
+                argsCatalog = messageSampleArgsCatalog[messageCategory]
+                try:
+                    args = argsCatalog[messageCode]
+                except KeyError:
+                    args = None
+            except KeyError:
+                args = None
         else:
-            args = None
-    else:
-        args = messageArgs
-    
-    for catalog in messageCatalogs:
-        if messageCode in catalog:
-            if args is None:
-                return catalog[messageCode]
-            elif isinstance(args, int):
-                # Retranslate with correct plural form
-                return catalog[messageCode](args)
-            else:
-                return catalog[messageCode].format(*args)
-    else:
+            args = messageArgs
+        
+        try:
+            catalog = messageCatalogs[messageCategory]
+            try:
+                message = catalog[messageCode]
+                if args is None:
+                    return message
+                elif isinstance(args, int):
+                    # Retranslate with correct plural form
+                    return message(args)
+                else:
+                    return message.format(*args)
+            except KeyError:
+                pass
+        except KeyError:
+            pass
+        
         if example:
             return None
         else:
@@ -553,7 +588,7 @@
     @rtype set of str
     """
     knownCodes = []
-    for catalog in messageCatalogs:
+    for catalog in messageCatalogs.values():
         knownCodes += list(catalog.keys())
     return {c.split(".", 1)[0] for c in knownCodes}
 

eric ide

mercurial