Skip to content

Commit

Permalink
[IMP] remove pre-commit exception and fix various style in odoo_patch…
Browse files Browse the repository at this point in the history
… folder
  • Loading branch information
legalsylvain committed Feb 26, 2021
1 parent 188ed85 commit 92c7bca
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 32 deletions.
8 changes: 2 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ repos:
rev: 20.8b1
hooks:
- id: black
exclude: ^upgrade_analysis/odoo_patch/
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.1.2
hooks:
Expand All @@ -54,7 +53,6 @@ repos:
- "@prettier/[email protected]"
args:
- --plugin=@prettier/plugin-xml
exclude: ^upgrade_analysis/odoo_patch/
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v7.8.1
hooks:
Expand Down Expand Up @@ -89,15 +87,14 @@ repos:
rev: v2.7.2
hooks:
- id: pyupgrade
exclude: ^upgrade_analysis/odoo_patch/
- repo: https://github.com/PyCQA/isort
rev: 5.5.1
hooks:
- id: isort
name: isort except __init__.py
args:
- --settings=.
exclude: /__init__\.py$|^upgrade_analysis/odoo_patch/
exclude: /__init__\.py$
- repo: https://github.com/acsone/setuptools-odoo
rev: 2.6.0
hooks:
Expand All @@ -113,14 +110,13 @@ repos:
hooks:
- id: flake8
name: flake8 except __init__.py
exclude: /__init__\.py$|^upgrade_analysis/odoo_patch/
exclude: /__init__\.py$
additional_dependencies: ["flake8-bugbear==20.1.4"]
- id: flake8
name: flake8 only __init__.py
args: ["--extend-ignore=F401"] # ignore unused imports in __init__.py
files: /__init__\.py$
additional_dependencies: ["flake8-bugbear==20.1.4"]
exclude: ^upgrade_analysis/odoo_patch/
- repo: https://github.com/PyCQA/pylint
rev: pylint-2.5.3
hooks:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# generated from manifests external_dependencies
raven
dataclasses
odoorpc
openupgradelib
raven
3 changes: 2 additions & 1 deletion upgrade_analysis/odoo_patch/addons/mrp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# flake8: noqa: B902
from odoo.addons import mrp
from ...odoo_patch import OdooPatch


class PreInitHookPatch(OdooPatch):
target = mrp
method_names = ['_pre_init_mrp']
method_names = ["_pre_init_mrp"]

def _pre_init_mrp(cr):
""" Don't try to create an existing column on reinstall """
3 changes: 2 additions & 1 deletion upgrade_analysis/odoo_patch/addons/point_of_sale/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# flake8: noqa: B902
from odoo import api
from odoo.addons.point_of_sale.models import pos_config
from ...odoo_patch import OdooPatch


class PreInitHookPatch(OdooPatch):
target = pos_config.PosConfig
method_names = ['post_install_pos_localisation']
method_names = ["post_install_pos_localisation"]

@api.model
def post_install_pos_localisation(cr):
Expand Down
3 changes: 2 additions & 1 deletion upgrade_analysis/odoo_patch/addons/stock/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# flake8: noqa: B902
from odoo.addons import stock
from ...odoo_patch import OdooPatch


class PreInitHookPatch(OdooPatch):
target = stock
method_names = ['pre_init_hook']
method_names = ["pre_init_hook"]

def pre_init_hook(cr):
""" Don't unlink stock data on reinstall """
12 changes: 7 additions & 5 deletions upgrade_analysis/odoo_patch/odoo/models.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
from odoo import api, models
from ..odoo_patch import OdooPatch

from ... import upgrade_log
from ..odoo_patch import OdooPatch


class BaseModelPatch(OdooPatch):
target = models.BaseModel
method_names = ['_convert_records']
method_names = ["_convert_records"]

@api.model
def _convert_records(self, records, log=lambda a: None):
""" Log data ids that are imported with `load` """
current_module = self.env.context['module']
current_module = self.env.context["module"]
for res in BaseModelPatch._convert_records._original_method(
self, records, log=log):
self, records, log=log
):
_id, xid, _record, _info = res
if xid:
xid = xid if '.' in xid else "%s.%s" % (current_module, xid)
xid = xid if "." in xid else "{}.{}".format(current_module, xid)
upgrade_log.log_xml_id(self.env.cr, current_module, xid)

yield res
20 changes: 12 additions & 8 deletions upgrade_analysis/odoo_patch/odoo/modules/registry.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import logging
from threading import current_thread
from odoo import api, SUPERUSER_ID
from ...odoo_patch import OdooPatch
from .... import upgrade_log

from odoo import SUPERUSER_ID, api
from odoo.modules.registry import Registry

from .... import upgrade_log
from ...odoo_patch import OdooPatch

_logger = logging.getLogger(__name__)


class RegistryPatch(OdooPatch):
target = Registry
method_names = ['init_models']
method_names = ["init_models"]

def init_models(self, cr, model_names, context, install=True):
module_name = context['module']
_logger.debug('Logging models of module %s', module_name)
module_name = context["module"]
_logger.debug("Logging models of module %s", module_name)
upg_registry = current_thread()._upgrade_registry
local_registry = {}
env = api.Environment(cr, SUPERUSER_ID, {})
Expand All @@ -23,7 +25,9 @@ def init_models(self, cr, model_names, context, install=True):
continue
upgrade_log.log_model(model, local_registry)
upgrade_log.compare_registries(
cr, context['module'], upg_registry, local_registry)
cr, context["module"], upg_registry, local_registry
)

return RegistryPatch.init_models._original_method(
self, cr, model_names, context, install=install)
self, cr, model_names, context, install=install
)
7 changes: 4 additions & 3 deletions upgrade_analysis/odoo_patch/odoo/tools/convert.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from ...odoo_patch import OdooPatch
from .... import upgrade_log
from odoo.tools.convert import xml_import

from .... import upgrade_log
from ...odoo_patch import OdooPatch


class XMLImportPatch(OdooPatch):
target = xml_import
method_names = ['_test_xml_id']
method_names = ["_test_xml_id"]

def _test_xml_id(self, xml_id):
res = XMLImportPatch._test_xml_id._original_method(self, xml_id)
Expand Down
14 changes: 8 additions & 6 deletions upgrade_analysis/odoo_patch/odoo_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


class OdooPatch(object):
""" Simple mechanism to apply a collection of monkeypatches using a
"""Simple mechanism to apply a collection of monkeypatches using a
context manager.
Classes can register their monkeypatches by inheriting from this class.
Expand Down Expand Up @@ -39,21 +39,23 @@ def do_something(self):
do_something()
```
"""

def __enter__(self):
for cls in OdooPatch.__subclasses__():
for method_name in cls.method_names:
method = getattr(cls, method_name)
setattr(method, '_original_method',
getattr(cls.target, method_name))
method._original_method = getattr(cls.target, method_name)
setattr(cls.target, method_name, method)

def __exit__(self, exc_type, exc_value, tb):
for cls in OdooPatch.__subclasses__():
for method_name in cls.method_names:
method = getattr(cls.target, method_name)
if hasattr(method, '_original_method'):
if hasattr(method, "_original_method"):
setattr(cls.target, method_name, method._original_method)
else:
_logger.warn(
'_original_method not found on method %s of class %s',
method_name, cls.target)
"_original_method not found on method %s of class %s",
method_name,
cls.target,
)

0 comments on commit 92c7bca

Please sign in to comment.