-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotate.py
More file actions
71 lines (66 loc) · 2.57 KB
/
annotate.py
File metadata and controls
71 lines (66 loc) · 2.57 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
DFM_SQL_MARK = 'SQL.Strings'
def _whitespaceSize(srcline):
for kw in ('UpdateSQL', 'RefreshSQL', 'InsertSQL', 'DeleteSQL', 'ModifySQL', 'SelectSQL'):
if kw in srcline:
return srcline.index(kw) + 2
return srcline.index(DFM_SQL_MARK) + 2
def _annotateDfmFile(sourcefile, destfile):
annotations = 0
srclines = None
dstlines = list()
with sourcefile.open() as srcf:
srclines = srcf.readlines()
for idx in range(len(srclines)):
srcline = srclines[idx]
dstlines.append(srcline)
if DFM_SQL_MARK in srcline:
annotation = '\'/*__SUPSQL__/{}/{}*/\''.format(sourcefile.name, idx + 2)
whitespace = str(' '*_whitespaceSize(srcline))
dstlines.append('{}{}\n'.format(whitespace, annotation))
annotations += 1
with destfile.open(mode='w') as dstf:
dstf.writelines(dstlines)
return annotations
def _annotatePasFile(sourcefile, destfile):
annotations = 0
srclines = None
dstlines = list()
with sourcefile.open() as srcf:
srclines = srcf.readlines()
firstStatementInserted = False
for idx in range(len(srclines)):
srcline = srclines[idx]
for mark in ('SQL.Add(', 'SQL.Text :=', 'GetSqlValues(', 'GetSQLValues(', 'getSQLValues(', 'ExecSQL(', 'SQLValuesToList(', 'GetID(', 'getId(', 'GetId('):
if mark in srcline:
annotation = '\'/*__SUPSQL__/{}/{}*/ \''.format(sourcefile.name, idx + 1)
dstline = ''
if mark == 'SQL.Add(' and firstStatementInserted:
dstline = srcline
elif mark == 'ExecSQL(' and 'ExecSQL()' in srcline:
dstline = srcline
elif mark.endswith('='):
dstline = srcline.replace(mark, '{} {} + '.format(mark, annotation))
else:
dstline = srcline.replace(mark, '{}{} + '.format(mark, annotation))
dstlines.append(dstline)
if mark == 'SQL.Add(':
firstStatementInserted = True
if dstline != srcline:
annotations += 1
break
else:
dstlines.append(srcline)
firstStatementInserted = False
with destfile.open(mode='w') as dstf:
dstf.writelines(dstlines)
return annotations
_annotatedict = {
'*.pas' : _annotatePasFile,
'*.dfm' : _annotateDfmFile,
}
def annotateFunc(filemask):
try:
result = _annotatedict[filemask]
return result
except KeyError:
return None