jqueryFileTree connector script for python cgi

If you use jquery and haven’t messed with the jqueryFileTree plugin, I’d highly recommend you do so. It’s pretty neat, and there are connector scripts already written in various languages for use with this plugin. However, there wasn’t one for python cgi (there was one for django however). So, I wrote my own! Try it out:

#!/usr/bin/python
# jqueryFileTree connector script for python cgi 
# Version: 1.0 / 10 August 2010
# Author: Charles Hamilton / musashi@nefaria.com
# Released under the GNU GPLv3
# Modifications and improvements are welcome

import os, cgi, cgitb, urllib, re
cgitb.enable()
form = cgi.FieldStorage()

print 'Content-Type: text/html\n\n'
print '<ul class="jqueryFileTree" style="display: none;">'

path = urllib.unquote(form['dir'].value)

dirs = []
files = []

filelist = sorted(os.listdir(path))

for object in filelist:
    if os.path.isfile(path + '/' + object):
       ext = os.path.splitext(object)
        files.append('<li class="file ext_' + re.sub('\.', '', ext[1]) + '"><a href="#" rel="'+ path + object + '">'+ object + '</a></li>')
    elif os.path.isdir(path + '/' + object):
        dirs.append('<li class="directory collapsed"><a href="#" rel="'+ path + object +'/">' + object + '</a></li>')

for d in dirs:
    print d

for f in files:
    print f
print '</ul>'

Python + Reportlab: example #1

So I’ve been using reportlab lately and I have to say, it’s pretty neat. This post will (hopefully) be the first of many to follow. It’s just a simple example that shows how to take input from a web form and insert it into a PDF. First, the code:

 1 #!/usr/bin/python
 2
 3 import cgi, sys
 4
 5 form = cgi.FieldStorage()
 6
 7 if not "namein form:
 8     print """Content-Type: text/html\n\n
 9 <html>
10     <head>
11         <title>Reportlab Example</title>
12     </head>
13     <body>
14         <form action="index2.cgi" method="post" enctype="multipart/form-data">
15             <fieldset>
16                 <legend>Personal Info:</legend>
17                 Name: <input type="text" name="name"><br />
18                 Photo: <input type="file" name="photo"><br />
19                 <input type="submit">
20             </fieldset>
21         </form>
22     </body>
23 </head>"""
24
25 else:
26
27     from reportlab.platypus import *
28     from reportlab.lib.styles import getSampleStyleSheet
29     from reportlab.lib.units import inch
30     from reportlab.lib import colors
31     doc = SimpleDocTemplate(sys.stdout)
32     styles = getSampleStyleSheet()
33     content = []
34     
35     if form[‘photo‘].filename:
36         image = Image(form[‘photo‘].file)
37         image.drawHeight = 2*inch*image.drawHeight / image.drawWidth
38         image.drawWidth = 2*inch
39
40     if form[‘name‘].value:
41         text1 = Paragraph(form[‘name‘].value, styles[‘Heading1‘])
42         text2 = Paragraph(form[‘name‘].value, styles[‘Heading2‘])
43         text3 = Paragraph(form[‘name‘].value, styles[‘Heading3‘])
44
45     content.append(text1)
46     content.append(text2)
47     content.append(text3)
48     content.append(image)
49     print "Content-Type: application/pdf"
50     print "Content-Disposition: attachment; filename=example.pdf\n\n"
51     doc.build(content)

Now the explanation:

Lines #1 – #5 handle specifying the interpreter, importing modules, and initializing the “FieldStorage” dictionary (as the variable ‘form’).

 1 #!/usr/bin/python
 2
 3 import cgi, sys
 4
 5 form = cgi.FieldStorage()

Line #7 tests whether the form has been submitted by checking to see if the ‘name’ field has been filled out. There’s many better ways to test for form submission, but for the purposes of our example, this will work just fine.

 7 if not "namein form:

