Skip to content

Commit

Permalink
Merge c79bd6b into a65fdf5
Browse files Browse the repository at this point in the history
  • Loading branch information
antonag32 committed Jun 14, 2022
2 parents a65fdf5 + c79bd6b commit de417ab
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 69 deletions.
44 changes: 26 additions & 18 deletions pylint_odoo/checkers/format.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import os
import tokenize
from sys import platform
Expand Down Expand Up @@ -48,7 +47,6 @@


class FormatChecker(PylintOdooTokenChecker):

name = settings.CFG_SECTION
msgs = ODOO_MSGS
odoo_check_versions = {
Expand All @@ -67,7 +65,7 @@ def get_magic_comment_type(self, comment, line_num):
return MAGIC_COMMENT_INTERPRETER
elif "# -*- coding: " in comment or "# coding: " in comment:
if "# -*- coding: utf-8 -*-" in comment \
or "# coding: utf-8" in comment:
or "# coding: utf-8" in comment:
return MAGIC_COMMENT_CODING_UTF8
return MAGIC_COMMENT_CODING
elif "# -*- encoding: " in comment:
Expand All @@ -90,19 +88,29 @@ def process_tokens(self, tokens):
if magic_comment_type != NO_IDENTIFIED:
tokens_identified[magic_comment_type] = [
token_content, line_num]
elif self.is_vim_comment(token_content):
elif (
self.linter.is_message_enabled('use-vim-comment') and
self.is_vim_comment(token_content)
):
self.add_message('use-vim-comment', line=line_num)
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])
if (not platform.startswith('win') and
bool(interpreter_content) != access_x):
self.add_message(
'incoherent-interpreter-exec-perm',
line=line_num, args=(
os.path.basename(self.linter.current_file)))

if self.linter.is_message_enabled('no-utf8-coding-comment'):
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 self.linter.is_message_enabled('unnecessary-utf8-coding-comment'):
if tokens_identified.get(MAGIC_COMMENT_CODING_UTF8):
self.add_message('unnecessary-utf8-coding-comment', line=1)

if self.linter.is_message_enabled('incoherent-interpreter-exec-perm'):
access_x = os.access(self.linter.current_file, os.X_OK)
interpreter_content, line_num = tokens_identified.get(
MAGIC_COMMENT_INTERPRETER, ['', 0]
)
if not platform.startswith('win') and bool(interpreter_content) != access_x:
self.add_message(
'incoherent-interpreter-exec-perm',
line=line_num,
args=(os.path.basename(self.linter.current_file))
)
113 changes: 62 additions & 51 deletions pylint_odoo/checkers/modules_odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,18 @@ def open(self):

def close(self):
"""Final process get all cached values and add messages"""
for (odoo_node, class_dup_name), nodes in self.inh_dup.items():
if len(nodes) == 1:
continue
path_nodes = []
for node in nodes[1:]:
relpath = os.path.relpath(node.file,
os.path.dirname(odoo_node.file))
path_nodes.append("%s:%d" % (relpath, node.lineno))
self.add_message('consider-merging-classes-inherited',
node=nodes[0],
args=(class_dup_name, ', '.join(path_nodes)))
if self.linter.is_message_enabled('consider-merging-classes-inherited'):
for (odoo_node, class_dup_name), nodes in self.inh_dup.items():
if len(nodes) == 1:
continue
path_nodes = []
for node in nodes[1:]:
relpath = os.path.relpath(node.file,
os.path.dirname(odoo_node.file))
path_nodes.append("%s:%d" % (relpath, node.lineno))
self.add_message('consider-merging-classes-inherited',
node=nodes[0],
args=(class_dup_name, ', '.join(path_nodes)))

def _get_odoo_module_imported(self, node):
odoo_module = []
Expand Down Expand Up @@ -364,11 +365,13 @@ def _get_odoo_module_imported(self, node):
odoo_module.append(packages[2])
return odoo_module

@utils.check_messages('odoo-addons-relative-import')
def check_odoo_relative_import(self, node):
if self.odoo_module_name in self._get_odoo_module_imported(node):
self.add_message('odoo-addons-relative-import', node=node,
args=(self.odoo_module_name))

@utils.check_messages('test-folder-imported')
def check_folder_test_imported(self, node):
if (hasattr(node.parent, 'file')
and os.path.basename(node.parent.file) == '__init__.py'):
Expand Down Expand Up @@ -422,48 +425,56 @@ def _is_module_name_in_whitelist(self, module_name):
return True
return False

@utils.check_messages('missing-import-error', 'missing-manifest-dependency')
def _check_imported_packages(self, node, module_name):
"""Check if the import node is a external dependency to validate it"""
if not module_name:
# skip local packages because is not a external dependency.
return
if not self.manifest_dict:
# skip if is not a module of odoo
return
if not isinstance(node.parent, astroid.Module):
# skip nested import sentences
return
if self._is_absolute_import(node, module_name):
# skip absolute imports
return
if self._is_module_name_in_whitelist(module_name):
# ignore whitelisted modules
return
isort_driver = misc.IsortDriver()
import_category = isort_driver.place_module(module_name)
if import_category not in ('FIRSTPARTY', 'THIRDPARTY'):
# skip if is not a external library or is a white list library
return
relpath = os.path.relpath(
node.parent.file, os.path.dirname(self.manifest_file))
if os.path.dirname(relpath) == 'tests':
# import errors rules don't apply to the test files
# since these files are loaded only when running tests
# and in such a case your
# module and their external dependencies are installed.
return
self.add_message('missing-import-error', node=node,
args=(module_name,))

ext_deps = self.manifest_dict.get('external_dependencies') or {}
py_ext_deps = ext_deps.get('python') or []
if isinstance(node, astroid.ImportFrom) and (node.level or 0) >= 1:
return
if module_name not in py_ext_deps and \
module_name.split('.')[0] not in py_ext_deps and \
not any(dep in module_name for dep in py_ext_deps):
self.add_message('missing-manifest-dependency', node=node,
args=(module_name,))
if self.linter.is_message_enabled('missing-import-error'):
if not module_name:
# skip local packages because is not a external dependency.
return
if not self.manifest_dict:
# skip if is not a module of odoo
return
if not isinstance(node.parent, astroid.Module):
# skip nested import sentences
return
if self._is_absolute_import(node, module_name):
# skip absolute imports
return
if self._is_module_name_in_whitelist(module_name):
# ignore whitelisted modules
return
isort_driver = misc.IsortDriver()
import_category = isort_driver.place_module(module_name)
if import_category not in ('FIRSTPARTY', 'THIRDPARTY'):
# skip if is not a external library or is a white list library
return
relpath = os.path.relpath(
node.parent.file, os.path.dirname(self.manifest_file))
if os.path.dirname(relpath) == 'tests':
# import errors rules don't apply to the test files
# since these files are loaded only when running tests
# and in such a case your
# module and their external dependencies are installed.
return
self.add_message('missing-import-error', node=node, args=(module_name,))

if self.linter.is_message_enabled('missing-manifest-dependency'):
if isinstance(node, astroid.ImportFrom) and (node.level or 0) >= 1:
return

ext_deps = self.manifest_dict.get('external_dependencies') or {}
py_ext_deps = ext_deps.get('python') or []
if (
module_name not in py_ext_deps and
module_name.split('.')[0] not in py_ext_deps and
not any(dep in module_name for dep in py_ext_deps)
):
self.add_message(
'missing-manifest-dependency',
node=node,
args=(module_name,)
)

@utils.check_messages('odoo-addons-relative-import',
'missing-import-error',
Expand Down

0 comments on commit de417ab

Please sign in to comment.