/usr/share/pyshared/quodlibet/util/fmps.py is in exfalso 3.0.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | # -*- coding: utf-8 -*-
# Copyright 2010 Christoph Reiter
#
# 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.
# Free Media Player Specifications 1.1 by Jeff Mitchell
# http://freedesktop.org/wiki/Specifications/free-media-player-specs
FMPS_NOTHING = u"FMPS_NOTHING"
LIST_SEPERATOR = u";;"
ENTRY_SEPERATOR = u"::"
def _split_left(val, sep):
"""Split a string by a delimiter which can be escaped by \\"""
result = []
temp = u""
escaped = False
index = 0
left = True
for c in val:
left = False
temp += c
if c == sep[index] and not escaped:
index += 1
else:
index = 0
if c == u"\\":
escaped ^= True
else:
escaped = False
if index >= len(sep):
left = True
index = 0
result.append(temp[:-len(sep)])
temp = u""
if temp or left:
result.append(temp)
return result
def _split(val, sep):
"""Split and filter invalid segments.
Entries seperated by ::: for example need to be set invalid
because they could be split in two ways.
"""
invalid = []
inval = []
valid = []
vals = _split_left(val, sep)
c = False
# Look if a segment starts with :/;
# and mark it and the next one as invalid (except the last one)
for i, v in enumerate(vals[-1::-1]):
new_c = v.startswith(sep[0])
excp = i == len(vals) - 1
if c or (new_c and not excp):
inval.insert(0, v)
else:
if inval:
invalid.insert(0, sep.join(inval))
inval = []
valid.insert(0, v)
c = new_c
if inval:
invalid.insert(0, sep.join(inval))
return invalid, valid
def _unescape(val):
return val.replace(
ur"\;", u";").replace(ur"\:", u":").replace(ur"\\", u"\\")
def _escape(val):
return val.replace(
u"\\", ur"\\").replace(u";", ur"\;").replace(u":", ur"\:")
def _escape_inval(val):
if val.startswith(";"):
return u"\\" + val
return val
class FmpsValue(object):
_data = None
def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, repr(self._data))
def native(self):
return self._data
def __unicode__(self):
return unicode(self._data)
def __str__(self):
return str(self._data)
class FmpsFloat(FmpsValue):
def __init__(self, data):
if not isinstance(data, basestring):
self._validate(data)
self._data = unicode(round(float(data), 6))
else:
data = unicode(data)
self._validate(data)
self._data = data
def _validate(self, data):
return float(data)
def native(self):
return float(self._data)
class FmpsPositiveFloat(FmpsFloat):
def _validate(self, data):
num = super(FmpsPositiveFloat, self)._validate(data)
if num < 0 or (not isinstance(data, basestring)
and num > 4294967294.999999):
raise ValueError("Value not in range.")
return num
class FmpsRatingFloat(FmpsFloat):
def _validate(self, data):
num = super(FmpsRatingFloat, self)._validate(data)
if not 0 <= num <= 1:
raise ValueError("Value not in range.")
return num
class FmpsPositiveIntegerFloat(FmpsPositiveFloat):
def _validate(self, data):
num = super(FmpsPositiveIntegerFloat, self)._validate(data)
if int(num) != num:
raise ValueError("Value not an integer.")
return num
def native(self):
return int(super(FmpsPositiveIntegerFloat, self).native())
class FmpsText(FmpsValue):
def __init__(self, data):
self._data = unicode(data)
class FmpsInvalidValue(FmpsText):
pass
class FmpsDict(object):
_kind = None
def __init__(self, data=None):
self.__dict = {}
self.__invalid = []
if data:
self.__load(data)
def __load(self, data):
if not isinstance(data, (list, tuple)):
data = [data]
data = map(unicode, data)
vals = []
for seg in data:
i, v = _split(seg, LIST_SEPERATOR)
vals.extend(v)
self.__invalid.extend(map(_escape_inval, i))
for val in vals:
inval, fields = _split(val, ENTRY_SEPERATOR)
if len(fields) != 2 or inval:
self.__invalid.append(_escape_inval(val))
continue
key, value = map(_unescape, fields)
try:
value = self._kind(value)
except ValueError:
value = FmpsInvalidValue(value)
if key in self.__dict:
self.__dict[key].append(value)
else:
self.__dict[key] = [value]
def iterkeys(self):
return self.__dict.iterkeys()
keys = lambda self: list(self.iterkeys())
def iteritems(self):
for key, values in self.__dict.iteritems():
for value in values:
if not isinstance(value, FmpsInvalidValue):
yield(key, value.native())
items = lambda s: list(s.iteritems())
def get_all(self, key):
if key in self.__dict:
return [f.native() for f in self.__dict[key]
if not isinstance(f, FmpsInvalidValue)]
return []
def remove_all(self, key):
if key in self.__dict:
del self.__dict[key]
def set_all(self, key, value):
key = unicode(key)
if not isinstance(value, (list, tuple)):
value = [value]
value = map(self._kind, value)
self.remove_all(key)
if key in self.__dict:
self.__dict[key].extend(value)
else:
self.__dict[key] = value
def append(self, key, value):
self.extend(key, [value])
def extend(self, key, values):
key = unicode(key)
values = map(self._kind, values)
if key in self.__dict:
self.__dict[key].extend(values)
else:
self.__dict[key] = values
def to_data(self):
fields = []
for key, values in self.__dict.iteritems():
values = map(_escape, map(unicode, values))
key = _escape(key)
for val in values:
fields.append(ENTRY_SEPERATOR.join([key, val]))
return LIST_SEPERATOR.join(fields + filter(None, self.__invalid))
def __repr__(self):
return "%s(%s, invalid=%s)" % (
self.__class__.__name__, repr(self.__dict), repr(self.__invalid))
class FmpsNamespaceDict(object):
_kind = None
def __init__(self, data=None):
self.__dict = {}
self.__invalid = []
if data:
self.__load(data)
def __load(self, data):
if not isinstance(data, (list, tuple)):
data = [data]
data = map(unicode, data)
vals = []
for seg in data:
i, v = _split(seg, LIST_SEPERATOR)
vals.extend(v)
self.__invalid.extend(map(_escape_inval, i))
for val in vals:
inval, fields = _split(val, ENTRY_SEPERATOR)
if len(fields) != 3 or inval:
self.__invalid.append(_escape_inval(val))
continue
namespace, key, value = fields
if not namespace or not key:
self.__invalid.append(val)
continue
namespace, key, value = map(_unescape, (namespace, key, value))
try:
value = self._kind(value)
except ValueError:
value = FmpsInvalidValue(value)
if namespace in self.__dict:
if key in self.__dict[namespace]:
self.__dict[namespace][key].append(value)
else:
self.__dict[namespace][key] = [value]
else:
self.__dict[namespace] = {key: [value]}
def get_all(self, namespace, key=None):
"""Will return a list of values if key is given, else a dict"""
if namespace in self.__dict:
if key is not None:
if key in self.__dict[namespace]:
return [f.native() for f in self.__dict[namespace][key]
if not isinstance(f, FmpsInvalidValue)]
else:
return []
else:
new_dict = {}
for key, values in self.__dict[namespace].iteritems():
new_dict[key] = [f.native() for f in values
if not isinstance(f, FmpsInvalidValue)]
return new_dict
else:
if key is not None:
return []
return {}
def remove_all(self, namespace, key=None):
if namespace in self.__dict:
if key is not None:
if key in self.__dict[namespace]:
del self.__dict[namespace][key]
if not self.__dict[namespace]:
del self.__dict[namespace]
else:
del self.__dict[namespace]
def set_all(self, namespace, key, value):
"""value can be a list or a singe value, or a dict
in case key is None"""
if not namespace or (key is not None and not key):
raise ValueError("No empty namespace/key allowed.")
if key is None and not isinstance(value, dict):
raise ValueError("You have to pass a dict in case key is None.")
if key is not None and isinstance(value, dict):
raise ValueError("You can only pass a dict in case key is None.")
namespace = unicode(namespace)
key = key is not None and unicode(key)
if isinstance(value, dict):
value = dict(value)
for key in value.iterkeys():
if not isinstance(value[key], (list, tuple)):
value[key] = [value[key]]
value[key] = map(self._kind, value[key])
self.__dict[namespace] = dict(value)
return
if not isinstance(value, (list, tuple)):
value = [value]
value = map(self._kind, value)
if namespace in self.__dict:
if key in self.__dict[namespace]:
self.__dict[namespace][key] = value
else:
self.__dict[namespace] = {key: value}
def append(self, namespace, key, value):
self.extend(namespace, key, [value])
def extend(self, namespace, key, values):
if not namespace or not key:
raise ValueError("No empty namespace/key allowed.")
namespace = unicode(namespace)
key = unicode(key)
values = map(self._kind, values)
if namespace in self.__dict:
if key in self.__dict[namespace]:
self.__dict[namespace][key].extend(values)
else:
self.__dict[namespace][key] = values
else:
self.__dict[namespace] = {key: values}
def iterkeys(self):
return self.__dict.iterkeys()
keys = lambda self: list(self.iterkeys())
def iteritems(self):
for namespace, sub in self.__dict.iteritems():
for key, values in sub.iteritems():
for val in values:
yield (namespace, key, val.native())
items = lambda self: list(self.iteritems())
def to_data(self):
fields = []
for namespace, sub in self.__dict.iteritems():
namespace = _escape(namespace)
for key, values in sub.iteritems():
key = _escape(key)
values = map(_escape, map(unicode, values))
for val in values:
fields.append(ENTRY_SEPERATOR.join([namespace, key, val]))
return LIST_SEPERATOR.join(fields + filter(None, self.__invalid))
def __repr__(self):
return "%s(%s, invalid=%s)" % (
self.__class__.__name__, repr(self.__dict), repr(self.__invalid))
class FmpsNoEmptyKeyDict(FmpsDict):
def set_all(self, key, values):
if not key:
raise ValueError("No empty key allowed.")
super(FmpsNoEmptyKeyDict, self).set_all(key, values)
def extend(self, key, values):
if not key:
raise ValueError("No empty key allowed.")
super(FmpsNoEmptyKeyDict, self).extend(key, values)
#All simple Formats
class Rating(FmpsRatingFloat):
pass
class Playcount(FmpsPositiveIntegerFloat):
pass
class Lyrics(FmpsText):
pass
#All Dicts
class RatingUser(FmpsNoEmptyKeyDict):
_kind = FmpsRatingFloat
class PlaycountUser(FmpsNoEmptyKeyDict):
_kind = FmpsPositiveIntegerFloat
class Performers(FmpsDict):
_kind = FmpsText
class LyricsSources(FmpsDict):
_kind = FmpsText
#All NamespaceDicts
class RatingCritic(FmpsNamespaceDict):
_kind = FmpsRatingFloat
class RatingAlgorithm(FmpsNamespaceDict):
_kind = FmpsRatingFloat
class PlaycountAlgorithm(FmpsNamespaceDict):
_kind = FmpsPositiveFloat
# AlbumsCompilations needs checks for unique
# Application(case sensitive)+Type(case insensitive)+Identifier(case sensitive)
# and should check if the keys are album/compilation only...
# but I don't care atm so that's left to the user.
class AlbumsCompilations(FmpsNamespaceDict):
_kind = FmpsText
|