summaryrefslogtreecommitdiffstats
path: root/amarok/src/scripts/playlist2html/Playlist.py
blob: 91d6c971ff9c271e7c46f5bf43a87e6be95e90ce (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
# -*- coding: Latin-1 -*-
import user
import os
from xml.dom import minidom


# the current.xml file
PLAYLISTFILE = "%s/.trinity/share/apps/amarok/current.xml"%(user.home)

# the fields to be shown via http
FIELDS = ("Artist", "Title", "Album", "Track", "Length", "Genre",  "Score" )

class Track(object):
    """Class that holds the information of one track in the current playlist"""
    __Q_SLOTS__ = FIELDS

    def __init__(self, **kwargs):
        for key,value in kwargs.items():
            setattr(self, key, value)


    def toRow(self, style=''):
        """Returns the a html-table-row with member values of this class"""
        tmp = ['<td>%s</td>'%(i) for i in [getattr(self,f) for f in \
                self.__Q_SLOTS__ ]]
        tr_style = ''        
        if style:
            tr_style='class="%s"'%(style)
        return '<tr %s>%s</tr>'%(tr_style, ''.join(tmp))        


class Playlist:
    """The Playlist class represents the Playlist as one object. It holds all
    needed information and does most of the work"""

    def __init__(self):
        """The Constructor takes no arguments."""
        self.sync()
        self.tracks = []
        self.mtime = self._getMtime()
        self.firstRun = 1
        self.code=""" <html>\n
                    <head>
                    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
                    <style>
                    body {
                        font-size:10pt;
                        margin-left:5%%;
                        margin-right:5%%;
                        background-color: #9cb2cd;
                        color: white;
                        }
                     table{border:1px white;}   
                    .tr_one { background-color:#394062; }
                    .tr_two { background-color:#202050;}
                    th{color:dark-grey;}
                    .window {background-color:#d9d9d9; border: 1px solid black;
                    padding:15px;}
                    a:link { color:#074876; border:0px; text-decoration:none; }
                    a:visited { color:#074876; text-decoration:none; text-decoration:none; }

                    a:active { color:#074876; text-decoration:none; text-decoration:none; } 
                    a:hover { text-decoration:none; text-decoration:none; background-color:#074876;
                            color:white; }
                    </style>
                    
                    </head> 
                    <title>AmaroK playlist</title>\n <body>
                    <div style="text-align:center;font-size:16pt;">current
                    amarok playlist of %s</div>
                    \n
                    <div class="window">%s</div> </body>\n
                </html>\n
              """
        self.encoding = "UTF-8"
        self.fullPage = ""
        self._buildDoc()



    def toHtml(self):
        """Returns a html representation of the whole Playlist"""
        if self.firstRun:
            self._setFullPage()
            self.firstRun = 0
        else:    
            newmtime = self._getMtime()    
            if newmtime !=self.mtime:
                self._buildDoc()
                self._setFullPage()
                self.mtime = newmtime
        return self.fullPage    


    def _setFullPage(self):
        self.fullPage = self.code%(os.environ['LOGNAME'],(self._createTable()).encode(self.encoding,'replace'))


    def _getMtime(self):
        """gets the mtime from the current.xml file, to check if the current.xml
        has been modified or not"""
        return os.stat(PLAYLISTFILE)[-2]
    

    def _buildDoc(self):
        """Build the DOM-doc and calls the _extractTracks-Method"""
        self.doc = minidom.parse(PLAYLISTFILE)
        self._extractTracks()

    def _extractTracks(self):
        """extracts all "item"-elements from the doc and creates an Track-object
        to store the associated information"""
        append = self.tracks.append
        for item in self.doc.getElementsByTagName("item"):
            curTrack = Track()
            for elem in FIELDS:
                try:
                    value = item.getElementsByTagName(elem)[0].firstChild.nodeValue
                except:
                    value = '  '
                setattr(curTrack, elem, value)    
            append(curTrack)


    def _createTable(self):
        """Returns the HTML-Table"""

        tbl = """<table width="100%%" cellpadding="5px" cellspacing="2">
        <!--<colgroup> <col width="150"> <col width="330"> <col width="150">
        <col width="10"> <col width="30"> <col width="10"> </colgroup>-->
        %s%s</table>"""
        rows = self._createRows() 
        thead = "".join(['<th>%s</th>'%(i) for i in FIELDS])
        return tbl  %(thead,"".join(rows))            

    def _createRows(self):
        """Returns the table rows"""
        retval = []
        i = 1
        for track in self.tracks:
            style = 'tr_one'
            if i %2 == 0:
                style = 'tr_two'
            retval.append(track.toRow(style=style))
            i = i+1
        return retval 
    
    def sync(self):
        """Saves the current Amarok-Playlist to the current.xml file. Calls
        amarok via dcop"""
        os.system("dcop amarok playlist saveCurrentPlaylist")