-
Notifications
You must be signed in to change notification settings - Fork 1
/
InstDB.py
492 lines (399 loc) · 14.2 KB
/
InstDB.py
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/usr/bin/python
#
# InstDB.py - Utility functions for web output and database
#
# PatchesDB
# Copyright (C) 2005-2007 Josh Green <jgreen@users.sourceforge.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA or point your web browser to http://www.gnu.org.
import string
import MySQLdb
from math import *
from cgi import escape
from config import *
RatingRadio = ( ("0","None"), "1", "2", "3", "4", "5" )
LoginError = ""
def connect ():
global DB, Cur
DB = MySQLdb.connect (host=dbHost, db=dbName, user=dbUser, passwd=dbPass)
Cur = DB.cursor (MySQLdb.cursors.DictCursor)
def header(title=None, minimal=False):
global LoginError
if title: s = " - " + title
else: s = ""
retstr = """\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Resonance Instrument DB%s</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="keywords" content="instruments midi music sounds SoundFont DLS GigaSampler">
<meta name="description" content="Database for sharing instrument sound files for computer music composition.">
<link REL="StyleSheet" HREF="images/style.css" TYPE="text/css">
<script type="text/javascript">
var RecaptchaOptions = {
theme : 'blackglass'
};
</script>
</head>
<body bgcolor="#000000">
""" % s
if not minimal:
retstr += """\
<center><img src="images/logo.jpg" width=420 height=143 alt="Resonance Instrument Database logo"></center>
<table border=0 cellpadding=4 cellspacing=4 width="100%">
<tr>
<td align="left" valign="top" width="160">
"""
box = html_box ()
box.tableattr = 'width="100%"'
retstr += box.start ("Menu") + """\
<table border=0 cellpadding=2 cellspacing=0 width="100%">
<tr><td>
<a href="patches.py">Home</a><br>
<a href="forums/">Forums</a><br>
<a href="patches.py?Action=software">Software/Credits</a><br>
</td></tr>
<tr bgcolor="#000066"><td align="center">
<b>Instruments</b>
</td></tr>
<tr><td>
<a href="patches.py?Action=browse">Browse by Category</a><br>
<a href="patches.py?Action=list&OrderBy=Date">New Files</a><br>
<a href="patches.py?Action=list&OrderBy=FileNameLink">List by FileName</a><br>
<a href="patches.py?Action=md5find">Find by MD5</a>
</td></tr>
<tr bgcolor="#000066"><td align="center">
<b>Documentation</b>
</td></tr>
<tr><td>
<a href="patches.py?Action=help&Topic=submit">Content Submission</a><br>
<a href="patches.py?Action=help&Topic=privacy">Privacy Policy</a><br>
</td></tr>
<tr bgcolor="#000066"><td align="center">
<b>Search</b>
</td></tr>
<tr><td align="center">
<form action="patches.py" method=GET>
<input type=hidden name=Action value="search">
<input style="font-size: 10px;" type=text name=Text size=16 maxlength=64>
</form>
<p>
</td></tr>
<tr bgcolor="#000066"><td align="center">
"""
if User:
retstr += "<b>Hello '%s'" % User['Login'] + """</b>
</td></tr>
<tr><td>
<a href="patches.py?Action=profile">Edit Profile</a><br>
<a href="patches.py?Action=submit">Submit Content</a><br>
"""
if "Admin" in User["Flags"]:
retstr += """
<a href="patches.py?Action=emailnews">Email News</a><br>
"""
retstr += """
<a href="patches.py?Action=logout">Logout</a><br>
</td></tr>
"""
else:
retstr += """\
<b>Login</b>
</td></tr>
<tr><td align="center">
<form action="" method=POST>
<input style=\"font-size: 10px;\" type=text name=Username size=16 maxlength=24><br>
Password<br>
<input style=\"font-size: 10px;\" type=password name=Pass size=16 maxlength=24>
<p>
<input type=submit value=\"Login\"> <a href=\"patches.py?Action=register\">Register</a>
<p>
<a href="patches.py?Action=resetpass">Forgot password?</a><br>
</form>
</td></tr>
"""
if LoginError:
retstr += '<tr><td><font class=err>%s</font></td></tr>\n' % LoginError
retstr += "</table>" + box.end () + '</td>\n<td valign="top">\n'
# END - if not minimal
return retstr
def footer (minimal=False):
if not minimal: return "</td>\n</tr>\n</table>\n</body>\n</html>\n"
else: return "</body>\n</html>\n"
class html_box:
def __init__ (self):
self.ttlpadding = 2
self.boxpadding = 4
self.tableattr = None
self.titlehtml = ""
self.brdrcolor = "#004b9b"
self.bgcolor = "#00001e"
def start (self, title):
if self.bgcolor: bgcolor = ' bgcolor="%s"' % self.bgcolor
else: bgcolor = ""
if self.tableattr: tableattr = ' ' + self.tableattr
else: tableattr = ""
return """\
<table border=0 cellpadding=%d cellspacing=0 bgcolor="%s"%s>
<tr bgcolor="%s">
<td align="left" style="background-image:url('images/storbg.gif');">
<font class="boxtitle">%s</font>%s
</td>
</tr>
<tr><td>
<table width="100%%" border=0 cellpadding=%d cellspacing=0>
<tr%s><td>
""" % (self.ttlpadding, self.brdrcolor, tableattr, self.brdrcolor,
escape (title), self.titlehtml, self.boxpadding, bgcolor)
def end (self):
return """\
</td></tr>
</table>
</td></tr>
</table>
"""
class tabular:
"Tabular HTML output class"
def __init__ (self, titles=['']):
self.titles = titles
self.rowalt = 0
self.colattr = []
self.cellspacing = 0
self.cellpadding = 2
self.tableattr = ""
self.ttlrowattr = "bgcolor=\"#110088\""
self.bgcolor1 = "#000000"
self.bgcolor2 = "#111111"
# private
self.displayed_titles = False
def addrow (self, values):
if not self.displayed_titles: s = self.showtitles ()
else: s = ""
if len (values) != len (self.titles):
raise ValueError, "Input values don't match title count"
s += "<tr bgcolor=\"%s\">\n" % (self.bgcolor1,
self.bgcolor2)[self.rowalt]
self.rowalt ^= 1
i = 0
for val in values:
if len (self.colattr) > i and self.colattr[i]:
attr = " " + self.colattr[i]
else: attr = ""
s += "<td%s>%s</td>\n" % (attr, val)
i += 1
s += "</tr>\n"
return s
def showtitles (self):
self.displayed_titles = True
if self.ttlrowattr: ttlattr = " " + self.ttlrowattr
else: ttlattr = ""
if self.tableattr: tabattr = " " + self.tableattr
else: tabattr = ""
s = '<table border=0 cellpadding="%s" cellspacing="%s"%s>\n' \
'<tr%s>\n' % (self.cellpadding, self.cellspacing, tabattr, ttlattr)
i = 0
for title in self.titles:
if len (self.colattr) > i and self.colattr[i]:
attr = " " + self.colattr[i]
else: attr = ""
s += '<td%s><b>%s</b></td>\n' % (attr, title)
i += 1
return s + '</tr>\n'
def end (self):
s = ""
if not self.displayed_titles: s += self.showtitles ()
return s + '</table>\n'
class tabsplits:
"Tabular HTML splits (Name/Value pairs)"
def __init__ (self, rows):
self.rows = rows # sequence of sequences for each cell
self.cellspacing = 2
self.cellpadding = 2
self.tableattr = "" # extra HTML attributes for <table>
self.rowattr = [] # sequence of HTML attributes for rows
self.cellattr = [] # sequence of HTML attributes for cells
self.keybgcolor = "#110088" # "Key" background color
self.valbgcolor = "#110044" # "Value" background color
self.skeytag = "<b>"
self.ekeytag = "</b>"
self.svaltag = ""
self.evaltag = ""
def render (self):
if self.tableattr: self.tableattr = " " + self.tableattr
else: self.tableattr = ""
s = '<table border=0 cellpadding="%s" cellspacing="%s"%s>\n' % (
self.cellpadding, self.cellspacing, self.tableattr)
# Find the maximum number of columns in all rows
maxcol = 0
for row in self.rows:
if len (row) > maxcol: maxcol = len (row)
rowindex = 0
totalcellindex = 0
for row in self.rows: # loop over rows
# Get row HTML attribute if any
if rowindex < len (self.rowattr) and self.rowattr[rowindex]:
rattr = " " + self.rowattr[rowindex]
else: rattr = ""
s += '<tr%s>\n' % rattr
cellalt = 0
cellindex = 0
cellcount = len (row)
for cell in row: # loop over cells in each row
# Add a colspan attribute if less than max row cells
if cellindex == cellcount - 1 and cellcount < maxcol:
cattr = ' colspan="%d"' % (maxcol - cellcount + 1)
else: cattr = ""
# Get cell HTML attribute if any
if totalcellindex < len (self.cellattr) \
and self.cellattr[totalcellindex]:
cattr += " " + self.cellattr[totalcellindex]
s += '<td bgcolor="%s"%s>%s%s%s</td>\n' \
% ((self.keybgcolor, self.valbgcolor)[cellalt], cattr,
(self.skeytag, self.svaltag)[cellalt], cell,
(self.ekeytag, self.evaltag)[cellalt])
cellindex += 1
totalcellindex += 1
cellalt ^= 1
s += '</tr>\n'
rowindex += 1
s += '</table>\n'
return s
def cells_to_fields (cells):
"Convert field cells (array of array of strings) to fields (array of strings)"
fields = []
for cell in cells:
for field in cell:
fields += (field,)
return fields
def delete_cells (cells, fields):
"Remove fields from a cell list (list of lists)"
newcells = []
for cell in cells:
newcell = []
for field in cell:
if not field in fields:
newcell += (field,)
if newcell:
newcells += (newcell,)
return newcells
def form_radio (name, values, selected=None):
"""Outputs HTML form radio buttons
name: HTML name to use for radio buttons
values: Tuple of value strings (also label) or value:label tuples
selected: 'value' for the currently selected radio button
"""
s = ""
for btn in values:
if type (btn) == str:
value = btn
label = btn
else:
value = btn[0]
label = btn[1]
if value == selected:
checked = " checked"
else: checked = ""
s += "<input type=radio name=\"%s\" value=\"%s\"%s>%s\n" \
% (escape (name), escape (value), checked, escape (label))
return s
def pretty_size (size):
if size == 0: return "0"
m = int (floor (log (size) / log (1024)))
div = 1024 ** m
s = ('b', 'k', 'M', 'G')[m]
if m >= 2: prec = 2
else: prec = m
return "%.*f%s" % (prec, size / float (div), s)
def show_pager (req, urlstr, offset, pagesize, total, maxpages=20):
pages = (total + pagesize - 1) / pagesize
if pages <= 1: return # Don't display pager if only 1 page
curpage = offset / pagesize
req.write ("""\
<table border=0 cellpadding=0 cellspacing=0 bgcolor=\"#110044\" width=\"100%\">
<tr><td>
<center>
<table border=0 cellpadding=0 cellspacing=2>
<tr>
""")
link = "<td>\n<a href=\"patches.py?%s&Offset=%d\">\n" \
"<b><font size=\"+1\">%s</font></b>\n</a>\n</td>\n"
txt = '<td><b><font size="+1">%s</font></b></td>\n'
# '|<' First page link
if offset != 0: req.write (link % (urlstr, 0, "|<"))
else: req.write (txt % "|<")
# Previous link
if offset > 0: req.write (link % (urlstr,max (0, offset - pagesize),"<"))
else: req.write (txt % "<")
# Numbered pages
start = curpage - curpage % maxpages
end = min (start + maxpages, pages)
if start > 0: req.write (txt % "...")
for p in range (curpage - curpage % maxpages, end):
if p != curpage: req.write (link % (urlstr, p * pagesize, p + 1))
else: req.write (txt % (p + 1))
if end < pages:
req.write (txt % "...")
req.write (link % (urlstr, (pages - 1) * pagesize, pages))
# Next link
if offset + pagesize < total:
req.write (link % (urlstr, offset + pagesize, ">"))
else: req.write (txt % ">")
# '|<' Last page link
if pages > 0 and offset != (pages - 1) * pagesize:
req.write (link % (urlstr, (pages - 1) * pagesize, ">|"))
else: req.write (txt % ">|")
req.write ("""\
</tr>
</table>
</center>
</tr>
</table>
""")
def show_status (req, str):
"""Display an HTML status message"""
req.write ("<center>\n<b>%s</b>\n</center>\n<hr>\n<p>\n" % str)
def sqlescape (str):
"""Escape a string for use in an SQL statement"""
s = string.replace (str, "'", "\\'")
return string.replace (str, "\"", "\\\"")
def emailmunge (str):
return string.replace (str, "@", " <at> ")
def IncFile (filename):
global IncludePath
f = open (IncludePath + "/" + filename)
return f.read ()
def AssertLogin (req, msg="Please login to access this page"):
global User
if not User:
req.write ('<font class="error">%s</font>\n' % msg)
return False
else: return True
def error(req, msg):
"""Display an HTML error"""
req.write ('<font class="error">%s</font>\n' % escape (msg))
class Error(Exception):
"""Base class for exceptions in this module."""
pass
class ImportError(Error):
"""Exception raised on patch file import errors.
Attributes:
message -- explanation of the error
"""
def __init__(self, message):
self.message = message
def __str__(self):
return (repr (self.message))