Skip to content

Commit

Permalink
Support multiple viselements (#33) (#47)
Browse files Browse the repository at this point in the history
* Support multiple viselements

* Apply linter

* per Fabien

* fix descriptors file path
  • Loading branch information
dinhlongviolin1 authored Nov 7, 2023
1 parent c8183af commit a38accb
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 101 deletions.
10 changes: 5 additions & 5 deletions l10n/bundle.l10n.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"Unmatched number of curly braces for expression": "Unmatched number of curly braces for expression",
"Select properties for element '{0}'": "Select properties for element '{0}'",
"Visual Element added": "Visual Element added",
"Find element descriptors file: ": "Find element descriptors file: ",
"Finding visual element descriptors file": "Finding visual element descriptors file",
"Visual element descriptors file was found and updated in workspace settings": "Visual element descriptors file was found and updated in workspace settings",
"Can't find visual element descriptors file with the selected environment": "Can't find visual element descriptors file with the selected environment",
"Find element descriptor files: ": "Find element descriptor files: ",
"Finding visual element descriptor files": "Finding visual element descriptor files",
"Visual element descriptor files was found and updated in workspace settings": "Visual element descriptor files was found and updated in workspace settings",
"Can't find visual element descriptor files with the selected environment": "Can't find visual element descriptor files with the selected environment",
"Missing closing syntax": "Missing closing syntax",
"Missing opening tag": "Missing opening tag",
"Missing matching opening tag identifier '{0}'": "Missing matching opening tag identifier '{0}'",
Expand All @@ -25,6 +25,6 @@
". Do you mean '{0}'?": ". Do you mean '{0}'?",
"Negated value of property '{0}' will be ignored": "Negated value of property '{0}' will be ignored",
"Function '{0}' in property '{1}' is not available": "Function '{0}' in property '{1}' is not available",
"Parse element descriptors file: ": "Parse element descriptors file: ",
"Parse element descriptor files: ": "Parse element descriptor files: ",
"Parse mock data file: ": "Parse mock data file: "
}
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@
"configuration": {
"title": "Taipy Studio GUI Helper",
"properties": {
"taipyStudio.gUI.elementsFilePath": {
"type": "string",
"default": "",
"description": "%taipyStudio.gUI.elementsFilePath.description%"
"taipyStudio.gUI.elementsFilePaths": {
"type": "array",
"items": {
"type": "string"
},
"default": [],
"description": "%taipyStudio.gUI.elementsFilePaths.description%"
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions package.nls.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"taipy.gui.md.generate.title": "Taipy: Generate visual element",
"taipy.gui.md.findElementFile.title": "Taipy: Locate visual element descriptors file in taipy-gui package",
"taipyStudio.gUI.elementsFilePath.description": "Specifies the path for visual element descriptors file. If this property is empty, the default visual element descriptors file will be used"
"taipy.gui.md.findElementFile.title": "Taipy: Locate visual element descriptor files in taipy-gui package",
"taipyStudio.gUI.elementsFilePaths.description": "Specifies the paths for visual element descriptor files. If this property is empty, the default visual element descriptor files will be used"
}
15 changes: 11 additions & 4 deletions src/assets/find_element_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

if util.find_spec("taipy") and util.find_spec("taipy.gui"):
from taipy.gui import Gui
element_file_path = f"{os.path.dirname(inspect.getfile(Gui))}{os.sep}webapp{os.sep}viselements.json"
if os.path.exists(element_file_path):
print(f"Path: {element_file_path}")

taipy_path = f"{os.path.dirname(os.path.dirname(inspect.getfile(Gui)))}"
potential_file_paths = [
f"{taipy_path}{os.sep}gui{os.sep}viselements.json",
f"{taipy_path}{os.sep}gui_core{os.sep}viselements.json",
]
if potential_file_paths := [
path for path in potential_file_paths if os.path.exists(path)
]:
print(f"Path: {';;;'.join(potential_file_paths)}")
else:
print("Visual element descriptors file not found in taipy-gui package")
print("Visual element descriptor files not found in taipy-gui package")
else:
print("taipy-gui package is not installed within the selected python environment")
18 changes: 9 additions & 9 deletions src/gui/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ExtensionContext, ProgressLocation, WorkspaceEdit, commands, l10n, wind

import { ElementProvider } from "./elementProvider";
import { getLog } from "./logging";
import { countChar, execShell, parseProperty, updateFilePath } from "./utils";
import { countChar, execShell, parseProperty, updateFilePaths } from "./utils";

interface GuiElement {
elementName: string;
Expand Down Expand Up @@ -120,36 +120,36 @@ export class FindElementsFileCommand {
workspace.workspaceFolders?.map(({ uri }) => uri.fsPath),
);
} catch (error: any) {
getLog().info(l10n.t("Find element descriptors file: "), error.message || error);
getLog().info(l10n.t("Find element descriptor files: "), error.message || error);
}
pythonPath = pythonPath || workspace.getConfiguration("python").get("defaultInterpreterPath", "python");
window.withProgress(
{
location: ProgressLocation.Notification,
cancellable: false,
title: l10n.t("Finding visual element descriptors file"),
title: l10n.t("Finding visual element descriptor files"),
},
async (progress) => {
async (_progress) => {
try {
const execResult = await execShell(
`${pythonPath} ${join(__dirname, "assets", "find_element_file.py")}`,
);
if (execResult.startsWith("Path: ")) {
updateFilePath(execResult.substring(6));
updateFilePaths(execResult.trim().substring(6).split(";;;"));
window.showInformationMessage(
l10n.t("Visual element descriptors file was found and updated in workspace settings"),
l10n.t("Visual element descriptor files was found and updated in workspace settings"),
);
} else if (execResult) {
window.showErrorMessage(execResult);
} else {
window.showErrorMessage(
l10n.t("Can't find visual element descriptors file with the selected environment"),
l10n.t("Can't find visual element descriptor files with the selected environment"),
);
}
} catch (error: any) {
getLog().error(l10n.t("Find element descriptors file: "), error.message || error);
getLog().error(l10n.t("Find element descriptor files: "), error.message || error);
window.showErrorMessage(
l10n.t("Can't find visual element descriptors file with the selected environment"),
l10n.t("Can't find visual element descriptor files with the selected environment"),
);
}
},
Expand Down
5 changes: 3 additions & 2 deletions src/gui/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import { join } from "path";
import {
getBlockElementList,
getControlElementList,
getElementFile,
getElementList,
getElementProperties,
getElementsFromFile,
getEmptyVisualElements,
getOnFunctionList,
getOnFunctionSignature,
} from "./utils";

const visualElements = getElementFile(join(__dirname, "assets", "viselements.json")) || {};
const visualElements = getElementsFromFile(join(__dirname, "assets", "viselements.json")) || getEmptyVisualElements();

// object of all elements each with all of its properties
export const defaultElementProperties = getElementProperties(visualElements);
Expand Down
44 changes: 25 additions & 19 deletions src/gui/elementProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ import {
} from "./constant";
import {
ElementProperty,
VisualElements,
getBlockElementList,
getControlElementList,
getElementFile,
getElementFilePath,
getElementFilePaths,
getElementList,
getElementProperties,
getElementsFromFiles,
getOnFunctionList,
getOnFunctionSignature,
} from "./utils";

export class ElementProvider {
private static elementCache: Record<string, Record<string, any>> = {};
private static elementCache: Record<string, any> = {};

public static getElementProperties(): Record<string, Record<string, ElementProperty>> {
return ElementProvider.resolve("elementProperties", defaultElementProperties, getElementProperties);
Expand All @@ -50,41 +51,46 @@ export class ElementProvider {
}

public static getOnFunctionList(): string[] {
const filePath = getElementFilePath();
if (filePath === null) {
const filePaths = getElementFilePaths();
if (filePaths.length === 0) {
return defaultOnFunctionList;
}
return getOnFunctionList(ElementProvider.getElementProperties());
}

public static getOnFunctionSignature(): Record<string, [string, string][]> {
const filePath = getElementFilePath();
if (filePath === null) {
const filePaths = getElementFilePaths();
if (filePaths.length === 0) {
return defaultOnFunctionSignature;
}
return getOnFunctionSignature(ElementProvider.getElementProperties());
}

private static resolve(propertyName: string, defaultValue: any, elementsAnalyzer: (viselements: object) => any) {
const filePath = getElementFilePath();
if (filePath === null) {
public static invalidateCache() {
ElementProvider.elementCache = {};
}

private static resolve(
propertyName: string,
defaultValue: any,
elementsAnalyzer: (visualElements: VisualElements) => any,
) {
const filePaths = getElementFilePaths();
if (filePaths.length === 0) {
return defaultValue;
}
if (ElementProvider.elementCache[filePath] === undefined) {
ElementProvider.elementCache[filePath] = {};
}
if (propertyName in ElementProvider.elementCache[filePath]) {
return ElementProvider.elementCache[filePath][propertyName];
if (propertyName in ElementProvider.elementCache) {
return ElementProvider.elementCache[propertyName];
}
const visualElements = ElementProvider.elementCache[filePath]["visualElements"] || getElementFile(filePath);
const visualElements = ElementProvider.elementCache["visualElements"] || getElementsFromFiles(filePaths);
if (!visualElements) {
return defaultValue;
}
if (!(propertyName in ElementProvider.elementCache[filePath])) {
ElementProvider.elementCache[filePath].visualElements = visualElements;
if (!(propertyName in ElementProvider.elementCache)) {
ElementProvider.elementCache.visualElements = visualElements;
}
const result = elementsAnalyzer(visualElements);
ElementProvider.elementCache[filePath][propertyName] = result;
ElementProvider.elementCache[propertyName] = result;
return result;
}
}
Loading

0 comments on commit a38accb

Please sign in to comment.