-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigParserFactory.cs
More file actions
129 lines (110 loc) · 4.3 KB
/
ConfigParserFactory.cs
File metadata and controls
129 lines (110 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using ConfigMerger.Parsers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace ConfigMerger
{
public static class ConfigParserFactory
{
public static IConfigParser GetParser(string filePath)
{
var extension = Path.GetExtension(filePath).ToLowerInvariant();
return extension switch
{
".py" => new PythonConfigParser(),
".yaml" or ".yml" => new YamlConfigParser(),
".json" => new JsonConfigParser(),
".xml" => new XmlConfigParser(),
_ => throw new NotSupportedException($"Формат файла {extension} не поддерживается")
};
}
public static bool IsSupported(string filePath)
{
var extension = Path.GetExtension(filePath).ToLowerInvariant();
return extension is ".py" or ".yaml" or ".yml" or ".json" or ".xml";
}
public static string GetSupportedFormats()
{
return "Python (*.py)|*.py|YAML (*.yaml;*.yml)|*.yaml;*.yml|JSON (*.json)|*.json|XML (*.xml)|*.xml|All supported|*.py;*.yaml;*.yml;*.json;*.xml";
}
}
public class PythonConfigParser : IConfigParser
{
public Dictionary<string, string> ParseConfig(string content)
{
var config = new Dictionary<string, string>();
var lines = content.Split('\n');
for (int i = 0; i < lines.Length; i++)
{
var line = lines[i].Trim();
if (line.StartsWith("#") || string.IsNullOrEmpty(line)) continue;
var match = Regex.Match(line, @"^([A-Z_][A-Z0-9_]*)\s*=\s*(.*)$");
if (match.Success)
{
var key = match.Groups[1].Value;
var value = match.Groups[2].Value.Trim();
// Обработка многострочных блоков
if (value.Contains("{") && !value.Contains("}"))
{
var fullValue = value;
var j = i + 1;
var braceCount = value.Count(c => c == '{');
while (j < lines.Length && braceCount > 0)
{
var nextLine = lines[j].Trim();
fullValue += "\n" + nextLine;
braceCount += nextLine.Count(c => c == '{');
braceCount -= nextLine.Count(c => c == '}');
j++;
}
value = fullValue;
i = j - 1;
}
config[key] = value;
}
}
return config;
}
public string GenerateMergedConfig(Dictionary<string, string> merged, string originalContent)
{
var lines = originalContent.Split('\n').ToList();
var result = new List<string>();
var processedKeys = new HashSet<string>();
foreach (var line in lines)
{
var trimmedLine = line.Trim();
if (trimmedLine.StartsWith("#") || string.IsNullOrEmpty(trimmedLine))
{
result.Add(line);
continue;
}
var match = Regex.Match(trimmedLine, @"^([A-Z_][A-Z0-9_]*)\s*=\s*(.*)$");
if (match.Success)
{
var key = match.Groups[1].Value;
if (merged.ContainsKey(key))
{
result.Add($"{key} = {merged[key]}");
processedKeys.Add(key);
}
else
{
result.Add(line);
}
}
else
{
result.Add(line);
}
}
// Добавляем новые ключи
foreach (var kvp in merged.Where(x => !processedKeys.Contains(x.Key)))
{
result.Add($"{kvp.Key} = {kvp.Value}");
}
return string.Join("\n", result);
}
}
}