Improve Python GUI extraction and audit all workbenches
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -54,7 +54,7 @@ const parseCppMenus = text => {
|
||||
? { type: "submenu", label: menus.get(item.ref).label, items: menus.get(item.ref).items }
|
||||
: item);
|
||||
}
|
||||
return [...menus.values()].filter(menu => menu.label && !/^&?(File|Edit|View|Tools|Macro|Windows|Help)$/i.test(menu.label));
|
||||
return [...menus.values()].filter(menu => menu.label && menu.items.length > 0 && !/^&?(File|Edit|View|Tools|Macro|Windows|Help)$/i.test(menu.label));
|
||||
};
|
||||
|
||||
const parseCppToolBars = text => {
|
||||
@@ -71,40 +71,80 @@ const parseCppToolBars = text => {
|
||||
return bars;
|
||||
};
|
||||
|
||||
const parsePythonLists = text => {
|
||||
const lists = new Map();
|
||||
for (const match of text.matchAll(/(\w+)\s*=\s*\[([^\]]*)\]/gs)) {
|
||||
const items = [...match[2].matchAll(/["']([^"']+)["']/g)].map(item => item[1]);
|
||||
if (items.length) lists.set(match[1], items);
|
||||
const pythonCalls = (text, name) => {
|
||||
const calls = [];
|
||||
let index = 0;
|
||||
while ((index = text.indexOf(`${name}(`, index)) >= 0) {
|
||||
let cursor = index + name.length + 1;
|
||||
let depth = 1; let quote = "";
|
||||
for (; cursor < text.length; cursor++) {
|
||||
const char = text[cursor];
|
||||
if (quote) { if (char === quote && text[cursor - 1] !== "\\") quote = ""; continue; }
|
||||
if (char === '"' || char === "'") { quote = char; continue; }
|
||||
if (char === "(" || char === "[") depth++;
|
||||
if (char === ")" || char === "]") depth--;
|
||||
if (depth === 0) break;
|
||||
}
|
||||
calls.push(text.slice(index + name.length + 1, cursor));
|
||||
index = cursor + 1;
|
||||
}
|
||||
return lists;
|
||||
return calls;
|
||||
};
|
||||
const pythonArgStrings = value => [...value.matchAll(/["']([^"']+)["']/g)].map(match => match[1]);
|
||||
const resolvePythonItems = (value, lists) => {
|
||||
const trimmed = value.trim();
|
||||
if (lists.has(trimmed)) return lists.get(trimmed);
|
||||
const bracket = /\[([\s\S]*)\]/.exec(trimmed);
|
||||
if (bracket) return pythonArgStrings(bracket[1]);
|
||||
return [];
|
||||
|
||||
const splitPythonArgs = value => {
|
||||
const parts = []; let depth = 0; let quote = ""; let start = 0;
|
||||
for (let index = 0; index < value.length; index++) {
|
||||
const char = value[index];
|
||||
if (quote) { if (char === quote && value[index - 1] !== "\\") quote = ""; continue; }
|
||||
if (char === '"' || char === "'") { quote = char; continue; }
|
||||
if (char === "(" || char === "[") depth++;
|
||||
if (char === ")" || char === "]") depth--;
|
||||
if (char === "," && depth === 0) { parts.push(value.slice(start, index)); start = index + 1; }
|
||||
}
|
||||
parts.push(value.slice(start));
|
||||
return parts.map(part => part.trim()).filter(Boolean);
|
||||
};
|
||||
|
||||
const parsePythonGui = text => {
|
||||
const lists = parsePythonLists(text);
|
||||
const vars = new Map();
|
||||
for (const match of text.matchAll(/^\s*(?:self\.)?(\w+)\s*=\s*\[([\s\S]*?)\]\s*$/gm)) {
|
||||
vars.set(match[1], { type: "list", value: [...match[2].matchAll(/["']([^"']+)["']/g)].map(item => item[1]) });
|
||||
}
|
||||
for (const match of text.matchAll(/^\s*(?:self\.)?(\w+)\s*=\s*QT_TRANSLATE_NOOP\([^,]+,\s*"([^"]*)"\)/gm)) {
|
||||
vars.set(match[1], { type: "label", value: match[2] });
|
||||
}
|
||||
const resolve = expr => {
|
||||
const value = expr.trim();
|
||||
if (vars.has(value)) return vars.get(value);
|
||||
if (/^\[/.test(value)) return { type: "list", value: [...value.matchAll(/["']([^"']+)["']/g)].map(item => item[1]) };
|
||||
const translated = /QT_TRANSLATE_NOOP\([^,]+,\s*"([^"]*)"\)/.exec(value);
|
||||
if (translated) return { type: "label", value: translated[1] };
|
||||
const quoted = /^["']([^"']+)["']$/.exec(value);
|
||||
if (quoted) return { type: "label", value: quoted[1] };
|
||||
return null;
|
||||
};
|
||||
const resolveList = expr => {
|
||||
if (expr.includes("+")) return splitPythonArgs(expr.replace(/\+/g, ",")).flatMap(resolveList);
|
||||
const resolved = resolve(expr);
|
||||
if (!resolved) return [];
|
||||
return resolved.type === "list" ? resolved.value : [resolved.value];
|
||||
};
|
||||
const menus = [];
|
||||
for (const match of text.matchAll(/appendMenu\(([\s\S]*?)\)\s*$/gm)) {
|
||||
const value = match[1];
|
||||
const strings = pythonArgStrings(value);
|
||||
const label = strings.at(-1);
|
||||
const items = resolvePythonItems(value.split(",").slice(0, -1).join(","), lists);
|
||||
if (label) menus.push({ label, items: items.map(id => ({ type: "command", id })) });
|
||||
for (const body of pythonCalls(text, "appendMenu")) {
|
||||
const args = splitPythonArgs(body);
|
||||
if (args.length < 2) continue;
|
||||
const labels = resolveList(args[0]);
|
||||
const items = resolveList(args.at(-1));
|
||||
for (const label of labels) menus.push({ label: label.replace(/^&/, ""), items: items.map(id =>
|
||||
({ type: id === "Separator" ? "separator" : "command", id })) });
|
||||
}
|
||||
const toolbars = [];
|
||||
for (const match of text.matchAll(/appendToolbar\(([\s\S]*?)\)\s*$/gm)) {
|
||||
const value = match[1];
|
||||
const parts = value.split(",");
|
||||
const name = pythonArgStrings(parts[0]).at(-1) ?? parts[0].trim();
|
||||
const items = resolvePythonItems(parts.slice(1).join(","), lists);
|
||||
if (name) toolbars.push({ name, items });
|
||||
for (const body of pythonCalls(text, "appendToolbar")) {
|
||||
const args = splitPythonArgs(body);
|
||||
if (args.length < 2) continue;
|
||||
const names = resolveList(args[0]);
|
||||
const items = resolveList(args[1]);
|
||||
for (const name of names) toolbars.push({ name: name.replace(/^&/, ""), items });
|
||||
}
|
||||
return { menus, toolbars };
|
||||
};
|
||||
@@ -149,7 +189,15 @@ for (const module of moduleIds) {
|
||||
const toolbars = isPython ? parsePythonGui(text).toolbars : parseCppToolBars(text);
|
||||
const docks = [...text.matchAll(/(?:addDockWidget|registerDockWindow)\(\s*"([^"]+)"/g)].map(match => match[1]);
|
||||
if (!isPython) for (const [id, meta] of Object.entries(parseCommandMetadata(text))) commandMetadata[id] = meta;
|
||||
workbenches.push({ id: module, source: path.relative(repoRoot, file), menus, toolbars, docks });
|
||||
const seenMenus = new Set();
|
||||
const cleanMenus = menus.filter(menu => menu.label && menu.label !== "Workbench"
|
||||
&& menu.items.some(item => item.type !== "separator"))
|
||||
.filter(menu => { if (seenMenus.has(menu.label)) return false; seenMenus.add(menu.label); return true; });
|
||||
const seenToolbars = new Set();
|
||||
const cleanToolbars = toolbars.filter(toolbar => toolbar.name && toolbar.name !== "Workbench"
|
||||
&& toolbar.items.length).filter(toolbar => { if (seenToolbars.has(toolbar.name)) return false; seenToolbars.add(toolbar.name); return true; });
|
||||
workbenches.push({ id: module, source: path.relative(repoRoot, file), menus: cleanMenus,
|
||||
toolbars: cleanToolbars, docks });
|
||||
}
|
||||
const commandDir = path.join(srcRoot, "Gui");
|
||||
for (const name of fs.existsSync(commandDir) ? fs.readdirSync(commandDir) : []) {
|
||||
|
||||
Reference in New Issue
Block a user