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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
# -*- coding: utf-8 -*-
# Copyright 2004-2005 Joe Wreschnig, Michael Urman, IƱigo Serna
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation
#
# $Id: tagsfrompath.py 3634 2006-07-14 19:38:41Z piman $
import os
import sre
import gtk
import config
import const
import qltk
import util
from qltk._editpane import EditPane, FilterCheckButton
from qltk.wlw import WritingWindow
class TagsFromPattern(object):
def __init__(self, pattern):
self.compile(pattern)
def compile(self, pattern):
self.headers = []
self.slashes = len(pattern) - len(pattern.replace(os.path.sep,'')) + 1
self.pattern = None
# patterns look like <tagname> non regexy stuff <tagname> ...
pieces = sre.split(r'(<[A-Za-z0-9_]+>)', pattern)
override = { '<tracknumber>': r'\d\d?', '<discnumber>': r'\d\d??' }
for i, piece in enumerate(pieces):
if not piece: continue
if piece[0]+piece[-1] == '<>' and piece[1:-1].isalnum():
piece = piece.lower() # canonicalize to lowercase tag names
pieces[i] = '(?P%s%s)' % (piece, override.get(piece, '.+?'))
self.headers.append(piece[1:-1].encode("ascii", "replace"))
else:
pieces[i] = sre.escape(piece)
# some slight magic to anchor searches "nicely"
# nicely means if it starts with a <tag>, anchor with a /
# if it ends with a <tag>, anchor with .xxx$
# but if it's a <tagnumber>, don't bother as \d+ is sufficient
# and if it's not a tag, trust the user
if pattern.startswith('<') and not pattern.startswith('<tracknumber>')\
and not pattern.startswith('<discnumber>'):
pieces.insert(0, os.path.sep)
if pattern.endswith('>') and not pattern.endswith('<tracknumber>')\
and not pattern.endswith('<discnumber>'):
pieces.append(r'(?:\.\w+)$')
self.pattern = sre.compile(''.join(pieces))
def match(self, song):
if isinstance(song, dict):
song = util.fsdecode(song['~filename'])
# only match on the last n pieces of a filename, dictated by pattern
# this means no pattern may effectively cross a /, despite .* doing so
sep = os.path.sep
matchon = sep+sep.join(song.split(sep)[-self.slashes:])
match = self.pattern.search(matchon)
# dicts for all!
if match is None: return {}
else: return match.groupdict()
class UnderscoresToSpaces(FilterCheckButton):
_label = _("Replace _underscores with spaces")
_section = "tagsfrompath"
_key = "underscores"
_order = 1.0
def filter(self, tag, value): return value.replace("_", " ")
class TitleCase(FilterCheckButton):
_label = _("_Title-case tags")
_section = "tagsfrompath"
_key = "titlecase"
_order = 1.1
def filter(self, tag, value): return util.title(value)
class SplitTag(FilterCheckButton):
_label = _("Split into multiple _values")
_section = "tagsfrompath"
_key = "split"
_order = 1.2
def filter(self, tag, value):
spls = config.get("editing", "split_on").decode('utf-8', 'replace')
spls = spls.split()
return "\n".join(util.split_value(value, spls))
class TagsFromPath(EditPane):
title = _("Tags From Path")
FILTERS = [UnderscoresToSpaces, TitleCase, SplitTag]
def __init__(self, parent, library):
plugins = parent.plugins.TagsFromPathPlugins()
super(TagsFromPath, self).__init__(
const.TBP, const.TBP_EXAMPLES.split("\n"), plugins)
vbox = self.get_children()[2]
addreplace = gtk.combo_box_new_text()
addreplace.append_text(_("Tags replace existing ones"))
addreplace.append_text(_("Tags are added to existing ones"))
addreplace.set_active(config.getboolean("tagsfrompath", "add"))
addreplace.connect('changed', self.__add_changed)
vbox.pack_start(addreplace)
addreplace.show()
self.preview.connect_object('clicked', self.__preview, None)
parent.connect_object('changed', self.__class__.__preview, self)
# Save changes
self.save.connect_object('clicked', self.__save, addreplace, library)
def __add_changed(self, combo):
config.set("tagsfrompath", "add", str(bool(combo.get_active())))
def __preview(self, songs):
if songs is None:
songs = [row[0] for row in self.view.get_model()]
if songs: pattern_text = self.combo.child.get_text().decode("utf-8")
else: pattern_text = ""
try: pattern = TagsFromPattern(pattern_text)
except sre.error:
qltk.ErrorMessage(
self, _("Invalid pattern"),
_("The pattern\n\t<b>%s</b>\nis invalid. "
"Possibly it contains the same tag twice or "
"it has unbalanced brackets (< / >).")%(
util.escape(pattern_text))).run()
return
else:
if pattern_text:
self.combo.prepend_text(pattern_text)
self.combo.write(const.TBP)
invalid = []
for header in pattern.headers:
if not min([song.can_change(header) for song in songs]):
invalid.append(header)
if len(invalid) and songs:
if len(invalid) == 1:
title = _("Invalid tag")
msg = _("Invalid tag <b>%s</b>\n\nThe files currently"
" selected do not support editing this tag.")
else:
title = _("Invalid tags")
msg = _("Invalid tags <b>%s</b>\n\nThe files currently"
" selected do not support editing these tags.")
qltk.ErrorMessage(
self, title, msg % ", ".join(invalid)).run()
pattern = TagsFromPattern("")
self.view.set_model(None)
model = gtk.ListStore(
object, str, *([str] * len(pattern.headers)))
for col in self.view.get_columns():
self.view.remove_column(col)
col = gtk.TreeViewColumn(_('File'), gtk.CellRendererText(),
text=1)
col.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
self.view.append_column(col)
for i, header in enumerate(pattern.headers):
render = gtk.CellRendererText()
render.set_property('editable', True)
render.connect('edited', self.__row_edited, model, i + 2)
col = gtk.TreeViewColumn(header, render, text=i + 2)
col.set_sizing(gtk.TREE_VIEW_COLUMN_AUTOSIZE)
self.view.append_column(col)
for song in songs:
basename = util.fsdecode(song("~basename"))
row = [song, basename]
match = pattern.match(song)
for h in pattern.headers:
text = match.get(h, '')
for f in self.filters:
if f.active: text = f.filter(h, text)
if not song.multiple_values: text = u", ".join(text)
row.append(text)
model.append(row=row)
# save for last to potentially save time
if songs: self.view.set_model(model)
self.preview.set_sensitive(False)
self.save.set_sensitive(len(pattern.headers) > 0)
def __save(self, addreplace, library):
pattern_text = self.combo.child.get_text().decode('utf-8')
pattern = TagsFromPattern(pattern_text)
model = self.view.get_model()
add = bool(addreplace.get_active())
win = WritingWindow(self, len(model))
was_changed = []
for row in model:
song = row[0]
changed = False
if not song.valid() and not qltk.ConfirmAction(
self, _("Tag may not be accurate"),
_("<b>%s</b> changed while the program was running. "
"Saving without refreshing your library may "
"overwrite other changes to the song.\n\n"
"Save this song anyway?") %(
util.escape(util.fsdecode(song("~basename"))))
).run():
break
for i, h in enumerate(pattern.headers):
if row[i + 2]:
text = row[i + 2].decode("utf-8")
if not add or h not in song or not song.multiple_values:
song[h] = text
changed = True
else:
for val in text.split("\n"):
if val not in song.list(h):
song.add(h, val)
changed = True
if changed:
try: song.write()
except:
qltk.ErrorMessage(
self, _("Unable to edit song"),
_("Saving <b>%s</b> failed. The file "
"may be read-only, corrupted, or you "
"do not have permission to edit it.")%(
util.escape(util.fsdecode(song('~basename'))))
).run()
library.reload(song)
break
was_changed.append(song)
if win.step(): break
win.destroy()
library.changed(was_changed)
self.save.set_sensitive(False)
def __row_edited(self, renderer, path, new, model, colnum):
row = model[path]
if row[colnum] != new:
row[colnum] = new
self.preview.set_sensitive(True)
|