Skip to content

Commit

Permalink
Merge pull request #45 from acsone/silence-env-manage-warning-sbi
Browse files Browse the repository at this point in the history
Silence Environment.manage warning in Odoo 15
  • Loading branch information
sbidoul authored Oct 8, 2021
2 parents 506f5f8 + 0a3b436 commit 47aea3c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 36 deletions.
4 changes: 2 additions & 2 deletions click_odoo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2018 ACSONE SA/NV (<http://acsone.eu>)
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

from .compat import odoo # noqa
from .compat import odoo_bin # noqa
from .env import OdooEnvironment # noqa
from .env import odoo # noqa
from .env import odoo_bin # noqa
from .env_options import env_options # noqa
47 changes: 47 additions & 0 deletions click_odoo/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import contextlib

__all__ = [
"Environment",
"environment_manage",
"odoo",
"odoo_bin",
"odoo_version_info",
]

try:
import odoo
from odoo.api import Environment
except ImportError as e:
if hasattr(e, "name") and e.name != "odoo":
raise
# Odoo < 10
try:
import openerp as odoo
from openerp.api import Environment
except ImportError:
if hasattr(e, "name") and e.name != "openerp":
raise
raise ImportError("No module named odoo nor openerp")


try:
from odoo.release import version_info as odoo_version_info
except ImportError:
from openerp.release import version_info as odoo_version_info


if odoo_version_info < (10, 0):
odoo_bin = "openerp-server"
else:
odoo_bin = "odoo"


if odoo_version_info < (15, 0):
environment_manage = Environment.manage
else:

@contextlib.contextmanager
def environment_manage():
# Environment.manage is a no-op in Odoo 15+, but it
# emits a noisy warning so let's avoid it.
yield
25 changes: 3 additions & 22 deletions click_odoo/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,14 @@
import logging
from contextlib import contextmanager

try:
import odoo
from odoo.api import Environment

odoo_bin = "odoo"
except ImportError as e:
if hasattr(e, "name") and e.name != "odoo":
raise
# Odoo < 10
try:
import openerp as odoo
from openerp.api import Environment

odoo_bin = "openerp-server"
except ImportError:
if hasattr(e, "name") and e.name != "openerp":
raise
raise ImportError("No module named odoo nor openerp")
from .compat import Environment, environment_manage, odoo, odoo_version_info

_logger = logging.getLogger(__name__)


@contextmanager
def OdooEnvironment(database, rollback=False, **kwargs):
with Environment.manage():
with environment_manage():
registry = odoo.registry(database)
try:
with registry.cursor() as cr:
Expand All @@ -53,9 +36,7 @@ def OdooEnvironment(database, rollback=False, **kwargs):
else:
cr.commit()
finally:
if odoo.tools.parse_version(
odoo.release.version
) < odoo.tools.parse_version("10.0"):
if odoo_version_info < (10, 0):
odoo.modules.registry.RegistryManager.delete(database)
else:
odoo.modules.registry.Registry.delete(database)
Expand Down
11 changes: 4 additions & 7 deletions click_odoo/env_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import click
from click.decorators import _param_memo # XXX undocumented click internal

from .compat import environment_manage, odoo_version_info
from .env import OdooEnvironment, odoo

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -112,9 +113,7 @@ def _register(self, ctx, param, value):
return value

def _fix_odoo_logging(self):
if odoo.tools.parse_version(odoo.release.version) < odoo.tools.parse_version(
"9.0c"
):
if odoo_version_info < (9, 0):
handlers = logging.getLogger().handlers
if handlers and len(handlers) == 1:
handler = handlers[0]
Expand All @@ -123,9 +122,7 @@ def _fix_odoo_logging(self):
handler.stream = sys.stderr

def _fix_disable_wsgi_module_handlers(self):
if odoo.tools.parse_version(odoo.release.version) < odoo.tools.parse_version(
"9.0c"
):
if odoo_version_info < (9, 0):
odoo.service.wsgi_server.module_handlers[:] = []

def get_odoo_args(self, ctx):
Expand Down Expand Up @@ -213,7 +210,7 @@ def _invoke(self, ctx):
ctx.params["env"] = env
return self.org_invoke(ctx)
else:
with odoo.api.Environment.manage():
with environment_manage():
ctx.params["env"] = None
return self.org_invoke(ctx)
except click.exceptions.Exit:
Expand Down
1 change: 1 addition & 0 deletions newsfragments/43.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Silenced a noisy warning about Environment.manage() being a no-op in Odoo 15.
8 changes: 3 additions & 5 deletions tests/scripts/script5.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# noqa
try:
import odoo.release as release
import odoo.tools.parse_version as parse_version
except (AttributeError, ImportError):
import openerp.release as release
import openerp.tools.parse_version as parse_version

if parse_version(release.version) > parse_version("9.0c"):
import odoo.addons.addon1 # noqa
else:
if release.version_info < (10, 0):
import openerp.addons.addon1 # noqa
else:
import odoo.addons.addon1 # noqa

0 comments on commit 47aea3c

Please sign in to comment.