diff --git a/.gitignore b/.gitignore index f9b9a402..0df76f45 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,7 @@ Examples/**/obj debug .vscode -.DS_Store \ No newline at end of file +.DS_Store + +# Jetbrains +.idea/ diff --git a/Source/actutils.go b/Source/actutils.go index d503143b..8662cbda 100644 --- a/Source/actutils.go +++ b/Source/actutils.go @@ -34,11 +34,52 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package main import ( + "errors" + "fmt" + "log" "os" ) +var ( + ErrPythonBuildFailed = errors.New("failed to build dynamic Python implementation") + ErrFileDeletionFailed = errors.New("failed to write to output file") + ErrReservedKeyword = errors.New("failed to generate bindings as you are using a reserved keyword") +) + +// Keep a map of reserved keywords in Python +var pythonReservedKeywords = map[string]bool{ + "False": true, "None": true, "True": true, "and": true, "as": true, "assert": true, "async": true, + "await": true, "break": true, "class": true, "continue": true, "def": true, "del": true, "elif": true, + "else": true, "except": true, "finally": true, "for": true, "from": true, "global": true, "if": true, + "import": true, "in": true, "is": true, "lambda": true, "nonlocal": true, "not": true, "or": true, + "pass": true, "raise": true, "return": true, "try": true, "while": true, "with": true, "yield": true, +} + // FileExists returns true if and only if the file in a given path exists func FileExists(path string) (bool) { _, err := os.Stat(path); return !os.IsNotExist(err); +} + +// ReservedKeywordExit logs the formatted error, deletes the partially created file, and returns a named error. +func ReservedKeywordExit(bindingPath string, format string, a ...interface{}) error { + // Format the message using variadic arguments + msg := fmt.Sprintf(format, a...) + + // Log the error message + log.Printf("%s", msg) + + // Attempt to delete the partially created file + log.Printf("Deleting partially created binding file: %s", bindingPath) + err := os.Remove(bindingPath) + if err != nil { + // Log and return a wrapped error with context + log.Printf("Failed to delete incomplete file %s: %v", bindingPath, err) + return fmt.Errorf("%w: %v", ErrFileDeletionFailed, err) + } + + log.Printf("Deleted binding file") + + // Return the reserved keyword error + return ErrReservedKeyword } \ No newline at end of file diff --git a/Source/automaticcomponenttoolkit.go b/Source/automaticcomponenttoolkit.go index 760a3167..56310ca3 100644 --- a/Source/automaticcomponenttoolkit.go +++ b/Source/automaticcomponenttoolkit.go @@ -732,10 +732,13 @@ func main() { err = createComponent(component, outfolderBase, bindingsDirectoryOverride, interfacesDirectoryOverride, stubDirectoryOverride, suppressBindings, suppressStub, suppressInterfaces, suppressSubcomponents, suppressLicense, suppressExamples) if (err != nil) { - log.Println("Fatal error") - log.Fatal(err) + if err == ErrPythonBuildFailed { + log.Println("Python binding generation failed (Due to usage of reserved keywords)") + } else { + log.Println("Fatal error") + log.Fatal(err) + } } else { log.Println("Success") } - } diff --git a/Source/buildbindingpython.go b/Source/buildbindingpython.go index 1837f9db..8c3b8e31 100644 --- a/Source/buildbindingpython.go +++ b/Source/buildbindingpython.go @@ -41,6 +41,10 @@ import ( "strings" ) +// Store the python file path +var pythonBindingFile = ""; + + // BuildBindingPythonDynamic builds dynamic Python bindings of a library's API in form of explicitly loaded // functions handles. func BuildBindingPythonDynamic(componentdefinition ComponentDefinition, outputFolder string, outputFolderExample string, indentString string) error { @@ -50,6 +54,7 @@ func BuildBindingPythonDynamic(componentdefinition ComponentDefinition, outputFo libraryname := componentdefinition.LibraryName DynamicPythonImpl := path.Join(outputFolder, namespace+".py"); + pythonBindingFile = DynamicPythonImpl; log.Printf("Creating \"%s\"", DynamicPythonImpl) dynpythonfile, err := CreateLanguageFile (DynamicPythonImpl, indentString) if err != nil { @@ -62,6 +67,9 @@ func BuildBindingPythonDynamic(componentdefinition ComponentDefinition, outputFo err = buildDynamicPythonImplementation(componentdefinition, dynpythonfile) if err != nil { + if err == ErrReservedKeyword { + return ErrPythonBuildFailed + } return err; } @@ -141,6 +149,9 @@ func buildDynamicPythonImplementation(componentdefinition ComponentDefinition, w w.Writeln(" SUCCESS = 0") for i := 0; i