working on code generation

This commit is contained in:
paul
2024-09-26 18:53:21 +05:30
parent 8c45f896f0
commit 7bba819c38
8 changed files with 194 additions and 6 deletions

View File

@@ -171,6 +171,9 @@ class Widget extends React.Component {
this.generateCode = this.generateCode.bind(this)
this.getImports = this.getImports.bind(this)
this.getRequirements = this.getRequirements.bind(this)
// this.openRenaming = this.openRenaming.bind(this)
this.isSelected = this.isSelected.bind(this)
@@ -334,11 +337,11 @@ class Widget extends React.Component {
return this.constructor.widgetType
}
getRequirements = () => {
getRequirements(){
return this.constructor.requirements
}
getImports = () => {
getImports(){
return this.constructor.requiredImports
}

34
src/codeEngine/utils.js Normal file
View File

@@ -0,0 +1,34 @@
import JSZip from "jszip"
/**
*
* @param {[]} fileDataArray - [{fileData: "", fileName: "", folder: ""}]
*/
export async function createFilesAndDownload(fileDataArray, zipName="untitled"){
const zip = new JSZip()
fileDataArray.forEach(file => {
const { fileData, fileName, folder } = file
// If a folder is specified, create it if it doesn't exist
if (folder) {
const folderRef = zip.folder(folder)
folderRef.file(fileName, fileData)
} else {
// If no folder is specified, place the file in the root
zip.file(fileName, fileData)
}
})
// Generate the ZIP asynchronously
zip.generateAsync({ type: "blob" }).then(function(content) {
const link = document.createElement("a")
link.href = URL.createObjectURL(content)
link.download = `${zipName}.zip`
link.click()
})
}

View File

@@ -1,3 +1,4 @@
import { createFilesAndDownload } from "../../../codeEngine/utils"
import MainWindow from "../widgets/mainWindow"
import { message } from "antd"
@@ -23,8 +24,71 @@ async function generateTkinterCode(projectName, widgetList=[], widgetRefs=[]){
if (mainWindowCount === 0){
message.error("Aborting. No instances of Main window found. Add one and try again")
return
}
let variableNames = [] // {widgetId: "", name: "", count: 1}
let imports = new Set([])
let requirements = new Set([])
let code = [`# This code is generated by PyUIbuilder: https://github.com/paulledemon \n`]
code.push("\n", "import tkinter as tk", "\n\n")
let mainVariable = "" // the main window variable
for (let widget of widgetList){
console.log("Key: ", widget)
const widgetRef = widgetRefs[widget.id].current
let varName = widgetRef.getVariableName()
imports.add(widgetRef.getImports())
requirements.add(widgetRef.getRequirements())
if (widget.widgetType === MainWindow){
mainVariable = varName
}
if (!variableNames.find((val) => val.variable === varName)){
variableNames.push({widgetId: widgetRef.id, name: varName, count: 1})
}else{
// Avoid duplicate names
const existingVariable = variableNames.find((val) => val.variable === varName)
const existingCount = existingVariable.count
existingVariable.count += 1
varName = `${existingVariable.name}${existingCount}`
variableNames.push({widgetId: widgetRef.id, name: varName, count: 1})
}
const parentVariable = variableNames.find(val => val.widgetId === widget.id)?.name || null
console.log("widget ref: ", widgetRef)
let widgetCode = widgetRef.generateCode(varName, parentVariable)
if (!widgetCode instanceof Array){
throw new Error("Generate code function should return array, each new line should be a new item")
}
// add \n after every line
widgetCode = widgetCode.flatMap((item, index) => index < code.length - 1 ? [item, "\n"] : [item])
code.push(...widgetCode)
code.push("\n")
}
code.push(`\n${mainVariable}.mainloop()`) // start the main loop for tkinter
console.log("Code: ", code.join(""))
message.info("starting zipping files, download will start in a few seconds")
// createFilesAndDownload([], projectName).then(() => {
// message.success("Download complete")
// }).catch(() => {
// message.error("Error while downloading")
// })
}

View File

@@ -36,11 +36,12 @@ class MainWindow extends Widget{
this.setWidgetName("main")
}
generateCode(parent){
generateCode(variableName, parent){
return (`
${this.getWidgetName()} = tk.Tk()
`)
return [
`${variableName} = tk.Tk()`,
`${variableName}.title("${this.getAttrValue("title")}")`
]
}
getToolbarAttrs(){

View File

@@ -37,6 +37,14 @@ class TopLevel extends Widget{
this.setWidgetName("toplevel")
}
generateCode(variableName, parent){
return [
`${variableName} = tk.TopLevel(root=${parent})`,
`${variableName}.title("${this.getAttrValue("title")}")`
]
}
getToolbarAttrs(){
const toolBarAttrs = super.getToolbarAttrs()