Login

Generating Taconite command documents

Author:
desfrenes
Posted:
May 7, 2008
Language:
Python
Version:
.96
Score:
3 (after 3 ratings)

Hello,

This is a port of a php class used to generate XML taconite command documents, useful for (very) easy and powerful ajaxy stuff, if you don't know what that is just check it there in french : http://www.desfrenes.com/playground/taconite/ or there in english : http://www.malsup.com/jquery/taconite/.

Basically what it does is generate an XML document that is later processed by a javascript plugin which executes a serie of DOM modifications.

About the code, I'm a Django beginner as well as a Python beginner so kind advices are welcome.

Cheers.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
# usage:
#
# t = Taconite()
#
# t.append("#toto","<label>test</label>")
# t.remove("#tutu")
# t.js('alert("hello world");')
# t.toggleClass('blue','body')
# t.css("body","background-color","white")
# [...]
# print t.toprettyxml()

import xml.dom.minidom as dom

class Taconite(dom.Document):
    def __init__(self):
        dom.Document.__init__(self)
        taconite = self.createElement("taconite")
        self.appendChild(taconite)

    def __str__(self):
        return self.toxml(encoding="utf-8")
    
    def camelizeCssProperty(self,property):
        words = property.split('-')
        camelized = words[0].lower()
        for word in words[1:] :
            camelized = camelized + word[0].upper() + word[1:]
        return camelized
    
    def js(self,script):
        command = self.createElement("eval")
        js = self.createTextNode(script)
        command.appendChild(js)
        self.childNodes[0].appendChild(command)
    
    def changeContentCommand(self,method,selector,content):
        html_dom = dom.parseString(content)
        command = self.createElement(method)
        command.setAttribute("select",selector)
        command.appendChild(html_dom.childNodes[0])
        self.childNodes[0].appendChild(command)
    
    def changeStateCommand(self,action,selector):
        command = self.createElement(action)
        command.setAttribute("select",selector)
        self.childNodes[0].appendChild(command)
    
    def CssCommand(self,action,css_class,selector):
        command1 = self.createElement(action)
        command1.setAttribute("select",selector)
        command1.setAttribute("arg1",css_class)
        command2 = self.createElement(action)
        command2.setAttribute("select",selector)
        command2.setAttribute("value",css_class)
        self.childNodes[0].appendChild(command1)
        self.childNodes[0].appendChild(command2)
    
    def addClass(self,css_class,selector):
        self.CssCommand("addClass",css_class,selector)

    def removeClass(self,css_class,selector):
        self.CssCommand("remove",css_class,selector)

    def toggleClass(self,css_class,selector):
        self.CssCommand("toggleClass",css_class,selector)
    
    def append(self,selector,content):
        self.changeContentCommand("append",selector,content)
    
    def prepend(self,selector,content):
        self.changeContentCommand("prepend",selector,content)
        
    
    def before(self,selector,content):
        self.changeContentCommand("before",selector,content)
        
    
    def after(self,selector,content):
        self.changeContentCommand("after",selector,content)
    
    def wrap(self,selector,content):
        self.changeContentCommand("wrap",selector,content)
    
    def replace(self,selector,content):
        self.changeContentCommand("replace",selector,content)
    
    def replaceContent(self,selector,content):
        self.changeContentCommand("replaceContent",selector,content)
    
    def remove(self,selector):
        self.changeStateCommand("remove",selector)
    
    def show(self,selector):
        self.changeStateCommand("show",selector)
    
    def hide(self,selector):
        self.changeStateCommand("hide",selector)
    
    def removeContent(self,selector):
        self.changeStateCommand("empty",selector)
    
    def css(self,selector,property,value):
        command = self.createElement("css")
        command.setAttribute("select",selector)
        command.setAttribute("name",self.camelizeCssProperty(property))
        command.setAttribute("value",value)
        self.childNodes[0].appendChild(command)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.