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
Section Tags Filter #5690
Section Tags Filter #5690
Conversation
coalib/parsing/DefaultArgParser.py
Outdated
@@ -177,6 +177,10 @@ def default_arg_parser(formatter_class=None): | |||
'-d', '--bear-dirs', type=PathArg, nargs='+', metavar='DIR', | |||
help='additional directories which may contain bears') | |||
|
|||
inputs_group.add_argument( |
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.
This belongs before --bear-dirs , probably after --files .
Lets wait for more feedback on position
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.
Currently,
Inputs:
-b, --bears names of bears to use
-f, --files files that should be checked
-i, --ignore files that should be ignored
--limit-files filter the `--files` argument's matches further
-d, --bear-dirs additional directories which may contain bears
-t, --tags tags of sections to execute
I think tags
should definitely be above bear-dirs
.
coalib/parsing/FilterHelper.py
Outdated
:param filter_args: | ||
Arguments of the filter to be passed in. | ||
For example: | ||
``['tags', ('java',)]`` |
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 use exampes from your cEP, and your own real needs.
this looks like language , not a tag which should be an arbitrary grouping which couldnt be achieved via any other means.
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.
I used them as examples, but I will update it.
coalib/parsing/FilterHelper.py
Outdated
:param filters: | ||
List of args based on ``sections`` has to be filtered. | ||
For example: | ||
``[['tags', ('save', 'change')], ['exclude', ('css', '!python')]]`` |
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.
again, dont use tags that are a language . it is confusing.
@@ -198,12 +198,16 @@ def test_merge_defaults(self): | |||
gather_configuration(lambda *args: True, | |||
self.log_printer, | |||
arg_list=['-S', 'value=1', 'test.value=2', | |||
'-c', escape(temporary, '\\')] + | |||
'--tags', 'save', '-c', |
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.
move the '-c',
back to the next line, so you dont modify that line.
it is also confusing to have the '-c',
on this line when its arg is on the next line.
@@ -198,12 +198,16 @@ def test_merge_defaults(self): | |||
gather_configuration(lambda *args: True, | |||
self.log_printer, | |||
arg_list=['-S', 'value=1', 'test.value=2', | |||
'-c', escape(temporary, '\\')] + | |||
'-c', escape(temporary, '\\'), | |||
'--tags', 'save',] + |
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.
The code does not comply to PEP8.
Origin: PEP8Bear, Section: autopep8
.
The issue can be fixed by applying the following patch:
--- a/tmp/tmpbnwihg5y/tests/settings/ConfigurationGatheringTest.py
+++ b/tmp/tmpbnwihg5y/tests/settings/ConfigurationGatheringTest.py
@@ -199,7 +199,7 @@
self.log_printer,
arg_list=['-S', 'value=1', 'test.value=2',
'-c', escape(temporary, '\\'),
- '--tags', 'save',] +
+ '--tags', 'save', ] +
self.min_args))
self.assertEqual(sections['cli'],
@@ -198,12 +198,16 @@ def test_merge_defaults(self): | |||
gather_configuration(lambda *args: True, | |||
self.log_printer, | |||
arg_list=['-S', 'value=1', 'test.value=2', | |||
'-c', escape(temporary, '\\')] + | |||
'-c', escape(temporary, '\\'), | |||
'--tags', 'save',] + |
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.
E231 missing whitespace after ','
Origin: PycodestyleBear (E231), Section: autopep8
.
coalib/parsing/FilterHelper.py
Outdated
|
||
|
||
def is_valid_filter(filter): | ||
return filter in available_filters | ||
|
||
|
||
def is_valid_section_filter(filter): |
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.
nope.
lets improve the filters system to support sections.
def foo_filter(thing, args):
every filter than checks whether is knows what is in thing
, e.g. using isinstance , and raises NotImplementedError if it doesnt get it. Existing filters only know about Bear
.
You make a small chance to the existing filters to make them compliant with the new API by adding a raise NotImplementedError
to them.
(Actually most of them understand could easily be improved to understand Section
, but that is beyond the scope of your change, and each will need to be carefully done wrt the CLI args to ensure we build a good user experience)
coalib/parsing/FilterHelper.py
Outdated
if not filter_args or len(filter_args) == 0: | ||
return all_sections | ||
|
||
filter_function = available_section_filters[filter_name] |
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.
not needed. below you would catch NotImplementedError and ignore it.
:return: ``True`` if this section matches the criteria inside args, | ||
``False`` otherwise. | ||
""" | ||
if section.name == 'cli': |
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.
I agree with @shreyans800755 that this doesnt belong in the filter.
ksdme@f8fc69c#r29964403
The cli
section should be kept hidden inside coalib/settings
, as a special case implementation detail. It annoys me that we have any UI messages that mention this section -- the user cant see it, so they shouldnt hear about it.
And likewise developers in other parts of coalib shouldnt need to deal with that section.
coalib/coala_main.py
Outdated
@@ -159,6 +161,15 @@ def run_coala(console_printer=None, | |||
(section_name, sections[section_name]) | |||
for section_name in targets) | |||
|
|||
# Collect all the section filters which are based on args, |
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.
I think we could remove the last ,
Updated to use
|
coalib/parsing/FilterHelper.py
Outdated
""" | ||
things = bears |
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.
Let's call it items rather than things
List of filters in standard filter format, i.e | ||
``[['filter_name', 'arg1', 'arg2']]``. | ||
""" | ||
if args is None: |
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.
Do we have any practical case for this condition ?
If so, is there way we can check this before calling this function ?
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.
run_coala
:param arg_parser: Instance of ArgParser that is used to non-setting arguments.
:param arg_list: The CLI argument list.
:param args: Alternative pre-parsed CLI arguments.
This is not just about the tests, since args to run_coala()
can be optionally passed instead of arg_list, I think we need to merge opts from both, so as to enable the internal usage of this filter. Tests also use can use this by directly calling run_coala()
with an arg list.
List of filters in standard filter format, i.e | ||
``[['filter_name', 'arg1', 'arg2']]``. | ||
""" | ||
if args is None: |
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.
Because this is not the job of this function. This function is just a getter
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.
It looks like a helper for the tests , so should be moved into the tests.
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.
run_coala
:param arg_parser: Instance of ArgParser that is used to non-setting arguments.
:param arg_list: The CLI argument list.
:param args: Alternative pre-parsed CLI arguments.
This is not just about the tests, since args to run_coala()
can be optionally passed instead of arg_list, I think we need to merge opts from both, so as to enable the internal usage of this filter. Tests also use can use this by directly calling run_coala()
with an arg list.
coalib/parsing/FilterHelper.py
Outdated
@@ -49,22 +50,83 @@ def apply_filter(filter_name, filter_args, all_bears=None): | |||
return local_bears, global_bears | |||
|
|||
|
|||
def apply_filters(filters, bears=None): | |||
def _apply_section_filter(filter_name, filter_args, all_sections): |
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.
move this after the apply_filters
function so that the diff better aligns, and probably at the end as it is a private function
List of filters in standard filter format, i.e | ||
``[['filter_name', 'arg1', 'arg2']]``. | ||
""" | ||
if args is None: |
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.
It looks like a helper for the tests , so should be moved into the tests.
from functools import wraps | ||
|
||
|
||
def typed_filter(type_classes, msg=None): |
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.
(almost) very generic ... belongs in coala_utils
::decorators.py
and this is enforce_duck_type
or enforce_duct_tape
? ;-)
and to make calling generic, use
@typed_filter(bear='bearclass')
def language_filter(bear, args):
```
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.
belongs in coala_utils::decorators.py
Unfortunately not ...
Because we are not allowed to do imports here! argh.
It was built: https://gitlab.com/coala/coala-utils/merge_requests/82
coalib/parsing/filters/decorators.py
Outdated
def decorator(filter): | ||
|
||
@wraps(filter) | ||
def decorated_filter(thing, *args, **kargs): |
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.
thing
-> obj
or object_
coalib/parsing/filters/decorators.py
Outdated
break | ||
else: | ||
raise NotImplementedError( | ||
msg or '\'{filter}\' filter can only handle {type_name}. ' |
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.
it was going so well until you mentioned filter here.
the thing you are wrapping has a name, which you are using here.
This decorator is a developer helper ... to catch when they have done the wrong thing. it shouldnt give pretty names ... the caller needs to detect this and make it user-friendly.
Why should a NotImplementedError
say "language_filter filter" .. ? language_filter
is better, and the backtrace is even better, so no need to tidy it up.
To help, create a subclass of NotImplementedError
and stuff into it extra attributes to record what went wrong, such as the callee, expected classes, and the incorrect object which was encountered. Then whoever is catching it can understand what went wrong and create a user-friendly message.
|
||
|
||
@typed_filter(('bearclass', 'Bear', 'Section')) | ||
def tag_section_filter(section_or_bear, args): |
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.
section_tag_filter
coalib/parsing/filters/__init__.py
Outdated
@@ -5,9 +5,11 @@ | |||
from .LanguageFilter import language_filter | |||
from .CanDetectFilter import can_detect_filter | |||
from .CanFixFilter import can_fix_filter | |||
from .SectionTagFilter import section_tag_filter |
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.
This file contains unused source code.
Origin: PyUnusedCodeBear, Section: flakes
.
The issue can be fixed by applying the following patch:
--- a/tmp/tmp9_9zjivn/coalib/parsing/filters/__init__.py
+++ b/tmp/tmp9_9zjivn/coalib/parsing/filters/__init__.py
@@ -5,7 +5,6 @@
from .LanguageFilter import language_filter
from .CanDetectFilter import can_detect_filter
from .CanFixFilter import can_fix_filter
-from .SectionTagFilter import section_tag_filter
available_filters = {'language': language_filter,
726414c
to
93efd22
Compare
Section filtering based on configurations is now possible. This commit also adds - Typed filters, filters can be decorated to specifically support certain types of things. - Supports section filtering.
The new API requires filters to be type annotated to support for proper usage resolution, this commit updates the filters with that requirement.
SectionTagsFilter can be used with --filter-by to filter sections that are to be executed based on their tags property.
93efd22
to
ba2c2f7
Compare
@gitmate-bot ff |
Hey! I'm GitMate.io! This pull request is being fastforwarded automatically. Please DO NOT push while fastforward is in progress or your changes would be lost permanently |
Automated fastforward with GitMate.io was successful! 🎉 |
PTAL cEP-0028