Exemplo n.º 1
0
        public void AppendError(string OutputText, ScriptEngineType engineType)
        {
            Unfreeze();
            string errorHeader = string.Empty;

            switch (engineType)
            {
            case ScriptEngineType.IronPython:
                errorHeader = ScriptConsoleConfigs.ToCustomHtmlTags(ScriptConsoleConfigs.IPYErrorHeader);
                break;

            case ScriptEngineType.CPython:
                errorHeader = ScriptConsoleConfigs.ToCustomHtmlTags(ScriptConsoleConfigs.CPYErrorHeader);
                break;

            case ScriptEngineType.CSharp:
                errorHeader = ScriptConsoleConfigs.ToCustomHtmlTags(ScriptConsoleConfigs.CLRErrorHeader);
                break;

            case ScriptEngineType.Invoke:
                break;

            case ScriptEngineType.VisualBasic:
                errorHeader = ScriptConsoleConfigs.ToCustomHtmlTags(ScriptConsoleConfigs.CLRErrorHeader);
                break;

            case ScriptEngineType.IronRuby:
                errorHeader = ScriptConsoleConfigs.ToCustomHtmlTags(ScriptConsoleConfigs.IRubyErrorHeader);
                break;

            case ScriptEngineType.DynamoBIM:
                break;

            case ScriptEngineType.Grasshopper:
                break;

            case ScriptEngineType.Content:
                break;
            }
            // add new line to header
            if (errorHeader != string.Empty)
            {
                errorHeader += "\n";
            }

            AppendText(errorHeader + OutputText, ScriptConsoleConfigs.ErrorBlock);
        }
Exemplo n.º 2
0
        public System.Windows.Forms.HtmlElement ComposeEntry(string contents, string HtmlElementType)
        {
            WaitReadyBrowser();

            // order is important
            // "<"      --->    &lt;
            contents = ScriptConsoleConfigs.EscapeForHtml(contents);
            // &clt;    --->    ">"
            contents = ScriptConsoleConfigs.FromCustomHtmlTags(contents);
            // "\n"     --->    <br/>
            contents = ScriptConsoleConfigs.EscapeForOutput(contents);
            // :heart:  --->    \uFFFF (emoji unicode)
            contents = Emojis.Emojize(contents);

            var htmlElement = ActiveDocument.CreateElement(HtmlElementType);

            htmlElement.InnerHtml = contents;
            return(htmlElement);
        }
Exemplo n.º 3
0
        public override int Execute(ref ScriptRuntime runtime)
        {
            // Setup the command scope in this engine with proper builtin and scope parameters
            var scope = Engine.CreateScope();

            // Create the script from source file
            var script = Engine.CreateScriptSourceFromFile(
                runtime.ScriptSourceFile,
                System.Text.Encoding.UTF8,
                SourceCodeKind.File
                );

            // Setting up error reporter and compile the script
            // setting module to be the main module so __name__ == __main__ is True
            var compiler_options = (PythonCompilerOptions)Engine.GetCompilerOptions(scope);

            compiler_options.ModuleName = "__main__";
            compiler_options.Module    |= IronPython.Runtime.ModuleOptions.Initialize;

            var errors  = new IronPythonErrorReporter();
            var command = script.Compile(compiler_options, errors);

            // Process compile errors if any
            if (command == null)
            {
                // compilation failed, print errors and return
                runtime.OutputStream.WriteError(string.Join(Environment.NewLine, errors.Errors.ToArray()), ScriptEngineType.IronPython);
                return(ScriptExecutorResultCodes.CompileException);
            }

            // Finally let's execute
            try {
                command.Execute(scope);
                return(ScriptExecutorResultCodes.Succeeded);
            }
            catch (SystemExitException) {
                // ok, so the system exited. That was bound to happen...
                return(ScriptExecutorResultCodes.SysExited);
            }
            catch (Exception exception) {
                // show (power) user everything!
                string clrTraceMessage = exception.ToString();
                string ipyTraceMessage = Engine.GetService <ExceptionOperations>().FormatException(exception);

                // Print all errors to stdout and return cancelled to Revit.
                // This is to avoid getting window prompts from Revit.
                // Those pop ups are small and errors are hard to read.
                ipyTraceMessage = ipyTraceMessage.NormalizeNewLine();

                clrTraceMessage = clrTraceMessage.NormalizeNewLine();

                // set the trace messages on runtime for later usage (e.g. logging)
                runtime.TraceMessage = string.Join("\n", ipyTraceMessage, clrTraceMessage);

                // manually add the CLR traceback since this is a two part error message
                clrTraceMessage = string.Join("\n", ScriptConsoleConfigs.ToCustomHtmlTags(ScriptConsoleConfigs.CLRErrorHeader), clrTraceMessage);

                runtime.OutputStream.WriteError(ipyTraceMessage + "\n\n" + clrTraceMessage, ScriptEngineType.IronPython);
                return(ScriptExecutorResultCodes.ExecutionException);
            }
            finally {
                if (!ExecEngineConfigs.persistent)
                {
                    // cleaning removes all references to revit content that's been casualy stored in global-level
                    // variables and prohibit the GC from cleaning them up and releasing memory
                    var scopeClearScript = Engine.CreateScriptSourceFromString(
                        "for __deref in dir():\n" +
                        "    if not __deref.startswith('__'):\n" +
                        "        del globals()[__deref]");
                    scopeClearScript.Compile();
                    scopeClearScript.Execute(scope);
                }
            }
        }
Exemplo n.º 4
0
        public void AppendError(string OutputText, ScriptEngineType engineType)
        {
            Unfreeze();
            string errorHeader = string.Empty;

            switch (engineType)
            {
            case ScriptEngineType.IronPython:
                errorHeader = ScriptConsoleConfigs.ToCustomHtmlTags(ScriptConsoleConfigs.IPYErrorHeader);
                break;

            case ScriptEngineType.CPython:
                errorHeader = ScriptConsoleConfigs.ToCustomHtmlTags(ScriptConsoleConfigs.CPYErrorHeader);
                break;

            case ScriptEngineType.CSharp:
                errorHeader = ScriptConsoleConfigs.ToCustomHtmlTags(ScriptConsoleConfigs.CSharpErrorHeader);
                break;

            case ScriptEngineType.Invoke:
                break;

            case ScriptEngineType.VisualBasic:
                errorHeader = ScriptConsoleConfigs.ToCustomHtmlTags(ScriptConsoleConfigs.VBErrorHeader);
                break;

            case ScriptEngineType.IronRuby:
                errorHeader = ScriptConsoleConfigs.ToCustomHtmlTags(ScriptConsoleConfigs.IRubyErrorHeader);
                break;

            case ScriptEngineType.DynamoBIM:
                break;

            case ScriptEngineType.Grasshopper:
                break;

            case ScriptEngineType.Content:
                break;
            }
            // add new line to header
            if (errorHeader != string.Empty)
            {
                errorHeader += "\n";
            }

            // if this is a know debugger stop error
            // make a nice report
            foreach (var dbgr in _supportedDebuggers)
            {
                foreach (var stopFinder in dbgr.StopFinders)
                {
                    if (stopFinder.Item1.IsMatch(OutputText))
                    {
                        AppendText(
                            errorHeader + stopFinder.Item2,
                            ScriptConsoleConfigs.ErrorBlock
                            );
                        return;
                    }
                }
            }

            // otherwise report the error
            AppendText(
                errorHeader + OutputText,
                ScriptConsoleConfigs.ErrorBlock
                );
        }