Files
bizhang_-obav/Tools/px4params/srcscanner.py

44 lines
1.4 KiB
Python
Raw Normal View History

2013-10-18 03:47:15 +04:00
import os
import re
2014-02-16 01:47:11 +01:00
import codecs
2013-10-18 03:47:15 +04:00
class SourceScanner(object):
2013-10-18 03:47:15 +04:00
"""
Traverses directory tree, reads all source files, and passes their contents
to the Parser.
"""
def ScanDir(self, srcdir, parser):
"""
Scans provided path and passes all found contents to the parser using
parser.Parse method.
"""
2015-08-28 11:06:12 +02:00
extensions1 = tuple([".h"])
extensions2 = tuple([".cpp", ".c"])
2013-10-18 03:47:15 +04:00
for dirname, dirnames, filenames in os.walk(srcdir):
for filename in filenames:
2015-08-28 11:06:12 +02:00
if filename.endswith(extensions1):
path = os.path.join(dirname, filename)
if not self.ScanFile(path, parser):
return False
for filename in filenames:
if filename.endswith(extensions2):
2015-07-22 09:01:31 -07:00
path = os.path.join(dirname, filename)
if not self.ScanFile(path, parser):
return False
return True
2013-10-18 03:47:15 +04:00
def ScanFile(self, path, parser):
"""
Scans provided file and passes its contents to the parser using
parser.Parse method.
"""
2014-02-16 01:47:11 +01:00
with codecs.open(path, 'r', 'utf-8') as f:
2014-12-23 14:34:53 +01:00
try:
contents = f.read()
except:
contents = ''
print('Failed reading file: %s, skipping content.' % path)
pass
2015-07-22 09:01:31 -07:00
return parser.Parse(contents)