0

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?

Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55
  • What do you mean by "one file is included in another file with an include tag"? – asn-0184 Apr 19 '19 at 04:22
  • This question might be more appropriate for [Code Review](https://codereview.stackexchange.com/help/on-topic). – MikaelF Apr 19 '19 at 04:23
  • @ajinLJA-0184 just like how contents are included in your code – Hari Krishnan Apr 19 '19 at 04:39
  • I just found this today, but it's missing substitution features: https://github.com/facebook/pyre2/ My thought is that you might be able to work around the substitution. I have no experience with it though, so, I'm not sure if it's appreciably faster, or if it will work well enough for your use case. – Hikash Apr 19 '19 at 04:57

0 Answers0