Skip to content

Commit

Permalink
Merge pull request #33 from alopezlago/alopezlago/public_v041
Browse files Browse the repository at this point in the history
Merged PR 222: v0.4.1 - Don't include </> in metadata for JSON, show non-3-part bonuses
  • Loading branch information
alopezlago committed Nov 14, 2021
2 parents c09856d + 54619be commit 0683fc4
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 30 deletions.
Expand Up @@ -50,8 +50,7 @@ private static void WriteTossup(TossupNode tossup, StringBuilder builder)
WriteQuestion(tossup.Question, builder);
if (!string.IsNullOrEmpty(tossup.Metadata))
{
builder.Append(tossup.Metadata);
builder.Append("<br>");
builder.Append($"&lt;{tossup.Metadata}&gt;<br>");
}

builder.Append("</p>");
Expand All @@ -71,8 +70,7 @@ private static void WriteBonus(BonusNode bonus, StringBuilder builder)

if (!string.IsNullOrEmpty(bonus.Metadata))
{
builder.Append(bonus.Metadata);
builder.Append("<br>");
builder.Append($"&lt;{bonus.Metadata}&gt;<br>");
}

builder.Append("</p>");
Expand Down
15 changes: 11 additions & 4 deletions YetAnotherPacketParser/YetAnotherPacketParser/PacketConverter.cs
Expand Up @@ -153,7 +153,7 @@ private static ConvertResult CreateFailedCompileResult(string streamName, string
return CreateFailedCompileResult(packetName, Strings.LexingError, linesResult.ErrorMessages);
}

options.Log?.Invoke(LogLevel.Verbose, $"{packetName}: Lexing complete.");
options.Log?.Invoke(LogLevel.Verbose, Strings.LexingComplete(packetName));

LinesParserOptions parserOptions = new LinesParserOptions()
{
Expand All @@ -173,7 +173,14 @@ private static ConvertResult CreateFailedCompileResult(string streamName, string
int tossupsCount = packetNode.Tossups.Count();
int bonusesCount = packetNode.Bonuses?.Count() ?? 0;
options.Log?.Invoke(
LogLevel.Informational, $"{packetName}: Parsing complete. {tossupsCount} tossup(s), {bonusesCount} bonus(es).");
LogLevel.Informational, Strings.ParsingComplete(packetName, tossupsCount, bonusesCount));

if (options.Log != null && packetNode.Bonuses?.Any(bonus => bonus.Parts.Count() != 3) == true)
{
options.Log.Invoke(
LogLevel.Informational, Strings.NonThreePartBonusesFound(
packetNode.Bonuses.Where(bonus => bonus.Parts.Count() != 3).Select((bonus) => bonus.Number)));
}

string outputContents;
switch (options.OutputFormat)
Expand All @@ -199,7 +206,7 @@ private static ConvertResult CreateFailedCompileResult(string streamName, string
stopwatch.Stop();
long timeInMsCompile = stopwatch.ElapsedMilliseconds;

options.Log?.Invoke(LogLevel.Informational, $"{packetName}: Compilation complete.");
options.Log?.Invoke(LogLevel.Informational, Strings.CompilationComplete(packetName));

if (string.IsNullOrEmpty(outputContents))
{
Expand All @@ -209,7 +216,7 @@ private static ConvertResult CreateFailedCompileResult(string streamName, string
long totalTimeMs = timeInMsLines + timeInMsParse + timeInMsCompile;
options.Log?.Invoke(
LogLevel.Verbose,
$"{packetName}: Lex {timeInMsLines}ms, Parse {timeInMsParse}ms, Compile {timeInMsCompile}ms. Total: {totalTimeMs}ms ");
Strings.TimingLog(packetName, timeInMsLines, timeInMsParse, timeInMsCompile, totalTimeMs));

return new ConvertResult(packetName, new SuccessResult<string>(outputContents));
}
Expand Down
35 changes: 23 additions & 12 deletions YetAnotherPacketParser/YetAnotherPacketParser/Parser/LinesParser.cs
Expand Up @@ -118,6 +118,27 @@ public IResult<PacketNode> Parse(IEnumerable<ILine> lines)
return new SuccessResult<FormattedText>(formattedText);
}

private static string? GetMetadataText(LinesEnumerator lines)
{
if (!TryGetPostQuestionMetadata(lines, out PostQuestionMetadataLine? metadataLine) || metadataLine == null)
{
return null;
}

string metadata = metadataLine.Text.UnformattedText;
if (metadata.Length > 2)
{
int metadataStart = metadata.IndexOf('<');
int metadataEnd = metadata.LastIndexOf('>');
if (metadataStart >= 0 && metadataStart < metadata.Length + 1 && metadataEnd > metadataStart)
{
metadata = metadata.Substring(metadataStart + 1, metadataEnd - 1);
}
}

return metadata;
}

private static string GetFailureMessage(LinesEnumerator lines, string message)
{
StringBuilder snippet = new StringBuilder(10);
Expand Down Expand Up @@ -336,12 +357,7 @@ private static IResult<TossupNode> ParseTossup(LinesEnumerator lines, int tossup
return new FailureResult<TossupNode>(questionResult.ErrorMessages);
}

string? metadata = null;
if (TryGetPostQuestionMetadata(lines, out PostQuestionMetadataLine? metadataLine) && metadataLine != null)
{
metadata = metadataLine.Text.UnformattedText;
}

string? metadata = GetMetadataText(lines);
return new SuccessResult<TossupNode>(new TossupNode(questionNumber, questionResult.Value, metadata));
}

Expand All @@ -367,12 +383,7 @@ private static IResult<BonusNode> ParseBonus(LinesEnumerator lines, int bonusNum
}

// Metadata is always at the end of all of the bonus parts
string? metadata = null;
if (TryGetPostQuestionMetadata(lines, out PostQuestionMetadataLine? metadataLine) && metadataLine != null)
{
metadata = metadataLine.Text.UnformattedText;
}

string? metadata = GetMetadataText(lines);
return new SuccessResult<BonusNode>(new BonusNode(
questionNumber, leadinResult.Value, bonusPartsResult.Value, metadata));
}
Expand Down
28 changes: 28 additions & 0 deletions YetAnotherPacketParser/YetAnotherPacketParser/Strings.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using YetAnotherPacketParser.Lexer;

