Bruno Fernandez-Ruiz Journal

random thoughts from a 0xCAFED00D

pywbxml revisited

I know I said I was not likely to fix pywbxml, but the alternative was even less appealing. I know I have to move away from Java, so here it is how to get pywbxml working.

  1. Continuing to use a mix of Java and PHP to leverage the PHP PECL wbxml extension creates an unnecessary complexity.
  2. Moving the PHP code doing wbxml2xml and xml2wbxml transformations to Java is possible but I noticed lack of activity on KML at sourceforge (the project that contains the wbxml library for Java). libwbxml in contrast is owned and maintained by the opensync folks, and it’s only the python bindings that needed some love.
  3. The XMPP library I use in Java, Smack uses a model of one-thread-per-socket, which for server-side and maintaining hundreds or thousands of users will require me to have much more memory in the box that I would like to. I rather use an event-based / reactor architectural style.

So I decided to patch the Python bindings for my needs (rather than fixing it properly by exposing the args through the API). The Python bindings seem to be generated out of SWIG, so editing src/pywbxml.pyx is enough to get the dictionary I wanted for IMPS (WBXML_LANG_WV_CSP12). The change is straight forward, simply:

params.lang = WBXML_LANG_WV_CSP12

or the unified patch:

--- pywbxml-0.1/src/pywbxml.pyx 2006-07-28 01:51:57.000000000 +0100
+++ pywbxml-0.1-wv/src/pywbxml.pyx  2009-01-14 21:55:40.000000000 +0000
@@ -14,7 +14,7 @@
     object PyString_FromStringAndSize(char *s, int len)
     int PyString_AsStringAndSize(object obj, char **buffer, int *length)

-class WBXMLParseError:
+class WBXMLParseError(Exception):
     def __init__(self, code):
         self.code = code
         self.description = <char *> wbxml_errors_string(code)
@@ -28,7 +28,7 @@
     cdef WBXMLGenXMLParams params

     params.gen_type = WBXML_GEN_XML_CANONICAL
-    params.lang = WBXML_LANG_AIRSYNC
+    params.lang = WBXML_LANG_WV_CSP12
     params.indent = 0
     params.keep_ignorable_ws = 1

You will notice an additional change in the patch for WBXMLParseError - this comes from the Debian package, and ensures the we are raising a proper exception.

With this applied, compiled, and installed, I ran the same test with an IMPS CSP document, this time successfully:

$ python
Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pywbxml import xml2wbxml, wbxml2xml
>>> xml = """<?xml version="1.0"?><!DOCTYPE WV-CSP-Message PUBLIC "-//OMA//DTD WV-CSP 1.2//EN" "http://www.openmobilealliance.org/DTD/WV-CSP.DTD"><WV-CSP-Message xmlns="http://www.wireless-village.org/CSP1.1"><Session><SessionDescriptor><SessionType>Outband</SessionType></SessionDescriptor><Transaction><TransactionDescriptor><TransactionMode>Request</TransactionMode><TransactionID>nok1</TransactionID></TransactionDescriptor><TransactionContent xmlns="http://www.wireless-village.org/TRC1.1"><Login-Request><UserID>hermes.onesoup</UserID><ClientID><URL>WV:IMPEC01$00001@NOK.S60</URL></ClientID><Password>xxxxxx</Password><TimeToLive>86400</TimeToLive><SessionCookie>wv:nokia.1789505498</SessionCookie></Login-Request></TransactionContent></Transaction></Session></WV-CSP-Message>"""
>>> binary = xml2wbxml(xml)
>>> text = wbxml2xml(binary)
>>> text
'<?xml version="1.0"?><!DOCTYPE WV-CSP-Message PUBLIC "-//OMA//DTD WV-CSP 1.2//EN" "http://www.openmobilealliance.org/DTD/WV-CSP.DTD"><WV-CSP-Message><Session><SessionDescriptor><SessionType>Outband</SessionType></SessionDescriptor><Transaction><TransactionDescriptor><TransactionMode>Request</TransactionMode><TransactionID>nok1</TransactionID></TransactionDescriptor><TransactionContent><Login-Request><UserID>hermes.onesoup</UserID><ClientID><URL>WV:IMPEC01$00001@NOK.S60</URL></ClientID><Password>xxxxxx</Password><TimeToLive>86400</TimeToLive><SessionCookie>wv:nokia.1789505498</SessionCookie></Login-Request></TransactionContent></Transaction></Session></WV-CSP-Message>'

Yes, I am now happier.

blog comments powered by Disqus