-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPyCodeTree.cs
More file actions
233 lines (220 loc) · 6.34 KB
/
PyCodeTree.cs
File metadata and controls
233 lines (220 loc) · 6.34 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
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
232
233
/*
* Creato da SharpDevelop.
* Utente: Miki
* Data: 09/03/2009
* Ora: 21.17
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Reflection;
using System.IO;
using System.Text.RegularExpressions;
namespace pyprova
{
public class PyCodeTree
{
protected XmlDocument _doc;
protected string _name;
protected XmlNodeList _classes;
protected int curclass;
public PyCodeTree()
{
_doc = new XmlDocument();
Stream str = Assembly.GetExecutingAssembly().GetManifestResourceStream("pyprova.wxclass.xsd");
_doc.Schemas.Add("wxclass", new XmlTextReader(str));
_name = "";
curclass = -1;
}
public static StringCollection ToStringCollection(string text)
{
string[] sep = {"\\n"};
string[] strs = text.Split(sep, StringSplitOptions.None);
StringCollection coll = new StringCollection();
coll.AddRange(strs);
return coll;
}
public static string ToText(StringCollection coll)
{
string res = "";
foreach (string s in coll)
{
res += s + "\\n";
}
return res;
}
public StringCollection GetFunctionBody(string classname, string funcname)
{
XmlElement root = _doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/pythonfile/class[@Name='" + classname + "']" +
"/function[@Name='" + funcname + "']/body");
StringCollection c = null;
if (nodes.Count > 0)
{
c = new StringCollection();
foreach (XmlNode n in nodes)
{
c.Add(n.InnerText);
}
}
return c;
}
public bool CreateNewFunction(string classname, string funcname, string parameters)
{
XmlElement root = _doc.DocumentElement;
XmlNode node = root.SelectSingleNode("/pythonfile/class[@Name='" + classname + "']");
if (node == null)
return false;
XmlElement func = _doc.CreateElement("function");
XmlAttribute funcname = _doc.CreateAttribute("Name");
XmlAttribute funcparams = _doc.CreateAttribute("Parameters");
funcname.InnerText = funcname;
if (parameters != "")
funcparams.InnerText = parameters;
else
funcparams.InnerText = "";
func.Attributes.Append(funcname);
func.Attributes.Append(funcparams);
func.InnerText = "";
node.AppendChild(func);
return true;
}
public void ParseXmlFile(string filename)
{
// Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
_doc.RemoveAll();
_doc.Load(filename);
XmlNode node = _doc.GetElementsByTagName("pythonfile")[0];
foreach (XmlNode n in node.ChildNodes)
{
}
_classes = _doc.GetElementsByTagName("class");
}
// ParseClass(cl, clbegin, clend, filelines);
private int ParseClass(XmlNode classnode, int begin, int end, StringCollection coll)
{
StringCollection c = new StringCollection();
string defexp = @"^[\t\s]*def\s*(?<defname>\w*)\((?<params>.*)\)";
for (int i=begin; i<end; i++)
{
string item = coll[i];
Match m = Regex.Match(item, defexp);
if (m.Success)
{
// this is a function...
// Create <function> element
XmlElement func = _doc.CreateElement("function");
XmlAttribute funcname = _doc.CreateAttribute("Name");
XmlAttribute funcparams = _doc.CreateAttribute("Parameters");
funcname.InnerText = m.Groups["defname"].Value;
if (m.Groups["params"] != null)
funcparams.InnerText = m.Groups["params"].Value;
else
funcparams.InnerText = "";
func.Attributes.Append(funcname);
func.Attributes.Append(funcparams);
// find the end of this class.
int funcbegin = i+1;
int funcend = funcbegin;
while (funcend <= end)
{
if (coll[funcend].Contains("# end " + funcname.InnerText))
{
break;
}
// str += coll[funcend] + "\\n";
XmlElement body = _doc.CreateElement("body");
if (coll[funcend] == "")
body.InnerText = "\\n";
else
body.InnerText = coll[funcend].Trim();
func.AppendChild(body);
funcend++;
}
classnode.AppendChild(func);
i = funcend;
} else {
// Create base element <pythonfile>
XmlElement dt = _doc.CreateElement("classtext");
if (item == "")
dt.InnerText = "\\n";
else
dt.InnerText = item.Trim();
classnode.AppendChild(dt);
}
}
return end;
}
public void ParsePythonFile(string pyfilename)
{
// Try to parse a .py file and create the XML struct
_doc.RemoveAll();
// Declaration
XmlDeclaration decl = _doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
_doc.AppendChild(decl);
// Create base element <pythonfile>
XmlElement pfile = _doc.CreateElement("pythonfile");
// Read the file...
StreamReader read = File.OpenText(pyfilename);
if (read == null)
{
// exit
return;
}
StringCollection filelines = new StringCollection();
while (!read.EndOfStream)
{
string item = read.ReadLine();
filelines.Add(item);
}
read.Close();
string classexp = @"^[\t\s]*class\s*(?<classname>\w*)\(";
for (int i=0; i<filelines.Count; i++)
{
string item = filelines[i];
Match m = Regex.Match(item, classexp);
if (m.Success)
{
// this is a class...
// Create <class> element
XmlElement cl = _doc.CreateElement("class");
XmlAttribute clname = _doc.CreateAttribute("Name");
clname.InnerText = m.Groups["classname"].Value;
cl.Attributes.Append(clname);
// find the end of this class.
int clbegin = i+1;
int clend = -1;
for (int j=clbegin; j<filelines.Count; j++)
{
if (filelines[j].Contains("# end " + m.Groups["classname"].Value))
{
clend = j;
break;
}
}
i = ParseClass(cl, clbegin, clend, filelines);
// Parse sub-nodes
pfile.AppendChild(cl);
} else {
XmlElement dt = _doc.CreateElement("doctext");
if (item == "")
dt.InnerText = "\\n";
else
dt.InnerText = item.Trim();
pfile.AppendChild(dt);
}
}
_doc.AppendChild(pfile);
XmlTextWriter xmlWriter = new XmlTextWriter("../../at.xml", System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
_doc.WriteContentTo(xmlWriter);
xmlWriter.Flush();
xmlWriter.Close();
}
}
}