~phcteam/slidewall/trunk

5 by Tommaso Bianucci
Aggiunte le funzionalità di parsing da xml esistente
1
#!/usr/bin/python2
2
# -*- encoding:utf-8 -*-
3
4
from xml.dom import minidom
5
from xml.sax.saxutils import quoteattr
6
7
class Leaf(minidom.Element):
8
    def __init__(self, TagName, Text):
9
        minidom.Element.__init__(self, TagName)
10
        t = minidom.Text()
11
        t.data = Text
12
        self.appendChild(t)
13
14
class StaticFrame(minidom.Element):
15
    def __init__(self, duration, filename):
16
        minidom.Element.__init__(self, "static")
17
        self.appendChild(Leaf("duration", duration))
18
        self.appendChild(Leaf("file", filename))
19
20
class TransitionFrame(minidom.Element):
21
    def __init__(self, duration, ffile, tfile):
22
        minidom.Element.__init__(self, "transition")
23
        self.appendChild(Leaf("duration", duration))
24
        self.appendChild(Leaf("from", ffile))
25
        self.appendChild(Leaf("to", tfile))
26
27
class StepFrame():
28
    def __init__(self, duration, transition, ffile, tfile):
29
        self.s = StaticFrame(duration, ffile)
30
        self.t = TransitionFrame(transition, ffile, tfile)
31
32
    def appendToNode(self, node):
33
        node.appendChild(self.s)
34
        node.appendChild(self.t)
35
36
class Header(minidom.Element):
37
    def __init__(self, data={"y":"1990","m":"12","d":"06","hh":"00","mm":"00","ss":"00"}):
38
        minidom.Element.__init__(self, "starttime")
39
        self.appendChild(Leaf("year", data["y"]))
40
        self.appendChild(Leaf("month", data["m"]))
41
        self.appendChild(Leaf("day", data["d"]))
42
        self.appendChild(Leaf("hour", data["hh"]))
43
        self.appendChild(Leaf("minute", data["mm"]))
44
        self.appendChild(Leaf("second", data["ss"]))
45
46
class XmlGen():
47
    
48
    name = 'nuova_prova'
49
    files = [u'/usr/share/backgrounds/Arboreal_ballet_by_Bob_Farrell.jpg',
50
 u'/usr/share/backgrounds/White_flowers_by_Garuna_bor-bor.jpg']
51
    
52
    hold = "1795.0"
53
    transition = "5.0"
54
    xml = 'nuova_prova.xml'
55
    r_xml = 'prova-slideshow.xml'
56
57
    def makeXml(self):
58
        xmldoc = minidom.Document()
59
        bg = minidom.Element("background")
60
        bg.appendChild(Header())
61
        l = len(self.files)
62
        for i in range(l):
63
            step = StepFrame(self.hold, self.transition, self.files[i], self.files[(i+1)%l])
64
            step.appendToNode(bg)
65
        
66
        xmldoc.appendChild(bg)
67
        f = open(self.xml, 'w')
68
        xmldoc.writexml(f, '', '', '', "utf8")
69
        f.close()
70
        print xmldoc.toprettyxml('\t', '\n', "utf8")
71
72
    def readXml(self):
73
        xmldoc = minidom.parse(self.r_xml)
74
        print xmldoc.toprettyxml('\t', '\n', "utf8")
75
        foo = raw_input()
76
        f_elements = xmldoc.getElementsByTagName('file')
77
        files = map(lambda x: x.firstChild.nodeValue, f_elements)
78
        self.files = files
79
80
def test():
81
    prova = XmlGen()
82
    prova.readXml()
83
    print prova.files
84
    prova.makeXml()