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.
- I've changed the variable name
filtertoignoresincefilteris a built-in type. - The lines in
ignore.txtare having the\nstripped and mapped to atupleinstead of alistso they can be used with thestartswithmethod when being read. - 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"