Converted all the Console.WriteLine to proper logging messages.

This commit is contained in:
Samuele Lorefice
2023-12-24 01:11:58 +01:00
parent b013917f54
commit ec0f8cb6ef
2 changed files with 50 additions and 98 deletions

View File

@@ -1,29 +1,25 @@
using System; using System.Text.Json;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Dynamic; using System.Dynamic;
using System.Net;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace ComfySharp.Types; namespace ComfySharp;
public class NodeDBGenerator { public class NodeDBGenerator {
private List<ExpandoObject> nodes; readonly private List<ExpandoObject> nodes;
private ConversionSettings settings; readonly private ConversionSettings settings;
private List<string> knownTypes = new(); readonly private List<string> knownTypes = new();
public List<string> GetKnownTypes() => knownTypes; public List<string> GetKnownTypes() => knownTypes;
private Dictionary<string, List<string>> knownEnums = new(); readonly private Dictionary<string, List<string>> knownEnums = new();
public Dictionary<string, List<string>> GetKnownEnums() => knownEnums; public Dictionary<string, List<string>> GetKnownEnums() => knownEnums;
private Dictionary<string, List<string>> knownStringLists = new(); readonly private Dictionary<string, List<string>> knownStringLists = new();
public Dictionary<string, List<string>> GetKnownStringLists() => knownStringLists; public Dictionary<string, List<string>> GetKnownStringLists() => knownStringLists;
public int Count => nodes.Count; public int Count => nodes.Count;
private int typeFields = 0; private int typeFields;
private int enumFields = 0; private int enumFields;
public NodeDBGenerator(ConversionSettings settings) { public NodeDBGenerator(ConversionSettings settings) {
nodes = new(); nodes = new();
@@ -39,7 +35,9 @@ public class NodeDBGenerator {
private void AddKnownType(string type) { private void AddKnownType(string type) {
if (!knownTypes.Contains(type)) { if (!knownTypes.Contains(type)) {
knownTypes.Add(type); knownTypes.Add(type);
Console.WriteLine("Added new known type: {0}", type); Logger.Info($"Added new known type: {type}");
} else {
Logger.Trace($"Skipped already known type: {type}");
} }
typeFields++; typeFields++;
} }
@@ -52,12 +50,15 @@ public class NodeDBGenerator {
private void AddKnownEnum(string type, ICollection<string> values) { private void AddKnownEnum(string type, ICollection<string> values) {
if (!knownEnums.ContainsKey(type)) { if (!knownEnums.ContainsKey(type)) {
knownEnums.Add(type, values.ToList()); knownEnums.Add(type, values.ToList());
Console.WriteLine("Added new known enum: {0}", type); Logger.Info($"Added new known enum: {type}");
Logger.Trace($"\tAdded values: {string.Join(", ", values)}");
} else { } else {
values.ToList().ForEach(value => { values.ToList().ForEach(value => {
if (!knownEnums[type].Contains(value)) { if (!knownEnums[type].Contains(value)) {
knownEnums[type].Add(value); knownEnums[type].Add(value);
Console.WriteLine("Added new value to already known enum: {0}", value); Logger.Info($"Added new value to already known enum: {value}");
} else {
Logger.Trace($"Skipped already known enum value: {type}.{value}");
} }
}); });
} }
@@ -72,48 +73,49 @@ public class NodeDBGenerator {
private void AddKnownStringList(string type, ICollection<string> values) { private void AddKnownStringList(string type, ICollection<string> values) {
if (!knownStringLists.ContainsKey(type)) { if (!knownStringLists.ContainsKey(type)) {
knownStringLists.Add(type, values.ToList()); knownStringLists.Add(type, values.ToList());
Console.WriteLine("Added new known stringList: {0}", type); Logger.Info($"Added new known stringList: {type}");
Logger.Trace($"\tAdded values: {string.Join(", ", values)}");
} else { } else {
values.ToList().ForEach(value => { values.ToList().ForEach(value => {
if (!knownStringLists[type].Contains(value)) { if (!knownStringLists[type].Contains(value)) {
knownStringLists[type].Add(value); knownStringLists[type].Add(value);
Console.WriteLine("Added new value to already known stringList: {0}", value); Logger.Info($"Added new value to already known stringList: {type}");
} else {
Logger.Trace($"Skipped already known stringList value: {type}.{value}");
} }
}); });
} }
} }
public void GenerateClasses(JsonDocument document) { public void GenerateClasses(JsonDocument document) {
foreach (var node in document.RootElement.EnumerateObject()) foreach (var node in document.RootElement.EnumerateObject())
ScanNode(node); ScanNode(node);
Console.WriteLine("List of recognized Types:"); string types = "";
foreach (var knownType in knownTypes) { foreach (var knownType in knownTypes)
Console.WriteLine(knownType); types += $"\t{knownType}";
} Logger.Info($"List of recognized Types:\n{types}");
Console.WriteLine("\nTotal amount of types iterated: {0}\n", typeFields); Logger.Info($"Total amount of types iterated: {typeFields}\n");
Console.WriteLine("List of recognized Enums:");
string enums = "";
foreach (var knownEnum in knownEnums) { foreach (var knownEnum in knownEnums) {
Console.WriteLine(knownEnum.Key); enums += $"{knownEnum.Key}\n";
foreach (var value in knownEnum.Value) { foreach (var value in knownEnum.Value)
Console.Write("\t{0}", value); enums += $"\t{value}";
enums += "\n";
} }
Console.WriteLine(); Logger.Info($"List of recognized Enums: {enums}");
} Logger.Info($"Total amount of enums iterated: {enumFields}\n");
Console.WriteLine("\nTotal amount of enums iterated: {0}\n", enumFields);
} }
/// <summary> /// <summary>
/// executed for a single node, progresses through the properties of the node /// executed for a single node, progresses through the properties of the node
/// </summary> /// </summary>
private void ScanNode(JsonProperty node) { private void ScanNode(JsonProperty node) {
#if DEBUG Logger.Debug($"Scanning node: {node.Name}");
Console.WriteLine("Scanning node: {0}", node.Name);
#endif
// if this node is blacklisted, skip it // if this node is blacklisted, skip it
if (settings.BlacklistedNodes.Contains(node.Name)) { if (settings.BlacklistedNodes.Contains(node.Name)) {
Console.WriteLine("Skipping blacklisted node: {0}", node.Name); Logger.Debug($"Skipping blacklisted node: {node.Name}");
return; return;
} }
@@ -129,9 +131,7 @@ public class NodeDBGenerator {
/// Executed on the input property inside a node /// Executed on the input property inside a node
/// </summary> /// </summary>
private void ScanInputs(JsonProperty input) { private void ScanInputs(JsonProperty input) {
#if DEBUG Logger.Debug($"Scanning inputs of node: {input.Name}");
Console.WriteLine("Scanning inputs of node: {0}", input.Name);
#endif
foreach (var inputType in input.Value.EnumerateObject()) { foreach (var inputType in input.Value.EnumerateObject()) {
//these are related to the nodes themselves and useless for us //these are related to the nodes themselves and useless for us
if (inputType.Name == "hidden") continue; if (inputType.Name == "hidden") continue;
@@ -145,9 +145,7 @@ public class NodeDBGenerator {
/// Executed for each of the elements inside a required or optional input /// Executed for each of the elements inside a required or optional input
/// </summary> /// </summary>
private void ScanInputField(JsonProperty inputProperty) { private void ScanInputField(JsonProperty inputProperty) {
#if DEBUG Logger.Debug($"Scanning input field: {inputProperty.Name}");
Console.WriteLine("Scanning input field: {0}", inputProperty.Name);
#endif
// if element 0 is a string, this is a type // if element 0 is a string, this is a type
if (inputProperty.Value[0].ValueKind == JsonValueKind.String) { if (inputProperty.Value[0].ValueKind == JsonValueKind.String) {
@@ -163,8 +161,8 @@ public class NodeDBGenerator {
if (firstElement.GetArrayLength() > 0) { if (firstElement.GetArrayLength() > 0) {
//if the elements inside the array are not strings, this is a list of objects and might require special handling //if the elements inside the array are not strings, this is a list of objects and might require special handling
if (firstElement[0].ValueKind != JsonValueKind.String) { if (firstElement[0].ValueKind != JsonValueKind.String) {
Console.WriteLine("Encountered a special case: {0}", inputProperty.Name); Logger.Info($"Encountered a special case: {inputProperty.Name}" +
Console.WriteLine("First element of array is not a string, but a {0}", firstElement[0].ValueKind); $"\nFirst element of array is not a string, but a {firstElement[0].ValueKind}");
return; return;
} }
@@ -180,9 +178,9 @@ public class NodeDBGenerator {
} }
} else { } else {
//if the array is empty, this is a list of unknown and might require special handling later on, for now, skip it //if the array is empty, this is a list of unknown and might require special handling later on, for now, skip it
Console.WriteLine("Encountered a special case: {0}", inputProperty.Name); Logger.Info($"Encountered a special case: {inputProperty.Name}" +
Console.WriteLine("First element of array is empty"); "\nFirst element of array is empty"+
Console.WriteLine("Adding to known types as empty list..."); "\nAdding to known types as empty list...");
AddKnownStringList(inputProperty.Name, new List<string>()); AddKnownStringList(inputProperty.Name, new List<string>());
} }
} }
@@ -193,9 +191,9 @@ public class NodeDBGenerator {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void GenerateNode(JsonElement data) { public void GenerateNode(JsonElement data) {
var node = new ExpandoObject(); var node = new ExpandoObject();
Console.WriteLine("Generating node: {0}", data.GetProperty("name").GetString() ?? ""); Logger.Info($"Generating node: {data.GetProperty("name").GetString() ?? ""}" );
foreach (var property in data.EnumerateObject()) { foreach (var property in data.EnumerateObject()) {
Console.WriteLine("Adding new property: {0}\nType: {2}\nValue: {1}\n", property.Name, property.Value, property.Value.GetType()); Logger.Info($"Adding new property: {property.Name}\nType: {property.Value}\nValue: {property.Value.GetType()}\n");
node.TryAdd(property.Name, property.Value); node.TryAdd(property.Name, property.Value);
} }
nodes.Add(node); nodes.Add(node);
@@ -230,5 +228,4 @@ public class NodeDBGenerator {
public List<ExpandoObject> GetNodes() => nodes; public List<ExpandoObject> GetNodes() => nodes;
} }

View File

@@ -1,45 +0,0 @@
using System.Dynamic;
using System.Text.Json;
using ComfySharp.Types;
namespace ComfySharp;
[Obsolete("This class is obsolete, use NodeDBGenerator instead",false)]
public static class ObjectInfoParser {
public static void Parse(JsonDocument document, out List<ExpandoObject> nodes) {
NodeDBGenerator dbGenerator = new(new ConversionSettings());
dbGenerator.GenerateNodes(document);
nodes = dbGenerator.GetNodes();
dbGenerator.GenerateClasses(document);
}
private static void ParseNode(JsonElement node, out Node n) {
n = new();
n.Name = node.GetProperty("name").GetString() ?? "";
n.Input = ParseInput(node.GetProperty("input"));
n.Outputs = ParseOutputs(node.GetProperty("output"));
n.OutputIsList = ParseOutputIsList(node.GetProperty("output"));
n.OutputNames = ParseOutputNames(node.GetProperty("output"));
n.DisplayName = node.GetProperty("display_name").GetString() ?? "";
n.Description = node.GetProperty("description").GetString() ?? "";
n.Category = node.GetProperty("category").GetString() ?? "";
n.IsOutputNode = node.GetProperty("output_node").GetBoolean();
}
static private List<string> ParseOutputNames(JsonElement getProperty) {
List<string> outputNames = new();
foreach (var output in getProperty.EnumerateArray()) {
outputNames.Add(output.GetProperty("name").GetString() ?? "");
}
return outputNames;
}
static private List<bool> ParseOutputIsList(JsonElement getProperty) {
throw new NotImplementedException();
}
static private List<PrimitiveType> ParseOutputs(JsonElement getProperty) {
throw new NotImplementedException();
}
static private Input ParseInput(JsonElement getProperty) {
throw new NotImplementedException();
}
}