84 lines
3.5 KiB
Python
84 lines
3.5 KiB
Python
import json
|
|
import os
|
|
import io
|
|
from contextlib import redirect_stdout
|
|
import traceback
|
|
from lxml import etree
|
|
from config_manager import DEFAULT_CONFIG
|
|
import logging
|
|
|
|
# Core processing logic
|
|
from . import element_processor
|
|
# Use the absolute import
|
|
from processing.inkscape_transform import SVGTransformer
|
|
from .scada_exporter import ScadaCreatorError # Updated import name
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class ProcessorError(Exception):
|
|
"""Custom exception for processing errors."""
|
|
pass
|
|
|
|
def process_svg_file(file_path, custom_options, message_callback, log_callback, center_mode='bbox'):
|
|
"""
|
|
Processes the SVG file using SVGTransformer.
|
|
|
|
Args:
|
|
file_path (str): Path to the SVG file.
|
|
custom_options (dict): Dictionary containing element mappings and SCADA settings.
|
|
message_callback (callable): Function to send status messages (e.g., thread_manager.put_message).
|
|
log_callback (callable): Function to log messages (e.g., log_frame.log_message).
|
|
|
|
Returns:
|
|
list: A list of processed element dictionaries.
|
|
|
|
Raises:
|
|
ProcessorError: If an error occurs during processing.
|
|
"""
|
|
if not os.path.exists(file_path):
|
|
raise ProcessorError(f"File not found: {file_path}")
|
|
|
|
message_callback(f"Processing: {os.path.basename(file_path)}...")
|
|
log_callback(f"Starting SVG processing for {file_path}...")
|
|
log_callback(f"Options: {json.dumps(custom_options, indent=2)}")
|
|
|
|
try:
|
|
# Use a string buffer to capture stdout from the transformer if needed
|
|
# log_capture = io.StringIO()
|
|
# with redirect_stdout(log_capture):
|
|
|
|
# Initialize transformer
|
|
# Pass only relevant parts of custom_options if SVGTransformer expects specific keys
|
|
transformer = SVGTransformer(file_path, custom_options)
|
|
|
|
# Process SVG
|
|
elements = transformer.process_svg(center_mode=center_mode)
|
|
|
|
# Log captured stdout if used
|
|
# captured_log = log_capture.getvalue()
|
|
# if captured_log:
|
|
# log_callback(f"Transformer Output:\n{captured_log}")
|
|
|
|
log_callback(f"Successfully processed {len(elements)} elements from {os.path.basename(file_path)}.")
|
|
message_callback("Processing complete.") # Intermediate message
|
|
|
|
# Simply return the elements processed by the transformer
|
|
return elements
|
|
|
|
except FileNotFoundError as fnf_err:
|
|
log_callback(f"ERROR: SVG file not found: {fnf_err}")
|
|
raise ProcessorError(f"SVG file not found: {fnf_err}") from fnf_err
|
|
except json.JSONDecodeError as json_err:
|
|
log_callback(f"ERROR: Failed to parse potential JSON within SVG or options: {json_err}")
|
|
raise ProcessorError(f"JSON parsing error: {json_err}") from json_err
|
|
except ImportError as imp_err:
|
|
log_callback(f"ERROR: Missing dependency, likely Inkscape: {imp_err}")
|
|
raise ProcessorError(f"Missing dependency (Inkscape?): {imp_err}") from imp_err
|
|
except ScadaCreatorError as sce: # Update except block if ScadaCreatorError could be raised here
|
|
log_callback(f"ERROR: SCADA View Creation related error during processing: {sce}")
|
|
raise ProcessorError(f"SCADA View Creation Error: {sce}") from sce
|
|
except Exception as e:
|
|
# Use imported functions
|
|
log_callback(f"ERROR: An unexpected error occurred during SVG processing: {e}")
|
|
log_callback(f"Traceback:\n{traceback.format_exc()}")
|
|
raise ProcessorError(f"Processing failed: {e}") from e |