I am preprocessing a set of files where one file is included in another file with an include tag like shown below:
file A
include file B
include file C
contents of file A
include file D
contents of file A
Here i need to replace the include tags with contents of each of the respective files. Just like what a compiler does. I have two functions for that
def parseContent(self, rdbFilePath, content):
finalContent = self.removeComments(content)
includeFileSearch = re.compile(r'(?P<tag>(\s)*include+(\s*)\"(\s*)(?P<file>[a-zA-Z0-9\.\_/]*)\")')
for includes in includeFileSearch.finditer(finalContent):
finalContent = re.sub(includes.group('tag'),self.parseIncludes(rdbFilePath, includes.group('file')), finalContent)
return finalContent
def parseIncludes(self, rdbFilePath, file):
path = rdbFilePath + "/" + file
f = open(path)
pathDir = os.path.dirname(path)
includedFileContent = self.parseContent(pathDir, f.read())
return includedFileContent
As you can see, functions parseContent
and parseIncludes
is called in each other recursively to replace all the include tags in every file. The logic works fine. But it takes a bit long time to execute. Is there any better way to do the same with lesser execution time?