Example #1
0
        private void OnCommandFailed(
            CommandFailed commandFailed,
            IJupyterMessageSender jupyterMessageSender)
        {
            var traceBack = new List <string>();

            switch (commandFailed.Exception)
            {
            case CodeSubmissionCompilationErrorException _:
                traceBack.Add(commandFailed.Message);
                break;

            default:
                traceBack.Add("Unhandled Exception");
                traceBack.Add(commandFailed.Message);
                traceBack.AddRange(commandFailed.Exception?.StackTrace?.Split(new[] { Environment.NewLine }, StringSplitOptions.None) ?? Enumerable.Empty <string>());
                break;
            }

            var errorContent = new Error(
                eName: "Unhandled Exception",
                eValue: commandFailed.Message,
                traceback: traceBack
                );

            // send on iopub
            jupyterMessageSender.Send(errorContent);


            //  reply Error
            var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: _executionCount);

            // send to server
            jupyterMessageSender.Send(executeReplyPayload);
        }
        private void OnLogEvent(
            DiagnosticLogEntryProduced logEvent,
            ZeroMQMessage request,
            IJupyterMessageSender jupyterMessageSender)
        {
            var transient = CreateTransient();

            var span = _textSpanFormatter.ParseToSpan($"{Ansi.Color.Foreground.DarkGray}{logEvent.Message}{Ansi.Text.AttributesOff}");

            var message = span.ToString(OutputMode.Ansi);

            var dataMessage = new DisplayData(
                transient: transient,
                data: new Dictionary <string, object> {
                [PlainTextFormatter.MimeType] = message
            });

            var isSilent = ((ExecuteRequest)request.Content).Silent;

            if (!isSilent)
            {
                // send on io
                jupyterMessageSender.Send(dataMessage);
            }
        }
        private void OnDisplayEvent(DisplayEventBase displayEvent,
                                    ZeroMQMessage request,
                                    IJupyterMessageSender jupyterMessageSender)
        {
            if (displayEvent is ReturnValueProduced && displayEvent.Value is DisplayedValue)
            {
                return;
            }

            var transient = CreateTransient(displayEvent.ValueId);

            var formattedValues = displayEvent
                                  .FormattedValues
                                  .ToDictionary(k => k.MimeType, v => PreserveJson(v.MimeType, v.Value));

            var           value = displayEvent.Value;
            PubSubMessage dataMessage;

            switch (displayEvent)
            {
            case DisplayedValueProduced _:
                dataMessage = new DisplayData(
                    transient: transient,
                    data: formattedValues);
                break;

            case DisplayedValueUpdated _:
                dataMessage = new UpdateDisplayData(
                    transient: transient,
                    data: formattedValues);
                break;

            case ReturnValueProduced _:
                dataMessage = new ExecuteResult(
                    _executionCount,
                    transient: transient,
                    data: formattedValues);
                break;

            case StandardOutputValueProduced _:
                dataMessage = Stream.StdOut(GetPlainTextValueOrDefault(formattedValues, value?.ToString() ?? string.Empty));
                break;

            case StandardErrorValueProduced _:
            case ErrorProduced _:
                dataMessage = Stream.StdErr(GetPlainTextValueOrDefault(formattedValues, value?.ToString() ?? string.Empty));
                break;

            default:
                throw new ArgumentException("Unsupported event type", nameof(displayEvent));
            }

            var isSilent = ((ExecuteRequest)request.Content).Silent;

            if (!isSilent)
            {
                // send on io
                jupyterMessageSender.Send(dataMessage);
            }
        }
        private void OnCommandFailed(
            CommandFailed commandFailed,
            IJupyterMessageSender jupyterMessageSender)
        {
            var traceBack = new List <string>();
            var ename     = "Unhandled exception";
            var emsg      = commandFailed.Message;

            switch (commandFailed.Exception)
            {
            case CodeSubmissionCompilationErrorException _:
                // The diagnostics have already been reported
                ename = "Cell not executed";
                emsg  = "compilation error";
                break;

            case null:

                traceBack.Add(commandFailed.Message);
                break;

            default:
                var exception = commandFailed.Exception;

                traceBack.Add(exception.ToString());

                traceBack.AddRange(
                    exception.StackTrace.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries));

                break;
            }

            var errorContent = new Error(
                eName: ename,
                eValue: emsg,
                traceback: traceBack
                );

            // send on iopub
            jupyterMessageSender.Send(errorContent);

            //  reply Error
            var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: _executionCount);

            // send to server
            jupyterMessageSender.Send(executeReplyPayload);
        }
        private void OnCommandHandled(IJupyterMessageSender jupyterMessageSender)
        {
            // reply ok
            var executeReplyPayload = new ExecuteReplyOk(executionCount: _executionCount);

            // send to server
            jupyterMessageSender.Send(executeReplyPayload);
        }
