Thursday, November 12, 2009

Wow. Python doesn’t have that? (Updated!)

This is going to be a rant, just so you are forewarned.

I have an object in Python that I’ve built that looks something like this:

Object = {
   'User': {
      'IsBanned': False, 
      'IsModerator': True, 
      'UserID': 'test@example.com', 
      'IsAdmin': True, 
      'LogoutURL': '/_ah/login?continue=http%3A//localhost%3A8080/CURRENT_URL&action=Logout', 
      'Location': u'Here.', 
      'Key': 'agVmb3J1bXIKCxIEVXNlchgCDA', 
      'UserLevel': 9L, 
      'NickName': u'Testing this works'
   }, 
   'Forum': [
      {
         'SubForums': [
            {
               'Threads': 0, 
               'Description': u'Test Three', 
               'Key': 'agVmb3J1bXILCxIFRm9ydW0YBQw', 
               'Title': u'Sub Forum'
            }
         ], 
         'Threads': 0, 
         'Description': u'Testing', 
         'Key': 'agVmb3J1bXILCxIFRm9ydW0YAww', 
         'Title': u'First Test'
      }, 
      {
         'Threads': 0, 
         'Description': u'Testing Two', 
         'Key': 'agVmb3J1bXILCxIFRm9ydW0YBAw', 
         'Title': u'Second Test'
      }, 
      {
         'SubForums': [
            {
               'SubForums': 
                  [
                     {'SubForums': 
                        [
                           {
                              'Threads': 0, 
                              'Description': u'Testing yet again', 
                              'Key': 'agVmb3J1bXILCxIFRm9ydW0YCQw', 
                              'Title': u'Another Forum'
                           }
                        ], 
                        'Threads': 0, 
                        'Description': u'Testing even more', 
                        'Key': 'agVmb3J1bXILCxIFRm9ydW0YCAw', 
                        'Title': u'Test Four'
                     }
                  ], 
                  'Threads': 0, 
                  'Description': u'Another test from JSON...', 
                  'Key': 'agVmb3J1bXILCxIFRm9ydW0YBww', 
                  'Title': u'Test Three'
               }
            ], 
            'Threads': 0, 
            'Description': u'Testing that JSON submitting a forum works...', 
            'Key': 'agVmb3J1bXILCxIFRm9ydW0YBgw', 
            'Title': u'test'
         }
      ]
   }

Taking advantage of the SimpleJSON library, I can serialise this into JSON with a single call. But I can’t find anything to serialise it into XML.

Well, technically speaking, I can. But all the libraries I have found want to do a LOT of massaging of the data structure, making it complex and unwieldy. What I want is a simple XML representation, like this:

<Object>
   <User>
      <IsBanned>False</IsBanned>
      <IsModerator>True</IsModerator>
      <UserID>test@example.com</UserID>
      <IsAdmin>True</IsAdmin>
      <LogoutURL>/_ah/login?continue=http%3A//localhost%3A8080/CURRENT_URL&action=Logout</LogoutURL>
      <Location>Here.</Location>
      <Key>agVmb3J1bXIKCxIEVXNlchgCDA</Key>
      <UserLevel>9</UserLevel>
      <NickName>Testing this works</NickName>
   </User>
   <Forum>
      <Item>
         <SubForums>
            <Item>
               <Threads>0</Thread>
               <Description>Test Three</Description>
               <Key>agVmb3J1bXILCxIFRm9ydW0YBQw</Key>
               <Title>Sub Forum</Title>
            </Item>
         </SubForums>
         <Threads>0</Threads>
         <Description>Testing</Description>
         <Key>agVmb3J1bXILCxIFRm9ydW0YAww</Key>
         <Title>First Test</Title>
      </Item>
      <Item>
         <Threads>0</Threads>
         <Description>Testing Two</Description>
         <Key>agVmb3J1bXILCxIFRm9ydW0YBAw</Key>
         <Title>Second Test</Title>
      </Item>
      <Item>
         <SubForums>
            <Item>
               <SubForums>
                  <Item>
                     <SubForums>
                        <Item>
                           <Threads>0</Threads>
                           <Description>Testing yet again</Description>
                           <Key>agVmb3J1bXILCxIFRm9ydW0YCQw</Key>
                           <Title>Another Forum</Title>
                        </Item>
                     </SubForums>
                     <Threads>0</Threads>
                     <Description>Testing even more</Description>
                     <Key>agVmb3J1bXILCxIFRm9ydW0YCAw</Key>
                     <Title>Test Four</Title>
                  </Item>
               </SubForums>
               <Threads>0</Threads>
               <Description>Another test from JSON...</Description>
               <Key>agVmb3J1bXILCxIFRm9ydW0YBww</Key>
               <Title>Test Three</Title>
            </Item>
         </SubForums>
         <Threads>0</Threads>
         <Description>Testing that JSON submitting a forum works...</Description>
         <Key>agVmb3J1bXILCxIFRm9ydW0YBgw</Key>
         <Title>test</Title>
      </Item>
   </Forum>
</Object>

It seems, however, that there is no Python library that can do that for me.

So why, I hear you asking, do I need such a thing?

Well, as you can probably tell from the listing(s) above, I’m writing an AJAX forum system. I’m doing it in Python so I can host it on Google App Engine (for free!), and can release it as open-source, but I’ve run into a pretty major snag.

I was using JSON to send data back and forth, which was working well. But I’ve found out that when you try and post using JSON to a different server (As the App Engine server will be, this is intended to be a “Widget” to use with Google Sites, among other things), an OPTIONS request is sent first to see if the cross-site request is allowed. I’ve grabbed the OPTIONS request, and replied with an “approved” message, but (even more to my consternation), the subsequent post from App Engine is empty. Oh, and cookies set before (on the GET) are not being sent with the POST, either. Thus I have decided to try switching to XML, and have run across the above roadblock.

I’m sure there are ways to do it, but searching across the ‘net has returned nothing. So I’m going to have to throw this open to the blogosphere, and hope something comes up. And soon.

[Update] As I found no-one had invented this particular wheel for me, I decided to roll my own, if you’ll excuse the pun(s).

No comments:

Post a Comment