namespace YetAnotherPacketParser
Expand Down Expand Up @@ -29,6 +30,11 @@ public static string BonusPart(int value, string part)
return $"[{value}] {part}";
}

public static string CompilationComplete(string packetName)
{
return $"{packetName}: Compilation complete.";
}

public static string CouldntFindNextPart(string context, int linesCount)
{
string lineString = linesCount == 1 ? "line" : "lines";
Expand All @@ -45,6 +51,11 @@ public static string InvalidData(string message)
return $"Invalid data: {message}";
}

public static string LexingComplete(string packetName)
{
return $"{packetName}: Lexing complete.";
}

public static string NoBonusQuestionNumberFound(int bonusNumber)
{
return $"Failed to parse bonus #{bonusNumber}. No question number found.";
Expand All @@ -55,6 +66,12 @@ public static string NoMoreLinesFound(string context, int linesChecked)
return $"Failed to parse {context}. No more lines found. Number of lines searched for after the last part: {linesChecked}";
}

public static string NonThreePartBonusesFound(IEnumerable<int> bonusNumbers)
{
string bonusNumbersText = string.Join(", ", bonusNumbers);
return $"Warning: non-three part bonuses found. Bonus number(s): {bonusNumbersText}";
}

public static string NoTossupQuestionNumberFound(int tossupNumber)
{
return $"Failed to parse tossup #{tossupNumber}. No question number found.";
Expand All @@ -71,6 +88,17 @@ public static string ParseFailureMessage(string message, int lineNumber, string
return $"{message} (Line #{lineNumber}{snippetMessage})";
}

public static string ParsingComplete(string packetName, int tossupsCount, int bonusesCount)
{
return $"{packetName}: Parsing complete. {tossupsCount} tossup(s), {bonusesCount} bonus(es).";
}

public static string TimingLog(
string packetName, long timeInMsLines, long timeInMsParse, long timeInMsCompile, long totalTimeMs)
{
return $"{packetName}: Lex {timeInMsLines}ms, Parse {timeInMsParse}ms, Compile {timeInMsCompile}ms. Total: {totalTimeMs}ms";
}

public static string TooManyPacketsToParse(int maximumPackets)
{
return $"Too many documents to parse. This only parses at most {maximumPackets} documents at a time.";
Expand Down
Expand Up @@ -17,9 +17,9 @@
<PackageTags>quizbowl packetparser quizbowlpacketparser</PackageTags>
<PackageProjectUrl>https://github.com/alopezlago/YetAnotherPacketParser</PackageProjectUrl>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<AssemblyVersion>0.4.0.0</AssemblyVersion>
<FileVersion>0.4.0.0</FileVersion>
<Version>0.4.0.0</Version>
<AssemblyVersion>0.4.1.0</AssemblyVersion>
<FileVersion>0.4.1.0</FileVersion>
<Version>0.4.1.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Expand Up @@ -7,9 +7,9 @@
<AssemblyName>YetAnotherPacketParserAzureFunction</AssemblyName>
<Authors>Alejandro Lopez-Lago</Authors>
<Product>YAPP Azure Function</Product>
<AssemblyVersion>0.4.0.0</AssemblyVersion>
<FileVersion>0.4.0.0</FileVersion>
<Version>0.4.0.0</Version>
<AssemblyVersion>0.4.1.0</AssemblyVersion>
<FileVersion>0.4.1.0</FileVersion>
<Version>0.4.1.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.18.0" />
Expand Down
Expand Up @@ -9,9 +9,9 @@
<Copyright>(c) 2020 Alejandro Lopez-Lago</Copyright>
<Description>Yet Another Packet Parser parses quiz bowl packets and translates them to different formats</Description>
<Product>YAPP</Product>
<AssemblyVersion>0.4.0.0</AssemblyVersion>
<FileVersion>0.4.0.0</FileVersion>
<Version>0.4.0.0</Version>
<AssemblyVersion>0.4.1.0</AssemblyVersion>
<FileVersion>0.4.1.0</FileVersion>
<Version>0.4.1.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
Expand Up @@ -547,7 +547,7 @@ private static NumberedQuestionLine CreateQuestionLine(int number, string text)

private static PostQuestionMetadataLine CreatePostQuestionMetadaLine(string metadata)
{
return new PostQuestionMetadataLine(CreateFormattedText(metadata));
return new PostQuestionMetadataLine(CreateFormattedText($"<{metadata}>"));
}
}
}

0 comments on commit 0683fc4

Please sign in to comment.