import os import yaml from collections import OrderedDict from flask import Flask, jsonify, send_from_directory, request, redirect, url_for, session, render_template from werkzeug.security import check_password_hash import database # --- Path Configurations --- BASE_DIR = os.path.dirname(os.path.abspath(__file__)) FRONTEND_DIR = os.path.join(BASE_DIR, '..', 'frontend') YML_PATH = os.path.join(BASE_DIR, '..', 'mxpic', 'PDKs', 'Silterra', 'directories.yaml') COMPS_ROOT = os.path.join(BASE_DIR, '..', 'mxpic', 'PDKs', 'Silterra') # Define where your new icons folder is located (adjust if it's placed elsewhere) ICONS_DIR = os.path.join(BASE_DIR, 'icons') app = Flask(__name__, template_folder=FRONTEND_DIR, static_folder=FRONTEND_DIR) app.secret_key = 'super_secret_mxpic_key' app.json.sort_keys = False database.init_db() # ... [Keep countSpaces and buildTree exactly as they are] ... def findComps(baseDir): """Scan component folders, return map of paths -> component info.""" compMap = {} refDir = baseDir for root, dirs, files in os.walk(baseDir): ymlFiles = [f for f in files if f.endswith('.yml')] if ymlFiles: parentDir = os.path.dirname(root) relPath = os.path.relpath(parentDir, refDir) parts = () if relPath == '.' else tuple(relPath.split(os.sep)) compName = os.path.basename(root) # Extract the category (the mother folder's name) category = os.path.basename(parentDir) compMap[parts] = { 'folder': compName, 'yml': ymlFiles[0], 'category': category # Save the category to the map } dirs.clear() return compMap def addCompsToTree(compMap): """Build a completely fresh tree from scratch and insert component nodes.""" fresh_tree = OrderedDict() for pathSeg, compItem in compMap.items(): compName = compItem['folder'] curNode = fresh_tree for seg in pathSeg: if seg not in curNode: curNode[seg] = OrderedDict() curNode = curNode[seg] curNode[compName] = OrderedDict({ "__type__": "component", "__name__": compName, "__yml__": compItem['yml'], "__category__": compItem['category'] # Inject category into the tree }) return fresh_tree if os.path.isdir(COMPS_ROOT): compMap = findComps(COMPS_ROOT) fresh_tree = addCompsToTree(compMap) print(compMap) print(fresh_tree)