-
Notifications
You must be signed in to change notification settings - Fork 472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Respect options from baseline #124
Changes from 4 commits
9e0b619
17fd458
446cb53
37d7e06
a2f74dd
47b4930
266999a
add69bc
024ffc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ def add_use_all_plugins_argument(parser): | |
parser.add_argument( | ||
'--use-all-plugins', | ||
action='store_true', | ||
help='Use all available plugins to scan files', | ||
help='Use all available plugins to scan files.', | ||
) | ||
|
||
|
||
|
@@ -309,6 +309,7 @@ def consolidate_args(args): | |
|
||
active_plugins = {} | ||
disabled_plugins = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that I think about it, what is our purpose of returning a set of disabled_plugins? Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above. |
||
param_from_default = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good idea. I would make a suggestion to rename it to |
||
|
||
for plugin in PluginOptions.all_plugins: | ||
arg_name = PluginOptions._convert_flag_text_to_argument_name( | ||
|
@@ -342,13 +343,15 @@ def consolidate_args(args): | |
|
||
if default_value and related_args[arg_name] is None: | ||
related_args[arg_name] = default_value | ||
param_from_default[arg_name] = True | ||
|
||
active_plugins.update({ | ||
plugin.classname: related_args, | ||
}) | ||
|
||
args.plugins = active_plugins | ||
args.disabled_plugins = disabled_plugins | ||
args.param_from_default = param_from_default | ||
|
||
def _add_custom_limits(self): | ||
high_entropy_help_text = ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,28 +32,68 @@ def from_parser_builder(plugins_dict): | |
|
||
|
||
def merge_plugin_from_baseline(baseline_plugins, args): | ||
# if --use-all-plugins | ||
# include all parsed plugins | ||
# else | ||
# include all baseline plugins | ||
# remove all disabled plugins | ||
""" | ||
:type baseline_plugins: tuple of BasePlugin | ||
:param baseline_plugins: BasePlugin instances from baseline file | ||
|
||
:type args: dict | ||
:param args: diction of arguments parsed from usage | ||
|
||
plugins = [] | ||
param priority: input param > baseline param > default | ||
|
||
:Returns tuple of initialized plugins | ||
""" | ||
def _remove_key(d, key): | ||
r = dict(d) | ||
r.pop(key) | ||
return r | ||
baseline_plugins_dict = { | ||
vars(plugin)["name"]: _remove_key(vars(plugin), "name") | ||
for plugin in baseline_plugins | ||
} | ||
|
||
# Use input plugin as starting point | ||
if args.use_all_plugins: | ||
plugins = from_parser_builder(args.plugins) | ||
elif args.disabled_plugins: # strip some plugins off baseline | ||
plugins = _merge_plugin_from_baseline(baseline_plugins, args) | ||
else: | ||
plugins = baseline_plugins | ||
return plugins | ||
|
||
|
||
def _merge_plugin_from_baseline(baseline_plugins, args): | ||
merged_plugins_dict = {vars(plugin)['name']: plugin for plugin in baseline_plugins} | ||
for plugin_name in args.disabled_plugins: | ||
if plugin_name in merged_plugins_dict: | ||
merged_plugins_dict.pop(plugin_name) | ||
return merged_plugins_dict.values() | ||
# input param and default param are used | ||
plugins_dict = dict(args.plugins) | ||
|
||
# baseline param priority > default | ||
for plugin_name, plugin_params in list(baseline_plugins_dict.items()): | ||
for param_name, param_value in list(plugin_params.items()): | ||
from_default = args.param_from_default.get(param_name, False) | ||
if from_default: | ||
try: | ||
plugins_dict[plugin_name][param_name] = param_value | ||
except KeyError: | ||
log.warning( | ||
'Baseline contain plugin %s which is not in all plugins! Ignoring...' | ||
% (plugin_name), | ||
) | ||
|
||
return from_parser_builder(plugins_dict) | ||
|
||
# Use baseline plugin as starting point | ||
plugins_dict = baseline_plugins_dict | ||
if args.disabled_plugins: | ||
for plugin_name in args.disabled_plugins: | ||
if plugin_name in plugins_dict: | ||
plugins_dict.pop(plugin_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
# input param priority > baseline | ||
input_plugins_dict = dict(args.plugins) | ||
for plugin_name, plugin_params in list(input_plugins_dict.items()): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't need to turn Same goes for other uses of this behavior. |
||
for param_name, param_value in list(plugin_params.items()): | ||
from_default = args.param_from_default.get(param_name, False) | ||
if from_default is False: | ||
try: | ||
plugins_dict[plugin_name][param_name] = param_value | ||
except KeyError: | ||
log.warning( | ||
'%s specified, but %s not configured! Ignoring...' | ||
% ("".join(["--", param_name.replace("_", "-")]), plugin_name), | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make this DRYer, seeing that we use it below as well? One idea might be to implement an iterator, or just encapsulate it in a function. For example:
Then, you can do:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's been updated now. Although having more lines, but I do think the logic is a little bit more clear. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clearer logic >> more lines, especially when it comes to long term maintenance 😸 |
||
|
||
return from_parser_builder(plugins_dict) | ||
|
||
|
||
def from_plugin_classname(plugin_classname, **kwargs): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put a comment here that explains the rationale for why we're returning these new values.
I understand the purpose of doing it, and that there's really no better way to incorporate the baseline values in it. It's just unfortunate that this hairy logic to perform prioritization of plugins needs to live in two places (unlike this doc string suggests).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
disabled_plugins
is mainly used to filter off some disabled plugins when reading the plugin list from baseline. Relates to the comment below, it's possible to generate thedisabled_plugins
list on-demand by calculating the difference between all plugins and active plugins. I can add a helper function to theusage.py
.I was using
disabled_plugins
earlier to avoid re-iterating on the all plugins to get such list.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha. Makes sense.
Thanks for adding the helper function anyway!