Odoo18-Base/odoo/upgrade_code/17.5-05-jsonrpc-to-rpc.py
KaySar12 fe1cac9656
All checks were successful
Setup Native Action / native (3.12.7) (push) Has been skipped
Setup Native Action / docker (3.12.7) (push) Has been skipped
update add script upgrade : ir.cron + jsonrcp-to-rpc
2025-03-10 17:50:47 +07:00

97 lines
3.1 KiB
Python

# -*- coding: utf-8 -*-
import re
from pathlib import Path
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def upgrade(file_manager):
"""Replace 'import { jsonrpc } from "@web/core/network/rpc_service";' with 'import { rpc } from "@web/core/network/rpc";' in JS files."""
# Filter files to only JavaScript files
files = [file for file in file_manager if file.path.suffix in (".js", ".jsx")]
if not files:
logger.info("No JavaScript files found to process")
return
# Regex pattern to match the specific import statement
jsonrpc_import_re = re.compile(
r"""
^\s* # Start of line with optional whitespace
import\s+ # 'import' keyword
\{\s*jsonrpc\s*\}\s+ # '{ jsonrpc }' with optional whitespace
from\s+ # 'from' keyword
"@web/core/network/rpc_service" # Exact module path
\s*;\s*$ # Semicolon and optional whitespace to end of line
""",
re.VERBOSE | re.MULTILINE, # MULTILINE for line-by-line matching
)
# Regex pattern to match jsonrpc( calls
jsonrpc_call_re = re.compile(
r"""
\bjsonrpc\s* # 'jsonrpc' followed by optional whitespace
\( # Opening parenthesis
""",
re.VERBOSE,
)
# Replacement string
replacement = 'import { rpc } from "@web/core/network/rpc";'
# Process each file
for fileno, file in enumerate(files, start=1):
content = file.content
original_content = content
# Replace the import statement
content = jsonrpc_import_re.sub(replacement, content)
content = jsonrpc_call_re.sub("rpc(", content)
# Only update if changes were made
if content != original_content:
file.content = content
logger.info(f"Updated import statement in {file.path}")
else:
logger.debug(f"No changes needed in {file.path}")
file_manager.print_progress(fileno, len(files))
# Example usage (for testing)
if __name__ == "__main__":
from types import SimpleNamespace
class MockFile:
def __init__(self, path, content):
self.path = Path(path)
self.content = content
class MockFileManager:
def __init__(self, files):
self.files = files
def __iter__(self):
return iter(self.files)
def print_progress(self, current, total):
print(f"Progress: {current}/{total}")
# Test data
sample_files = [
MockFile(
"test1.js",
'import { jsonrpc } from "@web/core/network/rpc_service";\nconsole.log("test");',
),
MockFile(
"test2.js",
'import {jsonrpc} from "@web/core/network/rpc_service";\nlet x = 5;',
),
MockFile(
"test3.js",
'import { something } from "@other/module";\nconsole.log("no change");',
),
]
upgrade(MockFileManager(sample_files))