Attachment 'MySQL.py'
Download 1 """
2 MoinMoin - Parser for MySQL data
3
4 @copyright: 2005 by Alexander Fischer <mail@fischer-alexander.net>
5 @license: GNU GPL, see http://www.gnu.org/licenses/gpl for details
6
7
8 Take care that a MySQL based database for source is assumed.
9 For additional information performing take parser without any data given.
10
11 """
12
13 from MoinMoin import wikiutil, config
14 import MySQLdb
15 import re
16
17 class Parser:
18 def __init__(self, raw, request, **kw):
19 self.raw = raw
20 self.request = request
21
22 def format(self, formatter):
23
24 lines = self.raw.splitlines()
25
26 result=[self.request.formatter.paragraph(0)]
27 cr=self.request.formatter.linebreak(0)
28
29 #### initialize flags ###################################################################
30 dbflag=0
31 qflag=0
32 hdflag=0
33 wlflag=0
34 hlflag=0
35 mflag=0
36 hflag=0
37
38 ##### section1 ###########################################################################
39
40 if lines[0:]:
41 parameterlist=[]
42 for index in range(len(lines[0:])):
43 parameterlist.append(lines[0:][index])
44 i=0
45 for parameterstring in parameterlist :
46 regexdb=re.compile("DBDATA")
47 matchdb=regexdb.search(parameterstring)
48 if matchdb:
49 dbflag=1
50 parameterdb=''''''
51 for char in range(matchdb.span(0)[1],len(parameterlist[i])):
52 if parameterlist[i][char]!=' ':
53 parameterdb=parameterdb+parameterlist[i][char]
54 parameterdb=parameterdb.split(',')
55
56
57 regexq=re.compile("QUERY")
58 matchq=regexq.search(parameterstring)
59 if matchq:
60 qflag=1
61 parameterq=''''''
62 for char in range(matchq.span(0)[1],len(parameterlist[i])):
63 parameterq=parameterq+parameterlist[i][char]
64
65
66 regexhd=re.compile("HEADLINE")
67 matchhd=regexhd.search(parameterstring)
68 if matchhd:
69 hdflag=1
70 parameterhd=''''''
71 for char in range(matchhd.span(0)[1],len(parameterlist[i])):
72 if parameterlist[i][char]!=' ':
73 parameterhd=parameterhd+parameterlist[i][char]
74 parameterhd=parameterhd.split(',')
75
76
77 regexwl=re.compile("WIKICOLUMN")
78 matchwl=regexwl.search(parameterstring)
79 if matchwl:
80 wlflag=1
81 parameterwl=''''''
82 for char in range(matchwl.span(0)[1],len(parameterlist[i])):
83 if parameterlist[i][char]!=' ':
84 parameterwl=parameterwl+parameterlist[i][char]
85 parameterwl=parameterwl.split(',')
86
87
88 regexhl=re.compile("HTMLCOLUMN")
89 matchhl=regexhl.search(parameterstring)
90 if matchhl:
91 hlflag=1
92 parameterhl=''''''
93 for char in range(matchhl.span(0)[1],len(parameterlist[i])):
94 if parameterlist[i][char]!=' ':
95 parameterhl=parameterhl+parameterlist[i][char]
96 parameterhl=parameterhl.split(',')
97
98
99 regexm=re.compile("MAILCOLUMN")
100 matchm=regexm.search(parameterstring)
101 if matchm:
102 mflag=1
103 parameterm=''''''
104 for char in range(matchm.span(0)[1],len(parameterlist[i])):
105 if parameterlist[i][char]!=' ':
106 parameterm=parameterm+parameterlist[i][char]
107 parameterm=parameterm.split(',')
108
109
110 regexh=re.compile("HIDDENCOLUMN")
111 matchh=regexh.search(parameterstring)
112 if matchh:
113 hflag=1
114 parameterh=''''''
115 for char in range(matchh.span(0)[1],len(parameterlist[i])):
116 if parameterlist[i][char]!=' ':
117 parameterh=parameterh+parameterlist[i][char]
118 parameterh=parameterh.split(',')
119 i=i+1
120
121 ##### sectiont 2 #######################################################################
122
123 output=[]
124 output.append(formatter.table(1))
125
126 if dbflag==1 and qflag==1:
127 if hdflag==1:
128 output.append(formatter.table_row(1))
129 for element in parameterhd:
130 output.append(formatter.table_cell(1))
131 output.append(formatter.strong(1))
132 output.append(formatter.text(element))
133 output.append(formatter.strong(0))
134 output.append(formatter.table_cell(0))
135 output.append(formatter.table_row(0))
136
137 h=parameterdb[0]
138 d=parameterdb[1]
139 u=parameterdb[2]
140 p=parameterdb[3]
141
142 query=parameterq
143
144 con=MySQLdb.connect(host=h,user=u,db=d,passwd=p)
145 c=con.cursor()
146 c.execute(parameterq)
147 list=c.fetchall()
148
149 for row in list:
150 output.append(formatter.table_row(1))
151 i=0
152 html=0
153 hidden=0
154 for cell in row:
155 i=i+1
156 element=str(cell)
157 if hlflag==1:
158 for value in parameterhl:
159 if i==int(value):
160 html=1
161 element='<a class=\"external\" href=\"'+str(cell)+'\"><img src=\"/wiki/rightsidebar/img/moin-www.png\" alt=\"[www]\" width=\"11\" height=\"11\">'+str(cell)+'</a>'
162
163 if wlflag==1:
164 for value in parameterwl:
165 if i==int(value):
166 html=1
167 element='<a href=\"'+str(cell)+'\">'+str(cell)+'</a>'
168
169 if mflag==1:
170 for value in parameterm:
171 if i==int(value):
172 html=1
173 element='<a href="mailto:%s">%s</a>'%(str(cell),str(cell))
174
175 if hflag==1:
176 for value in parameterh:
177 if i==int(value):
178 hidden=1
179
180 if html==1:
181 output.append(formatter.table_cell(1))
182 output.append(formatter.rawHTML(element))
183 output.append(formatter.table_cell(0))
184 html=0
185 elif hidden==1:
186 pass
187 hidden=0
188 else:
189 output.append(formatter.table_cell(1))
190 output.append(formatter.text(element))
191 output.append(formatter.table_cell(0))
192 output.append(formatter.table_row(0))
193 output.append(formatter.table(0))
194 result.extend(output)
195 con.close()
196
197
198 if dbflag==0 or not lines[1:] or qflag==0:
199
200 comment1=''''''
201 comment1="Transfer connectiondata and query for correct output!"
202 comment2=''''''
203 comment2="{{{#!MySQL"
204 comment3=''''''
205 comment3="DBDATA host,database,[user],[password]"
206 comment4=''''''
207 comment4="QUERY dbquery"
208 comment5=''''''
209 comment5="[HEADLINE columnname1,columnname2,...,...]"
210 comment6=''''''
211 comment6="[HTMLCOLUMN columnnumber1,columnnumber2,...,...]"
212 comment7=''''''
213 comment7="[WIKICOLUMN columnnumber1,columnnumber2,...,...]"
214 comment8=''''''
215 comment8="[MAILCOLUMN columnnumber1,columnnumber2,...,...]"
216 comment9=''''''
217 comment9="[HIDDENCOLUMN columnnumber1,columnnumber2,...,...]"
218 comment10=''''''
219 comment10="}}}"
220 comment11=''''''
221 comment11="Replace host, database, user, password, dbquery, columname, columnnumber with practical data!"
222 comment12=''''''
223 comment12="<b>Take care for lower and upper case. Squared bracket = Optional data, Large letters = Syntax</b>"
224
225 comment=[]
226 comment=[cr,comment1,cr,cr,comment2,cr,comment3,cr,comment4,cr,comment5,cr,comment6,cr,comment7,cr,comment8,cr,comment9,cr,comment10,cr,cr,comment11,cr,cr,comment12]
227
228 result.extend(comment)
229 self.request.write(''.join(result))
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.