Yearly Archives: 2006

LibXML2 and LibXSLT documentation

If you use libxml2 and libxslt (windows binaries) in python, you’ll know how annoying it is trying to figure out how to do something as the documentation is quite limited.

You can find out what is available in the python shell using dir() and __doc__ and help() but that’s a pain so I wrote a script to output a HTML file with the complete listing of the methods and prperties that libxml2 and libxslt have. Run it on your machine and you’ll have a ful list of all the methods (with __doc__ output) available with a permalink to each one and a link to google searching for references to that method.

Hope it helps

Bobby Super Dog

After much persuasion and torture, we are now the proud parents of “Bobby”, An 8 week old golden labrador. He is sleeping at my feet as I type. He does wake and start barking incredibly early but fingers crossed that will end soon. He’s only 8 weeks but already I have hime sitting, giving paw, playing football and pissing and pooping everywhere he sees fit (Gill taught him that).
Bobby Super Dog

Clean up CVS and SVN directories

A quick python script to clean up a “used to be” cvs or svn working directory – I know that TortoiseSVN has an export feature but TortoiseCVS does not.
[code lang=”python”]
“””
Removes all cvs and svn directories
usage: “CVSSVNCleanup.py true” to delete all cvs and svn directories
“CVSSVNCleanup.py” to view all cvs and svn directories
“””

import os
import shutil
import sys

#get a list of all the root level directories
lDirs=os.listdir(os.curdir)

if len(sys.argv) > 1 and sys.argv[1].lower()==”true”:
delete = True
else:
delete = False

for d in lDirs:
for root,dirs,files in os.walk(d):

for dirName in dirs:
if dirName.lower() == “.svn” or dirName.lower() == “cvs”:
curDir = os.getcwd()+”\\”+root+”\\”+dirName
if delete == True:
shutil.rmtree(curDir)
curDir = curDir + ” – Deleted”
print curDir
continue
print “Finished”
[/code]

Python Publisher Posted Vars

I was trying to get all of the variables posted to a python script added to a dictionary and was trying
[code lang=”python”]
form = cgi.FieldStorage()
[/code]
but it seems that you cannot use FieldStorage when using the publisher handler in mod_python. As the request variable req is always available, use the following.
[code lang=”python”]
styleArgs = {}
for k in req.form.keys():
styleArgs[k] = “‘”+req.form[k]+”‘”
[/code]

I couldn’t find any docs on this so I thought I’d post it.

What’s my bloody session ID

While having a wander over at xulplanet.com I stumbled across the Cookie Manager Interface (Interface to the Gecko engine that is).

We have a simple HTML page in work that we use to emulate Payment Service Providers[PSP] (like Paypal or SecureTrading). It’s a right pain to test as we have to edit the page each time we test a different site. We also have to find our session ID from that sites cookie if we want to test it.

I wrote a few functions to allow me to populate a select drop down with the cookie domains (and session IDs) for a filtered list of all my cookies. On Page load the drop down is genereated and onChange of that dropdown the forms are updated with the chosen sites SessionID and the forms actions is also changed. Pretty neat and saves me about 10 clicks everytime I need to test my payment emails or that the PSP integration is working.

[code lang=”javascript”]
function _loadCookies()
{

//need to ask for permission to look at the users cookies
try {
netscape.security.PrivilegeManager.enablePrivilege(“UniversalXPConnect”);
} catch (e) {
alert(“Permission to save file was denied.”);
}
var nsICookie = Components.interfaces.nsICookie;
var _cm = Components.classes[“@mozilla.org/cookiemanager;1”]
.getService(Components.interfaces.nsICookieManager);

var _ds = Components.classes[“@mozilla.org/intl/scriptabledateformat;1″]
.getService(Components.interfaces.nsIScriptableDateFormat);
var _hosts = {};
var e = _cm.enumerator;
var hostCount = { value: 0 };

while (e.hasMoreElements()) {
var cookie = e.getNext();
if (cookie && cookie instanceof Components.interfaces.nsICookie)
{

var strippedHost = _makeStrippedHost(cookie.host);
if (!(strippedHost in _hosts) || !_hosts[strippedHost])
{
_hosts[strippedHost] = { cookies : [],
rawHost : strippedHost,
level : 0,
open : false,
container : true };

++hostCount.value;
var c = _makeCookieObject(strippedHost, cookie);
_hosts[strippedHost].cookies.push(c);
}
}
else
break;
}

var _select = document.createElement(‘SELECT’)
for (var host in _hosts) {
var cookies = _hosts[host].cookies;
var hostName =_hosts[host].rawHost;
var rExp = /kf/gi;
var rExpResults = hostName.match(rExp);
if(rExpResults && rExpResults.length > 0)
{
for (var _cookie in cookies)
{
var _option = document.createElement(‘OPTION’)
_option.innerHTML = _hosts[host].rawHost;
_option.value = cookies[_cookie].value;
_select.appendChild(_option);
}

}
}
_select.id=”siteURLChange”;
_select.onchange = changeActionAndSessID;

var URLForm = document.getElementById(‘siteurl’);
var URLInput = document.getElementById(‘url_site’);
URLInput.style.display = “none”;
insertAfter(URLForm, _select, URLInput);
changeActionAndSessID();
}

function _makeStrippedHost(aHost)
{
var formattedHost = aHost.charAt(0) == “.” ? aHost.substring(1, aHost.length) : aHost;
return formattedHost.substring(0, 4) == “www.” ? formattedHost
.substring(4, formattedHost.length) : formattedHost;
}

function _makeCookieObject(aStrippedHost, aCookie)
{
var host = aCookie.host;
var formattedHost = host.charAt(0) == “.” ? host.substring(1, host.length) : host;
var c = { name : aCookie.name,
value : aCookie.value,
isDomain : aCookie.isDomain,
host : aCookie.host,
rawHost : aStrippedHost,
path : aCookie.path,
isSecure : aCookie.isSecure,
expires : aCookie.expires,
level : 1,
container : false };
return c;
}
[/code]

I hope this code helps someone wanting to view cookies from a certain domain or from a filtered list.