Require Complete

IDE0060
This commit is contained in:
2024-11-20 10:34:18 -07:00
parent 8ae8386b30
commit 66f5acc6fa
9 changed files with 146 additions and 127 deletions

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;
@ -88,7 +87,7 @@ public class ProcessData : IProcessData
List<object> Shared.Properties.IProcessData.Details => _Details;
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, string ghostPCLFileName, string pdfTextStripperFileName)
public ProcessData(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, ReadOnlyCollection<string> lines)
{
fileInfoCollection.Clear();
_Details = new List<object>();
@ -98,7 +97,7 @@ public class ProcessData : IProcessData
Date = GetDateTime(logistics);
MesEntity = logistics.MesEntity;
_Log = LogManager.GetLogger(typeof(ProcessData));
Parse(fileRead, logistics, fileInfoCollection, ghostPCLFileName, pdfTextStripperFileName);
Parse(fileRead, logistics, fileInfoCollection, lines);
}
private static DateTime GetDateTime(Logistics logistics) =>
@ -129,27 +128,6 @@ public class ProcessData : IProcessData
return results;
}
/// <summary>
/// Convert the raw data file to parsable file format - in this case from PCL to PDF
/// </summary>
/// <param name="sourceFile">source file to be converted to PDF</param>
/// <returns></returns>
private static string ConvertSourceFileToPdf(Logistics logistics, string ghostPCLFileName)
{
string result = Path.ChangeExtension(logistics.ReportFullPath, ".pdf");
if (!File.Exists(result))
{
//string arguments = string.Concat("-i \"", sourceFile, "\" -o \"", result, "\"");
string arguments = string.Concat("-dSAFER -dBATCH -dNOPAUSE -dFIXEDMEDIA -dFitPage -dAutoRotatePages=/All -dDEVICEWIDTHPOINTS=792 -dDEVICEHEIGHTPOINTS=612 -sOutputFile=\"", result, "\" -sDEVICE=pdfwrite \"", logistics.ReportFullPath, "\"");
//Process process = Process.Start(configData.LincPDFCFileName, arguments);
Process process = Process.Start(ghostPCLFileName, arguments);
_ = process.WaitForExit(30000);
if (!File.Exists(result))
throw new Exception("PDF file wasn't created");
}
return result;
}
private void ScanPast(string text)
{
int num = _Data.IndexOf(text, _I);
@ -213,24 +191,6 @@ public class ProcessData : IProcessData
return text.Trim();
}
private static string GetTextFromPDF(string pdfTextStripperFileName, string sourceFileNamePdf, string altHeaderFileName)
{
string result;
ProcessStartInfo processStartInfo = new(pdfTextStripperFileName, $"s \"{sourceFileNamePdf}\"")
{
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
};
Process process = Process.Start(processStartInfo);
_ = process.WaitForExit(30000);
if (!File.Exists(altHeaderFileName))
result = string.Empty;
else
result = File.ReadAllText(altHeaderFileName);
return result;
}
private static (string, string) GetReactorAndRDS(string defaultReactor, string defaultRDS, string text, string formattedText, string[] segments)
{
string rds;
@ -533,57 +493,11 @@ public class ProcessData : IProcessData
#nullable enable
private Complete? GetComplete(string altHeaderFileName)
private void Parse(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, ReadOnlyCollection<string> lines)
{
Complete? result;
Constant constant = new();
string[] lines = File.ReadAllLines(altHeaderFileName);
ReadOnlyCollection<string> collection = new(lines);
if (collection.Count > constant.Take)
result = Complete.Get(constant, collection);
else
{
result = null;
_Log.Warn($"File {altHeaderFileName} has less than {constant.Take} collection");
}
return result;
}
#pragma warning disable IDE0060
private void Parse(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection, string ghostPCLFileName, string pdfTextStripperFileName)
#pragma warning restore IDE0060
{
string headerText;
string sourceFileNamePdf = ConvertSourceFileToPdf(logistics, ghostPCLFileName);
fileInfoCollection.Add(new FileInfo(sourceFileNamePdf));
string altHeaderFileName = Path.ChangeExtension(logistics.ReportFullPath, ".txt");
if (File.Exists(altHeaderFileName))
{
headerText = File.ReadAllText(altHeaderFileName);
fileInfoCollection.Add(new FileInfo(altHeaderFileName));
}
else
{
try
{
//Pdfbox, IKVM.AWT.WinForms
org.apache.pdfbox.pdmodel.PDDocument pdfDocument = org.apache.pdfbox.pdmodel.PDDocument.load(sourceFileNamePdf);
org.apache.pdfbox.util.PDFTextStripper stripper = new();
headerText = stripper.getText(pdfDocument);
pdfDocument.close();
File.AppendAllText(altHeaderFileName, headerText);
fileInfoCollection.Add(new FileInfo(altHeaderFileName));
}
catch (Exception)
{
if (!File.Exists(pdfTextStripperFileName))
throw;
headerText = GetTextFromPDF(pdfTextStripperFileName, sourceFileNamePdf, altHeaderFileName);
if (string.IsNullOrEmpty(headerText))
throw;
fileInfoCollection.Add(new FileInfo(altHeaderFileName));
}
}
if (fileRead is null)
throw new ArgumentNullException(nameof(fileRead));
string headerText = string.Join(Environment.NewLine, lines);
if (headerText.Contains("G A T E V O L T A G E"))
throw new Exception("Ignore: GATEVOLTAGE runs are not parsed.");
if (!string.IsNullOrEmpty(headerText))
@ -692,23 +606,6 @@ public class ProcessData : IProcessData
detail.UniqueId = string.Concat(detail, detail.UniqueId);
}
fileInfoCollection.Add(logistics.FileInfo);
try
{
Complete? complete = GetComplete(altHeaderFileName);
if (complete is null)
_Log.Warn($"Could not get Complete from {altHeaderFileName}");
else
{
FileInfo fileInfo = new($"{altHeaderFileName}.json");
string json = JsonSerializer.Serialize(complete, CompleteSourceGenerationContext.Default.Complete);
File.WriteAllText(fileInfo.FullName, json);
fileInfoCollection.Add(fileInfo);
}
}
catch (Exception ex)
{
_Log.Error($"Error in {nameof(GetComplete)}", ex);
}
}
internal static List<Description> GetDescriptions(JsonElement[] jsonElements)