]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/UPT/Library/CommentParsing.py
BaseTools: Clean up source files
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Library / CommentParsing.py
index 8ee788bd7724e097efb0fbfea980a4ef997bb4c2..285812c9c2f8465674e077449339333e8ff69480 100644 (file)
@@ -3,9 +3,9 @@
 #\r
 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
 #\r
-# This program and the accompanying materials are licensed and made available \r
-# under the terms and conditions of the BSD License which accompanies this \r
-# distribution. The full text of the license may be found at \r
+# This program and the accompanying materials are licensed and made available\r
+# under the terms and conditions of the BSD License which accompanies this\r
+# distribution. The full text of the license may be found at\r
 # http://opensource.org/licenses/bsd-license.php\r
 #\r
 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
@@ -69,7 +69,7 @@ def ParseHeaderCommentSection(CommentList, FileName = None, IsBinaryHeader = Fal
     else:\r
         STR_HEADER_COMMENT_START = "@file"\r
     HeaderCommentStage = HEADER_COMMENT_NOT_STARTED\r
-    \r
+\r
     #\r
     # first find the last copyright line\r
     #\r
@@ -79,24 +79,24 @@ def ParseHeaderCommentSection(CommentList, FileName = None, IsBinaryHeader = Fal
         if _IsCopyrightLine(Line):\r
             Last = Index\r
             break\r
-    \r
+\r
     for Item in CommentList:\r
         Line = Item[0]\r
         LineNo = Item[1]\r
-        \r
+\r
         if not Line.startswith(TAB_COMMENT_SPLIT) and Line:\r
             Logger.Error("\nUPT", FORMAT_INVALID, ST.ERR_INVALID_COMMENT_FORMAT, FileName, Item[1])\r
         Comment = CleanString2(Line)[1]\r
         Comment = Comment.strip()\r
         #\r
-        # if there are blank lines between License or Description, keep them as they would be \r
+        # if there are blank lines between License or Description, keep them as they would be\r
         # indication of different block; or in the position that Abstract should be, also keep it\r
         # as it indicates that no abstract\r
         #\r
         if not Comment and HeaderCommentStage not in [HEADER_COMMENT_LICENSE, \\r
                                                       HEADER_COMMENT_DESCRIPTION, HEADER_COMMENT_ABSTRACT]:\r
             continue\r
-        \r
+\r
         if HeaderCommentStage == HEADER_COMMENT_NOT_STARTED:\r
             if Comment.startswith(STR_HEADER_COMMENT_START):\r
                 HeaderCommentStage = HEADER_COMMENT_ABSTRACT\r
@@ -114,20 +114,20 @@ def ParseHeaderCommentSection(CommentList, FileName = None, IsBinaryHeader = Fal
                     ValidateCopyright(Result, ST.WRN_INVALID_COPYRIGHT, FileName, LineNo, ErrMsg)\r
                     Copyright += Comment + EndOfLine\r
                     HeaderCommentStage = HEADER_COMMENT_COPYRIGHT\r
-                else:                    \r
+                else:\r
                     Abstract += Comment + EndOfLine\r
                     HeaderCommentStage = HEADER_COMMENT_DESCRIPTION\r
             elif HeaderCommentStage == HEADER_COMMENT_DESCRIPTION:\r
                 #\r
                 # in case there is no description\r
-                #                \r
+                #\r
                 if _IsCopyrightLine(Comment):\r
                     Result, ErrMsg = _ValidateCopyright(Comment)\r
                     ValidateCopyright(Result, ST.WRN_INVALID_COPYRIGHT, FileName, LineNo, ErrMsg)\r
                     Copyright += Comment + EndOfLine\r
                     HeaderCommentStage = HEADER_COMMENT_COPYRIGHT\r
                 else:\r
-                    Description += Comment + EndOfLine                \r
+                    Description += Comment + EndOfLine\r
             elif HeaderCommentStage == HEADER_COMMENT_COPYRIGHT:\r
                 if _IsCopyrightLine(Comment):\r
                     Result, ErrMsg = _ValidateCopyright(Comment)\r
@@ -136,23 +136,23 @@ def ParseHeaderCommentSection(CommentList, FileName = None, IsBinaryHeader = Fal
                 else:\r
                     #\r
                     # Contents after copyright line are license, those non-copyright lines in between\r
-                    # copyright line will be discarded \r
+                    # copyright line will be discarded\r
                     #\r
                     if LineNo > Last:\r
                         if License:\r
                             License += EndOfLine\r
                         License += Comment + EndOfLine\r
-                        HeaderCommentStage = HEADER_COMMENT_LICENSE                \r
+                        HeaderCommentStage = HEADER_COMMENT_LICENSE\r
             else:\r
                 if not Comment and not License:\r
                     continue\r
                 License += Comment + EndOfLine\r
-                  \r
+\r
     return Abstract.strip(), Description.strip(), Copyright.strip(), License.strip()\r
 \r
 ## _IsCopyrightLine\r
-# check whether current line is copyright line, the criteria is whether there is case insensitive keyword "Copyright" \r
-# followed by zero or more white space characters followed by a "(" character \r
+# check whether current line is copyright line, the criteria is whether there is case insensitive keyword "Copyright"\r
+# followed by zero or more white space characters followed by a "(" character\r
 #\r
 # @param LineContent:  the line need to be checked\r
 # @return: True if current line is copyright line, False else\r
@@ -160,7 +160,7 @@ def ParseHeaderCommentSection(CommentList, FileName = None, IsBinaryHeader = Fal
 def _IsCopyrightLine (LineContent):\r
     LineContent = LineContent.upper()\r
     Result = False\r
-    \r
+\r
     ReIsCopyrightRe = re.compile(r"""(^|\s)COPYRIGHT *\(""", re.DOTALL)\r
     if ReIsCopyrightRe.search(LineContent):\r
         Result = True\r
@@ -169,23 +169,23 @@ def _IsCopyrightLine (LineContent):
 \r
 ## ParseGenericComment\r
 #\r
-# @param GenericComment: Generic comment list, element of \r
+# @param GenericComment: Generic comment list, element of\r
 #                        (CommentLine, LineNum)\r
 # @param ContainerFile:  Input value for filename of Dec file\r
-# \r
+#\r
 def ParseGenericComment (GenericComment, ContainerFile=None, SkipTag=None):\r
     if ContainerFile:\r
         pass\r
-    HelpTxt = None         \r
-    HelpStr = '' \r
-        \r
+    HelpTxt = None\r
+    HelpStr = ''\r
+\r
     for Item in GenericComment:\r
         CommentLine = Item[0]\r
         Comment = CleanString2(CommentLine)[1]\r
         if SkipTag is not None and Comment.startswith(SkipTag):\r
             Comment = Comment.replace(SkipTag, '', 1)\r
         HelpStr += Comment + '\n'\r
-        \r
+\r
     if HelpStr:\r
         HelpTxt = TextObject()\r
         if HelpStr.endswith('\n') and not HelpStr.endswith('\n\n') and HelpStr != '\n':\r
@@ -196,22 +196,22 @@ def ParseGenericComment (GenericComment, ContainerFile=None, SkipTag=None):
 \r
 ## ParsePcdErrorCode\r
 #\r
-# @param Value: original ErrorCode value \r
+# @param Value: original ErrorCode value\r
 # @param ContainerFile: Input value for filename of Dec file\r
-# @param LineNum: Line Num \r
-# \r
-def ParsePcdErrorCode (Value = None, ContainerFile = None, LineNum = None):      \r
-    try: \r
+# @param LineNum: Line Num\r
+#\r
+def ParsePcdErrorCode (Value = None, ContainerFile = None, LineNum = None):\r
+    try:\r
         if Value.strip().startswith((TAB_HEX_START, TAB_CAPHEX_START)):\r
             Base = 16\r
         else:\r
             Base = 10\r
         ErrorCode = long(Value, Base)\r
         if ErrorCode > PCD_ERR_CODE_MAX_SIZE or ErrorCode < 0:\r
-            Logger.Error('Parser', \r
+            Logger.Error('Parser',\r
                         FORMAT_NOT_SUPPORTED,\r
                         "The format %s of ErrorCode is not valid, should be UNIT32 type or long type" % Value,\r
-                        File = ContainerFile, \r
+                        File = ContainerFile,\r
                         Line = LineNum)\r
         #\r
         # To delete the tailing 'L'\r
@@ -220,27 +220,27 @@ def ParsePcdErrorCode (Value = None, ContainerFile = None, LineNum = None):
     except ValueError as XStr:\r
         if XStr:\r
             pass\r
-        Logger.Error('Parser', \r
+        Logger.Error('Parser',\r
                     FORMAT_NOT_SUPPORTED,\r
                     "The format %s of ErrorCode is not valid, should be UNIT32 type or long type" % Value,\r
-                    File = ContainerFile, \r
+                    File = ContainerFile,\r
                     Line = LineNum)\r
-    \r
+\r
 ## ParseDecPcdGenericComment\r
 #\r
-# @param GenericComment: Generic comment list, element of (CommentLine, \r
+# @param GenericComment: Generic comment list, element of (CommentLine,\r
 #                         LineNum)\r
 # @param ContainerFile:  Input value for filename of Dec file\r
-# \r
-def ParseDecPcdGenericComment (GenericComment, ContainerFile, TokenSpaceGuidCName, CName, MacroReplaceDict):       \r
-    HelpStr = '' \r
+#\r
+def ParseDecPcdGenericComment (GenericComment, ContainerFile, TokenSpaceGuidCName, CName, MacroReplaceDict):\r
+    HelpStr = ''\r
     PromptStr = ''\r
     PcdErr = None\r
     PcdErrList = []\r
     ValidValueNum = 0\r
     ValidRangeNum = 0\r
     ExpressionNum = 0\r
-        \r
+\r
     for (CommentLine, LineNum) in GenericComment:\r
         Comment = CleanString2(CommentLine)[1]\r
         #\r
@@ -252,13 +252,13 @@ def ParseDecPcdGenericComment (GenericComment, ContainerFile, TokenSpaceGuidCNam
             if MatchedStr:\r
                 Macro = MatchedStr.strip().lstrip('$(').rstrip(')').strip()\r
                 if Macro in MacroReplaceDict:\r
-                    Comment = Comment.replace(MatchedStr, MacroReplaceDict[Macro])    \r
+                    Comment = Comment.replace(MatchedStr, MacroReplaceDict[Macro])\r
         if Comment.startswith(TAB_PCD_VALIDRANGE):\r
             if ValidValueNum > 0 or ExpressionNum > 0:\r
-                Logger.Error('Parser', \r
+                Logger.Error('Parser',\r
                              FORMAT_NOT_SUPPORTED,\r
                              ST.WRN_MULTI_PCD_RANGES,\r
-                             File = ContainerFile, \r
+                             File = ContainerFile,\r
                              Line = LineNum)\r
             else:\r
                 PcdErr = PcdErrorObject()\r
@@ -280,21 +280,21 @@ def ParseDecPcdGenericComment (GenericComment, ContainerFile, TokenSpaceGuidCNam
             else:\r
                 Logger.Error("Parser",\r
                          FORMAT_NOT_SUPPORTED,\r
-                         Cause, \r
-                         ContainerFile, \r
+                         Cause,\r
+                         ContainerFile,\r
                          LineNum)\r
         elif Comment.startswith(TAB_PCD_VALIDLIST):\r
             if ValidRangeNum > 0 or ExpressionNum > 0:\r
-                Logger.Error('Parser', \r
+                Logger.Error('Parser',\r
                              FORMAT_NOT_SUPPORTED,\r
                              ST.WRN_MULTI_PCD_RANGES,\r
-                             File = ContainerFile, \r
+                             File = ContainerFile,\r
                              Line = LineNum)\r
             elif ValidValueNum > 0:\r
-                Logger.Error('Parser', \r
+                Logger.Error('Parser',\r
                              FORMAT_NOT_SUPPORTED,\r
                              ST.WRN_MULTI_PCD_VALIDVALUE,\r
-                             File = ContainerFile, \r
+                             File = ContainerFile,\r
                              Line = LineNum)\r
             else:\r
                 PcdErr = PcdErrorObject()\r
@@ -317,15 +317,15 @@ def ParseDecPcdGenericComment (GenericComment, ContainerFile, TokenSpaceGuidCNam
             else:\r
                 Logger.Error("Parser",\r
                          FORMAT_NOT_SUPPORTED,\r
-                         Cause, \r
-                         ContainerFile, \r
+                         Cause,\r
+                         ContainerFile,\r
                          LineNum)\r
         elif Comment.startswith(TAB_PCD_EXPRESSION):\r
             if ValidRangeNum > 0 or ValidValueNum > 0:\r
-                Logger.Error('Parser', \r
+                Logger.Error('Parser',\r
                              FORMAT_NOT_SUPPORTED,\r
                              ST.WRN_MULTI_PCD_RANGES,\r
-                             File = ContainerFile, \r
+                             File = ContainerFile,\r
                              Line = LineNum)\r
             else:\r
                 PcdErr = PcdErrorObject()\r
@@ -344,24 +344,24 @@ def ParseDecPcdGenericComment (GenericComment, ContainerFile, TokenSpaceGuidCNam
                 else:\r
                     PcdErr.SetExpression(Expression)\r
                 PcdErrList.append(PcdErr)\r
-            else:        \r
+            else:\r
                 Logger.Error("Parser",\r
                          FORMAT_NOT_SUPPORTED,\r
-                         Cause, \r
-                         ContainerFile, \r
-                         LineNum)                \r
+                         Cause,\r
+                         ContainerFile,\r
+                         LineNum)\r
         elif Comment.startswith(TAB_PCD_PROMPT):\r
             if PromptStr:\r
-                Logger.Error('Parser', \r
+                Logger.Error('Parser',\r
                              FORMAT_NOT_SUPPORTED,\r
                              ST.WRN_MULTI_PCD_PROMPT,\r
-                             File = ContainerFile, \r
+                             File = ContainerFile,\r
                              Line = LineNum)\r
             PromptStr = Comment.replace(TAB_PCD_PROMPT, "", 1).strip()\r
         else:\r
             if Comment:\r
                 HelpStr += Comment + '\n'\r
-    \r
+\r
     #\r
     # remove the last EOL if the comment is of format 'FOO\n'\r
     #\r
@@ -384,9 +384,9 @@ def ParseDecPcdTailComment (TailCommentList, ContainerFile):
     LineNum = TailCommentList[0][1]\r
 \r
     Comment = TailComment.lstrip(" #")\r
-    \r
+\r
     ReFindFirstWordRe = re.compile(r"""^([^ #]*)""", re.DOTALL)\r
-    \r
+\r
     #\r
     # get first word and compare with SUP_MODULE_LIST\r
     #\r
@@ -398,7 +398,7 @@ def ParseDecPcdTailComment (TailCommentList, ContainerFile):
     # parse line, it must have supported module type specified\r
     #\r
     if Comment.find(TAB_COMMENT_SPLIT) == -1:\r
-        Comment += TAB_COMMENT_SPLIT    \r
+        Comment += TAB_COMMENT_SPLIT\r
     SupMode, HelpStr = GetSplitValueList(Comment, TAB_COMMENT_SPLIT, 1)\r
     SupModuleList = []\r
     for Mod in GetSplitValueList(SupMode, TAB_SPACE_SPLIT):\r
@@ -407,8 +407,8 @@ def ParseDecPcdTailComment (TailCommentList, ContainerFile):
         elif Mod not in SUP_MODULE_LIST:\r
             Logger.Error("UPT",\r
                          FORMAT_INVALID,\r
-                         ST.WRN_INVALID_MODULE_TYPE%Mod, \r
-                         ContainerFile, \r
+                         ST.WRN_INVALID_MODULE_TYPE%Mod,\r
+                         ContainerFile,\r
                          LineNum)\r
         else:\r
             SupModuleList.append(Mod)\r
@@ -417,15 +417,15 @@ def ParseDecPcdTailComment (TailCommentList, ContainerFile):
 \r
 ## _CheckListExpression\r
 #\r
-# @param Expression: Pcd value list expression \r
+# @param Expression: Pcd value list expression\r
 #\r
 def _CheckListExpression(Expression):\r
     ListExpr = ''\r
     if TAB_VALUE_SPLIT in Expression:\r
-        ListExpr = Expression[Expression.find(TAB_VALUE_SPLIT)+1:] \r
+        ListExpr = Expression[Expression.find(TAB_VALUE_SPLIT)+1:]\r
     else:\r
         ListExpr = Expression\r
-        \r
+\r
     return IsValidListExpr(ListExpr)\r
 \r
 ## _CheckExpreesion\r
@@ -443,14 +443,14 @@ def _CheckExpression(Expression):
 ## _CheckRangeExpression\r
 #\r
 # @param Expression:    Pcd range expression\r
-#          \r
+#\r
 def _CheckRangeExpression(Expression):\r
     RangeExpr = ''\r
     if TAB_VALUE_SPLIT in Expression:\r
         RangeExpr = Expression[Expression.find(TAB_VALUE_SPLIT)+1:]\r
     else:\r
         RangeExpr = Expression\r
-        \r
+\r
     return IsValidRangeExpr(RangeExpr)\r
 \r
 ## ValidateCopyright\r
@@ -459,28 +459,28 @@ def _CheckRangeExpression(Expression):
 #\r
 def ValidateCopyright(Result, ErrType, FileName, LineNo, ErrMsg):\r
     if not Result:\r
-        Logger.Warn("\nUPT", ErrType, FileName, LineNo, ErrMsg) \r
+        Logger.Warn("\nUPT", ErrType, FileName, LineNo, ErrMsg)\r
 \r
 ## _ValidateCopyright\r
 #\r
 # @param Line:    Line that contains copyright information, # stripped\r
-# \r
+#\r
 # @retval Result: True if line is conformed to Spec format, False else\r
 # @retval ErrMsg: the detailed error description\r
-#  \r
+#\r
 def _ValidateCopyright(Line):\r
     if Line:\r
         pass\r
     Result = True\r
     ErrMsg = ''\r
-    \r
+\r
     return Result, ErrMsg\r
 \r
 def GenerateTokenList (Comment):\r
     #\r
     # Tokenize Comment using '#' and ' ' as token seperators\r
     #\r
-    RelplacedComment = None    \r
+    RelplacedComment = None\r
     while Comment != RelplacedComment:\r
         RelplacedComment = Comment\r
         Comment = Comment.replace('##', '#').replace('  ', ' ').replace(' ', '#').strip('# ')\r
@@ -500,25 +500,25 @@ def ParseComment (Comment, UsageTokens, TypeTokens, RemoveTokens, ParseVariable)
     Usage = None\r
     Type = None\r
     String = None\r
-    \r
+\r
     Comment = Comment[0]\r
-    \r
-    NumTokens = 2  \r
+\r
+    NumTokens = 2\r
     if ParseVariable:\r
-        # \r
-        # Remove white space around first instance of ':' from Comment if 'Variable' \r
+        #\r
+        # Remove white space around first instance of ':' from Comment if 'Variable'\r
         # is in front of ':' and Variable is the 1st or 2nd token in Comment.\r
         #\r
-        List = Comment.split(':', 1)    \r
+        List = Comment.split(':', 1)\r
         if len(List) > 1:\r
             SubList = GenerateTokenList (List[0].strip())\r
             if len(SubList) in [1, 2] and SubList[-1] == 'Variable':\r
-                if List[1].strip().find('L"') == 0:      \r
+                if List[1].strip().find('L"') == 0:\r
                     Comment = List[0].strip() + ':' + List[1].strip()\r
-        \r
-        # \r
+\r
+        #\r
         # Remove first instance of L"<VariableName> from Comment and put into String\r
-        # if and only if L"<VariableName>" is the 1st token, the 2nd token.  Or \r
+        # if and only if L"<VariableName>" is the 1st token, the 2nd token.  Or\r
         # L"<VariableName>" is the third token immediately following 'Variable:'.\r
         #\r
         End = -1\r
@@ -533,25 +533,25 @@ def ParseComment (Comment, UsageTokens, TypeTokens, RemoveTokens, ParseVariable)
                 End = String[2:].find('"')\r
         if End >= 0:\r
             SubList = GenerateTokenList (Comment[:Start])\r
-            if len(SubList) < 2: \r
+            if len(SubList) < 2:\r
                 Comment = Comment[:Start] + String[End + 3:]\r
                 String = String[:End + 3]\r
                 Type = 'Variable'\r
-                NumTokens = 1  \r
-    \r
+                NumTokens = 1\r
+\r
     #\r
-    # Initialze HelpText to Comment.  \r
+    # Initialze HelpText to Comment.\r
     # Content will be remove from HelpText as matching tokens are found\r
-    #  \r
+    #\r
     HelpText = Comment\r
-    \r
+\r
     #\r
     # Tokenize Comment using '#' and ' ' as token seperators\r
     #\r
     List = GenerateTokenList (Comment)\r
-    \r
+\r
     #\r
-    # Search first two tokens for Usage and Type and remove any matching tokens \r
+    # Search first two tokens for Usage and Type and remove any matching tokens\r
     # from HelpText\r
     #\r
     for Token in List[0:NumTokens]:\r
@@ -563,39 +563,39 @@ def ParseComment (Comment, UsageTokens, TypeTokens, RemoveTokens, ParseVariable)
             if Type is None and Token in TypeTokens:\r
                 Type = TypeTokens[Token]\r
                 HelpText = HelpText.replace(Token, '')\r
-            if Usage is not None:    \r
+            if Usage is not None:\r
                 for Token in List[0:NumTokens]:\r
                     if Token in RemoveTokens:\r
                         HelpText = HelpText.replace(Token, '')\r
-    \r
+\r
     #\r
     # If no Usage token is present and set Usage to UNDEFINED\r
-    #  \r
+    #\r
     if Usage is None:\r
         Usage = 'UNDEFINED'\r
-    \r
+\r
     #\r
     # If no Type token is present and set Type to UNDEFINED\r
-    #  \r
+    #\r
     if Type is None:\r
         Type = 'UNDEFINED'\r
-    \r
+\r
     #\r
     # If Type is not 'Variable:', then set String to None\r
-    #  \r
+    #\r
     if Type != 'Variable':\r
-        String = None  \r
-    \r
+        String = None\r
+\r
     #\r
     # Strip ' ' and '#' from the beginning of HelpText\r
-    # If HelpText is an empty string after all parsing is \r
+    # If HelpText is an empty string after all parsing is\r
     # complete then set HelpText to None\r
-    #  \r
+    #\r
     HelpText = HelpText.lstrip('# ')\r
     if HelpText == '':\r
         HelpText = None\r
-      \r
+\r
     #\r
     # Return parsing results\r
-    #  \r
-    return Usage, Type, String, HelpText  \r
+    #\r
+    return Usage, Type, String, HelpText\r