#!/usr/bin/env python
 
# import the needed modules
import sys
import zipfile
import xml.parsers.expat
 
# get content xml data from OpenDocument file
ziparchive = zipfile.ZipFile(sys.argv[1], "r")
xmldata = ziparchive.read("content.xml")
ziparchive.close()

class TreeBuilder:
    def __init__(self):
        self.cmds = ['LitCommand']
        self.codes = ['LitCode']
        self.cmd = False
        self.task = None
        self.file = None
        self.code = False

    def start_element(self, name, attrs):
        if name == 'text:p':
            style = attrs.get('text:style-name')
            if style in self.cmds:
                self.cmd = True
            elif style in self.codes:
                self.code = True
        elif name == 'text:tab':
            if self.code and self.task != 'ignore':
                self.file.write('    ')
        elif name == 'text:s':
            print name, attrs
            if self.code and self.task != 'ignore':
                count = int(attrs.get('text:c', 1))
                self.file.write(' ' * count)
        elif name == 'style:style':
            # styles in the document are often generated by OO.org, and may be
            # sub-children of our styles
            if attrs.get('style:parent-style-name') in self.cmds:
                self.cmds.append(attrs['style:name'])
            elif attrs.get('style:parent-style-name') in self.codes:
                self.codes.append(attrs['style:name'])
    def end_element(self, name):
        if name == 'text:p':
            if self.cmd:
                self.cmd = False
            elif self.code:
                if self.task != 'ignore':
                    self.file.write('\n')
                self.code = False
    def char_data(self, data):
        if self.cmd:
            parts = data.split()
            if len(parts) == 1:
                self.task = parts[0]
                filename = None
            else:
                self.task = parts[0]
                filename = parts[1]
            if self.task == 'create':
                if self.file:
                    self.file.close()
                self.file = open(filename, 'w')
            elif self.task == 'append':
                if filename:
                    if self.file:
                        self.file.close()
                    self.file = open(filename, 'a')
                else:
                    assert self.file
            elif self.task == 'ignore':
                pass
            else:
                raise RuntimeError('Unknown command %s' % self.task)
        elif self.code and self.task != 'ignore':
            self.file.write(data)

 
# create parser and parsehandler
parser = xml.parsers.expat.ParserCreate()
treebuilder = TreeBuilder()
# assign the handler functions
parser.StartElementHandler  = treebuilder.start_element
parser.EndElementHandler    = treebuilder.end_element
parser.CharacterDataHandler = treebuilder.char_data
 
# parse the data
parser.Parse(xmldata, True)

if treebuilder.file:
   treebuilder.file.close()


