From ec0f8cb6efb2bfe7aa93fe27bc8a7645630a8ef4 Mon Sep 17 00:00:00 2001 From: Samuele Lorefice Date: Sun, 24 Dec 2023 01:11:58 +0100 Subject: [PATCH] Converted all the `Console.WriteLine` to proper logging messages. --- ComfySharp/{Types => }/NodeDBGenerator.cs | 103 +++++++++++----------- ComfySharp/ObjectInfoParser.cs | 45 ---------- 2 files changed, 50 insertions(+), 98 deletions(-) rename ComfySharp/{Types => }/NodeDBGenerator.cs (71%) delete mode 100644 ComfySharp/ObjectInfoParser.cs diff --git a/ComfySharp/Types/NodeDBGenerator.cs b/ComfySharp/NodeDBGenerator.cs similarity index 71% rename from ComfySharp/Types/NodeDBGenerator.cs rename to ComfySharp/NodeDBGenerator.cs index 40fa0b7..f8c655f 100644 --- a/ComfySharp/Types/NodeDBGenerator.cs +++ b/ComfySharp/NodeDBGenerator.cs @@ -1,29 +1,25 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text.Json; +using System.Text.Json; using System.Dynamic; -using System.Net; using System.Runtime.CompilerServices; -namespace ComfySharp.Types; +namespace ComfySharp; public class NodeDBGenerator { - private List nodes; - private ConversionSettings settings; - - private List knownTypes = new(); + readonly private List nodes; + readonly private ConversionSettings settings; + + readonly private List knownTypes = new(); public List GetKnownTypes() => knownTypes; - - private Dictionary> knownEnums = new(); + + readonly private Dictionary> knownEnums = new(); public Dictionary> GetKnownEnums() => knownEnums; - - private Dictionary> knownStringLists = new(); + + readonly private Dictionary> knownStringLists = new(); public Dictionary> GetKnownStringLists() => knownStringLists; public int Count => nodes.Count; - private int typeFields = 0; - private int enumFields = 0; + private int typeFields; + private int enumFields; public NodeDBGenerator(ConversionSettings settings) { nodes = new(); @@ -39,7 +35,9 @@ public class NodeDBGenerator { private void AddKnownType(string type) { if (!knownTypes.Contains(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++; } @@ -52,12 +50,15 @@ public class NodeDBGenerator { private void AddKnownEnum(string type, ICollection values) { if (!knownEnums.ContainsKey(type)) { 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 { values.ToList().ForEach(value => { if (!knownEnums[type].Contains(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 values) { if (!knownStringLists.ContainsKey(type)) { 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 { values.ToList().ForEach(value => { if (!knownStringLists[type].Contains(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) { foreach (var node in document.RootElement.EnumerateObject()) ScanNode(node); - - Console.WriteLine("List of recognized Types:"); - foreach (var knownType in knownTypes) { - Console.WriteLine(knownType); - } - Console.WriteLine("\nTotal amount of types iterated: {0}\n", typeFields); - Console.WriteLine("List of recognized Enums:"); + + string types = ""; + foreach (var knownType in knownTypes) + types += $"\t{knownType}"; + Logger.Info($"List of recognized Types:\n{types}"); + Logger.Info($"Total amount of types iterated: {typeFields}\n"); + + string enums = ""; foreach (var knownEnum in knownEnums) { - Console.WriteLine(knownEnum.Key); - foreach (var value in knownEnum.Value) { - Console.Write("\t{0}", value); - } - Console.WriteLine(); + enums += $"{knownEnum.Key}\n"; + foreach (var value in knownEnum.Value) + enums += $"\t{value}"; + enums += "\n"; } - Console.WriteLine("\nTotal amount of enums iterated: {0}\n", enumFields); + Logger.Info($"List of recognized Enums: {enums}"); + Logger.Info($"Total amount of enums iterated: {enumFields}\n"); } /// /// executed for a single node, progresses through the properties of the node /// private void ScanNode(JsonProperty node) { -#if DEBUG - Console.WriteLine("Scanning node: {0}", node.Name); -#endif + Logger.Debug($"Scanning node: {node.Name}"); // if this node is blacklisted, skip it if (settings.BlacklistedNodes.Contains(node.Name)) { - Console.WriteLine("Skipping blacklisted node: {0}", node.Name); + Logger.Debug($"Skipping blacklisted node: {node.Name}"); return; } @@ -129,9 +131,7 @@ public class NodeDBGenerator { /// Executed on the input property inside a node /// private void ScanInputs(JsonProperty input) { -#if DEBUG - Console.WriteLine("Scanning inputs of node: {0}", input.Name); -#endif + Logger.Debug($"Scanning inputs of node: {input.Name}"); foreach (var inputType in input.Value.EnumerateObject()) { //these are related to the nodes themselves and useless for us if (inputType.Name == "hidden") continue; @@ -145,9 +145,7 @@ public class NodeDBGenerator { /// Executed for each of the elements inside a required or optional input /// private void ScanInputField(JsonProperty inputProperty) { -#if DEBUG - Console.WriteLine("Scanning input field: {0}", inputProperty.Name); -#endif + Logger.Debug($"Scanning input field: {inputProperty.Name}"); // if element 0 is a string, this is a type if (inputProperty.Value[0].ValueKind == JsonValueKind.String) { @@ -163,8 +161,8 @@ public class NodeDBGenerator { 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 (firstElement[0].ValueKind != JsonValueKind.String) { - Console.WriteLine("Encountered a special case: {0}", inputProperty.Name); - Console.WriteLine("First element of array is not a string, but a {0}", firstElement[0].ValueKind); + Logger.Info($"Encountered a special case: {inputProperty.Name}" + + $"\nFirst element of array is not a string, but a {firstElement[0].ValueKind}"); return; } @@ -180,9 +178,9 @@ public class NodeDBGenerator { } } else { //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); - Console.WriteLine("First element of array is empty"); - Console.WriteLine("Adding to known types as empty list..."); + Logger.Info($"Encountered a special case: {inputProperty.Name}" + + "\nFirst element of array is empty"+ + "\nAdding to known types as empty list..."); AddKnownStringList(inputProperty.Name, new List()); } } @@ -193,9 +191,9 @@ public class NodeDBGenerator { [MethodImpl(MethodImplOptions.AggressiveInlining)] public void GenerateNode(JsonElement data) { 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()) { - 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); } nodes.Add(node); @@ -230,5 +228,4 @@ public class NodeDBGenerator { public List GetNodes() => nodes; - } diff --git a/ComfySharp/ObjectInfoParser.cs b/ComfySharp/ObjectInfoParser.cs deleted file mode 100644 index 2577733..0000000 --- a/ComfySharp/ObjectInfoParser.cs +++ /dev/null @@ -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 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 ParseOutputNames(JsonElement getProperty) { - List outputNames = new(); - foreach (var output in getProperty.EnumerateArray()) { - outputNames.Add(output.GetProperty("name").GetString() ?? ""); - } - return outputNames; - } - static private List ParseOutputIsList(JsonElement getProperty) { - throw new NotImplementedException(); - } - static private List ParseOutputs(JsonElement getProperty) { - throw new NotImplementedException(); - } - static private Input ParseInput(JsonElement getProperty) { - throw new NotImplementedException(); - } -} \ No newline at end of file