90 lines
3.9 KiB
Python
90 lines
3.9 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):
|
|
"""Remove <field name='numbercall'> and <field name='doall'> within <record model='ir.cron'> in XML files."""
|
|
# Filter files to only XML files
|
|
files = [file for file in file_manager if file.path.suffix == ".xml"]
|
|
if not files:
|
|
logger.info("No XML files found to process")
|
|
return
|
|
|
|
# Regex pattern to match <record model="ir.cron"> blocks and remove specified fields
|
|
record_cron_re = re.compile(
|
|
r"""
|
|
(<record\s+[^>]*model="ir\.cron"[^>]*>) # Capture opening <record model="ir.cron"> with attributes
|
|
( # Start capturing content
|
|
.*? # Any content before fields (non-greedy)
|
|
(?: # Non-capturing group for fields to remove
|
|
<field\s+name="numbercall"[^>]*> # Match <field name="numbercall"> with any content
|
|
.*? # Content inside field
|
|
</field> # Closing tag
|
|
| # OR
|
|
<field\s+name="numbercall"[^>]*> # Match unclosed <field name="numbercall">
|
|
| # OR
|
|
<field\s+name="numbercall"[^>]*\/> # Match self-closing <field name="numbercall">
|
|
| # OR
|
|
<field\s+name="doall"[^>]*> # Match <field name="doall"> with any content
|
|
.*? # Content inside field
|
|
</field> # Closing tag
|
|
| # OR
|
|
<field\s+name="doall"[^>]*> # Match unclosed <field name="doall">
|
|
| # OR
|
|
<field\s+name="doall"[^>]*\/> # Match self-closing <field name="doall">
|
|
)
|
|
.*? # Any content after fields (non-greedy)
|
|
)
|
|
(</record>) # Capture closing </record>
|
|
""",
|
|
re.VERBOSE | re.DOTALL,
|
|
)
|
|
|
|
def clean_record(match):
|
|
"""Replace function to remove numbercall and doall fields."""
|
|
opening_tag = match.group(1) # <record model="ir.cron" ...>
|
|
content = match.group(2) # Content between tags
|
|
closing_tag = match.group(3) # </record>
|
|
|
|
# Patterns for fields to remove within the content
|
|
field_patterns = [
|
|
r'<field\s+name="numbercall"[^>]*>.*?</field>', # Standard field
|
|
r'<field\s+name="numbercall"[^>]*>', # Unclosed field
|
|
r'<field\s+name="numbercall"[^>]*\/>', # Self-closing field
|
|
r'<field\s+name="doall"[^>]*>.*?</field>', # Standard field
|
|
r'<field\s+name="doall"[^>]*>', # Unclosed field
|
|
r'<field\s+name="doall"[^>]*\/>', # Self-closing field
|
|
]
|
|
|
|
# Remove all matching fields
|
|
for pattern in field_patterns:
|
|
content = re.sub(pattern, "", content, flags=re.DOTALL)
|
|
|
|
return f"{opening_tag}{content}{closing_tag}"
|
|
|
|
# Process each file
|
|
for fileno, file in enumerate(files, start=1):
|
|
content = file.content
|
|
original_content = content
|
|
|
|
# Replace matching <record model="ir.cron"> blocks
|
|
content = record_cron_re.sub(clean_record, content)
|
|
|
|
# Only update if changes were made
|
|
if content != original_content:
|
|
file.content = content
|
|
logger.info(
|
|
f"Removed numbercall/doall fields in <record model='ir.cron'> in {file.path}"
|
|
)
|
|
else:
|
|
logger.debug(f"No changes needed in {file.path}")
|
|
|
|
file_manager.print_progress(fileno, len(files))
|