Skip to content

Commit

Permalink
[ADD] unnecessary-utf8-coding-comment: The utf8 comment is unnecessar…
Browse files Browse the repository at this point in the history
…y if the version of odoo is 11.0 because use py3 (#168)

- Odoo version get from manifest file if is valid or use valid_odoo_version parameter.
- Use a variable to assign range of valid odoo versions for each check.
  • Loading branch information
Jesus Zapata authored and moylop260 committed Dec 2, 2017
1 parent 2f26b15 commit fc41ef8
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 42 deletions.
25 changes: 18 additions & 7 deletions pylint_odoo/checkers/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import tokenize
from sys import platform

from pylint.checkers import BaseTokenChecker
from pylint.interfaces import ITokenChecker

from .. import settings
from ..misc import PylintOdooTokenChecker

ODOO_MSGS = {
# C->convention R->refactor W->warning E->error F->fatal
Expand All @@ -17,6 +15,11 @@
'no-utf8-coding-comment',
settings.DESC_DFLT
),
'C%d02' % settings.BASE_FORMAT_ID: (
'UTF-8 coding is not necessary',
'unnecessary-utf8-coding-comment',
settings.DESC_DFLT
),
'W%d01' % settings.BASE_FORMAT_ID: (
'You have a python file with execution permissions but you don\'t '
'have a interpreter magic comment. '
Expand All @@ -41,13 +44,19 @@
NO_IDENTIFIED = -1


class FormatChecker(BaseTokenChecker):

# Auto call to `process_tokens` method
__implements__ = (ITokenChecker)
class FormatChecker(PylintOdooTokenChecker):

name = settings.CFG_SECTION
msgs = ODOO_MSGS
odoo_check_versions = {
'no-utf8-coding-comment': {
'min_odoo_version': '4.2',
'max_odoo_version': '10.0',
},
'unnecessary-utf8-coding-comment': {
'min_odoo_version': '11.0',
},
}

def get_magic_comment_type(self, comment, line_num):
if line_num >= 1 and line_num <= 2:
Expand Down Expand Up @@ -83,6 +92,8 @@ def process_tokens(self, tokens):
if not tokens_identified.get(MAGIC_COMMENT_CODING_UTF8) and \
not os.path.basename(self.linter.current_file) == '__init__.py':
self.add_message('no-utf8-coding-comment', line=1)
if (tokens_identified.get(MAGIC_COMMENT_CODING_UTF8)):
self.add_message('unnecessary-utf8-coding-comment', line=1)
access_x = os.access(self.linter.current_file, os.X_OK)
interpreter_content, line_num = tokens_identified.get(
MAGIC_COMMENT_INTERPRETER, ['', 0])
Expand Down
4 changes: 0 additions & 4 deletions pylint_odoo/checkers/modules_odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,6 @@ class ModuleChecker(misc.WrapperModuleChecker):

class_inherit_names = []

@utils.check_messages(*(ODOO_MSGS.keys()))
def visit_module(self, node):
self.wrapper_visit_module(node)

@utils.check_messages('consider-merging-classes-inherited')
def visit_assign(self, node):
if not self.odoo_node:
Expand Down
20 changes: 4 additions & 16 deletions pylint_odoo/checkers/no_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
import astroid
import rfc3986
from six import string_types
from pylint.checkers import BaseChecker, utils
from pylint.checkers import utils
from pylint.interfaces import IAstroidChecker

from .. import settings
Expand Down Expand Up @@ -231,10 +231,6 @@
'create', 'write', 'read', 'unlink', 'copy',
'setUp', 'setUpClass', 'tearDown', 'default_get',
]
DFTL_VALID_ODOO_VERSIONS = [
'4.2', '5.0', '6.0', '6.1', '7.0', '8.0', '9.0', '10.0', '11.0',
]
DFTL_MANIFEST_VERSION_FORMAT = r"({valid_odoo_versions})\.\d+\.\d+\.\d+$"
DFTL_CURSOR_EXPR = [
'self.env.cr', 'self._cr', # new api
'self.cr', # controllers and test
Expand Down Expand Up @@ -262,7 +258,7 @@
]


class NoModuleChecker(BaseChecker):
class NoModuleChecker(misc.PylintOdooChecker):

__implements__ = IAstroidChecker

Expand Down Expand Up @@ -326,7 +322,7 @@ class NoModuleChecker(BaseChecker):
('manifest_version_format', {
'type': 'string',
'metavar': '<string>',
'default': DFTL_MANIFEST_VERSION_FORMAT,
'default': misc.DFTL_MANIFEST_VERSION_FORMAT,
'help': 'Regex to check version format in manifest file. '
'Use "{valid_odoo_versions}" to check the parameter of '
'"valid_odoo_versions"'
Expand All @@ -346,7 +342,7 @@ class NoModuleChecker(BaseChecker):
('valid_odoo_versions', {
'type': 'csv',
'metavar': '<comma separated values>',
'default': DFTL_VALID_ODOO_VERSIONS,
'default': misc.DFTL_VALID_ODOO_VERSIONS,
'help': 'List of valid odoo versions separated by a comma.'
}),
('no_missing_return', {
Expand Down Expand Up @@ -654,14 +650,6 @@ def visit_name(self, node):
def camelize(self, string):
return re.sub(r"(?:^|_)(.)", lambda m: m.group(1).upper(), string)

def formatversion(self, string):
valid_odoo_versions = '|'.join(
map(re.escape, self.config.valid_odoo_versions))
self.config.manifest_version_format_parsed = (
self.config.manifest_version_format.format(
valid_odoo_versions=valid_odoo_versions))
return re.match(self.config.manifest_version_format_parsed, string)

def get_decorators_names(self, decorators):
nodes = []
if decorators:
Expand Down
90 changes: 75 additions & 15 deletions pylint_odoo/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import subprocess
import inspect

from distutils.version import LooseVersion
from lxml import etree
from pylint.checkers import BaseChecker
from pylint.interfaces import IAstroidChecker
from pylint.checkers import BaseChecker, BaseTokenChecker
from pylint.interfaces import UNDEFINED
from pylint.interfaces import IAstroidChecker, ITokenChecker
from pylint.utils import _basename_in_blacklist_re
from restructuredtext_lint import lint_file as rst_lint
from six import string_types
Expand All @@ -19,6 +21,11 @@
except ImportError:
from whichcraft import which

DFTL_VALID_ODOO_VERSIONS = [
'4.2', '5.0', '6.0', '6.1', '7.0', '8.0', '9.0', '10.0', '11.0', '12.0',
]
DFTL_MANIFEST_VERSION_FORMAT = r"({valid_odoo_versions})\.\d+\.\d+\.\d+$"


def get_plugin_msgs(pylint_run_res):
"""Get all message of this pylint plugin.
Expand All @@ -44,23 +51,25 @@ def join_node_args_kwargs(node):
return args


# TODO: Change all methods here

class WrapperModuleChecker(BaseChecker):
class PylintOdooChecker(BaseChecker):

# Auto call to `process_tokens` method
__implements__ = IAstroidChecker

node = None
module_path = None
msg_args = None
msg_code = None
msg_name_key = None
odoo_node = None
odoo_module_name = None
manifest_file = None
module = None
manifest_dict = None
is_main_odoo_module = None

def formatversion(self, string):
valid_odoo_versions = self.linter._all_options[
'valid_odoo_versions'].config.valid_odoo_versions
valid_odoo_versions = '|'.join(
map(re.escape, DFTL_VALID_ODOO_VERSIONS))
self.config.manifest_version_format_parsed = (
DFTL_MANIFEST_VERSION_FORMAT.format(
valid_odoo_versions=valid_odoo_versions))
return re.match(self.config.manifest_version_format_parsed, string)

def get_manifest_file(self, node_file):
"""Get manifest file path
Expand Down Expand Up @@ -107,9 +116,6 @@ def leave_module(self, node):
"""Clear caches"""
self.clear_caches()

def open(self):
self.odoo_node = None

def wrapper_visit_module(self, node):
"""Call methods named with name-key from self.msgs
Method should be named with next standard:
Expand Down Expand Up @@ -172,6 +178,60 @@ def _check_{NAME_KEY}(self, module_path)
node.file = node_file_original
node.lineno = node_lineno_original

def visit_module(self, node):
self.wrapper_visit_module(node)

def add_message(self, msg_id, line=None, node=None, args=None,
confidence=UNDEFINED):
version = (self.manifest_dict.get('version')
if isinstance(self.manifest_dict, dict) else '')
match = self.formatversion(version)
short_version = match.group(1) if match else ''
if not short_version:
valid_odoo_versions = self.linter._all_options[
'valid_odoo_versions'].config.valid_odoo_versions
short_version = (valid_odoo_versions[0] if
len(valid_odoo_versions) == 1 else '')
if not self._is_version_supported(short_version, msg_id):
return
return super(PylintOdooChecker, self).add_message(
msg_id, line, node, args, confidence)

def _is_version_supported(self, version, name_check):
if not version or not hasattr(self, 'odoo_check_versions'):
return True
odoo_check_versions = self.odoo_check_versions.get(name_check, {})
if not odoo_check_versions:
return True
version = LooseVersion(version)
min_odoo_version = LooseVersion(odoo_check_versions.get(
'min_odoo_version', DFTL_VALID_ODOO_VERSIONS[0]))
max_odoo_version = LooseVersion(odoo_check_versions.get(
'max_odoo_version', DFTL_VALID_ODOO_VERSIONS[-1]))
return (min_odoo_version <= version <= max_odoo_version)


class PylintOdooTokenChecker(BaseTokenChecker, PylintOdooChecker):

# Auto call to `process_tokens` method
__implements__ = (ITokenChecker, IAstroidChecker)


# TODO: Change all methods here

class WrapperModuleChecker(PylintOdooChecker):

node = None
module_path = None
msg_args = None
msg_code = None
msg_name_key = None
module = None
is_main_odoo_module = None

def open(self):
self.odoo_node = None

def set_extra_file(self, node, msg_args, msg_code):
if isinstance(msg_args, string_types):
msg_args = (msg_args,)
Expand Down
13 changes: 13 additions & 0 deletions pylint_odoo/test/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'missing-readme': 1,
'missing-return': 1,
'no-utf8-coding-comment': 3,
'unnecessary-utf8-coding-comment': 18,
'odoo-addons-relative-import': 4,
'old-api7-method-defined': 2,
'openerp-exception-warning': 3,
Expand Down Expand Up @@ -240,6 +241,18 @@ def test_90_valid_odoo_versions(self):
self.assertListEqual(list(real_errors.items()),
list([('xml-attribute-translatable', 1)]))

def test_100_read_version_from_manifest(self):
"""Test the functionality to get the version from the file manifest
to avoid the parameter --valid_odoo_versions"""
modules = [mod for mod in self.paths_modules if
'eleven_module' in mod or 'twelve_module' in mod]
extra_params = ['--disable=all', '--enable=no-utf8-coding-comment,'
'unnecessary-utf8-coding-comment']
pylint_res = self.run_pylint(modules, extra_params)
real_errors = pylint_res.linter.stats['by_msg']
self.assertListEqual(list(real_errors.items()),
list([('unnecessary-utf8-coding-comment', 2)]))


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions pylint_odoo/test_repo/eleven_module/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Eleven module module for tests
2 changes: 2 additions & 0 deletions pylint_odoo/test_repo/eleven_module/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import utf8_models
12 changes: 12 additions & 0 deletions pylint_odoo/test_repo/eleven_module/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
'name': 'Eleven module for tests',
'license': 'AGPL-3',
'author': u'Jesus, Odoo Community Association (OCA)',
'version': '11.0.1.0.0',
'depends': [
'base',
],
'data': [
'security/ir.model.access.csv',
],
}
5 changes: 5 additions & 0 deletions pylint_odoo/test_repo/eleven_module/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from odoo import models


class EleveModel(models.Model):
_name = 'eleve.model'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_product_product_account_user,product.product.account.user,product.model_product_product,group_account_user,1,0,0,0
access_account_payment_term,account.payment.term,model_account_payment_term,account.group_account_user,1,0,0,0
access_account_payment_term_line,account.payment.term.line,model_account_payment_term_line,account.group_account_user,1,0,0,0
access_account_account_type,account.account.type,model_account_account_type,account.group_account_user,1,0,0,0
access_account_account_tax,account.tax internal user,model_account_tax,base.group_user,1,0,0,0
access_account_account,account.account,model_account_account,account.group_account_user,1,0,0,0
7 changes: 7 additions & 0 deletions pylint_odoo/test_repo/eleven_module/utf8_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# coding: utf-8

from odoo import models


class EleveModel(models.Model):
_name = 'eleve.model'
1 change: 1 addition & 0 deletions pylint_odoo/test_repo/twelve_module/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Eleven module module for tests
2 changes: 2 additions & 0 deletions pylint_odoo/test_repo/twelve_module/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from . import utf8_models
12 changes: 12 additions & 0 deletions pylint_odoo/test_repo/twelve_module/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
'name': 'Twelve module for tests',
'license': 'AGPL-3',
'author': u'Jesus, Odoo Community Association (OCA)',
'version': '12.0.1.0.0',
'depends': [
'base',
],
'data': [
'security/ir.model.access.csv',
],
}
5 changes: 5 additions & 0 deletions pylint_odoo/test_repo/twelve_module/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from odoo import models


class TwelveModel(models.Model):
_name = 'twelve.model'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_product_product_account_user,product.product.account.user,product.model_product_product,group_account_user,1,0,0,0
access_account_payment_term,account.payment.term,model_account_payment_term,account.group_account_user,1,0,0,0
access_account_payment_term_line,account.payment.term.line,model_account_payment_term_line,account.group_account_user,1,0,0,0
access_account_account_type,account.account.type,model_account_account_type,account.group_account_user,1,0,0,0
access_account_account_tax,account.tax internal user,model_account_tax,base.group_user,1,0,0,0
access_account_account,account.account,model_account_account,account.group_account_user,1,0,0,0
7 changes: 7 additions & 0 deletions pylint_odoo/test_repo/twelve_module/utf8_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# coding: utf-8

from odoo import models


class TwelveModel(models.Model):
_name = 'twelve.model'

0 comments on commit fc41ef8

Please sign in to comment.