-
Notifications
You must be signed in to change notification settings - Fork 0
/
coll_diff
187 lines (150 loc) · 6.47 KB
/
coll_diff
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#! /usr/bin/python
# vim: set fileencoding=utf-8 :
# (c) 2009 Marcos Dione <mdione@grulic.org.ar>
# This file is part of satyr.
# satyr is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# satyr is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with satyr. If not, see <http://www.gnu.org/licenses/>.
# qt/kde related
from PyKDE4.kdecore import KCmdLineArgs, KAboutData, i18n, ki18n
from PyKDE4.kdecore import KCmdLineOptions
from PyKDE4.kdeui import KApplication, KMainWindow
from PyQt4.QtCore import Qt
from PyQt4 import uic
# std python
import sys, os.path
# local
# from satyr.song import Song
from satyr.collection import Collection
from satyr.song import Song
from satyr import utils
def createApp (args=sys.argv):
#########################################
# all the bureaucratic init of a KDE App
# the appName must not contain any chars besides a-zA-Z0-9_
# because KMainWindowPrivate::polish() calls QDBusConnection::sessionBus().registerObject()
# see QDBusUtil::isValidCharacterNoDash()
appName = "coll_diff"
catalog = ""
programName = ki18n ("Collection Diff") #ki18n required here
version = "0.1a"
description = ki18n ("Once I told Amarok to organize my Colection and left a mess. This program will let me fix that.") #ki18n required here
license = KAboutData.License_GPL
copyright = ki18n ("(c) 2009 Marcos Dione") #ki18n required here
text = ki18n ("none") #ki18n required here
homePage = "http://savannah.nongnu.org/projects/satyr/"
bugEmail = "mdione@grulic.org.ar"
aboutData = KAboutData (appName, catalog, programName, version, description,
license, copyright, text, homePage, bugEmail)
# ki18n required for first two addAuthor () arguments
aboutData.addAuthor (ki18n ("Marcos Dione"), ki18n ("design and implementation"))
KCmdLineArgs.init (args, aboutData)
options= KCmdLineOptions ()
options.add ("+path", ki18n ("paths to your music collections"))
KCmdLineArgs.addCmdLineOptions (options)
app= KApplication ()
args= KCmdLineArgs.parsedArgs ()
return app, args
class CollDiffApp (KMainWindow):
def __init__ (self):
self.app, args= createApp ()
KMainWindow.__init__ (self)
self.leftRoot= utils.qstring2path (args.arg (0))
self.left= Collection (self.app, self.leftRoot)
self.left.scan (loadMetadata=True)
self.leftSong= None
if args.count ()==2:
self.left.scanFinished.connect (self.scanRight)
self.rightRoot= utils.qstring2path (args.arg (1))
self.right= Collection (self.app, self.rightRoot)
self.right.scanFinished.connect (self.enable)
else:
self.left.scanFinished.connect (self.enable)
self.right= self.left
UIMainWindow, _= uic.loadUiType ('coll_diff.ui')
self.ui= UIMainWindow ()
self.ui.setupUi (self)
self.ui.leftView.currentRowChanged.connect (self.findMatches)
self.ui.rightView.currentRowChanged.connect (self.showMatchingSongs)
def scanRight (self):
self.right.scan (loadMetadata=True)
def enable (self):
self.ui.leftView.setEnabled (True)
self.ui.rightView.setEnabled (True)
# fill left list
for leftSong in self.left.songs:
self.ui.leftView.addItem (leftSong.relPath ())
self.ui.leftView.setFocus ()
def compare (self, leftSong, rightSong):
attrPoints= dict (title=0.2, trackno=0.05, album=0.1, year=0.05, autor=0.1, length=0.3)
match= 0.0
matches= []
# first try to match tags
for tag, value in attrPoints.items ():
try:
leftTag= leftSong[tag]
rightTag= rightSong[tag]
except AttributeError:
pass
else:
if leftTag!=u'' and rightTag!=u'' and leftTag!=0 and rightTag!=0 and leftTag==rightTag:
match+= value
matches.append (tag)
# next, filename
leftFN= os.path.basename (leftSong.filepath)
rightFN= os.path.basename (rightSong.filepath)
if leftFN==rightFN:
match+= 0.20
matches.append ('filename')
else:
# last chance: basename
leftBN= leftFN.rsplit ('.', 1)
rightBN= rightFN.rsplit ('.', 1)
if leftBN==rightBN:
match+= 0.15
matches.append ('basename')
return match, matches
def findMatches (self, index):
# yes, this is gonna be O(n) or worse
if index>=0:
self.leftSong= self.left.songs[index]
# clear the other views
self.ui.matchesView.clear ()
self.ui.rightView.clear ()
for rightSong in self.right.songs:
add= False
match, matches= self.compare (self.leftSong, rightSong)
# yes, this could be a big or. sue me
if match>0.69 and self.ui.sure.isChecked ():
add= True
elif match>0.49 and self.ui.probable.isChecked ():
add= True
elif match>0.26 and self.ui.maybe.isChecked ():
add= True
elif match>0.0 and self.ui.all.isChecked ():
add= True
if add:
# add to the right view
self.ui.rightView.addItem (rightSong.relPath ())
def showMatchingSongs (self, index):
if index>=0 and self.leftSong is not None:
self.ui.matchesView.clear ()
path= utils.qstring2path (self.ui.rightView.currentItem ().data (Qt.DisplayRole).toString ())
rightSong= Song (None, self.rightRoot+'/'+path)
match, matches= self.compare (self.leftSong, rightSong)
for match in matches:
self.ui.matchesView.addItem (match)
def main ():
mw= CollDiffApp ()
mw.show ()
mw.app.exec_ ()
if __name__=='__main__':
main ()
# end