/
PythonClass.cs
231 lines (204 loc) · 7.37 KB
/
PythonClass.cs
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using Motio.CSharp2Py.Doc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Linq;
namespace Motio.CSharp2Py
{
public class PythonClass
{
Type className;
List<Type> parentClasses = new List<Type>();
PythonConstructor ctr;
List<PythonEvent> events = new List<PythonEvent>();
List<PythonField> fields = new List<PythonField>();
List<PythonStaticField> staticFields = new List<PythonStaticField>();
List<PythonMethod> methods = new List<PythonMethod>();
List<PythonStaticMethod> staticMethods = new List<PythonStaticMethod>();
public DocParser xmlDoc;
public string Namespace => className.Namespace;
public string Name => Utils.CleanupNamePython(className);
public PythonClass(Type type, XDocument xmlDoc = null)
{
if(xmlDoc != null)
this.xmlDoc = new DocParser(xmlDoc);
className = type;
ctr = new PythonConstructor(this);
if (type.BaseType != null)
parentClasses.Add(type.BaseType);
parentClasses.AddRange(type.GetInterfaces());
foreach (MemberInfo member in type.GetMembers(BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly))
{
Add(member, true);
}
foreach (MemberInfo member in type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))
{
Add(member, false);
}
}
public void Set(PythonConstructor constructor)
{
ctr = constructor;
}
public void Add(MemberInfo member, bool isStatic = false)
{
if (member is FieldInfo || member is PropertyInfo)
{
if (member is FieldInfo field)
{
if (isStatic)
staticFields.Add(new PythonStaticField(this, field));
else
fields.Add(new PythonField(this, field));
}
if (member is PropertyInfo prop)
{
if (isStatic)
staticFields.Add(new PythonStaticField(this, prop));
else
fields.Add(new PythonField(this, prop));
}
}
else if (member is MethodInfo method)
{
if (!method.Name.StartsWith("set_")
&& !method.Name.StartsWith("get_")
&& !method.Name.StartsWith("add_")
&& !method.Name.StartsWith("remove_"))
{
if (isStatic)
staticMethods.Add(new PythonStaticMethod(this, method));
else
methods.Add(new PythonMethod(this, method));
}
}
else if (member is ConstructorInfo ctr)
{
this.ctr = new PythonConstructor(this, ctr);
}
else if (member is EventInfo ev)
{
events.Add(new PythonEvent(this, ev));
}
else if (member is Type)
{
//TODO add child classes
Console.WriteLine("child class found " + member);
}
else
{
throw new Exception("unsupported member " + member);
}
}
public string StringRepr()
{
PythonFileBuilder builder = new PythonFileBuilder();
//this creates an import line above each python class,
//there might be duplicate import line after merging all the classes
//in a single file but since it'll never never anyway
Imports(builder);
builder.Append($"class {Utils.CleanupNamePython(className)}({ParentClasses()}):");
builder.AddIndent();
builder.LineBreak();
StaticFields(builder);
StaticMethods(builder);
Constructor(builder);
Methods(builder);
return builder.ToString();
}
public void Imports(PythonFileBuilder builder)
{
Dictionary<string, HashSet<string>> namespaces = new Dictionary<string, HashSet<string>>();
foreach (Type type in parentClasses)
{
string ns = type.Namespace;
string name = Utils.CleanupNamePython(type);
HashSet<string> inNs;
if (!namespaces.TryGetValue(ns, out inNs))
{
inNs = new HashSet<string>();
namespaces.Add(ns, inNs);
}
inNs.Add(name);
}
foreach (var pair in namespaces)
{
builder.Append("from ");
builder.Append(pair.Key);
builder.Append(" import ");
builder.Append(String.Join(", ", pair.Value));
builder.LineBreak();
}
}
//public static string CleanupName(Type type)
//{
// string name = type.Name;
// if (name.Contains("&"))
// name = name.Replace("&", "");
// if (!name.Contains("`"))
// return name;
// string[] split = name.Split(new char[] { '`' }, 2);
// name = split[0] + "[" + String.Join(", ", type.GetGenericArguments().Select(p => CleanupName(p)).ToArray()) + "]";
// return name;
//}
public string ParentClasses()
{
return String.Join(", ", parentClasses.Select(p => Utils.CleanupNamePython(p)));
}
public void StaticFields(PythonFileBuilder builder)
{
foreach (PythonStaticField field in staticFields)
{
field.StringRepr(builder);
builder.LineBreak();
}
if(staticFields.Count != 0)
builder.LineBreak();
}
public void StaticMethods(PythonFileBuilder builder)
{
foreach (PythonStaticMethod method in staticMethods)
{
method.StringRepr(builder);
builder.LineBreak();
}
if(staticMethods.Count != 0)
builder.LineBreak();
}
public void Constructor(PythonFileBuilder builder)
{
ctr.StringRepr(builder);
builder.AddIndent();
builder.LineBreak();
if (fields.Count == 0 && events.Count == 0)
{
builder.Append("pass");
builder.RemoveIndent();
builder.LineBreak();
return;
}
foreach (PythonField field in fields)
{
field.StringRepr(builder);
builder.LineBreak();
}
foreach (PythonEvent ev in events)
{
ev.StringRepr(builder);
builder.LineBreak();
}
builder.RemoveIndent();
builder.LineBreak();
}
public void Methods(PythonFileBuilder builder)
{
foreach (PythonMethod method in methods)
{
method.StringRepr(builder);
builder.LineBreak();
}
}
}
}