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

Add a PAPI data provider that reads scripture using ParatextData #261

Merged
merged 5 commits into from
Jun 27, 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
16 changes: 9 additions & 7 deletions c-sharp-tests/ParatextDataConnectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Reflection;
using Paranext.DataProvider;
using Paranext.DataProvider.ParatextUtils;
using Paratext.Data;
using PtxUtils;

Expand All @@ -12,7 +11,7 @@ internal class ParatextDataConnectionTests
{
// Work around a known issue where NUnit doesn't pick up config files
// https://github.com/nunit/nunit3-vs-adapter/issues/356
private void EnsureIcuConfigFileIsInPlace()
private static void EnsureIcuConfigFileIsInPlace()
{
string appConfigFile = ConfigurationManager
.OpenExeConfiguration(ConfigurationUserLevel.None)
Expand Down Expand Up @@ -49,12 +48,15 @@ public void LoadPackagedWEB_LoadsProject()
EnsureIcuConfigFileIsInPlace();

Console.WriteLine(Assembly.GetExecutingAssembly().Location);
Program.InitializeParatextData("assets");
ParatextGlobals.Initialize("assets");

ScrText scrText = ScrTextCollection.Find("WEB");
Assert.That(scrText, Is.Not.Null);
Assert.That(scrText.Name, Is.EqualTo("WEB"));
Assert.That(scrText.Settings.BooksPresentSet.Count, Is.EqualTo(83));
Assert.Multiple(() =>
{
Assert.That(scrText, Is.Not.Null);
Assert.That(scrText.Name, Is.EqualTo("WEB"));
Assert.That(scrText.Settings.BooksPresentSet.Count, Is.EqualTo(83));
});
}

#region DummyAlert class
Expand Down
99 changes: 99 additions & 0 deletions c-sharp/JsonUtils/VerseRefConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using SIL.Scripture;
using Newtonsoft.Json.Linq;

namespace Paranext.DataProvider.JsonUtils
{
internal class VerseRefConverter
{
public static bool TryCreateVerseRef(
string jsonString,
out VerseRef verseRef,
out string errorMessage
)
{
try
{
return TryCreateVerseRefInternal(jsonString, out verseRef, out errorMessage);
}
catch (Exception e)
{
verseRef = new VerseRef();
Console.Error.Write(e.ToString());
errorMessage = $"Invalid VerseRef ({jsonString}): {e.Message}";
return false;
}
}

private static bool TryCreateVerseRefInternal(
string jsonString,
out VerseRef verseRef,
out string errorMessage
)
{
// Default values for out parameters
verseRef = new VerseRef();
errorMessage = string.Empty;

JObject parsedArgs = JObject.Parse(jsonString);

ScrVers? versification = null;
if (parsedArgs.TryGetValue("versification", out var versificationText))
versification = new ScrVers(versificationText.Value<string>());

if (parsedArgs.TryGetValue("verseString", out var verseString))
{
verseRef =
(versification != null)
? new VerseRef(verseString.Value<string>(), versification)
: new VerseRef(verseString.Value<string>());
return true;
}

if (parsedArgs.TryGetValue("bookChapterVerse", out var bookChapterVerse))
{
verseRef =
(versification != null)
? new VerseRef(bookChapterVerse.Value<int>(), versification)
: new VerseRef(bookChapterVerse.Value<int>());
return true;
}

if (!parsedArgs.ContainsKey("book"))
{
errorMessage = $"Invalid VerseRef ({jsonString}): No recognized properties";
return false;
}

if (parsedArgs["book"]!.Type == JTokenType.Integer)
{
verseRef =
(versification != null)
? new VerseRef(
parsedArgs["book"]!.Value<int>(),
parsedArgs["chapter"]!.Value<int>(),
parsedArgs["verse"]!.Value<int>(),
versification
)
: new VerseRef(
parsedArgs["book"]!.Value<int>(),
parsedArgs["chapter"]!.Value<int>(),
parsedArgs["verse"]!.Value<int>()
);
}
else
{
if (versification == null)
throw new Exception(
"Versification required when book, chapter, and verse are strings"
);
verseRef = new VerseRef(
parsedArgs["book"]!.Value<string>(),
parsedArgs["chapter"]!.Value<string>(),
parsedArgs["verse"]!.Value<string>(),
versification
);
}
return true;
}
}
}
33 changes: 22 additions & 11 deletions c-sharp/NetworkObjects/DataProvider.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Collections.Concurrent;
using Paranext.DataProvider.MessageHandlers;
using Paranext.DataProvider.Messages;
using Paranext.DataProvider.MessageTransports;
using PtxUtils;
using System.Collections.Concurrent;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace Paranext.DataProvider.NetworkObjects
{
Expand Down Expand Up @@ -54,15 +55,25 @@ public void RegisterDataProvider()
// Data providers must provide "get" and "set" functions.
private ResponseToRequest FunctionHandler(dynamic? request)
{
string[] arguments = JsonSerializer.Deserialize<string[]>(request);
if (arguments.Length == 0)
return ResponseToRequest.Failed(
$"No function name provided when calling data provider {DataProviderName}"
);
string functionName;
JsonArray jsonArray;
try
{
jsonArray = ((JsonElement)request!).Deserialize<JsonNode>()!.AsArray();
if (jsonArray.Count == 0)
return ResponseToRequest.Failed(
$"No function name provided when calling data provider {DataProviderName}"
);
functionName = (string)jsonArray[0]!;
jsonArray.RemoveAt(0);
}
catch (Exception e)
{
Console.Error.WriteLine(e.ToString());
return ResponseToRequest.Failed("Invalid function call data");
}

string functionName = arguments[0];
string[] parameters = arguments.Skip(1).ToArray();
return HandleRequest(functionName, parameters);
return HandleRequest(functionName, jsonArray);
}

/// <summary>
Expand All @@ -87,8 +98,8 @@ protected void SendDataUpdateEvent(string dataScope)
/// Handle a request from a service using this data provider
/// </summary>
/// <param name="functionName">This would typically be "getXYZ" or "setXYZ", where "XYZ" is a type of data handled by this provider</param>
/// <param name="arguments">Optional arguments provided by the requester for the function indicated</param>
/// <param name="args">Optional arguments provided by the requester for the function indicated</param>
/// <returns>ResponseToRequest value that either contains a response for the function or an error message</returns>
protected abstract ResponseToRequest HandleRequest(string functionName, string[] arguments);
protected abstract ResponseToRequest HandleRequest(string functionName, JsonArray args);
}
}
3 changes: 2 additions & 1 deletion c-sharp/NetworkObjects/TimeDataProvider.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json.Nodes;
using Paranext.DataProvider.MessageHandlers;
using Paranext.DataProvider.MessageTransports;
using SIL.Extensions;
Expand Down Expand Up @@ -25,7 +26,7 @@ protected override void StartDataProvider()
_timer.Enabled = true;
}

