General

General rants

new jQuery stuff!

Check it out: http://nefaria.com/project_index/jqueryfilebrowser/

Enjoy!

Today’s Relevant Quote

“When a candidate for public office faces the voters he does not face men of sense; he faces a mob of men whose chief distinguishing mark is the fact that they are quite incapable of weighing ideas, or even of comprehending any save the most elemental — men whose whole thinking is done in terms of emotion, and whose dominant emotion is dread of what they cannot understand. So confronted, the candidate must either bark with the pack or be lost… All the odds are on the man who is, intrinsically, the most devious and mediocre — the man who can most adeptly disperse the notion that his mind is a virtual vacuum. The Presidency tends, year by year, to go to such men. As democracy is perfected, the office represents, more and more closely, the inner soul of the people. We move toward a lofty ideal. On some great and glorious day the plain folks of the land will reach their heart’s desire at last, and the White House will be adorned by a downright moron.”H.L. Mencken

What Patton would have said…

Yesterday I received a chain-letter in my inbox. I’m not going to repost it here, but here’s a few links:

Link #1 (scroll down a bit)

Link #2

Anyway, I get quite a lot of chain-letters like this so it goes without saying that I don’t respond to all of them. However, sometimes I feel compelled to do so, depending on the content and/or the issue being “discussed” (I use that term lightly). This particular letter struck a nerve with me; it’s full of the usual nationalistic/fascist psychobabble — it’s obvious that it was written specifically for a certain subsection of American society.

*sigh* …

Anyway, here’s my response:

Indeed, this is a disgrace. But I think that this point needs clarification. The disgrace isn’t with the Muslim Americans who wish to exercise their right to religious freedom by building a mosque or community center on privately owned property; the disgrace is that some Americans are trying to deny other Americans from exercising that right.

This e-mail claims that the group who wants to build the mosque is an extremist group. However, by any realistic measure, this couldn’t be further from the truth. The proponents of this claim say that the leader of the group has ties to a man who supports extremism and funds madrasas(sp?), but notice how they never actually name this person.

Alwaleed bin Talal. This is the man they’re talking about. Who is he? For starters, he’s a Saudi prince, but that’s not as interesting as this next fact:

He is also the part owner (2nd largest shareholder) of News Corp, which is the parent company of Fox News — the main media outlet that has been relentlessly stirring the pot on the issue of the “terror mosque”.

So, either Fox News is funding terrorism via Alwaleed bin Talal, which means that Fox News is funding the construction of this “terror mosque” while simultaneously opposing it; or Fox News isn’t funding terrorism *and* the members of the group building the mosque are actually peaceful American Muslims just trying to exercise their right to practice their religion — a right guaranteed by the Constitution — in the face of immense xenophobia and ignorance on behalf of everyone opposed to the building of the mosque.

But “what about this radical Imam?” you might be asking. Oh you mean Imam Feisal Abdul Rauf; the same guy who has been working with the U.S. State Department (since the Bush administration) to promote religious tolerance in the Arab world.

Read that again.

The Imam — the leader of the group building the “terror mosque”, worked with the U.S. State Department on at least two separate occasions in 2007 and is currently working with the State Department to promote religious tolerance amongst Muslims.

…Some “radical” he surely is.

By now a few things should be painfully obvious:

(1) The Muslim group building the mosque is not a collection of terrorists, extremists, America-haters, etc.

(2) Fox News is keeping this non-issue going because it’s good for ratings.

(3) Opponents of the mosque actually have no valid reason to oppose it, all of the “reasons” they have can be attributed to not knowing the details of the situation (mentioned previously), xenophobia (i.e., fear of people of other races/nationalities/religions), racism, politically or religiously-motivated hatred of Muslims, or just plain stupidity.

Again, I find it absolutely disgusting that any American would work to restrict the rights of another American, while claiming this to be his or her “patriotic duty”. There is no greater enemy to the United States than this fever of ultra-nationalism that gripped our nation around the throat… we truly are on the road to fascism. Let me put it this way: There is nothing “patriotic” or “American” about ignorantly laboring to limit the Constitutionally protected rights of fellow Americans just because their religion or even their politics disagree with yours.

I also think it’s worth repeating that on 9/11, it wasn’t just Christians who died when the twin towers fell. Within the walls of the WTC buildings, many people of different races, nationalities, and religions carried out their daily routines — Christians, Muslims, Jews, Buddhists, Baha’is, etc., all died in the attack.

…and one more thing

The despicable acts committed at Abu Garib were atrocities, and Iraq had absolutely nothing to do with the 9/11 attacks so attempting to justify them by invoking 9/11 is either disingenuous or ignorant… or both. Also, two wrongs don’t make a right.

###

But maybe General Patton would have something to say about the current state of our nation…

I wonder if he would question the wisdom of fighting a virtually un-winnable war on two fronts — one of those war fronts being a product of deception, corruption, and outright lies on behalf of the Bush administration… I wonder if he’d question the integrity of the leaders who started war in Iraq and then let their corporate pals practically rape the treasury department in the form of charging the government and the taxpayers exorbitant bills for sub-par or completely nonexistent services…

I wonder if he would be proud that half a century after WWII, the citizens of his homeland have forgotten the lessons of the Weimar Republic and the Nazis, and instead are happily marching down the road to fascism themselves…

I wonder if he would be proud that our politicians have embraced fearmongering as a means of controlling the populace…

Somehow, I doubt it.

But in the end it doesn’t really matter because he’s not around to comment on current events, so any speculation about what he may or may not have said is completely pointless. Whomever wrote this e-mail, claiming to know what Patton would have said, has no way of knowing if he or she were right, so it might just be a good idea to ignore useless drivel like this rather than forward it on to others.

In closing, I’d like to issue a challenge:

If you claim to be a patriot, educate yourself about what the Constitution and other founding documents *really* say. Don’t just continue believing in a rosy interpretation of what you imagine them to mean just because it fits your biases, politics, prejudices, etc. Recognize that ALL citizens of this great nation deserve the same rights as guaranteed by the Constitution. There are no “privileged” classes based on race, gender, religion, political beliefs, or sexual orientation — under the laws of our secular government, we are all equals. And if you really, really think that you’re a patriot, read the following excerpt from William N. Grigg, and then take a hard look around you. Take a hard look at what this nation has become. Take a hard look at what YOU have become… and then do something about it.

Quote:

While the terms patriotism and nationalism can refer to the same thing — the love of one’s native country — in practice they have acquired very different meanings. Nationalism, in practice, describes not to the love of a country but rather the veneration of its central government.

As historian John J. Dwyer notes, nationalism is a degenerate impostor of patriotism. “The patriot says, `I love my country,’ works for its good, and defends it if necessary — against enemies within and without,” writes Dwyer. “He strives and prays not primarily that God will bless his country, but that his country will bless God. The nationalist, meanwhile, says, `My country is better than yours.’ `My country is the greatest there has ever been.’ `The greatest nation on God’s green earth.’ `They hate my country because it is so good.’”

Nationalism focuses on the State, rather than the community. It is unambiguously based on zero-sum assumptions about power, and nationalists define victory in terms of imposing their will on others.

Source:

http://freedominourtime.blogspot.com/2007/03/patriotism-vs-nationalism.html

###

Sincerely,

Charles Hamilton
U.S. Army Iraq War Veteran (2003)

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 :-)

Go to Top