97 lines
3.1 KiB
Python
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))
|