# -*- 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 and within 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 blocks and remove specified fields record_cron_re = re.compile( r""" (]*model="ir\.cron"[^>]*>) # Capture opening with attributes ( # Start capturing content .*? # Any content before fields (non-greedy) (?: # Non-capturing group for fields to remove ]*> # Match with any content .*? # Content inside field # Closing tag | # OR ]*> # Match unclosed | # OR ]*\/> # Match self-closing | # OR ]*> # Match with any content .*? # Content inside field # Closing tag | # OR ]*> # Match unclosed | # OR ]*\/> # Match self-closing ) .*? # Any content after fields (non-greedy) ) () # Capture closing """, re.VERBOSE | re.DOTALL, ) def clean_record(match): """Replace function to remove numbercall and doall fields.""" opening_tag = match.group(1) # content = match.group(2) # Content between tags closing_tag = match.group(3) # # Patterns for fields to remove within the content field_patterns = [ r']*>.*?', # Standard field r']*>', # Unclosed field r']*\/>', # Self-closing field r']*>.*?', # Standard field r']*>', # Unclosed field r']*\/>', # 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 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 in {file.path}" ) else: logger.debug(f"No changes needed in {file.path}") file_manager.print_progress(fileno, len(files))