1 # -*- coding: utf-8 -*-
3 Boîte de dialogue pour sélection des chansons.
9 from pgu
.gui
import FileDialog
12 from xml
.etree
import ElementTree
13 from minwii
.musicxml
import musicXml2Song
15 INDEX_TXT
= 'index.txt'
17 class FileOpenDialog(FileDialog
):
21 def __init__(self
, path
):
22 FileDialog
.__init
__(self
,
23 title_txt
="Ouvrir une chanson",
27 self
.list.style
.width
= 700
28 self
.list.style
.height
= 250
31 self
.input_dir
.value
= self
.curdir
32 self
.input_dir
.pos
= len(self
.curdir
)
33 self
.input_dir
.vpos
= 0
37 for i
in os
.listdir(self
.curdir
):
38 if i
.startswith('.') : continue
39 if os
.path
.isdir(os
.path
.join(self
.curdir
, i
)): dirs
.append(i
)
42 self
.input_file
.value
= "Dossier innacessible !"
49 self
.list.add(i
, image
=self
.dir_img
, value
=i
)
53 if not i
.endswith('.xml') :
55 filepath
= os
.path
.join(self
.curdir
, i
)
56 xmlFiles
.append(filepath
)
57 # self.list.add(FileOpenDialog.getSongTitle(filepath), value=i)
60 printableLines
= self
.getPrintableLines(xmlFiles
)
61 for l
in printableLines
:
62 self
.list.add(l
[0], value
= l
[1])
64 self
.list.set_vertical_scroll(0)
66 def getPrintableLines(self
, xmlFiles
) :
67 index
= self
.getUpdatedIndex(xmlFiles
)
73 printableLines
.append(('%s - %s / %s' % (l
[2], l
[3], l
[4]), l
[0]))
79 def getSongTitle(file) :
80 it
= ElementTree
.iterparse(file, ['start', 'end'])
82 title
= os
.path
.basename(file)
85 if el
.tag
== 'credit' :
87 if el
.tag
== 'credit-words' and creditFound
:
90 if el
.tag
== 'part-list' :
91 # au delà de ce tag : aucune chance de trouver un titre
96 def getSongMetadata(file) :
98 metadata
['title'] = FileOpenDialog
.getSongTitle(file).encode('iso-8859-1')
99 metadata
['mtime'] = str(os
.stat(file).st_mtime
)
100 metadata
['file'] = os
.path
.basename(file)
101 song
= musicXml2Song(file)
102 metadata
['distinctNotes'] = len(song
.distinctNotes
)
104 histo
= song
.intervalsHistogram
105 coeffInter
= reduce(lambda a
, b
: a
+ b
,
106 [abs(k
) * v
for k
, v
in histo
.items()])
108 totInter
= reduce(lambda a
, b
: a
+b
, histo
.values())
109 totInter
= totInter
- histo
.get(0, 0)
110 difficulty
= int(round(float(coeffInter
) / totInter
, 0))
111 metadata
['difficulty'] = difficulty
115 def getUpdatedIndex(self
, xmlFiles
) :
116 indexTxtPath
= os
.path
.join(self
.curdir
, INDEX_TXT
)
119 if not os
.path
.exists(indexTxtPath
) :
120 musicXmlFound
= False
121 tmp
= tempfile
.TemporaryFile(mode
='r+')
122 for file in xmlFiles
:
124 metadata
= FileOpenDialog
.getSongMetadata(file)
126 except ValueError, e
:
128 if e
.args
and e
.args
[0] == 'not a musicxml file' :
131 line
= '%(file)s\t%(mtime)s\t%(title)s\t%(distinctNotes)d\t%(difficulty)d\n' % metadata
137 indexFile
= open(indexTxtPath
, 'w')
138 indexFile
.write(tmp
.read())
143 indexTxt
= open(indexTxtPath
, 'r')
145 # check if index is up to date, and update entries if so.
146 for l
in filter(None, indexTxt
.readlines()) :
147 parts
= l
.split('\t')
148 fileBaseName
, modificationTime
= parts
[0], parts
[1]
149 filePath
= os
.path
.join(self
.curdir
, fileBaseName
)
151 if not os
.path
.exists(filePath
) :
154 indexedFiles
[fileBaseName
] = l
155 currentMtime
= str(os
.stat(filePath
).st_mtime
)
157 # check modification time missmatch
158 if currentMtime
!= modificationTime
:
160 metadata
= FileOpenDialog
.getSongMetadata(filePath
)
162 except ValueError, e
:
164 if e
.args
and e
.args
[0] == 'not a musicxml file' :
167 metadata
= FileOpenDialog
.getSongMetadata(filePath
)
168 line
= '%(file)s\t%(mtime)s\t%(title)s\t%(distinctNotes)d\t%(difficulty)d\n' % metadata
169 indexedFiles
[fileBaseName
] = line
171 # check for new files.
172 for file in xmlFiles
:
173 fileBaseName
= os
.path
.basename(file)
174 if not indexedFiles
.has_key(fileBaseName
) :
176 metadata
= FileOpenDialog
.getSongMetadata(filePath
)
178 except ValueError, e
:
180 if e
.args
and e
.args
[0] == 'not a musicxml file' :
183 metadata
= FileOpenDialog
.getSongMetadata(file)
184 line
= '%(file)s\t%(mtime)s\t%(title)s\t%(distinctNotes)d\t%(difficulty)d\n' % metadata
185 indexedFiles
[fileBaseName
] = line
187 # ok, the index is up to date !
189 index
= indexedFiles
.values()