4949import inspect
5050
5151## Class for converting Google Protocol Buffers .proto files into C++ style output to enable Doxygen usage.
52- ##
52+ ##
5353## The C++ style output is printed into standard output.<br />
5454## There are three different logging levels for the class:
5555## <ul><li>#logNone: do not log anything</li>
@@ -75,7 +75,7 @@ def __init__(self):
7575 self .logFile = "proto2cpp.log"
7676 ## Error log file name.
7777 self .errorLogFile = "proto2cpp.error.log"
78- ## Logging level.
78+ ## Logging level.
7979 self .logLevel = self .logNone
8080
8181 ## Handles a file.
@@ -90,7 +90,7 @@ def handleFile(self, fileName):
9090 if fnmatch .fnmatch (filename , '*.proto' ):
9191 self .log ('\n XXXXXXXXXX\n XX ' + filename + '\n XXXXXXXXXX\n \n ' )
9292 # Open the file. Use try to detect whether or not we have an actual file.
93- if (sys .version_info > (3 , 0 )):
93+ if (sys .version_info >= (3 , 0 )):
9494 try :
9595 with open (filename , 'r' , encoding = 'utf8' ) as inputFile :
9696 self .parseFile (inputFile )
@@ -136,11 +136,11 @@ def handleFile(self, fileName):
136136 self .log ('\n XXXXXXXXXX\n XX ' + filename + ' --skipped--\n XXXXXXXXXX\n \n ' )
137137
138138 ## Parser function.
139- ##
139+ ##
140140 ## The function takes a .proto file object as input
141141 ## parameter and modifies the contents into C++ style.
142142 ## The modified data is printed into standard output.
143- ##
143+ ##
144144 ## @param inputFile Input file object
145145 #
146146 def parseFile (self , inputFile ):
@@ -161,47 +161,46 @@ def parseFile(self, inputFile):
161161 if matchSemicolon is not None and (matchComment is not None and matchSemicolon .start () < matchComment .start ()):
162162 comment = "///<" + line [matchComment .end ():]
163163 # Replace '.' in nested message references with '::'
164- # don't work for multi-nested references and generates problems with URLs and acronyms
164+ # don't work for multi-nested references and generates problems with URLs and acronyms
165165 #comment = re.sub(r'\s(\w+)\.(\w+)\s', r' \1::\2 ', comment)
166166 line = line [:matchComment .start ()]
167167 elif matchComment is not None :
168168 comment = "///" + line [matchComment .end ():]
169169 # replace '.' in nested message references with '::'
170- # don't work for multi-nested references and generates problems with URLs and acronyms
170+ # don't work for multi-nested references and generates problems with URLs and acronyms
171171 #comment = re.sub(r'\s(\w+)\.(\w+)\s', r' \1::\2 ', comment)
172172 line = line [:matchComment .start ()]
173173 else :
174174 comment = ""
175-
175+
176176 # line = line.replace(".", "::") but not in quoted strings (Necessary for import statement)
177177 line = re .sub (r'\.(?=(?:[^"]*"[^"]*")*[^"]*$)' ,r'::' ,line )
178178
179179 # Search for " option ...;", remove it
180180 line = re .sub (r'\boption\b[^;]+;' , r'' , line )
181-
181+
182182 # Search for " package ", make a namespace
183183 matchPackage = re .search (r"\bpackage\b" , line )
184184 if matchPackage is not None :
185185 isPackage = True
186186 # Convert to C++-style separator and block instead of statement
187187 line = "namespace" + line [:matchPackage .start ()] + line [matchPackage .end ():].replace (";" , " {" )
188-
188+
189189 # Search for " repeated " fields and make them ...
190190 #matchRepeated = re.search(r"\brepeated\b", line)
191191 #if matchRepeated is not None:
192192 # # Convert
193193 # line = re.sub(r'\brepeated\s+(\S+)', r' repeated \1', line)
194-
194+
195195 # Search for "enum", start changing all semicolons (";") to commas (",").
196196 matchEnum = re .search (r"\benum\b" , line )
197197 if matchEnum is not None :
198198 isEnum = True
199-
200- # Search again for semicolon if we have detected an enum, and replace semicolon with comma.
201- if isEnum is True and re .search (";" , line ) is not None :
202- matchSemicolon = re .search (";" , line )
203- line = line [:matchSemicolon .start ()] + "," + line [matchSemicolon .end ():]
204-
199+
200+ # Search semicolon if we have detected an enum, and replace semicolon with comma.
201+ if isEnum is True and matchSemicolon is not None :
202+ line = line .replace (";" , "," )
203+
205204 # Search for a closing brace.
206205 matchClosingBrace = re .search ("}" , line )
207206 if isEnum is True and matchClosingBrace is not None :
@@ -211,32 +210,32 @@ def parseFile(self, inputFile):
211210 # Message (to be struct) ends => add semicolon so that it'll
212211 # be a proper C(++) struct and Doxygen will handle it correctly.
213212 line = line [:matchClosingBrace .start ()] + "};" + line [matchClosingBrace .end ():]
214-
213+
215214 # Replacements change start of comment...
216215 matchMsg = re .search (r"\bmessage\b" , line )
217216 if matchMsg is not None :
218217 line = line [:matchMsg .start ()] + "struct" + line [matchMsg .end ():]
219-
218+
220219 # Replacements change start of comment...
221220 matchExt = re .search (r"\bextend\b" , line )
222221 if matchExt is not None :
223222 a_extend = line [matchExt .end ():]
224- matchName = re .search (r"\b\w[\S:]+ \b" , a_extend )
223+ matchName = re .search (r"\b\w[\S:]* \b" , a_extend )
225224 if matchName is not None :
226225 name = a_extend [matchName .start ():matchName .end ()]
227226 name = re .sub (r'\w+::' ,r'' ,name )
228227 a_extend = a_extend [:matchName .start ()] + name + ": public " + a_extend [matchName .start ():]
229228 else :
230- a_extend = "_Dummy: public " + a_extend ;
229+ a_extend = "_Dummy: public " + a_extend ;
231230 line = line [:matchExt .start ()] + "struct " + a_extend
232-
231+
233232 theOutput += line + comment
234-
233+
235234 if isPackage :
236235 # Close the package namespace
237236 theOutput += "}"
238237 isPackage = False
239-
238+
240239 # Now that we've got all lines in the string let's split the lines and print out
241240 # one by one.
242241 # This is a workaround to get rid of extra empty line at the end which print() method adds.
@@ -247,7 +246,7 @@ def parseFile(self, inputFile):
247246 # Our logger does not add extra line breaks so explicitly adding one to make the log more readable.
248247 self .log (line + '\n ' )
249248 else :
250- self .log ('\n --- skipped empty line' )
249+ self .log ('\n --- skipped empty line\n ' )
251250
252251 ## Writes @p string to log file.
253252 ##
0 commit comments