Changing the way baseline formatters are indicated

This commit adds a decorator for formatters to use to indicate
that they are able to display baseline results.  A formatter
should add @accepts_baseline before the report function to show
that it is capable of displaying candidate results.

Change-Id: I5532d4b322b7df9dbca8a2275bf6e273d2801a3a
This commit is contained in:
Travis McPeak 2015-11-12 11:48:55 +01:00
parent b7256cfcd2
commit 0e2d3c8e25
2 changed files with 30 additions and 0 deletions

View File

@ -107,6 +107,11 @@ def main():
os.environ['XDG_CONFIG_DIRS'] = '/etc:/usr/local/etc'
extension_mgr = _init_extensions()
baseline_formatters = [f.name for f in filter(lambda x:
hasattr(x.plugin,
'_accepts_baseline'),
extension_mgr.formatters)]
# now do normal startup
parser = argparse.ArgumentParser(
description='Bandit - a Python source code analyzer.',
@ -184,6 +189,8 @@ def main():
parser.add_argument(
'-b', '--baseline', dest='baseline', action='store',
default=None, help='Path to a baseline report, in JSON format. '
'Note: baseline reports must be output in one of '
'the following formats: ' + str(baseline_formatters)
)
parser.set_defaults(debug=False)
parser.set_defaults(verbose=False)
@ -228,6 +235,11 @@ def main():
logger.warn("Could not open baseline report: %s", args.baseline)
sys.exit(2)
if args.output_format not in baseline_formatters:
logger.warn('Baseline must be used with one of the following '
'formats: ' + str(baseline_formatters))
sys.exit(2)
if args.output_format != "json":
logger.info("using config: %s", config_file)
logger.info("running on Python %d.%d.%d", sys.version_info.major,

View File

@ -102,3 +102,21 @@ def takes_config(*args):
else:
name = args[0]
return _takes_config
def accepts_baseline(*args):
"""Decorator to indicate formatter accepts baseline results
Use of this decorator before a formatter indicates that it is able to deal
with baseline results. Specifically this means it has a way to display
candidate results and know when it should do so.
"""
def wrapper(func):
if not hasattr(func, '_accepts_baseline'):
func._accepts_baseline = True
logger.debug('accepts_baseline() decorator executed on %s',
func.__name__)
return func
return wrapper(args[0])