]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/UPT/Library/CommentGenerating.py
BaseTools/UPT: Porting UPT Tool from Python2 to Python3
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Library / CommentGenerating.py
CommitLineData
4234283c
LG
1## @file\r
2# This file is used to define comment generating interface\r
3#\r
64285f15 4# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
4234283c 5#\r
f7496d71
LG
6# This program and the accompanying materials are licensed and made available\r
7# under the terms and conditions of the BSD License which accompanies this\r
8# distribution. The full text of the license may be found at\r
4234283c
LG
9# http://opensource.org/licenses/bsd-license.php\r
10#\r
11# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13#\r
14\r
15'''\r
16CommentGenerating\r
17'''\r
18\r
19##\r
20# Import Modules\r
21#\r
64285f15 22from Library.StringUtils import GetSplitValueList\r
4234283c
LG
23from Library.DataType import TAB_SPACE_SPLIT\r
24from Library.DataType import TAB_INF_GUIDTYPE_VAR\r
25from Library.DataType import USAGE_ITEM_NOTIFY\r
26from Library.DataType import ITEM_UNDEFINED\r
421ccda3
HC
27from Library.DataType import TAB_HEADER_COMMENT\r
28from Library.DataType import TAB_BINARY_HEADER_COMMENT\r
29from Library.DataType import TAB_COMMENT_SPLIT\r
30from Library.DataType import TAB_SPECIAL_COMMENT\r
31from Library.DataType import END_OF_LINE\r
32from Library.DataType import TAB_COMMENT_EDK1_SPLIT\r
33from Library.DataType import TAB_COMMENT_EDK1_START\r
34from Library.DataType import TAB_COMMENT_EDK1_END\r
35from Library.DataType import TAB_STAR\r
36from Library.DataType import TAB_PCD_PROMPT\r
37from Library.UniClassObject import ConvertSpecialUnicodes\r
38from Library.Misc import GetLocalValue\r
4234283c
LG
39## GenTailCommentLines\r
40#\r
41# @param TailCommentLines: the tail comment lines that need to be generated\r
f7496d71 42# @param LeadingSpaceNum: the number of leading space needed for non-first\r
4234283c 43# line tail comment\r
f7496d71 44#\r
4234283c 45def GenTailCommentLines (TailCommentLines, LeadingSpaceNum = 0):\r
421ccda3
HC
46 TailCommentLines = TailCommentLines.rstrip(END_OF_LINE)\r
47 CommentStr = TAB_SPACE_SPLIT*2 + TAB_SPECIAL_COMMENT + TAB_SPACE_SPLIT + \\r
48 (END_OF_LINE + LeadingSpaceNum * TAB_SPACE_SPLIT + TAB_SPACE_SPLIT*2 + TAB_SPECIAL_COMMENT + \\r
49 TAB_SPACE_SPLIT).join(GetSplitValueList(TailCommentLines, END_OF_LINE))\r
f7496d71 50\r
4234283c
LG
51 return CommentStr\r
52\r
53## GenGenericComment\r
54#\r
55# @param CommentLines: Generic comment Text, maybe Multiple Lines\r
f7496d71 56#\r
4234283c
LG
57def GenGenericComment (CommentLines):\r
58 if not CommentLines:\r
59 return ''\r
421ccda3
HC
60 CommentLines = CommentLines.rstrip(END_OF_LINE)\r
61 CommentStr = TAB_SPECIAL_COMMENT + TAB_SPACE_SPLIT + (END_OF_LINE + TAB_COMMENT_SPLIT + TAB_SPACE_SPLIT).join\\r
62 (GetSplitValueList(CommentLines, END_OF_LINE)) + END_OF_LINE\r
4234283c
LG
63 return CommentStr\r
64\r
65## GenGenericCommentF\r
66#\r
67# similar to GenGenericComment but will remove <EOL> at end of comment once,\r
68# and for line with only <EOL>, '#\n' will be generated instead of '# \n'\r
69#\r
70# @param CommentLines: Generic comment Text, maybe Multiple Lines\r
f7496d71
LG
71# @return CommentStr: Generated comment line\r
72#\r
421ccda3 73def GenGenericCommentF (CommentLines, NumOfPound=1, IsPrompt=False, IsInfLibraryClass=False):\r
4234283c
LG
74 if not CommentLines:\r
75 return ''\r
4234283c
LG
76 #\r
77 # if comment end with '\n', then remove it to prevent one extra line\r
78 # generate later on\r
79 #\r
421ccda3 80 if CommentLines.endswith(END_OF_LINE):\r
4234283c 81 CommentLines = CommentLines[:-1]\r
4234283c 82 CommentStr = ''\r
421ccda3
HC
83 if IsPrompt:\r
84 CommentStr += TAB_COMMENT_SPLIT * NumOfPound + TAB_SPACE_SPLIT + TAB_PCD_PROMPT + TAB_SPACE_SPLIT + \\r
85 CommentLines.replace(END_OF_LINE, '') + END_OF_LINE\r
86 else:\r
87 CommentLineList = GetSplitValueList(CommentLines, END_OF_LINE)\r
88 FindLibraryClass = False\r
89 for Line in CommentLineList:\r
90 # If this comment is for @libraryclass and it has multiple lines\r
91 # make sure the second lines align to the first line after @libraryclass as below\r
92 #\r
93 # ## @libraryclass XYZ FIRST_LINE\r
94 # ## ABC SECOND_LINE\r
95 #\r
96 if IsInfLibraryClass and Line.find(u'@libraryclass ') > -1:\r
97 FindLibraryClass = True\r
98 if Line == '':\r
99 CommentStr += TAB_COMMENT_SPLIT * NumOfPound + END_OF_LINE\r
100 else:\r
101 if FindLibraryClass and Line.find(u'@libraryclass ') > -1:\r
102 CommentStr += TAB_COMMENT_SPLIT * NumOfPound + TAB_SPACE_SPLIT + Line + END_OF_LINE\r
103 elif FindLibraryClass:\r
104 CommentStr += TAB_COMMENT_SPLIT * NumOfPound + TAB_SPACE_SPLIT * 16 + Line + END_OF_LINE\r
105 else:\r
106 CommentStr += TAB_COMMENT_SPLIT * NumOfPound + TAB_SPACE_SPLIT + Line + END_OF_LINE\r
f7496d71 107\r
4234283c
LG
108 return CommentStr\r
109\r
110\r
111## GenHeaderCommentSection\r
112#\r
113# Generate Header comment sections\r
114#\r
f7496d71 115# @param Abstract One line of abstract\r
4234283c
LG
116# @param Description multiple lines of Description\r
117# @param Copyright possible multiple copyright lines\r
118# @param License possible multiple license lines\r
119#\r
421ccda3
HC
120def GenHeaderCommentSection(Abstract, Description, Copyright, License, IsBinaryHeader=False, \\r
121 CommChar=TAB_COMMENT_SPLIT):\r
4234283c 122 Content = ''\r
421ccda3
HC
123\r
124 #\r
125 # Convert special character to (c), (r) and (tm).\r
126 #\r
421ccda3 127 if IsBinaryHeader:\r
1b2e0772 128 Content += CommChar * 2 + TAB_SPACE_SPLIT + TAB_BINARY_HEADER_COMMENT + '\n'\r
421ccda3
HC
129 elif CommChar == TAB_COMMENT_EDK1_SPLIT:\r
130 Content += CommChar + TAB_SPACE_SPLIT + TAB_COMMENT_EDK1_START + TAB_STAR + TAB_SPACE_SPLIT +\\r
1b2e0772 131 TAB_HEADER_COMMENT + '\n'\r
421ccda3 132 else:\r
1b2e0772 133 Content += CommChar * 2 + TAB_SPACE_SPLIT + TAB_HEADER_COMMENT + '\n'\r
4234283c 134 if Abstract:\r
1b2e0772
YZ
135 Abstract = Abstract.rstrip('\n')\r
136 Content += CommChar + TAB_SPACE_SPLIT + ('\n' + CommChar + TAB_SPACE_SPLIT).join(GetSplitValueList\\r
cf2b2bde 137 (Abstract, '\n'))\r
1b2e0772 138 Content += '\n' + CommChar + '\n'\r
4234283c 139 else:\r
1b2e0772 140 Content += CommChar + '\n'\r
4234283c
LG
141\r
142 if Description:\r
1b2e0772
YZ
143 Description = Description.rstrip('\n')\r
144 Content += CommChar + TAB_SPACE_SPLIT + ('\n' + CommChar + TAB_SPACE_SPLIT).join(GetSplitValueList\\r
cf2b2bde 145 (Description, '\n'))\r
1b2e0772 146 Content += '\n' + CommChar + '\n'\r
f7496d71 147\r
4234283c 148 #\r
f7496d71 149 # There is no '#\n' line to separate multiple copyright lines in code base\r
4234283c
LG
150 #\r
151 if Copyright:\r
1b2e0772
YZ
152 Copyright = Copyright.rstrip('\n')\r
153 Content += CommChar + TAB_SPACE_SPLIT + ('\n' + CommChar + TAB_SPACE_SPLIT).join\\r
cf2b2bde 154 (GetSplitValueList(Copyright, '\n'))\r
1b2e0772 155 Content += '\n' + CommChar + '\n'\r
4234283c
LG
156\r
157 if License:\r
1b2e0772
YZ
158 License = License.rstrip('\n')\r
159 Content += CommChar + TAB_SPACE_SPLIT + ('\n' + CommChar + TAB_SPACE_SPLIT).join(GetSplitValueList\\r
cf2b2bde 160 (License, '\n'))\r
1b2e0772 161 Content += '\n' + CommChar + '\n'\r
f7496d71 162\r
421ccda3 163 if CommChar == TAB_COMMENT_EDK1_SPLIT:\r
1b2e0772 164 Content += CommChar + TAB_SPACE_SPLIT + TAB_STAR + TAB_COMMENT_EDK1_END + '\n'\r
421ccda3 165 else:\r
1b2e0772 166 Content += CommChar * 2 + '\n'\r
f7496d71 167\r
4234283c
LG
168 return Content\r
169\r
170\r
171## GenInfPcdTailComment\r
172# Generate Pcd tail comment for Inf, this would be one line comment\r
173#\r
174# @param Usage: Usage type\r
175# @param TailCommentText: Comment text for tail comment\r
f7496d71 176#\r
4234283c
LG
177def GenInfPcdTailComment (Usage, TailCommentText):\r
178 if (Usage == ITEM_UNDEFINED) and (not TailCommentText):\r
179 return ''\r
f7496d71 180\r
4234283c
LG
181 CommentLine = TAB_SPACE_SPLIT.join([Usage, TailCommentText])\r
182 return GenTailCommentLines(CommentLine)\r
183\r
184## GenInfProtocolPPITailComment\r
185# Generate Protocol/PPI tail comment for Inf\r
186#\r
187# @param Usage: Usage type\r
188# @param TailCommentText: Comment text for tail comment\r
f7496d71 189#\r
4234283c
LG
190def GenInfProtocolPPITailComment (Usage, Notify, TailCommentText):\r
191 if (not Notify) and (Usage == ITEM_UNDEFINED) and (not TailCommentText):\r
192 return ''\r
f7496d71 193\r
4234283c
LG
194 if Notify:\r
195 CommentLine = USAGE_ITEM_NOTIFY + " ## "\r
196 else:\r
197 CommentLine = ''\r
f7496d71 198\r
4234283c
LG
199 CommentLine += TAB_SPACE_SPLIT.join([Usage, TailCommentText])\r
200 return GenTailCommentLines(CommentLine)\r
201\r
202## GenInfGuidTailComment\r
203# Generate Guid tail comment for Inf\r
204#\r
205# @param Usage: Usage type\r
206# @param TailCommentText: Comment text for tail comment\r
f7496d71 207#\r
4234283c
LG
208def GenInfGuidTailComment (Usage, GuidTypeList, VariableName, TailCommentText):\r
209 GuidType = GuidTypeList[0]\r
210 if (Usage == ITEM_UNDEFINED) and (GuidType == ITEM_UNDEFINED) and \\r
211 (not TailCommentText):\r
212 return ''\r
f7496d71
LG
213\r
214 FirstLine = Usage + " ## " + GuidType\r
4234283c
LG
215 if GuidType == TAB_INF_GUIDTYPE_VAR:\r
216 FirstLine += ":" + VariableName\r
f7496d71 217\r
4234283c
LG
218 CommentLine = TAB_SPACE_SPLIT.join([FirstLine, TailCommentText])\r
219 return GenTailCommentLines(CommentLine)\r
220\r
221## GenDecGuidTailComment\r
222#\r
223# @param SupModuleList: Supported module type list\r
f7496d71
LG
224#\r
225def GenDecTailComment (SupModuleList):\r
4234283c
LG
226 CommentLine = TAB_SPACE_SPLIT.join(SupModuleList)\r
227 return GenTailCommentLines(CommentLine)\r
228\r
229\r
230## _GetHelpStr\r
f7496d71 231# get HelpString from a list of HelpTextObject, the priority refer to\r
4234283c
LG
232# related HLD\r
233#\r
234# @param HelpTextObjList: List of HelpTextObject\r
f7496d71 235#\r
4234283c
LG
236# @return HelpStr: the help text string found, '' means no help text found\r
237#\r
238def _GetHelpStr(HelpTextObjList):\r
f7496d71 239 ValueList = []\r
4234283c 240 for HelpObj in HelpTextObjList:\r
421ccda3
HC
241 ValueList.append((HelpObj.GetLang(), HelpObj.GetString()))\r
242 return GetLocalValue(ValueList, True)\r