Example #6
0
        private void OnExecutionInterrupted(IJupyterMessageSender jupyterMessageSender)
        {
            // reply
            var interruptReplyPayload = new InterruptReply();

            // send to server
            jupyterMessageSender.Send(interruptReplyPayload);
        }
Example #7
0
        private void OnCommandFailed(
            CommandFailed commandFailed,
            IJupyterMessageSender jupyterMessageSender)
        {
            var traceBack = new List <string>();

            switch (commandFailed.Exception)
            {
            case CodeSubmissionCompilationErrorException _:
                traceBack.Add(commandFailed.Message);
                break;

            case null:

                traceBack.Add(commandFailed.Message);
                break;

            default:
                var exception = commandFailed.Exception;

                traceBack.Add(
                    $"{exception.GetType().FullName}: {exception.Message}");

                traceBack.AddRange(
                    exception.StackTrace.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries));

                break;
            }

            var errorContent = new Error(
                eName: "Unhandled exception",
                eValue: commandFailed.Message,
                traceback: traceBack
                );

            // send on iopub
            jupyterMessageSender.Send(errorContent);


            //  reply Error
            var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: _executionCount);

            // send to server
            jupyterMessageSender.Send(executeReplyPayload);
        }
Example #8
0
        private static void OnCompletionRequestCompleted(CompletionRequestCompleted completionRequestCompleted, IJupyterMessageSender jupyterMessageSender)
        {
            var command = completionRequestCompleted.Command as RequestCompletion;

            var pos   = SourceUtilities.ComputeReplacementStartPosition(command.Code, command.CursorPosition);
            var reply = new CompleteReply(pos, command.CursorPosition, matches: completionRequestCompleted.CompletionList.Select(e => e.InsertText ?? e.DisplayText).ToList());

            jupyterMessageSender.Send(reply);
        }
        private void Reply(bool isComplete, ZeroMQMessage request, IJupyterMessageSender jupyterMessageSender)
        {
            var status = isComplete ? "complete" : "incomplete";
            var indent = isComplete ? string.Empty : "*";
            // reply
            var isCompleteReplyPayload = new IsCompleteReply(indent: indent, status: status);

            // send to server
            jupyterMessageSender.Send(isCompleteReplyPayload);
        }
Example #10
0
        private static void SendDisplayData(PubSubMessage messageMessage,
                                            Envelope request,
                                            IJupyterMessageSender ioPubChannel)
        {
            var isSilent = ((ExecuteRequest)request.Content).Silent;

            if (!isSilent)
            {
                // send on io
                ioPubChannel.Send(messageMessage);
            }
        }
