MonitorProgress/scada_checker.py
2025-04-10 04:08:55 +04:00

60 lines
2.8 KiB
Python

import os
import json
from utils import normalize, get_views_dir_path
def check_scada(project_name, manifest_data):
"""
Checks for aliases in SCADA JSON view files for a specific project.
Updates the 'found_scada' flag in the manifest_data items directly.
"""
if not manifest_data:
print(f"[{project_name}] SCADA Check: No manifest data provided.")
return
views_dir = get_views_dir_path(project_name)
print(f"[{project_name}] Starting SCADA check in directory: {views_dir}...")
found_count = 0
processed_files = 0
if not os.path.exists(views_dir):
print(f"Warning: SCADA Views directory not found at {views_dir}. Skipping SCADA check.")
# No need to mark all as False, they default to False
return
# Create a quick lookup map of normalized_alias -> list of manifest items (handles duplicate aliases)
alias_map = {}
for item in manifest_data:
na = item['normalized_alias']
if na not in alias_map:
alias_map[na] = []
alias_map[na].append(item)
try:
for root, _, files in os.walk(views_dir):
for filename in files:
if filename == 'view.json':
filepath = os.path.join(root, filename)
processed_files += 1
try:
with open(filepath, 'r', encoding='utf-8') as f:
# Read the whole file, normalize it for substring search
# Consider JSON loading for more robust checks? For now, string search.
content = f.read()
normalized_content = normalize(content)
# Check manifest aliases against this file's normalized content
for norm_alias, items in alias_map.items():
# Use 'in' for substring check
if norm_alias and norm_alias in normalized_content:
for item in items:
if not item['found_scada']: # Update only if not already found elsewhere
item['found_scada'] = True
# Count unique aliases found the *first* time
found_count += 1
except Exception as e:
print(f" Warning: Could not read or process JSON file {filepath}: {e}")
except Exception as e:
print(f"Error walking SCADA views directory {views_dir}: {e}")
print(f"[{project_name}] SCADA check finished. Processed {processed_files} view.json files. Found {found_count} unique manifest aliases.")
# No return value needed as manifest_data is modified in-place