Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize API for Microsoft.DotNet.Interactive.Documents (Part 1) #3139

Merged
merged 2 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
Microsoft.DotNet.Interactive.Documents
public static class CodeSubmission
public static System.Text.Encoding Encoding { get;}
public static InteractiveDocument Parse(System.String content, KernelInfoCollection kernelInfos = null)
public static InteractiveDocument Read(System.IO.Stream stream, KernelInfoCollection kernelInfos)
public static System.Threading.Tasks.Task<InteractiveDocument> ReadAsync(System.IO.Stream stream, KernelInfoCollection kernelInfos)
public static System.String ToCodeSubmissionContent(System.String newline =
)
public static System.Void Write(InteractiveDocument document, System.IO.Stream stream, System.String newline =
)
public static System.Void Write(InteractiveDocument document, System.IO.TextWriter writer, System.String newline =
)
public static System.Void Write(InteractiveDocument document, System.IO.Stream stream, KernelInfoCollection kernelInfos, System.String newline =
)
public static System.Void Write(InteractiveDocument document, System.IO.TextWriter writer, KernelInfoCollection kernelInfos, System.String newline =
)
public class DisplayElement : InteractiveDocumentOutputElement, IDataElement
.ctor(System.Collections.Generic.IDictionary<System.String,System.Object> data)
Expand All @@ -27,7 +19,6 @@ Microsoft.DotNet.Interactive.Documents
.ctor(System.String valueName, System.String typeHint = text)
public System.String TypeHint { get; set;}
public System.String ValueName { get; set;}
protected System.Boolean Equals(InputField other)
public System.Boolean Equals(System.Object obj)
public System.Int32 GetHashCode()
public class InteractiveDocument, System.Collections.IEnumerable
Expand Down Expand Up @@ -91,7 +82,6 @@ Microsoft.DotNet.Interactive.Documents.Jupyter
public static System.Text.Json.JsonSerializerOptions JsonSerializerOptions { get;}
public static Microsoft.DotNet.Interactive.Documents.InteractiveDocument Parse(System.String json, Microsoft.DotNet.Interactive.Documents.KernelInfoCollection kernelInfos = null)
public static Microsoft.DotNet.Interactive.Documents.InteractiveDocument Read(System.IO.Stream stream, Microsoft.DotNet.Interactive.Documents.KernelInfoCollection kernelInfos)
public static System.Threading.Tasks.Task<Microsoft.DotNet.Interactive.Documents.InteractiveDocument> ReadAsync(System.IO.Stream stream, Microsoft.DotNet.Interactive.Documents.KernelInfoCollection kernelInfo = null)
public static System.String ToJupyterJson(System.String defaultLanguage = null)
public static System.Void Write(Microsoft.DotNet.Interactive.Documents.InteractiveDocument document, System.IO.Stream stream)
public static System.Void Write(Microsoft.DotNet.Interactive.Documents.InteractiveDocument document, System.IO.Stream stream, Microsoft.DotNet.Interactive.Documents.KernelInfoCollection kernelInfos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Microsoft.DotNet.Interactive.Journey
public static System.Collections.Generic.List<System.String> AllDirectiveNames { get;}
public static System.Threading.Tasks.Task<Microsoft.DotNet.Interactive.Documents.InteractiveDocument> LoadNotebookFromUrl(System.Uri uri, System.Net.Http.HttpClient httpClient = null, Microsoft.DotNet.Interactive.CompositeKernel kernel = null)
public static System.Void Parse(Microsoft.DotNet.Interactive.Documents.InteractiveDocument document, ref LessonDefinition& lesson, ref System.Collections.Generic.List<ChallengeDefinition> challenges)
public static System.Threading.Tasks.Task<Microsoft.DotNet.Interactive.Documents.InteractiveDocument> ReadFileAsInteractiveDocument(System.IO.FileInfo file, Microsoft.DotNet.Interactive.CompositeKernel kernel = null)
public static Microsoft.DotNet.Interactive.Documents.InteractiveDocument ReadFileAsInteractiveDocument(System.IO.FileInfo file, Microsoft.DotNet.Interactive.CompositeKernel kernel = null)
.ctor()
public enum Outcome : System.Enum, System.IComparable, System.IConvertible, System.IFormattable
Failure=0
Expand Down
42 changes: 7 additions & 35 deletions src/Microsoft.DotNet.Interactive.Documents/CodeSubmission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive.Documents.Utility;
using Microsoft.DotNet.Interactive.Utility;

namespace Microsoft.DotNet.Interactive.Documents;

public static class CodeSubmission
{
internal const string MagicCommandPrefix = "#!";
private const string MagicCommandPrefix = "#!";

public static Encoding Encoding => new UTF8Encoding(false);
private static readonly Encoding _encoding = new UTF8Encoding(false);

public static InteractiveDocument Parse(
string content,
Expand Down Expand Up @@ -145,20 +144,11 @@ public static InteractiveDocument Read(
Stream stream,
KernelInfoCollection kernelInfos)
{
using var reader = new StreamReader(stream, Encoding);
using var reader = new StreamReader(stream, _encoding);
var content = reader.ReadToEnd();
return Parse(content, kernelInfos);
}

public static async Task<InteractiveDocument> ReadAsync(
Stream stream,
KernelInfoCollection kernelInfos)
{
using var reader = new StreamReader(stream, Encoding);
var content = await reader.ReadToEndAsync();
return Parse(content, kernelInfos);
}

public static string ToCodeSubmissionContent(
this InteractiveDocument document,
string newline = "\n")
Expand Down Expand Up @@ -199,30 +189,12 @@ public static string ToCodeSubmissionContent(
return content;
}

public static void Write(InteractiveDocument document, Stream stream, string newline = "\n")
{
using var writer = new StreamWriter(stream, Encoding, 1024, true);
Write(document, writer, newline);
writer.Flush();
}

public static void Write(InteractiveDocument document, TextWriter writer, string newline = "\n")
{
var content = document.ToCodeSubmissionContent(newline);
writer.Write(content);
}

public static void Write(InteractiveDocument document, Stream stream, KernelInfoCollection kernelInfos, string newline = "\n")
{
InteractiveDocument.MergeKernelInfos(document, kernelInfos);
Write(document, stream, newline);
}



public static void Write(InteractiveDocument document, TextWriter writer, KernelInfoCollection kernelInfos, string newline = "\n")
{
InteractiveDocument.MergeKernelInfos(document, kernelInfos);
Write(document, writer, newline);
using var writer = new StreamWriter(stream, _encoding, 1024, true);
var content = document.ToCodeSubmissionContent(newline);
writer.Write(content);
writer.Flush();
}
}
13 changes: 5 additions & 8 deletions src/Microsoft.DotNet.Interactive.Documents/InputField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ public InputField(string valueName, string typeHint = "text")

public string TypeHint { get; set; }

protected bool Equals(InputField other)
{
return ValueName == other.ValueName && TypeHint == other.TypeHint;
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
Expand All @@ -34,12 +29,14 @@ public override bool Equals(object? obj)
return true;
}

if (obj.GetType() != this.GetType())
if (obj is InputField other)
{
return ValueName == other.ValueName && TypeHint == other.TypeHint;
}
else
{
return false;
}

return Equals((InputField)obj);
}

public override int GetHashCode()
Expand Down
38 changes: 18 additions & 20 deletions src/Microsoft.DotNet.Interactive.Documents/InteractiveDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public IEnumerable<InputField> GetInputFields()

return inputFields.Distinct().ToArray();


static IReadOnlyCollection<InputField> ParseInputFields(string line)
{
var inputFields = new List<InputField>();
Expand Down Expand Up @@ -256,13 +255,13 @@ internal static void MergeKernelInfos(InteractiveDocument document, KernelInfoCo

internal static void MergeKernelInfos(KernelInfoCollection destination, KernelInfoCollection source)
{
var added = new HashSet<string>();
foreach (var kernelInfo in destination)
{
added.Add(kernelInfo.Name);
}
destination.AddRange(source.Where(ki => added.Add(ki.Name)));
var added = new HashSet<string>();
foreach (var kernelInfo in destination)
{
added.Add(kernelInfo.Name);
}

destination.AddRange(source.Where(ki => added.Add(ki.Name)));
}

internal static bool TryGetKernelInfoFromMetadata(
Expand All @@ -271,7 +270,7 @@ internal static bool TryGetKernelInfoFromMetadata(
{
if (metadata is not null)
{
if (metadata.TryGetValue("kernelInfo", out var kernelInfoObj) )
if (metadata.TryGetValue("kernelInfo", out var kernelInfoObj))
{
if (kernelInfoObj is JsonElement kernelInfoJson &&
JsonSerializer.Deserialize<KernelInfoCollection>(kernelInfoJson, JsonSerializerOptions) is
Expand All @@ -284,7 +283,7 @@ internal static bool TryGetKernelInfoFromMetadata(
// todo: the kernelInfo should not deserialize as a dictionary
if (kernelInfoObj is Dictionary<string, object> kernelInfoAsDictionary)
{
var deserializedKernelInfo = new KernelInfoCollection();
var deserializedKernelInfo = new KernelInfoCollection();
if (kernelInfoAsDictionary.TryGetValue("defaultKernelName", out var defaultKernelNameObj) &&
defaultKernelNameObj is string defaultKernelName)
{
Expand Down Expand Up @@ -332,7 +331,6 @@ internal static bool TryGetKernelInfoFromMetadata(
return true;
}
}


if (metadata.TryGetValue("dotnet_interactive", out var dotnetInteractiveObj))
{
Expand All @@ -344,17 +342,17 @@ internal static bool TryGetKernelInfoFromMetadata(
return true;

case IDictionary<string, object> dotnetInteractiveDict:
{
kernelInfo = new();

if (dotnetInteractiveDict.TryGetValue("defaultKernelName", out var nameObj) &&
nameObj is string name)
{
kernelInfo.DefaultKernelName = name;
}
kernelInfo = new();

return true;
}
if (dotnetInteractiveDict.TryGetValue("defaultKernelName", out var nameObj) &&
nameObj is string name)
{
kernelInfo.DefaultKernelName = name;
}

return true;
}
}
}

Expand Down
12 changes: 1 addition & 11 deletions src/Microsoft.DotNet.Interactive.Documents/Jupyter/Notebook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive.Documents.Json;

namespace Microsoft.DotNet.Interactive.Documents.Jupyter;
Expand Down Expand Up @@ -65,15 +64,6 @@ public static InteractiveDocument Read(
return Parse(content, kernelInfos);
}

public static async Task<InteractiveDocument> ReadAsync(
Stream stream,
KernelInfoCollection? kernelInfo = null)
{
using var reader = new StreamReader(stream, Encoding);
var content = await reader.ReadToEndAsync();
return Parse(content, kernelInfo);
}

public static void Write(InteractiveDocument document, Stream stream)
{
using var writer = new StreamWriter(stream, Encoding, 1024, true);
Expand All @@ -91,7 +81,7 @@ public static string ToJupyterJson(
this InteractiveDocument document,
string? defaultLanguage = null)
{
if (defaultLanguage is {})
if (defaultLanguage is { })
{
document.WithJupyterMetadata(defaultLanguage);
}
Expand Down
29 changes: 14 additions & 15 deletions src/Microsoft.DotNet.Interactive.Journey.Tests/ParsingTests.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.DotNet.Interactive.Journey.Tests.Utilities;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Documents;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Documents;
using Microsoft.DotNet.Interactive.Journey.Tests.Utilities;
using Xunit;

namespace Microsoft.DotNet.Interactive.Journey.Tests;

public class ParsingTests : ProgressiveLearningTestBase
{
[Fact]
public async Task parser_can_parse_teacher_notebook_with_two_challenges_with_all_components_defined()
public void parser_can_parse_teacher_notebook_with_two_challenges_with_all_components_defined()
{
InteractiveDocument document = await ReadDibAsync("forParsing1.dib");
InteractiveDocument document = ReadDib("forParsing1.dib");

NotebookLessonParser.Parse(document, out var lesson, out var challenges);

Expand Down Expand Up @@ -47,9 +46,9 @@ public async Task parser_can_parse_teacher_notebook_with_two_challenges_with_all
}

[Fact]
public async Task duplicate_challenge_name_causes_parser_to_throw_exception()
public void duplicate_challenge_name_causes_parser_to_throw_exception()
{
InteractiveDocument document = await ReadDibAsync("forParsing2DuplicateChallengeName.dib");
InteractiveDocument document = ReadDib("forParsing2DuplicateChallengeName.dib");

Action parsingDuplicateChallengeName = () => NotebookLessonParser.Parse(document, out var _, out var _);

Expand All @@ -59,9 +58,9 @@ public async Task duplicate_challenge_name_causes_parser_to_throw_exception()
}

[Fact]
public async Task notebook_with_no_challenge_causes_parser_to_throw_exception()
public void notebook_with_no_challenge_causes_parser_to_throw_exception()
{
InteractiveDocument document = await ReadDibAsync("noChallenge.dib");
InteractiveDocument document = ReadDib("noChallenge.dib");

Action parsingDuplicateChallengeName = () => NotebookLessonParser.Parse(document, out var _, out var _);

Expand All @@ -71,19 +70,19 @@ public async Task notebook_with_no_challenge_causes_parser_to_throw_exception()
}

[Fact]
public async Task a_challenge_with_no_question_causes_parser_to_throw_exception()
public void a_challenge_with_no_question_causes_parser_to_throw_exception()
{
InteractiveDocument document = await ReadDibAsync("challengeWithNoQuestion.dib");
InteractiveDocument document = ReadDib("challengeWithNoQuestion.dib");

Action parsingDuplicateChallengeName = () => NotebookLessonParser.Parse(document, out var _, out var _);

parsingDuplicateChallengeName
.Should().Throw<ArgumentException>()
.Which.Message.Should().Contain("empty question");
}
private async Task<InteractiveDocument> ReadDibAsync(string notebookName)

private InteractiveDocument ReadDib(string notebookName)
{
return await NotebookLessonParser.ReadFileAsInteractiveDocument(new FileInfo(GetPatchedNotebookPath(notebookName)));
return NotebookLessonParser.ReadFileAsInteractiveDocument(new FileInfo(GetPatchedNotebookPath(notebookName)));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.DotNet.Interactive.Journey.Tests.Utilities;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Events;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Journey.Tests.Utilities;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Xunit;

Expand All @@ -17,9 +17,9 @@ public class TeacherValidationTests : ProgressiveLearningTestBase
{
private async Task RunAllCells(FileInfo file, CompositeKernel kernel)
{
var notebook = await NotebookLessonParser.ReadFileAsInteractiveDocument(file, kernel);
foreach (var cell in notebook.Elements.Where(e=> e.KernelName != "markdown"))
var notebook = NotebookLessonParser.ReadFileAsInteractiveDocument(file, kernel);

foreach (var cell in notebook.Elements.Where(e => e.KernelName != "markdown"))
{
await kernel.SendAsync(new SubmitCode(cell.Contents, cell.KernelName));
}
Expand All @@ -30,7 +30,7 @@ public async Task teacher_can_use_scratchpad_to_validate_their_material()
{
var filename = "teacherValidation.dib";
var file = new FileInfo(GetPatchedNotebookPath(filename));

var kernel = await CreateKernel(LessonMode.TeacherMode);
using var events = kernel.KernelEvents.ToSubscribedList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async Task StartCommandHandler(InvocationContext cmdlLineContext)

var document = fromFile switch
{
{ } => await NotebookLessonParser.ReadFileAsInteractiveDocument(fromFile, kernel),
{ } => NotebookLessonParser.ReadFileAsInteractiveDocument(fromFile, kernel),
_ => await NotebookLessonParser.LoadNotebookFromUrl(fromUrl, httpClient)
};

Expand Down
Loading