Example #11
0
        private void OnCommandFailed(
            CommandFailed commandFailed,
            IJupyterMessageSender jupyterMessageSender)
        {
            var errorContent = new Error(
                eName: "Unhandled Exception",
                eValue: commandFailed.Message
                );


            //  reply Error
            var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: _executionCount);

            // send to server
            jupyterMessageSender.Send(executeReplyPayload);
        }
        private static void OnCompletionRequestCompleted(CompletionRequestCompleted completionRequestCompleted, IJupyterMessageSender jupyterMessageSender)
        {
            var command = completionRequestCompleted.Command as RequestCompletion;

            int startPosition, endPosition;

            if (completionRequestCompleted.ReplacementStartIndex != null)
            {
                startPosition = completionRequestCompleted.ReplacementStartIndex.Value;
                endPosition   = completionRequestCompleted.ReplacementEndIndex.Value;
            }
            else
            {
                startPosition = SourceUtilities.ComputeReplacementStartPosition(command.Code, command.CursorPosition);
                endPosition   = command.CursorPosition;
            }

            var reply = new CompleteReply(startPosition, endPosition, matches: completionRequestCompleted.CompletionList.Select(e => e.InsertText ?? e.DisplayText).ToList());

            jupyterMessageSender.Send(reply);
        }
        private static void OnCompletionRequestCompleted(CompletionRequestCompleted completionRequestCompleted, IJupyterMessageSender jupyterMessageSender)
        {
            var command = completionRequestCompleted.Command as RequestCompletion;

            int startPosition, endPosition;

            if (completionRequestCompleted.Range != null)
            {
                startPosition = SourceUtilities.GetCursorOffsetFromPosition(command.Code, completionRequestCompleted.Range.GetValueOrDefault().Start);
                endPosition   = SourceUtilities.GetCursorOffsetFromPosition(command.Code, completionRequestCompleted.Range.GetValueOrDefault().End);
            }
            else
            {
                var cursorOffset = SourceUtilities.GetCursorOffsetFromPosition(command.Code, command.Position);
                startPosition = SourceUtilities.ComputeReplacementStartPosition(command.Code, cursorOffset);
                endPosition   = cursorOffset;
            }

            var reply = new CompleteReply(startPosition, endPosition, matches: completionRequestCompleted.CompletionList.Select(e => e.InsertText ?? e.DisplayText).ToList());

            jupyterMessageSender.Send(reply);
        }
        private static void OnCompletionRequestCompleted(CompletionsProduced completionsProduced, IJupyterMessageSender jupyterMessageSender)
        {
            var startPosition = 0;
            var endPosition   = 0;

            if (completionsProduced.Command is RequestCompletions command)
            {
                if (completionsProduced.LinePositionSpan is not null)
                {
                    startPosition = SourceUtilities.GetCursorOffsetFromPosition(command.Code, completionsProduced.LinePositionSpan.Start);
                    endPosition   = SourceUtilities.GetCursorOffsetFromPosition(command.Code, completionsProduced.LinePositionSpan.End);
                }
                else
                {
                    var cursorOffset = SourceUtilities.GetCursorOffsetFromPosition(command.Code, command.LinePosition);
                    startPosition = SourceUtilities.ComputeReplacementStartPosition(command.Code, cursorOffset);
                    endPosition   = cursorOffset;
                }
            }

            var reply = new CompleteReply(startPosition, endPosition, matches: completionsProduced.Completions.Select(e => e.InsertText ?? e.DisplayText).ToList());

            jupyterMessageSender.Send(reply);
        }
        private void OnDisplayEvent(DisplayEvent displayEvent,
                                    ZeroMQMessage request,
                                    IJupyterMessageSender jupyterMessageSender)
        {
            if (displayEvent is ReturnValueProduced && displayEvent.Value is DisplayedValue)
            {
                return;
            }

            var transient = CreateTransient(displayEvent.ValueId);


            // Currently there is at most one formatted value with at most
            // and we return a dictionary for JSON formatting keyed by that mime type
            //
            // In the case of DiagnosticsProduced however there are multiple entries, one
            // for each diagnsotic, all with the same type
            Dictionary <string, object> GetFormattedValuesByMimeType()
            {
                return
                    (displayEvent
                     .FormattedValues
                     .ToDictionary(k => k.MimeType, v => PreserveJson(v.MimeType, v.Value)));
            }

            var           value       = displayEvent.Value;
            PubSubMessage dataMessage = null;

            switch (displayEvent)
            {
            case DisplayedValueProduced _:
                dataMessage = new DisplayData(
                    transient: transient,
                    data: GetFormattedValuesByMimeType());
                break;

            case DisplayedValueUpdated _:
                dataMessage = new UpdateDisplayData(
                    transient: transient,
                    data: GetFormattedValuesByMimeType());
                break;

            case ReturnValueProduced _:
                dataMessage = new ExecuteResult(
                    _executionCount,
                    transient: transient,
                    data: GetFormattedValuesByMimeType());
                break;

            case StandardOutputValueProduced _:
                dataMessage = Stream.StdOut(GetPlainTextValueOrDefault(GetFormattedValuesByMimeType(), value?.ToString() ?? string.Empty));
                break;

            case StandardErrorValueProduced _:
            case ErrorProduced _:
                dataMessage = Stream.StdErr(GetPlainTextValueOrDefault(GetFormattedValuesByMimeType(), value?.ToString() ?? string.Empty));
                break;

            default:
                throw new ArgumentException("Unsupported event type", nameof(displayEvent));
            }

            var isSilent = ((ExecuteRequest)request.Content).Silent;

            if (!isSilent)
            {
                // send on io
                jupyterMessageSender.Send(dataMessage);
            }
        }