2006-04-05
A Python script to sync. with Perforce
I have been using Fitnesse for a while. But I am not happy with the file system based backup. There is no easy way to setup a SCM back end for the Fitnesse. So I end up have to write a Python script to periodically sync. my local page with a Perforce depot. It works quite convenient.
Bitten by Maven
I have been struggling with my Maven script for a few hours. Finally it turned out is because of a property name I used. After I changed the name from "someA-someB" to "someA.someB", the problem is gone. Maven just does not like '-'.
2006-03-06
Turn off caching for Firefox
I have a need to turn off the caching for a particular page. I suppose the correct way is to set the cache directive in my Http request:
Too bad.
Cache-Control: no-cachenetwork.http.use-cache = false.
The server correctly returns the new version. The Firefox, however, insists to return me the cached old version.
So I have to turn the entire Firefox caching capability off through its about:config page
Too bad.
My first grease monkey script
I wrote my first grease monkey script today. It is to apply a XSLT style sheet on certain types of XML files. This is because usually the xml contains too much information and I just want ot view certain part of it and in more clean format such as a table. Here is the code:
/*
Title:
xml transformer
Description:
This is a Greasemonkey user script for Firefox.
This script applies XSLT on plain XML files
*/
// ==UserScript==
// @name xml transformer
// @namespace http://sample.com
// @description apply XSLT on plain XML
// @include http://localhost/*.xml
// ==/UserScript==
var contentType = 'text/html';
detectFormats();
function detectFormats() {
// define classNames and URLs of corresponding XSLT files - edit this to add new formats
var b = new Array();
b[0] = new Array('Root1', 'http://localhost/gmonkey/xslt/root1.xslt', 'text/html');
b[1] = new Array('Root2', 'http://localhost/gmonkey/xslt/root2.xslt', 'text/html');
// look for certain signature in document
for (i=0; i<b.len; i++) {
var schemaType = b[i][0];
if (document.getElementsByTagName(schemaType).length > 0){
xsltURL = b[i][1];
contentType = b[i][2];
GM_log(xsltURL);
fetchXSLT(xsltURL);
}
}
}
function fetchXSLT(url){
GM_xmlhttpRequest({
method: "GET",
url: url,
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
'Accept': 'application/atom+xml,application/xml,text/xml',
'Cache-Control': 'no-cache',
},
onload: function(response) {
var xslt = new DOMParser().parseFromString(response.responseText, "text/xml");
transformXML(xslt);
}
});
}
// transform the document to RDF using the XSLT file
function transformXML(xslt){
var processor = new XSLTProcessor();
processor.importStylesheet(xslt);
var dataXML = document;
var processed = processor.transformToDocument(dataXML);
var dataString = (new XMLSerializer()).serializeToString(processed);
location.href = 'data:' + contentType + ',' + dataString;
}
2006-02-17
Mapping xml to Python objects
I need to process XML in my python code and prefer to deal with the data in the pythonic way. From a wide options available, I choose to play with 3 of them.
* generateDS
* gnosis.xml.objectify
* amara
Because I am a fan of XmlBeans, it seems that generateDS should be my favorite choice. However, when I tried to run the code-generating script on my schema file, it encountered some recursive problem and complained:
.... parent.collectElementNames(elementNames) File "C:\Python\Lib\site-packages\generateDS.py", line 478, in collectElementNames parent.collectElementNames(elementNames) File "C:\Python\Lib\site-packages\generateDS.py", line 475, in collectElementNames base = self.getBase() RuntimeError: maximum recursion depth exceeded
<ns:book xmlns:ns="http://mycompany/bookproject">..
It generates an object called ns_book. It does not recognize : as a namespace prefix and just blindly translates it to _. I tried to search for a solution, but could not find anything.
The amara module, however, correct translates the object name to book and also keeps the namespace information. It seems that I will use this module at this time.
* generateDS
* gnosis.xml.objectify
* amara
Because I am a fan of XmlBeans, it seems that generateDS should be my favorite choice. However, when I tried to run the code-generating script on my schema file, it encountered some recursive problem and complained:
.... parent.collectElementNames(elementNames) File "C:\Python\Lib\site-packages\generateDS.py", line 478, in collectElementNames parent.collectElementNames(elementNames) File "C:\Python\Lib\site-packages\generateDS.py", line 475, in collectElementNames base = self.getBase() RuntimeError: maximum recursion depth exceeded
The same schema file works fine with XMLBeans however.
<ns:book xmlns:ns="http://mycompany/bookproject">..
It generates an object called ns_book. It does not recognize : as a namespace prefix and just blindly translates it to _. I tried to search for a solution, but could not find anything.
The amara module, however, correct translates the object name to book and also keeps the namespace information. It seems that I will use this module at this time.
2006-02-16
2 tips about JBoss
* to retrieve the server name, such as "minimal", "default", "max", in your code
* to set an unique Xid for each JBoss instance
if you are running multiple JBoss instances on the same machine, you probably want to give each instance a unique Xid base name. To do it, you need to add
the following line in the jboss-service.xml:
String serverName = System.getProperty( org.jboss.system.server.ServerConfig.SERVER_NAME); * to set an unique Xid for each JBoss instance
if you are running multiple JBoss instances on the same machine, you probably want to give each instance a unique Xid base name. To do it, you need to add
the following line in the jboss-service.xml:
<mbean code="org.jboss.tm.XidFactory" name="jboss:service=XidFactory">
<attribute name="BaseGlobalId">UniqueInstanceName</attribute>
<!--attribute name="Pad">true</attribute-->
</mbean>
2006-02-15
Successfully installed Twisted
I need to install the latest Twisted with Python 2.4. Unfortunately I don't have Visual Studio on my machine. Is there a way to do it. I find this excellent instruction on how to install Python extensions without VS. It works as advertised. I am happily running Twisted now!
2006-02-13
Upgrade to CruiseControl 2.4.0
Today I upgraded my CruiseControl to 2.4.0. The main reason is my project's SCM has been moved from Visual Source Safe to Perforce and I cannot get the CruiseControl 2.2.1 to work with Perforce. After the upgrade, everything comes back to normal.
2006-02-09
Python web service
In my previous post, I have discussed using Perl (SoapLite) as client to consume a web service implemented using Java(Apache Axis). I am curious to find how it will work out for other lanuages such as Python and Ruby. Here is my try for Python.
The tool I use is ZSI which stands for Zolera SOAP Infrastructure. It provides a tool wsdl2py that can generate Python stub code from a wsdl file. Run the tool on the wsdl file described in my previous post, I get two Python modules: MessageRouter_services_types.py and MessageRouter_services.py. With the help of these two modules, consuming a web service is straight forward. Here is the code:
The tool I use is ZSI which stands for Zolera SOAP Infrastructure. It provides a tool wsdl2py that can generate Python stub code from a wsdl file. Run the tool on the wsdl file described in my previous post, I get two Python modules: MessageRouter_services_types.py and MessageRouter_services.py. With the help of these two modules, consuming a web service is straight forward. Here is the code:
def sendTo(destination, payload):
loc = MessageRoutingServiceLocator()
kw = { 'tracefile' : sys.stdout }
portType = loc.getMessageRouterPortType(**kw)
dest = ns1.DestinationType_Def()
dest._protocol = "http"
dest._category = "post"
dest._server = "127.0.0.1"
dest._port = 80
dest._target = "/router"
msg = ns1.MessageBodyType_Def()
msg._content = "payload"
req = RouteRequestWrapper()
req._destination = dest
req._request = msg
try:
response = portType.RouteMessage(req)._response._content
print "Response message is:\n %s" % response
except Exception, e:
response = repr(e)
print "Failed: ", e
raise e
2006-02-03
Programming languages I use now
Programming languages I use
Java - the language I use to earn my bread-and-butter and the one I will choose to do serious work
Ruby - it's style is cute I think, and it is fun to work with,
Python - as powerful as it's name suggests, I have been bitten by the white spaces, but now recovered
Perl - there is more than one way to do it, but many times I prefer there is only one way
Java - the language I use to earn my bread-and-butter and the one I will choose to do serious work
Ruby - it's style is cute I think, and it is fun to work with,
Python - as powerful as it's name suggests, I have been bitten by the white spaces, but now recovered
Perl - there is more than one way to do it, but many times I prefer there is only one way
Subscribe to:
Posts (Atom)