Skip to content Skip to sidebar Skip to footer

Filtering Directories When Parsing Cpp Files In Get-includes In Python Bindings/clang

I should write a python-clang parser which returns all the inclusions in cpp files. So I use sth like the following code: def _main(): from clang.cindex import Index from o

Solution 1:

You can do this in your for loop:

...
for i in t.get_includes():
    if not i.include in filter:
        print i.include
...

As for the config file containing the exclusions. You could do something like this:

def_main():
    ...
    withopen('/path/to/file/ignore.txt') as f:
        filter = f.readlines()
    ...

Then in ignore.txt:

/usr/lib
/usr/include
...

UPDATE

Based on your comments and edits to your question.

def_main():
    ...
    withopen('/path/to/file/ignore.txt') as f:
        ignore = map(lambda l: l.strip(), f.readlines())

    for i in t.get_includes():
        ifnot i.include.startswith(ignore):
            print i.include

Couple of things to note here.

  1. I've changed the variable name filter to ignore since filter is a built-in type.
  2. The lines in ignore.txt are having the \n stripped and mapped to a tuple instead of a list so they can be used with the startswith method when being read.
  3. You could also use list comprehension to put the filtered results into a list to be used later.

results = [i.include for i in t.get_includes() if not i.startswith(ignore)]

Post a Comment for "Filtering Directories When Parsing Cpp Files In Get-includes In Python Bindings/clang"