Skip to content

Commit

Permalink
[IMP] *: deprecate ustr()
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviedoanhduy committed Nov 8, 2024
1 parent 2bfa7bb commit c1ffc99
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
24 changes: 24 additions & 0 deletions odoo_module_migrate/migration_scripts/migrate_170_180.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,33 @@ def replace_user_has_groups(
logger.error(f"Error processing file {file}: {str(e)}")


def replace_ustr(
logger, module_path, module_name, manifest_path, migration_steps, tools
):
files_to_process = tools.get_files(module_path, (".py",))
replaces = {
r"from\s+odoo\.tools\s+import\s+ustr\s*\n": "",
r"from\s+odoo\.tools\.misc\s+import\s+ustr\s*\n": "",
r"from\s+odoo\.tools\s+import\s+([^,\n]*,\s*)?ustr,\s*([^,\n]*)": r"from odoo.tools import \1\2",
r"from\s+odoo\.tools\.misc\s+import\s+([^,\n]*,\s*)?ustr,\s*([^,\n]*)": r"from odoo.tools.misc import \1\2",
r",\s*ustr(\s*,)?": r"\1",
r"tools\.ustr\(([^)]+)\)": r"\1",
r"misc\.ustr\(([^)]+)\)": r"\1",
r"=\s*ustr\(([^)]+)\)": r"= \1",
}
for file in files_to_process:
try:
tools._replace_in_file(
file, replaces, log_message=f"Deprecate ustr in: {file}"
)
except Exception as e:
logger.error(f"Error processing file {file}: {str(e)}")


class MigrationScript(BaseMigrationScript):
_GLOBAL_FUNCTIONS = [
replace_tree_with_list_in_views,
replace_chatter_blocks,
replace_user_has_groups,
replace_ustr,
]
1 change: 1 addition & 0 deletions tests/data_result/module_170_180/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import res_partner
from . import fetchmail
20 changes: 20 additions & 0 deletions tests/data_result/module_170_180/models/fetchmail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from odoo import models, tools, _
from odoo.tools import misc
from odoo.tools.misc import find_in_path
from odoo.tools import config, consteq, file_path
from odoo.tools.misc import lazy
from odoo.tools import config, consteq, file_path
from ssl import SSLError
from odoo.exceptions import UserError


class FetchMailServer(models.Model):
_inherit = "fetchmail.server"

def example_method_use_ustr(self):
try:
server_name = self.name
description = self._description
connection = self.connect(allow_archived=True)
except SSLError as e:
raise UserError(_(e))
1 change: 1 addition & 0 deletions tests/data_template/module_170/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import res_partner
from . import fetchmail
22 changes: 22 additions & 0 deletions tests/data_template/module_170/models/fetchmail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from odoo import models, tools, _
from odoo.tools import misc
from odoo.tools import ustr
from odoo.tools.misc import ustr
from odoo.tools.misc import ustr, find_in_path
from odoo.tools import ustr, config, consteq, file_path
from odoo.tools.misc import lazy, ustr
from odoo.tools import config, consteq, ustr, file_path
from ssl import SSLError
from odoo.exceptions import UserError


class FetchMailServer(models.Model):
_inherit = "fetchmail.server"

def example_method_use_ustr(self):
try:
server_name = ustr(self.name)
description = misc.ustr(self._description)
connection = self.connect(allow_archived=True)
except SSLError as e:
raise UserError(_(tools.ustr(e)))

0 comments on commit c1ffc99

Please sign in to comment.