-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* *Add custom exception for parsing *Fix off-by-one error *Handle triplicate, quadruplicate, n-plicate verses *Add test to cover triplicate verse * Update test; add better error messages to tests * Move try-catch to ProcessTokens * Change error messages for ref asserts * Updates from review * Remove commented out code * Add ParatextProjectTextUpdaterBase class * Draft: Fill in zip classes * Use new class in manual tests * Fix typo in file name * Add direct-from-settings constructor * Remove constructor * Add constructor * Remove zip base class - unnecessary * Move error-handling to updater base; use updater in manual test --------- Co-authored-by: John Lambert <[email protected]> Co-authored-by: Damien Daspit <[email protected]>
- Loading branch information
1 parent
136a3ba
commit cd90626
Showing
9 changed files
with
236 additions
and
54 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.IO; | ||
|
||
namespace SIL.Machine.Corpora | ||
{ | ||
public class FileParatextProjectTextUpdater : ParatextProjectTextUpdaterBase | ||
{ | ||
private readonly string _projectDir; | ||
|
||
public FileParatextProjectTextUpdater(string projectDir) | ||
: base(new FileParatextProjectSettingsParser(projectDir)) | ||
{ | ||
_projectDir = projectDir; | ||
} | ||
|
||
protected override bool Exists(string fileName) | ||
{ | ||
return File.Exists(Path.Combine(_projectDir, fileName)); | ||
} | ||
|
||
protected override Stream Open(string fileName) | ||
{ | ||
return File.OpenRead(Path.Combine(_projectDir, fileName)); | ||
} | ||
} | ||
} |
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,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace SIL.Machine.Corpora | ||
{ | ||
public abstract class ParatextProjectTextUpdaterBase | ||
{ | ||
private readonly ParatextProjectSettings _settings; | ||
|
||
protected ParatextProjectTextUpdaterBase(ParatextProjectSettings settings) | ||
{ | ||
_settings = settings; | ||
} | ||
|
||
protected ParatextProjectTextUpdaterBase(ParatextProjectSettingsParserBase settingsParser) | ||
{ | ||
_settings = settingsParser.Parse(); | ||
} | ||
|
||
public string UpdateUsfm( | ||
string bookId, | ||
IReadOnlyList<(IReadOnlyList<ScriptureRef>, string)> rows, | ||
string fullName = null, | ||
bool stripAllText = false, | ||
bool preferExistingText = true | ||
) | ||
{ | ||
string fileName = _settings.GetBookFileName(bookId); | ||
if (!Exists(fileName)) | ||
return null; | ||
|
||
string usfm; | ||
using (var reader = new StreamReader(Open(fileName))) | ||
{ | ||
usfm = reader.ReadToEnd(); | ||
} | ||
|
||
var handler = new UpdateUsfmParserHandler( | ||
rows, | ||
fullName is null ? null : $"- {fullName}", | ||
stripAllText, | ||
preferExistingText: preferExistingText | ||
); | ||
try | ||
{ | ||
UsfmParser.Parse(usfm, handler, _settings.Stylesheet, _settings.Versification); | ||
return handler.GetUsfm(_settings.Stylesheet); | ||
} | ||
catch (Exception ex) | ||
{ | ||
var sb = new StringBuilder(); | ||
sb.Append($"An error occurred while parsing the usfm for '{bookId}`"); | ||
if (!string.IsNullOrEmpty(_settings.Name)) | ||
sb.Append($" in project '{_settings.Name}'"); | ||
sb.Append($". Error: '{ex.Message}'"); | ||
throw new InvalidOperationException(sb.ToString(), ex); | ||
} | ||
} | ||
|
||
protected abstract bool Exists(string fileName); | ||
protected abstract Stream Open(string fileName); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -41,6 +41,7 @@ public static void Parse( | |
versification, | ||
preserveWhitespace | ||
); | ||
|
||
parser.ProcessTokens(); | ||
} | ||
|
||
|
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,29 @@ | ||
using System.IO; | ||
using System.IO.Compression; | ||
|
||
namespace SIL.Machine.Corpora | ||
{ | ||
public class ZipParatextProjectTextUpdater : ParatextProjectTextUpdaterBase | ||
{ | ||
private readonly ZipArchive _archive; | ||
|
||
public ZipParatextProjectTextUpdater(ZipArchive archive) | ||
: base(new ZipParatextProjectSettingsParser(archive)) | ||
{ | ||
_archive = archive; | ||
} | ||
|
||
protected override bool Exists(string fileName) | ||
{ | ||
return _archive.GetEntry(fileName) != null; | ||
} | ||
|
||
protected override Stream Open(string fileName) | ||
{ | ||
ZipArchiveEntry entry = _archive.GetEntry(fileName); | ||
if (entry == null) | ||
return null; | ||
return entry.Open(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.