-
Notifications
You must be signed in to change notification settings - Fork 1
/
PyFileEditor.cs
342 lines (315 loc) · 8.16 KB
/
PyFileEditor.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Creato da SharpDevelop.
* Utente: michele
* Data: 02/03/2009
* Ora: 10.21
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Specialized;
namespace textops
{
public enum PyFileSection
{
PY_HEADER_SECTION = 0,
PY_INIT_SECTION = 1,
PY_EVENTDECL_SECTION = 2,
PY_EVENTIMPL_SECTION = 3,
PY_APP_SECTION = 4,
//
PYBASE_INIT_SECTION = 5,
PYBASE_PROPS_SECTION = 6,
PYBASE_LAYOUT_SECTION = 7
}
/*
* 0 : # --- begin [Sec1] section --- B
* 1 : # --- end [Sec1] section --- E
* 2 : # --- begin [Sec2] section --- B
* (3) : data 1
* 3 : # --- begin [Sec2] section --- B
*
* B = 0, S = 0, E = B + S + 1
* Insert PS = B + 1 + Pos, If (Pos = -1) PS = E;
*/
public class PySection
{
private int _begin;
private int _size;
private PyFileSection _sec;
public PySection()
{
_begin = 0;
_size = 0;
}
public int Begin
{
get { return _begin; }
set { _begin = value; }
}
public int Size
{
get { return _size; }
set { _size = value; }
}
public int End
{
get { return _begin + _size + 1; }
}
public PyFileSection Section
{
get { return _sec; }
set { _sec = value; }
}
public void MoveBeginIndex(int count)
{
_begin += count;
}
public void MoveSizeIndex(int count)
{
_size += count;
if (_size < 0) _size = 0;
}
}
public class PyFileEditor
{
private string _pyname;
private string _pybasename;
private StringCollection _py_mem;
// pyfile.py
private const int _sec_count = 8;
public PySection[] _py_sections;
public PyFileEditor()
{
_py_mem = new StringCollection();
_py_sections = new PySection[_sec_count];
for (int i=0; i<_sec_count; i++)
{
_py_sections[i] = new PySection();
_py_sections[i].Section = (PyFileSection)i;
}
// SetDefaultIndexes();
CreateEmpty();
}
public string PyFileName
{
get { return _pyname; }
set { _pyname = value; }
}
public string PyBaseFileName
{
get { return _pybasename; }
set { _pybasename = value; }
}
public StringCollection Lines
{
get { return _py_mem; }
}
private int SeekToSection(PyFileSection section, int pos)
{
/*
* B = 0, S = 0, E = B + S + 1
* Insert PS = B + 1 + Pos, If (Pos = -1) PS = E;
*/
int B = _py_sections[(int)section].Begin;
int E = _py_sections[(int)section].End;
int ps = B + pos + 1;
if (pos == -1) ps = E;
if (ps < 0) ps = B + 1;
if (ps > E) ps = E;
return ps;
}
public void InsertSingleLine(int pos, PyFileSection section, string str)
{
// pos : relative to section, -1 mean end of section
int row = SeekToSection(section, pos);
if (row >= _py_mem.Count)
{
_py_mem.Add(str);
} else {
_py_mem.Insert(row, str);
}
MoveSectionSize(section, 1);
}
public void InsertLines(int pos, PyFileSection section, string str)
{
// pos : relative to section, -1 mean end of section
string[] arr = Regex.Split(str, "\r\n");
int count = arr.Length;
int row = SeekToSection(section, pos);
for (int i=0; i<count; i++)
{
if (row + i >= _py_mem.Count)
{
_py_mem.Add(arr[i]);
} else {
_py_mem.Insert(row + i, arr[i]);
}
}
MoveSectionSize(section, count);
}
public void DeleteLine(PyFileSection section, int pos, int rowcount)
{
// MemoryStream temp = new MemoryStream();
int row = SeekToSection(section, pos);
for (int i=0; i<rowcount; i++)
{
_py_mem.RemoveAt(row + i);
}
MoveSectionSize(section, -rowcount);
}
public void ReplaceEntireLine(int pos, PyFileSection section, string str)
{
// pos : relative to section, -1 mean end of section
int row = SeekToSection(section, pos);
if (row < _py_mem.Count)
{
_py_mem[row] = str;
}
}
public void InsertBeforeSection(PyFileSection section, string str)
{
// pos : relative to section, -1 mean end of section
int row = SeekToSection(section, 0);
if (row - 1 < 0) row = 0; else row--;
_py_mem.Insert(row, str);
MoveIndexes(section, 1);
}
public void InsertAfterSection(PyFileSection section, string str)
{
// pos : relative to section, -1 mean end of section
int row = SeekToSection(section, -1);
row++;
if (row >= _py_mem.Count)
{
_py_mem.Add(str);
} else {
_py_mem.Insert(row, str);
}
MoveIndexes((PyFileSection)(section+1), 1);
}
public void MoveIndexes(PyFileSection startFrom, int count)
{
for (int i = (int)startFrom; i<_sec_count; i++)
{
_py_sections[i].MoveBeginIndex(count);
}
}
public void MoveSectionSize(PyFileSection startFrom, int count)
{
_py_sections[(int)startFrom].MoveSizeIndex(count);
for (int i = (int)startFrom + 1; i<_sec_count; i++)
{
_py_sections[i].MoveBeginIndex(count);
}
}
public void ParsePyFile(string pyfilename, string pybasefilename)
{
_py_mem.Clear();
StreamReader read = new StreamReader(pyfilename);
while (!read.EndOfStream)
{
_py_mem.Add(read.ReadLine());
}
read.Close();
read = new StreamReader(pybasefilename);
while (!read.EndOfStream)
{
_py_mem.Add(read.ReadLine());
}
read.Close();
// Create Indexes
CreateIndexes();
}
private void CreateIndexes()
{
int count = 0;
int cursize = 0;
foreach (string item in _py_mem)
{
switch (item) {
case "# --- begin Header section ---\n" :
_py_sections[0].Begin = count;
cursize = 0;
break;
case "# --- end Header section ---\n" :
_py_sections[0].Size = cursize;
break;
case "# --- begin Init section ---\n" :
_py_sections[1].Begin = count;
cursize = 0;
break;
case "# --- end Init section ---\n" :
_py_sections[1].Size = cursize;
break;
case "# --- begin EventDecl section ---\n" :
_py_sections[2].Begin = count;
cursize = 0;
break;
case "# --- end EventDecl section ---\n" :
_py_sections[2].Size = cursize;
break;
case "# --- begin EventImpl section ---\n" :
_py_sections[3].Begin = count;
cursize = 0;
break;
case "# --- end EventImpl section ---\n" :
_py_sections[3].Size = cursize;
break;
case "# --- begin App section ---\n" :
_py_sections[4].Begin = count;
cursize = 0;
break;
case "# --- end App section ---\n" :
_py_sections[4].Size = cursize;
break;
}
count++;
cursize++;
}
}
public void CreateEmpty()
{
// Header
_py_sections[0].Begin = 1;
_py_mem.Add("#!/usr/bin/env python\n");
_py_mem.Add("# --- begin Header section ---\n");
_py_mem.Add("# --- end Header section ---\n");
_py_mem.Add("\n");
_py_sections[1].Begin = _py_mem.Count;
_py_mem.Add("# --- begin Init section ---\n");
_py_mem.Add("# --- end Init section ---\n");
_py_mem.Add("\n");
_py_sections[2].Begin = _py_mem.Count;
_py_mem.Add("# --- begin EventDecl section ---\n");
_py_mem.Add("# --- end EventDecl section ---\n");
_py_mem.Add("\n");
_py_sections[3].Begin = _py_mem.Count;
_py_mem.Add("# --- begin EventImpl section ---\n");
_py_mem.Add("# --- end EventImpl section ---\n");
_py_mem.Add("\n");
_py_sections[4].Begin = _py_mem.Count;
_py_mem.Add("# --- begin App section ---\n");
_py_mem.Add("# --- end App section ---\n");
_py_sections[5].Begin = _py_mem.Count;
_py_mem.Add("# --- begin InitBase section ---\n");
_py_mem.Add("# --- end InitBase section ---\n");
_py_mem.Add("\n");
_py_sections[6].Begin = _py_mem.Count;
_py_mem.Add("# --- begin Props section ---\n");
_py_mem.Add("# --- end Props section ---\n");
_py_mem.Add("\n");
_py_sections[7].Begin = _py_mem.Count;
_py_mem.Add("# --- begin Layout section ---\n");
_py_mem.Add("# --- end Layout section ---\n");
InsertSingleLine(-1, PyFileSection.PY_HEADER_SECTION, "# generated by mkdb\n");
InsertSingleLine(-1, PyFileSection.PY_HEADER_SECTION, "\n");
InsertSingleLine(-1, PyFileSection.PY_HEADER_SECTION, "import wx\n");
InsertSingleLine(-1, PyFileSection.PY_HEADER_SECTION, "\n");
}
}
}