Skip to content

Instantly share code, notes, and snippets.

@blaggacao
Last active July 27, 2019 16:59
Show Gist options
  • Save blaggacao/13cf79fad489bca962e4a43eb580e9ee to your computer and use it in GitHub Desktop.
Save blaggacao/13cf79fad489bca962e4a43eb580e9ee to your computer and use it in GitHub Desktop.
pylint-odoo: attribute-string-redundant
# Copyright 2019 David Arnold
# Licensed under the MIT License
import re
from bowler.helpers import print_tree, print_selector_pattern, find_last
from bowler.query import Query
from bowler.types import TOKEN, SYMBOL
from fissix.pytree import Leaf, Node
from fissix.fixer_util import Comma
FIELDS_METHOD = {
'Many2many': 4,
'One2many': 2,
'Many2one': 1,
'Reference': 1,
'Selection': 1,
}
KWARGS_PATTERN = """
simple_stmt< any*
expr_stmt<
field_name=NAME '=' power< 'fields'
trailer< '.' field_class=any >
trailer< '('
(
arglist<
any* field_kwarg=argument<'string' '=' field_string=any> any*
>
|
field_kwarg=argument<'string' '=' field_string=any>
)
')'>
>
>
any* >
"""
ARGS_PATTERN = """
simple_stmt< any*
expr_stmt<
field_name=NAME '=' power< 'fields'
trailer< '.' field_class=any >
trailer< '(' field_arglist=arglist ')'>
>
>
any* >
"""
def main():
(
Query("./src")
.select(KWARGS_PATTERN)
.filter(is_kwarg_string_redundant)
.modify(remove_kwarg_string)
.select(ARGS_PATTERN)
.filter(is_arg_string_redundant)
.modify(remove_arg_string)
.idiff()
)
def _san_field_name(field_name):
return (
field_name[:-4] if field_name.endswith('_ids') else
field_name[:-3] if field_name.endswith('_id') else field_name
).replace('_', ' ')
def _san_string(sstring):
return sstring.strip("\"").strip("'")
def is_kwarg_string_redundant(ln, cap, fn):
field_name = _san_field_name(cap.get('field_name').value)
field_string = _san_string(cap.get('field_string').value)
return field_string.title() == field_name.title()
def is_arg_string_redundant(ln, cap, fn):
field_name = _san_field_name(cap.get('field_name').value)
attslist = cap.get('field_arglist').children
args = [arg for arg in attslist if
arg.type != TOKEN.COMMA and arg.type != SYMBOL.argument]
index = FIELDS_METHOD.get(cap.get('field_class').value, 0)
if len(args) <= index:
return False
field_string = _san_string(args[index].value)
return field_string.title() == field_name.title()
def remove_kwarg_string(ln, cap, fn):
# print_tree(ln, cap, fn)
node = cap.get('field_kwarg')
assert node.type == SYMBOL.argument
if node.next_sibling == Comma():
node.next_sibling.remove()
node.remove()
def remove_arg_string(ln, cap, fn):
# print_tree(ln, cap, fn)
attslist = cap.get('field_arglist').children
args = [arg for arg in attslist if
arg.type != TOKEN.COMMA and arg.type != SYMBOL.argument]
index = FIELDS_METHOD.get(cap.get('field_class').value, 0)
leaf = args[index]
assert leaf.type == TOKEN.STRING
if leaf.next_sibling == Comma():
leaf.next_sibling.remove()
leaf.remove()
# Copyright 2019 David Arnold
# Licensed under the MIT License
# from bowler.helpers import print_tree, print_selector_pattern, find_last
from bowler.query import Query
# from bowler.types import TOKEN, SYMBOL
# from fissix.pytree import Leaf, Node
# from fissix.fixer_util import Comma
def main():
(
Query("./src")
.select_method("warn")
.rename("warning")
.idiff()
)
# Copyright 2019 David Arnold
# Licensed under the MIT License
from bowler.helpers import print_tree, print_selector_pattern, find_last
from bowler.query import Query
# from bowler.types import TOKEN, SYMBOL
# from fissix.pytree import Leaf, Node
from fissix.fixer_util import Comma
_ALL_LOGGERS = """
function_call=power < '_logger'
trailer < '.' logging_level=any >
trailer < '('
any*
')' >
>
"""
PATTERN_NAME = """
function_call=power<
'_logger'
trailer <
'.'
logging_level=any
>
trailer <
'('
term <
logging_string=any
interpol_token='%'
loggin_arguments=( NAME | power)
>
')'
>
>
"""
PATTERN_ATOM = """
function_call=power<
'_logger'
trailer <
'.'
logging_level=any
>
trailer <
'('
term <
logging_string=any
interpol_token='%'
loggin_args_container=atom <
'('
(
loggin_arguments=NAME
|
loggin_arguments=testlist_gexp
)
')'
>
>
')'
>
>
"""
def main():
(
Query("./src")
# .select(_ALL_LOGGERS)
# .filter(_print_selector_pattern)
.select(PATTERN_NAME)
.modify(refactor_with_name)
.select(PATTERN_ATOM)
.modify(refactor_with_name)
.modify(refactor_atom)
.idiff()
)
def _print_selector_pattern(ln, cap, fn):
print_selector_pattern(ln, cap, fn)
print('\n')
def refactor_with_name(ln, cap, fn):
cap.get('interpol_token').replace(Comma())
...
def refactor_atom(ln, cap, fn):
cap.get('loggin_args_container').replace(
cap.get('loggin_arguments').clone()
)
...
@agent-z28
Copy link

I like

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment