Started deprecating the old parser in favor of the new object generator.

Added a conversion settings file.
This commit is contained in:
Samuele Lorefice
2023-12-18 19:19:14 +01:00
parent 5e036a6a8b
commit c6285c4f56
7 changed files with 79 additions and 11 deletions

View File

@@ -10,6 +10,7 @@ namespace ComfySharp;
public class ComfyClient {
private HttpClient client;
private List<ExpandoObject> nodes;
private NodeDBGenerator dbGenerator;
public string BaseUrl { get; set; }
@@ -20,6 +21,19 @@ public class ComfyClient {
DefaultRequestHeaders = { { "User-Agent", "ComfySharp" } }
};
nodes= new();
try {
dbGenerator = new(ConversionSettings.FromFile("conv_config.json"));
}
catch (Exception e) {
Console.WriteLine(e);
}
finally {
ConversionSettings settings = new();
settings.Save("conv_config.json");
dbGenerator = new(settings);
Console.WriteLine("created empty settings file");
}
}
public async Task<string[]?> GetEmbeddings() {
@@ -42,14 +56,20 @@ public class ComfyClient {
return null;
}
//public async Task<>GetHistory
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public async Task<List<HistoryEntry>?> GetHistory() {
throw new NotImplementedException();
}
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
public async Task<List<Node>?> GetObjectInfo() {
var req = await client.GetAsync("/object_info");
if (req is { IsSuccessStatusCode: true, Content: not null }) {
var doc = await req.Content.ReadFromJsonAsync<JsonDocument>();
ObjectInfoParser.Parse(doc, out nodes);
//ObjectInfoParser.Parse(doc, out nodes);
}
return null;

View File

@@ -6,4 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Update="conv_config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,29 @@
using System.Text.Json;
namespace ComfySharp;
[Serializable]
public class ConversionSettings {
public List<string> EnumConvertAsString = new();
public List<string> EnumConvertAsBool = new();
public static ConversionSettings FromFile(string path) {
if (!File.Exists(path)) throw new FileNotFoundException("Could not find settings file", path);
string json = File.ReadAllText(path);
ConversionSettings? settings = FromJson(json);
return settings;
}
public static ConversionSettings FromJson(string json) {
ConversionSettings? settings = JsonSerializer.Deserialize<ConversionSettings>(json);
if(settings is null) throw new NullReferenceException("Could not deserialize settings file");
return settings;
}
public static string ToJson(ConversionSettings settings) => JsonSerializer.Serialize(settings);
public void Save(string path) => File.WriteAllText(path, ToJson());
public string ToJson() => JsonSerializer.Serialize(this);
}

View File

@@ -3,11 +3,11 @@ 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();
NodeDBGenerator dbGenerator = new(new ConversionSettings());
dbGenerator.GenerateNodes(document);
nodes = dbGenerator.GetNodes();
dbGenerator.GenerateClasses(document);

View File

@@ -0,0 +1,4 @@
namespace ComfySharp.Types;
public class HistoryEntry {
}

View File

@@ -3,12 +3,14 @@ using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Dynamic;
using System.Net;
using System.Runtime.CompilerServices;
namespace ComfySharp.Types;
public class NodeDBGenerator {
private List<ExpandoObject> nodes;
private ConversionSettings settings;
private List<string> knownTypes = new();
public List<string> GetKnownTypes() => knownTypes;
@@ -20,8 +22,9 @@ public class NodeDBGenerator {
private int typeFields = 0;
private int enumFields = 0;
public NodeDBGenerator() {
public NodeDBGenerator(ConversionSettings settings) {
nodes = new();
this.settings = settings;
}
public void ResetDb() => nodes.Clear();
@@ -34,8 +37,6 @@ public class NodeDBGenerator {
if (!knownTypes.Contains(type)) {
knownTypes.Add(type);
Console.WriteLine("Added new known type: {0}", type);
} else {
Console.WriteLine("Type {0} already known, skipped.", type);
}
typeFields++;
}
@@ -54,7 +55,7 @@ public class NodeDBGenerator {
values.ToList().ForEach(value => {
if (!knownEnums[type].Contains(value)) {
knownEnums[type].Add(value);
Console.WriteLine("Added new value to known enum: {0}", value);
Console.WriteLine("Added new value to already known enum: {0}", value);
}
});
}
@@ -112,12 +113,20 @@ public class NodeDBGenerator {
// if element 0 is a string, this is a type
if (inputProperty.Value[0].ValueKind == JsonValueKind.String)
AddKnownType(inputProperty.Value[0].ToString());
// else, if element 0 is an array, this is an enum
// else, if element 0 is an array, this is an enum or a list.
else if (inputProperty.Value[0].ValueKind == JsonValueKind.Array) {
//if the elements inside the array are not strings, this is a list of objects and might require special handling
if (inputProperty.Value[0].EnumerateArray().Current.ValueKind != JsonValueKind.String) {
Console.WriteLine("Encountered a special case: {0}", inputProperty.Name);
return;
}
// these are all lists of strings and not enums
if (inputProperty.Name == "image" || inputProperty.Name == "ckpt_name" || inputProperty.Name == "lora_name" || inputProperty.Name == "control_net_name"){
}
List<string> enumValues = new();
inputProperty.Value[0].EnumerateArray().ToList().ForEach(value => enumValues.Add(value.ToString()));
AddKnownEnum(inputProperty.Name, enumValues);
}
}

View File