[IMP] remove pre-commit exception and fix various style in odoo_patch folder

pull/2417/head
Sylvain LE GAL 2021-02-24 09:20:42 +01:00 committed by Stefan Rijnhart
parent 8cf7f08649
commit cc83a275a3
7 changed files with 37 additions and 25 deletions

View File

@ -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 """

View File

@ -1,3 +1,4 @@
# flake8: noqa: B902
from odoo import api
from odoo.addons.point_of_sale.models import pos_config
from ...odoo_patch import OdooPatch
@ -5,7 +6,7 @@ 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):

View File

@ -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 """

View File

@ -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

View File

@ -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, {})
@ -23,7 +25,9 @@ class RegistryPatch(OdooPatch):
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
)

View File

@ -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)

View File

@ -4,7 +4,7 @@ _logger = logging.getLogger(__name__)
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.
@ -39,21 +39,23 @@ class OdooPatch(object):
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,
)