protected override ResponseToRequest HandleRequest(string functionName, string[] arguments)
protected override ResponseToRequest HandleRequest(string functionName, JsonArray args)
{
return functionName switch
{
Expand Down
73 changes: 73 additions & 0 deletions c-sharp/NetworkObjects/UsfmDataProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Paranext.DataProvider.JsonUtils;
using Paranext.DataProvider.MessageHandlers;
using Paranext.DataProvider.MessageTransports;
using Paranext.DataProvider.ParatextUtils;
using Paratext.Data;
using SIL.Scripture;

namespace Paranext.DataProvider.NetworkObjects
{
internal class UsfmDataProvider : DataProvider
{
private readonly string _collectionName;
private ScrText? _scrText;

public UsfmDataProvider(PapiClient papiClient, string dataFolderPath, string collectionName)
: base("usfm", papiClient)
{
_collectionName = collectionName;
ParatextGlobals.Initialize(dataFolderPath);
}

protected override void StartDataProvider()
{
_scrText = ScrTextCollection.Find(_collectionName);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems you would want to use FindById to get the project - which would mean that you would need to have id as part of the arguments. Name isn't guaranteed to be unique.

Also, you later give an error about StartDataProvider not being called based on _scrText being null, but the Find and FindById methods will return null if the project isn't found. You probably want a separate error for that.

Copy link
Member Author

@lyonsil lyonsil Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the moment we're loading out of fixed data files checked into the repo. Once we start working with "real" projects with multiple collections, this will definitely need to be reworked. There isn't a design in play yet for how we will work with projects, so I went with something very simple.


protected override ResponseToRequest HandleRequest(string functionName, JsonArray args)
{
if (_scrText == null)
{
Console.Error.WriteLine("StartDataProvider must be called first");
return ResponseToRequest.Failed("Data provider must be started first");
}

try
{
return functionName switch
{
"getBookNames" => GetBookNames(),
"getChapter" => GetChapter(args[0]!.ToJsonString()),
"getVerse" => GetVerse(args[0]!.ToJsonString()),
_ => ResponseToRequest.Failed($"Unexpected function: {functionName}")
};
}
catch (Exception e)
{
Console.Error.WriteLine(e.ToString());
return ResponseToRequest.Failed(e.Message);
}
}

private static ResponseToRequest GetBookNames()
{
return ResponseToRequest.Succeeded(JsonSerializer.Serialize(Canon.AllBookIds));
}

private ResponseToRequest GetChapter(string args)
{
return VerseRefConverter.TryCreateVerseRef(args, out var verseRef, out string errorMsg)
? ResponseToRequest.Succeeded(_scrText!.GetText(verseRef, true, true))
: ResponseToRequest.Failed(errorMsg);
}

private ResponseToRequest GetVerse(string args)
{
return VerseRefConverter.TryCreateVerseRef(args, out var verseRef, out string errorMsg)
? ResponseToRequest.Succeeded(_scrText!.GetVerseText(verseRef))
: ResponseToRequest.Failed(errorMsg);
}
}
}
1 change: 1 addition & 0 deletions c-sharp/ParanextDataProvider.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="SIL.Scripture" Version="12.0.0.0" />
<PackageReference Include="SIL.WritingSystems" Version="12.0.0.0" />
<PackageReference Include="System.Net.WebSockets" Version="4.3.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
<PackageReference Include="icu.net" Version="2.9.0" />
<PackageReference Include="Microsoft.ICU.ICU4C.Runtime" Version="68.2.0.9" Condition="$([MSBuild]::IsOsPlatform('Windows'))" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion c-sharp/ParanextDataProvider.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=paratext/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=setTime/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Unregister/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Usfm/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Usfm/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Versification/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
42 changes: 42 additions & 0 deletions c-sharp/ParatextUtils/AlertStub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.ComponentModel;
using PtxUtils;

namespace Paranext.DataProvider.ParatextUtils
{
internal sealed class AlertStub : Alert
{
protected override AlertResult ShowInternal(
IComponent? owner,
string text,
string caption,
AlertButtons alertButtons,
AlertLevel alertLevel,
AlertDefaultButton defaultButton,
bool showInTaskbar
)
{
if (text.Contains("unable to find a language definition file for English"))
return AlertResult.Positive;

Console.WriteLine("Unexpected dialog box:\n" + text);
return AlertResult.Negative;
}

protected override void ShowLaterInternal(
string text,
string caption,
AlertLevel alertLevel
)
{
ShowInternal(
null,
text,
caption,
AlertButtons.Ok,
alertLevel,
AlertDefaultButton.Button1,
false
);
}
}
}
42 changes: 42 additions & 0 deletions c-sharp/ParatextUtils/ParatextGlobals.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Text;
using Paratext.Data;
using PtxUtils;

namespace Paranext.DataProvider.ParatextUtils
{
internal static class ParatextGlobals
{
private static readonly object s_locker = new();
private static bool s_initialized = false;

public static void Initialize(string dataFolderPath)
{
// Paratext not supported on MacOS for now
if (OperatingSystem.IsMacOS())
return;

if (s_initialized)
return;

lock (s_locker)
{
if (s_initialized)
return;

// Required for the Paratext.Data.Encodings.StringEncoders static constructor
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

// Required for non-Windows platforms
Alert.Implementation = new AlertStub();
RegistryU.Implementation = new RegistryStub();

// Required for ICU.NET
ICUDllLocator.Initialize(false, false);

// Now tell Paratext.Data to use the specified folder
ParatextData.Initialize(dataFolderPath, false);
s_initialized = true;
}
}
}
}
Loading