-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
10db40a
Add a PAPI data provider that reads scripture using ParatextData
lyonsil 14177b5
Fix some bugs and add a VerseRef for selecting data
lyonsil e598770
Updates from PR feedback
lyonsil cc7ab5d
PR updates
lyonsil 7ade69e
Shorten the import statement for using the USFM data provider
lyonsil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.