updated using CODEX, refresh in canvas layout and functionalities
This commit is contained in:
+293
-115
@@ -1,128 +1,90 @@
|
||||
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import json
|
||||
import yaml
|
||||
from collections import OrderedDict
|
||||
from functools import wraps
|
||||
from flask import Flask, jsonify, send_from_directory, request, redirect, url_for, session, render_template
|
||||
from werkzeug.security import check_password_hash
|
||||
import database # Imports the database.py you created earlier
|
||||
import database
|
||||
from flask import Response
|
||||
|
||||
# --- Path Configurations ---
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
FRONTEND_DIR = os.path.join(BASE_DIR, '..', 'frontend')
|
||||
|
||||
# Use os.path.join exclusively for cross-platform safety
|
||||
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')
|
||||
|
||||
#build layout save path
|
||||
DATABASE_ROOT = os.path.abspath(os.path.join(BASE_DIR, '..', 'database'))
|
||||
|
||||
|
||||
# Initialize Flask, pointing to the frontend folder for HTML/CSS/JS
|
||||
app = Flask(__name__, template_folder=FRONTEND_DIR, static_folder=FRONTEND_DIR)
|
||||
app.secret_key = 'super_secret_mxpic_key' # Required for session management
|
||||
app.json.sort_keys = False # Keep dictionary order
|
||||
app.secret_key = 'super_secret_mxpic_key'
|
||||
app.json.sort_keys = False
|
||||
|
||||
# Ensure database tables exist when the server boots
|
||||
database.init_db()
|
||||
|
||||
# --- YAML & PDK Parsing Helper Functions (Unchanged) ---
|
||||
def countSpaces(line):
|
||||
"""Count leading spaces (tab=4)."""
|
||||
expanded = line.expandtabs(4)
|
||||
return len(expanded) - len(expanded.lstrip(' '))
|
||||
|
||||
def buildTree(filepath):
|
||||
"""Build nested tree from indented yaml."""
|
||||
if not os.path.exists(filepath):
|
||||
return OrderedDict()
|
||||
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
lines = f.readlines()
|
||||
def login_required_json(view_func):
|
||||
@wraps(view_func)
|
||||
def wrapper(*args, **kwargs):
|
||||
if 'user_id' not in session:
|
||||
return jsonify({"error": "Authentication required"}), 401
|
||||
return view_func(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
rootIdx = None
|
||||
for i, line in enumerate(lines):
|
||||
if line.strip().startswith('root') and ':' in line.strip():
|
||||
rootIdx = i
|
||||
break
|
||||
if rootIdx is None:
|
||||
return OrderedDict()
|
||||
|
||||
entries = []
|
||||
for line in lines[rootIdx + 1:]:
|
||||
stripped = line.strip()
|
||||
if not stripped or stripped.startswith('#'):
|
||||
continue
|
||||
if stripped.startswith('- '):
|
||||
spaceNum = countSpaces(line)
|
||||
# FIX 1: Strip trailing colons off the string so 'composites:' becomes 'composites'
|
||||
name = stripped[2:].strip().rstrip(':')
|
||||
if name:
|
||||
entries.append((spaceNum, name))
|
||||
def safe_name(value, fallback):
|
||||
"""Keep user/project/cell names filesystem-friendly without changing display names."""
|
||||
value = (value or '').strip()
|
||||
if not value:
|
||||
value = fallback
|
||||
value = re.sub(r'[^A-Za-z0-9_.-]+', '_', value)
|
||||
value = value.strip('._')
|
||||
return value or fallback
|
||||
|
||||
if not entries:
|
||||
return OrderedDict()
|
||||
|
||||
minIndent = min(indent for indent, _ in entries)
|
||||
nest = OrderedDict()
|
||||
levelStack = [(minIndent - 1, nest)]
|
||||
def user_layout_root():
|
||||
username = safe_name(session.get('username'), 'anonymous')
|
||||
return os.path.join(DATABASE_ROOT, username, 'layout')
|
||||
|
||||
for spaceNum, name in entries:
|
||||
while levelStack and levelStack[-1][0] >= spaceNum:
|
||||
levelStack.pop()
|
||||
parent = levelStack[-1][1]
|
||||
child = OrderedDict()
|
||||
parent[name] = child
|
||||
levelStack.append((spaceNum, child))
|
||||
|
||||
return nest
|
||||
def project_root(project_name):
|
||||
return os.path.join(user_layout_root(), safe_name(project_name, 'project_1'))
|
||||
|
||||
# def addCompsToTree(tree, compMap):
|
||||
# """Insert component nodes into the tree."""
|
||||
# for pathSeg, compItem in compMap.items():
|
||||
# compName = compItem['folder']
|
||||
# curNode = tree
|
||||
|
||||
# # FIX 2: Automatically build missing folder paths
|
||||
# for seg in pathSeg:
|
||||
# if seg not in curNode:
|
||||
# # If a folder like MZM_1600G isn't in the YAML, gracefully auto-create it
|
||||
# curNode[seg] = OrderedDict()
|
||||
# curNode = curNode[seg]
|
||||
|
||||
# curNode[compName] = OrderedDict({
|
||||
# "__type__": "component",
|
||||
# "__name__": compName,
|
||||
# "__yml__": compItem['yml']
|
||||
# })
|
||||
# return tree
|
||||
|
||||
def addCompsToTree(compMap):
|
||||
"""
|
||||
Build a completely fresh tree from scratch and insert component nodes.
|
||||
No previous tree object or inspection required.
|
||||
"""
|
||||
# Initialize a clean, empty root tree
|
||||
fresh_tree = OrderedDict()
|
||||
|
||||
for pathSeg, compItem in compMap.items():
|
||||
compName = compItem['folder']
|
||||
curNode = fresh_tree
|
||||
|
||||
# Sequentially build the nested path segments dynamically
|
||||
for seg in pathSeg:
|
||||
if seg not in curNode:
|
||||
curNode[seg] = OrderedDict()
|
||||
curNode = curNode[seg]
|
||||
|
||||
# Place the component metadata dictionary into its leaf node
|
||||
curNode[compName] = OrderedDict({
|
||||
"__type__": "component",
|
||||
"__name__": compName,
|
||||
"__yml__": compItem['yml']
|
||||
})
|
||||
|
||||
return fresh_tree
|
||||
def cell_file_path(project_name, cell_name):
|
||||
return os.path.join(project_root(project_name), f"{safe_name(cell_name, 'canvas_1')}.yml")
|
||||
|
||||
|
||||
def project_meta_path(project_name):
|
||||
return os.path.join(project_root(project_name), ".project.json")
|
||||
|
||||
|
||||
def read_project_meta(project_name):
|
||||
path = project_meta_path(project_name)
|
||||
if not os.path.exists(path):
|
||||
return {}
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
def write_project_meta(project_name, meta):
|
||||
os.makedirs(project_root(project_name), exist_ok=True)
|
||||
with open(project_meta_path(project_name), 'w', encoding='utf-8') as f:
|
||||
json.dump(meta, f, indent=2)
|
||||
|
||||
# ... [Keep countSpaces and buildTree exactly as they are] ...
|
||||
|
||||
def findComps(baseDir):
|
||||
"""Scan component folders, return map of paths -> component info."""
|
||||
compMap = {}
|
||||
# refDir = os.path.dirname(baseDir)
|
||||
refDir = baseDir
|
||||
for root, dirs, files in os.walk(baseDir):
|
||||
ymlFiles = [f for f in files if f.endswith('.yml')]
|
||||
@@ -131,29 +93,67 @@ def findComps(baseDir):
|
||||
relPath = os.path.relpath(parentDir, refDir)
|
||||
parts = () if relPath == '.' else tuple(relPath.split(os.sep))
|
||||
compName = os.path.basename(root)
|
||||
compMap[parts] = {
|
||||
|
||||
# Extract the category (the mother folder's name)
|
||||
category = os.path.basename(parentDir)
|
||||
|
||||
# Include compName in the key so multiple cells in one category do not overwrite each other.
|
||||
compMap[parts + (compName,)] = {
|
||||
'folder': compName,
|
||||
'yml': ymlFiles[0]
|
||||
'yml': ymlFiles[0],
|
||||
'category': category # Save the category to the map
|
||||
}
|
||||
dirs.clear()
|
||||
return compMap
|
||||
|
||||
# def addCompsToTree(tree, compMap):
|
||||
# """Insert component nodes into the tree."""
|
||||
# for pathSeg, compItem in compMap.items():
|
||||
# compName = compItem['folder']
|
||||
# curNode = tree
|
||||
# try:
|
||||
# for seg in pathSeg:
|
||||
# curNode = curNode[seg]
|
||||
# except KeyError:
|
||||
# continue
|
||||
# curNode[compName] = OrderedDict({
|
||||
# "__type__": "component",
|
||||
# "__name__": compName,
|
||||
# "__yml__": compItem['yml']
|
||||
# })
|
||||
# return tree
|
||||
def addCompsToTree(compMap):
|
||||
"""Build a completely fresh tree from scratch and insert component nodes."""
|
||||
fresh_tree = OrderedDict()
|
||||
|
||||
for mapKey, compItem in compMap.items():
|
||||
pathSeg = mapKey[:-1]
|
||||
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
|
||||
|
||||
# ... [Keep readCompYaml and Page Routes exactly as they are] ...
|
||||
|
||||
# --- API ROUTES (Library, Components & Icons) ---
|
||||
|
||||
@app.route('/api/icon/<category>')
|
||||
def getIcon(category):
|
||||
"""Serve the icon corresponding to the component category."""
|
||||
for ext in ('.png', '.svg', '.jpg'):
|
||||
icon_path = os.path.join(ICONS_DIR, f"{category}{ext}")
|
||||
if os.path.exists(icon_path):
|
||||
return send_from_directory(ICONS_DIR, f"{category}{ext}")
|
||||
|
||||
fallback = os.path.join(ICONS_DIR, "default.png")
|
||||
if os.path.exists(fallback):
|
||||
return send_from_directory(ICONS_DIR, "default.png")
|
||||
|
||||
# return png if not found
|
||||
transparent_png = (
|
||||
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01'
|
||||
b'\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\nIDATx\x9cc\x00\x01'
|
||||
b'\x00\x00\x05\x00\x01\r\n\xf4\xc0\x00\x00\x00\x00IEND\xaeB`\x82'
|
||||
)
|
||||
return Response(transparent_png, mimetype='image/png')
|
||||
|
||||
# ... [Keep existing API routes below] ...
|
||||
|
||||
def readCompYaml(compName):
|
||||
"""Load YAML from component folder."""
|
||||
@@ -214,11 +214,185 @@ def logout():
|
||||
session.clear()
|
||||
return redirect(url_for('home'))
|
||||
|
||||
|
||||
@app.route('/api/technologies', methods=['GET'])
|
||||
@login_required_json
|
||||
def list_technologies():
|
||||
"""List technology choices from mxpic/PDKs/<foundry>/<technology>."""
|
||||
technologies = []
|
||||
pdks_root = os.path.join(BASE_DIR, '..', 'mxpic', 'PDKs')
|
||||
if os.path.isdir(pdks_root):
|
||||
for foundry in sorted(os.listdir(pdks_root)):
|
||||
foundry_path = os.path.join(pdks_root, foundry)
|
||||
if not os.path.isdir(foundry_path):
|
||||
continue
|
||||
for technology in sorted(os.listdir(foundry_path)):
|
||||
technology_path = os.path.join(foundry_path, technology)
|
||||
if not os.path.isdir(technology_path):
|
||||
continue
|
||||
technologies.append({
|
||||
"foundry": foundry,
|
||||
"technology": technology,
|
||||
"id": f"{foundry}/{technology}",
|
||||
"label": f"{foundry} / {technology}"
|
||||
})
|
||||
return jsonify({"technologies": technologies})
|
||||
|
||||
|
||||
@app.route('/api/projects', methods=['GET'])
|
||||
@login_required_json
|
||||
def list_projects():
|
||||
"""List projects stored under database/<username>/layout."""
|
||||
root = user_layout_root()
|
||||
os.makedirs(root, exist_ok=True)
|
||||
|
||||
projects = []
|
||||
for name in sorted(os.listdir(root)):
|
||||
path = os.path.join(root, name)
|
||||
if not os.path.isdir(path):
|
||||
continue
|
||||
cells = []
|
||||
for filename in sorted(os.listdir(path)):
|
||||
if not filename.lower().endswith(('.yml', '.yaml')):
|
||||
continue
|
||||
cell_name = os.path.splitext(filename)[0]
|
||||
yml_path = os.path.join(path, filename)
|
||||
cells.append({
|
||||
"name": cell_name,
|
||||
"has_layout": os.path.exists(yml_path)
|
||||
})
|
||||
meta = read_project_meta(name)
|
||||
projects.append({
|
||||
"name": name,
|
||||
"cells": cells,
|
||||
"technology": meta.get("technology")
|
||||
})
|
||||
|
||||
return jsonify({"projects": projects})
|
||||
|
||||
|
||||
@app.route('/api/projects', methods=['POST'])
|
||||
@login_required_json
|
||||
def create_project():
|
||||
data = request.get_json(silent=True) or {}
|
||||
requested_name = safe_name(data.get('name'), 'project_1')
|
||||
technology = data.get('technology') or ''
|
||||
root = user_layout_root()
|
||||
os.makedirs(root, exist_ok=True)
|
||||
|
||||
project_name = requested_name
|
||||
counter = 1
|
||||
while os.path.exists(os.path.join(root, project_name)):
|
||||
counter += 1
|
||||
project_name = f"{requested_name}_{counter}"
|
||||
|
||||
os.makedirs(project_root(project_name), exist_ok=True)
|
||||
write_project_meta(project_name, {
|
||||
"name": project_name,
|
||||
"technology": technology
|
||||
})
|
||||
return jsonify({"name": project_name, "technology": technology}), 201
|
||||
|
||||
|
||||
@app.route('/api/projects/<project_name>', methods=['GET'])
|
||||
@login_required_json
|
||||
def get_project(project_name):
|
||||
"""Load all saved cells for a project."""
|
||||
root = project_root(project_name)
|
||||
if not os.path.isdir(root):
|
||||
return jsonify({"error": "Project not found"}), 404
|
||||
|
||||
cells = []
|
||||
for filename in sorted(os.listdir(root)):
|
||||
if not filename.lower().endswith(('.yml', '.yaml')):
|
||||
continue
|
||||
cell_name = os.path.splitext(filename)[0]
|
||||
yml_path = os.path.join(root, filename)
|
||||
if not os.path.exists(yml_path):
|
||||
continue
|
||||
with open(yml_path, 'r', encoding='utf-8') as f:
|
||||
cells.append({
|
||||
"name": cell_name,
|
||||
"content": f.read()
|
||||
})
|
||||
|
||||
return jsonify({
|
||||
"name": safe_name(project_name, 'project_1'),
|
||||
"cells": cells,
|
||||
"technology": read_project_meta(project_name).get("technology")
|
||||
})
|
||||
|
||||
|
||||
@app.route('/api/projects/<project_name>', methods=['DELETE'])
|
||||
@login_required_json
|
||||
def delete_project(project_name):
|
||||
"""Delete a user's project folder under database/<username>/layout."""
|
||||
root = project_root(project_name)
|
||||
layout_root = os.path.abspath(user_layout_root())
|
||||
target = os.path.abspath(root)
|
||||
|
||||
if not target.startswith(layout_root + os.sep):
|
||||
return jsonify({"error": "Invalid project path"}), 400
|
||||
if not os.path.isdir(target):
|
||||
return jsonify({"error": "Project not found"}), 404
|
||||
|
||||
shutil.rmtree(target)
|
||||
return jsonify({"message": "deleted", "project": safe_name(project_name, 'project_1')})
|
||||
|
||||
|
||||
@app.route('/api/projects/<project_name>/cells/<cell_name>', methods=['PATCH'])
|
||||
@login_required_json
|
||||
def rename_cell(project_name, cell_name):
|
||||
data = request.get_json(silent=True) or {}
|
||||
old_cell = safe_name(cell_name, 'canvas_1')
|
||||
new_cell = safe_name(data.get('name'), old_cell)
|
||||
if old_cell == new_cell:
|
||||
return jsonify({"message": "unchanged", "cell": new_cell})
|
||||
|
||||
old_path = cell_file_path(project_name, old_cell)
|
||||
new_path = cell_file_path(project_name, new_cell)
|
||||
if os.path.exists(new_path):
|
||||
return jsonify({"error": "Cell name already exists"}), 409
|
||||
if os.path.exists(old_path):
|
||||
os.rename(old_path, new_path)
|
||||
|
||||
return jsonify({"message": "renamed", "old_cell": old_cell, "cell": new_cell})
|
||||
|
||||
|
||||
|
||||
|
||||
@app.route('/api/save-layout', methods=['POST'])
|
||||
@login_required_json
|
||||
def save_layout():
|
||||
try:
|
||||
data = request.get_json()
|
||||
project = safe_name(data.get('project'), 'project_1')
|
||||
cell = safe_name(data.get('cell'), 'canvas_1')
|
||||
content = data.get('content', '')
|
||||
|
||||
save_path = cell_file_path(project, cell)
|
||||
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
||||
|
||||
with open(save_path, 'w', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
|
||||
return jsonify({
|
||||
"message": "successfully saved",
|
||||
"project": project,
|
||||
"cell": cell,
|
||||
"path": save_path
|
||||
}), 200
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
|
||||
|
||||
# --- API ROUTES (Library & Components) ---
|
||||
@app.route('/api/library')
|
||||
def getLib():
|
||||
"""Get library structure."""
|
||||
tree = buildTree(YML_PATH)
|
||||
# tree = buildTree(YML_PATH)
|
||||
if os.path.isdir(COMPS_ROOT):
|
||||
compMap = findComps(COMPS_ROOT)
|
||||
fresh_tree = addCompsToTree(compMap)
|
||||
@@ -249,4 +423,8 @@ def getCompImg(component_name):
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("Starting mxpic EDA Server on http://127.0.0.1:3000")
|
||||
app.run(host='127.0.0.1', port=3000, debug=True)
|
||||
app.run(host='127.0.0.1', port=3000, debug=True)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user