Lines #8 – #23 print the form

 8     print """Content-Type: text/html\n\n
 9 <html>
10     <head>
11         <title>Reportlab Example</title>
12     </head>
13     <body>
14         <form action="index2.cgi" method="post" enctype="multipart/form-data">
15             <fieldset>
16                 <legend>Personal Info:</legend>
17                 Name: <input type="text" name="name"><br />
18                 Photo: <input type="file" name="photo"><br />
19                 <input type="submit">
20             </fieldset>
21         </form>
22     </body>
23 </head>"""

Lines #26 – #29 import some more modules (i.e., reportlab related stuff).

26     from reportlab.platypus import *
27     from reportlab.lib.styles import getSampleStyleSheet
28     from reportlab.lib.units import inch
29     from reportlab.lib import colors

Now line #30 is important, this is where we decide where we want to write the output. We can either save the output to a file, or we can dump it to stdout (i.e., back to the web browser). In this example, we’re going to send the output back to the web browser.

30     doc = SimpleDocTemplate(sys.stdout)

Line #31 handles getting the style sheet that we’re going to use to format our text.

31     styles = getSampleStyleSheet()

In line #32, we initialize the ‘content’ dictionary — this is where we’re going to keep the elements of our PDF until we’re ready to write it to stdout.

32     content = []

In lines #34 – #37, we test for the ‘photo’ field — if it has been submitted, we create an Image object out of it. We’re assuming that the user is going to submit a photo, but in reality, the user could submit anything so some further “hardening” of this form would be required in order to ensure that the only things that actually get submitted are image files.

34     if form[‘photo‘].filename:
35         image = Image(form[‘photo‘].file)
36         image.drawHeight = 2*inch*image.drawHeight / image.drawWidth
37         image.drawWidth = 2*inch

Lines #39 – #42 test to see if the ‘name’ field has been submitted; if it has, it creates some text objects to insert into our PDF.

39     if form[‘name‘].value:
40         text1 = Paragraph(form[‘name‘].value, styles[‘Heading1‘])
41         text2 = Paragraph(form[‘name‘].value, styles[‘Heading2‘])
42         text3 = Paragraph(form[‘name‘].value, styles[‘Heading3‘])

Lines #44 – #47 append all the objects that we want to appear in our PDF, to the ‘content’ dictionary we created earlier.

44     content.append(text1)
45     content.append(text2)
46     content.append(text3)
47     content.append(image)

Lines #48 – #49 send the appropriate headers to the web browser, before we send our completed PDF file.

48     print "Content-Type: application/pdf"
49     print "Content-Disposition: attachment; filename=example.pdf\n\n"

And finally, line #50 builds our PDF and sends the output to stdout.

50     doc.build(content)

Test it out; you should end up with a PDF that looks something like this:

reportlab_example1_img1

Of course, this example doesn’t even scratch the surface of what you can do with reportlab. Hopefully I’ll post some more examples later on :-)

Autoresponse 1.6.3 (bugfix) Released

Autoresponse 1.6.3 has been released. This is a bugfix release that fixed an issue with “case sensitive” e-mail addresses. Basically, UsEr@domain.tld or USER@DOMAIN.TLD — both valid addresses, were not able to set an autoresponse message for user@domain.tld (…after the user authenticated with the server via SASL of course). This is no longer an issue thanks to this release.

Autoresponse on HowToForge

Autoresponse now has a tutorial on HowToForge! Be sure to check it out. I also suggest signing up because there is a wealth of information on there, and with a paid subscription you can download any tutorial as a PDF (other formats are available as well) without the ads, graphics, and other unrelated items… it’s well worth it.

Autoresponse 1.6.2 (bugfix) released!

This version of autoresponse addresses a minor bug that could cause any text in the message body that resembles a “To” header to be replaced with something other than what the user intended. There was a fairly small chance that this would happen, but it’s an improvement nonetheless. You can find the new version on the projects page.