~thelama/+junk/do-plugins

« back to all changes in this revision

Viewing changes to QuodLibet/data/Do.Addins.QuodLibet.unpickle.py

  • Committer: Mathieu Cadet
  • Date: 2008-03-04 23:42:04 UTC
  • Revision ID: mathieu_cadet_mathieu__cadet__gmail-20080304234204-775hovzde14od30t
Updated for build with autotools

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##
 
2
## Extract pickled data from the QuodLibet songs database
 
3
## to an easy to parse XML file.
 
4
##
 
5
## By Mathieu Cadet <mathieu cadet gmail>
 
6
##
 
7
 
 
8
import os,sys,cPickle
 
9
import codecs
 
10
 
 
11
sys.path += ["/usr/local/share/quodlibet",
 
12
             "/usr/share/quodlibet"]
 
13
 
 
14
def quit(msg=None):
 
15
    if msg:
 
16
        print msg
 
17
    sys.exit (1)
 
18
 
 
19
user = os.environ.get("USER")
 
20
home = os.environ.get("HOME")
 
21
if not user or not home:
 
22
    quit("USER and/or HOME environnement variable not defined")
 
23
 
 
24
try:
 
25
    f = open("%s/.quodlibet/songs" % home)
 
26
    try:
 
27
        d = cPickle.load(f)
 
28
    finally:
 
29
        f.close()
 
30
except IOError:
 
31
    print "Unable to load QuodLibet database."
 
32
    raise
 
33
 
 
34
try:
 
35
    xml = codecs.open("/tmp/quodlibet-songs-%s" % user, "wt","utf-8")
 
36
    try:
 
37
        xml.write("<songs>\n")
 
38
 
 
39
        for s in d:
 
40
            if len(s.get("title", '')) > 0 and \
 
41
            len(s.get("artist", '')) > 0 or len(s.get("album", '')) > 0:
 
42
                xml.write("<song>\n")
 
43
                xml.write("<title><![CDATA[%s]]></title>\n" % s.get("title"))
 
44
                xml.write("<artist><![CDATA[%s]]></artist>\n" % s.get("artist", "Unknown artist").replace("\n", ","))
 
45
                xml.write("<album><![CDATA[%s]]></album>\n" % s.get("album", ""))
 
46
                xml.write("<filename><![CDATA[%s]]></filename>\n" % s.get("~filename","").decode("utf-8", 'replace'))
 
47
                xml.write("</song>\n")
 
48
 
 
49
        xml.write("</songs>\n")
 
50
    finally:
 
51
        xml.close()
 
52
except IOError:
 
53
    print "Unable to write the XML song database."
 
54
    raise