eyeD3 / src / eyed3 / id3 / tag.py

   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
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
# -*- coding: utf-8 -*-
################################################################################
#  Copyright (C) 2007-2012  Travis Shirk <travis@pobox.com>
#
#  This program 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.
#
#  This program 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 this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
################################################################################
import types, sys, string, re, os, shutil, types, tempfile

from ..utils import requireUnicode, chunkCopy
from .. import core
from . import (ID3_ANY_VERSION, ID3_V1, ID3_V1_0, ID3_V1_1,
               ID3_V2, ID3_V2_2, ID3_V2_3, ID3_V2_4, versionToString)
from . import DEFAULT_LANG
from . import Genre
from . import frames
from .headers import TagHeader, ExtendedTagHeader


import logging
log = logging.getLogger(__name__)

from .. import Exception as BaseException
class TagException(BaseException):
   pass

ID3_V1_COMMENT_DESC = u"ID3v1.x Comment"
DEFAULT_PADDING = 1024

class Tag(core.Tag):
    def __init__(self):
        core.Tag.__init__(self)
        self.clear()

    def clear(self):
        ## ID3 tag header
        self.header = TagHeader()
        ## Optional extended header in v2 tags.
        self.extended_header = ExtendedTagHeader()
        ## Contains the tag's frames. ID3v1 fields are read and converted
        #  the the corresponding v2 frame.  
        self.frame_set = frames.FrameSet()
        self._comments = CommentsAccessor(self.frame_set)
        self._images = ImagesAccessor(self.frame_set)
        self._lyrics = LyricsAccessor(self.frame_set)
        self._objects = ObjectsAccessor(self.frame_set)
        self._privates = PrivatesAccessor(self.frame_set)
        self._user_texts = UserTextsAccessor(self.frame_set)
        self._unique_file_ids = UniqueFileIdAccessor(self.frame_set)
        self._user_urls = UserUrlsAccessor(self.frame_set)
        self._chapters = ChaptersAccessor(self.frame_set)
        self._popularities = PopularitiesAccessor(self.frame_set)
        self.file_info = None

    def parse(self, fileobj, version=ID3_ANY_VERSION):
        assert(fileobj)
        self.clear()
        version = version or ID3_ANY_VERSION

        close_file = False
        if type(fileobj) is types.FileType:
            filename = fileobj.name
        elif type(fileobj) in types.StringTypes:
            filename = fileobj
            fileobj = file(filename, "rb")
            close_file = True
        else:
            raise ValueError("Invalid type: %s" % str(type(fileobj)))

        self.file_info = FileInfo(filename)

        try:
            tag_found = False
            padding = 0
            if version[0] & 2:
                tag_found, padding = self._loadV2Tag(fileobj)

            if not tag_found and version[0] & 1:
                tag_found, padding = self._loadV1Tag(fileobj)
                if tag_found:
                    self.extended_header = None

            if tag_found and self.isV2():
                self.file_info.tag_size = (TagHeader.SIZE +
                                           self.header.tag_size)
        finally:
            if close_file:
                fileobj.close()

        return tag_found

    ## returns (tag_found, padding_len)
    def _loadV2Tag(self, fp):
        padding = 0
        # Look for a tag and if found load it.
        if not self.header.parse(fp):
            return (False, 0)

        # Read the extended header if present.
        if self.header.extended:
            self.extended_header.parse(fp, self.header.version)

        # Header is definitely there so at least one frame *must* follow.
        padding = self.frame_set.parse(fp, self.header,
                                       self.extended_header)

        log.debug("Tag contains %d bytes of padding." % padding)
        return (True, padding)

    def _loadV1Tag(self, fp):
        v1_enc = "latin1"

        # Seek to the end of the file where all v1x tags are written.
        # v1.x tags are 128 bytes min and max
        fp.seek(0, 2)
        if fp.tell() < 128:
            return (False, 0)
        fp.seek(-128, 2)
        tag_data = fp.read(128)

        if tag_data[0:3] != "TAG":
            return (False, 0)

        log.debug("Located ID3 v1 tag")
        # v1.0 is implied until a v1.1 feature is recognized.
        self.version = ID3_V1_0

        STRIP_CHARS = string.whitespace + "\x00"
        title = tag_data[3:33].strip(STRIP_CHARS)
        log.debug("Tite: %s" % title)
        if title:
            self.title = unicode(title, v1_enc)

        artist = tag_data[33:63].strip(STRIP_CHARS)
        log.debug("Artist: %s" % artist)
        if artist:
            self.artist = unicode(artist, v1_enc)


        album = tag_data[63:93].strip(STRIP_CHARS)
        log.debug("Album: %s" % album)
        if album:
            self.album = unicode(album, v1_enc)

        year = tag_data[93:97].strip(STRIP_CHARS)
        log.debug("Year: %s" % year)
        try:
            if year and int(year):
                # Values here typically mean the year of release
                self.release_date = int(year)
        except ValueError:
            # Bogus year strings.
            log.warn("ID3v1.x tag contains invalid year: %s" % year)
            pass

        # Can't use STRIP_CHARS here, since the final byte is numeric
        comment = tag_data[97:127].rstrip("\x00")
        # Track numbers stuffed in the comment field is what makes v1.1
        if comment:
            if (len(comment) >= 2 and
                    comment[-2] == "\x00" and comment[-1] != "\x00"):
                log.debug("Track Num found, setting version to v1.1")
                self.version = ID3_V1_1

                track = ord(comment[-1])
                self.track_num = (track, None)
                log.debug("Track: " + str(track))
                comment = comment[:-2].strip(STRIP_CHARS)

            # There may only have been a track #
            if comment:
                log.debug("Comment: %s" % comment)
                self.comments.set(unicode(comment, v1_enc), ID3_V1_COMMENT_DESC)

        genre = ord(tag_data[127:128])
        log.debug("Genre ID: %d" % genre)
        try:
            self.genre = genre
        except ValueError as ex:
            log.warning(ex)
            self.genre = None

        return (True, 0)

    @property
    def version(self):
        return self.header.version

    @version.setter
    def version(self, v):
        self.header.version = v

    ## Test ID3 major version for v1.x
    def isV1(self):
        return self.header.major_version == 1
    ## Test ID3 major version for v2.x
    def isV2(self):
        return self.header.major_version == 2

    @requireUnicode(2)
    def setTextFrame(self, fid, txt):
        if not fid.startswith("T") or fid.startswith("TX"):
            raise ValueError("Invalid frame-id for text frame")

        if not txt and self.frame_set[fid]:
            del self.frame_set[fid]
        else:
            self.frame_set.setTextFrame(fid, txt)

    def getTextFrame(self, fid):
        if not fid.startswith("T") or fid.startswith("TX"):
            raise ValueError("Invalid frame-id for text frame")
        f = self.frame_set[fid]
        return f[0].text if f else None

    @requireUnicode(1)
    def _setArtist(self, val):
        self.setTextFrame(frames.ARTIST_FID, val)
    def _getArtist(self):
        return self.getTextFrame(frames.ARTIST_FID)

    @requireUnicode(1)
    def _setAlbum(self, val):
        self.setTextFrame(frames.ALBUM_FID, val)
    def _getAlbum(self):
        return self.getTextFrame(frames.ALBUM_FID)

    @requireUnicode(1)
    def _setTitle(self, val):
        self.setTextFrame(frames.TITLE_FID, val)
    def _getTitle(self):
        return self.getTextFrame(frames.TITLE_FID)

    def _setTrackNum(self, val):
        self._setNum(frames.TRACKNUM_FID, val)

    def _getTrackNum(self):
        return self._splitNum(frames.TRACKNUM_FID)

    def _splitNum(self, fid):
        f = self.frame_set[fid]
        first, second = None, None
        if f and f[0].text:
            n = f[0].text.split('/')
            try:
                first = int(n[0])
                second = int(n[1]) if len(n) == 2 else None
            except ValueError as ex:
                log.warning(str(ex))
        return (first, second)

    def _setNum(self, fid, val):
        if type(val) is tuple:
            tn, tt = val
        elif type(val) is int:
            tn, tt = val, None
        elif val is None:
            tn, tt = None, None

        n = (tn, tt)

        if n[0] == None and n[1] == None:
            if self.frame_set[fid]:
                del self.frame_set[fid]
            return

        total_str = ""
        if n[1] != None:
            if n[1] >= 0 and n[1] <= 9:
                total_str = "0" + str(n[1])
            else:
                total_str = str(n[1])

        t = n[0] if n[0] else 0
        track_str = str(t)

        # Pad with zeros according to how large the total count is.
        if len(track_str) == 1:
            track_str = "0" + track_str
        if len(track_str) < len(total_str):
            track_str = ("0" * (len(total_str) - len(track_str))) + track_str

        final_str = ""
        if track_str and total_str:
            final_str = "%s/%s" % (track_str, total_str)
        elif track_str and not total_str:
            final_str = track_str

        self.frame_set.setTextFrame(fid, unicode(final_str))

    @property
    def comments(self):
        return self._comments

    def _getBpm(self):
        bpm = None
        if frames.BPM_FID in self.frame_set:
            bpm_str = self.frame_set[frames.BPM_FID][0].text or u"0"
            try:
                # Round floats since the spec says this is an integer
                bpm = int(float(bpm_str) + 0.5)
            except ValueError as ex:
                log.warning(ex)
        return bpm

    def _setBpm(self, bpm):
       assert(bpm >= 0)
       self.setTextFrame(frames.BPM_FID, unicode(str(bpm)))

    bpm = property(_getBpm, _setBpm)

    @property
    def play_count(self):
        if frames.PLAYCOUNT_FID in self.frame_set:
            pc = self.frame_set[frames.PLAYCOUNT_FID][0]
            assert(type(pc.count) in (int,))
            return pc.count
        else:
            return None

    @play_count.setter
    def play_count(self, count):
        if count is None:
            del self.frame_set[frames.PLAYCOUNT_FID]
            return

        if count < 0:
            raise ValueError("Invalid play count value: %d" % count)

        if self.frame_set[frames.PLAYCOUNT_FID]:
            pc = self.frame_set[frames.PLAYCOUNT_FID][0]
            pc.count = count
        else:
            self.frame_set[frames.PLAYCOUNT_FID] = \
                    frames.PlayCountFrame(count=count)

    def _getPublisher(self):
        if frames.PUBLISHER_FID in self.frame_set:
            pub = self.frame_set[frames.PUBLISHER_FID]
            return pub[0].text
        else:
            return None

    @requireUnicode(1)
    def _setPublisher(self, p):
        self.setTextFrame(frames.PUBLISHER_FID, p)

    publisher = property(_getPublisher, _setPublisher)

    @property
    def cd_id(self):
        if frames.CDID_FID in self.frame_set:
            return self.frame_set[frames.CDID_FID][0].toc
        else:
            return None

    @cd_id.setter
    def cd_id(self, toc):
        if len(toc) > 804:
            raise ValueError("CD identifier table of contents can be no "
                             "greater than 804 bytes")

        if self.frame_set[frames.CDID_FID]:
            cdid = self.frame_set[frames.CDID_FID][0]
            cdid.toc = str(toc)
        else:
            self.frame_set[frames.CDID_FID] = \
                    frames.MusicCDIdFrame(toc=toc)

    @property
    def toc(self):
        for toc_frame in self.frame_set[frames.TOC_FID]:
            if toc_frame.toplevel:
                return toc_frame
        return None

    @property
    def images(self):
        return self._images

    def _getEncodingDate(self):
        return self._getDate("TDEN")
    def _setEncodingDate(self, date):
        self._setDate("TDEN", date)
    encoding_date = property(_getEncodingDate, _setEncodingDate)

    @property
    def best_release_date(self):
        return (self.release_date or
                self.recording_date or
                self.original_release_date)

    def _getReleaseDate(self):
        return self._getDate("TDRL")
    def _setReleaseDate(self, date):
        self._setDate("TDRL", date)
    release_date = property(_getReleaseDate, _setReleaseDate)

    def _getOrigReleaseDate(self):
        return self._getDate("TDOR") or self._v23OrignalReleaseDate()
    def _setOrigReleaseDate(self, date):
        self._setDate("TDOR", date)
    original_release_date = property(_getOrigReleaseDate, _setOrigReleaseDate)

    def _getRecordingDate(self):
        return self._getDate("TDRC") or self._v23RecordingDate()
    def _setRecordingDate(self, date):
        self._setDate("TDRC", date)
    recording_date = property(_getRecordingDate, _setRecordingDate)

    def _v23RecordingDate(self):
        # v2.3 TYER (yyyy), TDAT (DDMM), TIME (HHmm)
        date = None
        try:
            date_str = ""
            if self.frame_set["TYER"]:
                date_str = self.frame_set["TYER"][0].text.encode("latin1")
                date = core.Date.parse(date_str)
            if self.frame_set["TDAT"]:
                text = self.frame_set["TDAT"][0].text.encode("latin1")
                date_str += "-%s-%s" % (text[2:], text[:2])
                date = core.Date.parse(date_str)
            if self.frame_set["TIME"]:
                text = self.frame_set["TIME"][0].text.encode("latin1")
                date_str += "T%s:%s" % (text[:2], text[2:])
                date = core.Date.parse(date_str)
        except ValueError as ex:
            log.warning("Invalid v2.3 TYER, TDAT, or TIME frame: %s" % ex)

        return date

    def _v23OrignalReleaseDate(self):
        # v2.3 TORY (yyyy)
        date = None
        try:
            if self.frame_set["TORY"]:
                # YYYY
                date_str = self.frame_set["TORY"][0].text.encode("latin1")
                date = core.Date.parse(date_str)
        except ValueError as ex:
            log.warning("Invalid v2.3 TORY frame: %s" % ex)

        return date

    def _getTaggingDate(self):
        return self._getDate("TDTG")
    def _setTaggingDate(self, date):
        self._setDate("TDTG", date)
    tagging_date = property(_getTaggingDate, _setTaggingDate)

    def _setDate(self, fid, date):
        assert(fid in frames.DATE_FIDS)

        if date is None:
            try:
                del self.frame_set[fid]
            except KeyError:
                pass
            finally:
                return

        # Convert to ISO format which is what FrameSet wants.
        date_type = type(date)
        if date_type is int:
            # The integer year
            date = core.Date(date)
        elif date_type in types.StringTypes:
            date = core.Date.parse(date)
        elif not isinstance(date, core.Date):
            raise TypeError("Invalid type: %s" % str(type(date)))

        date_text = unicode(str(date))
        if fid in self.frame_set:
            self.frame_set[fid][0].date = date
        else:
            self.frame_set[fid] = frames.DateFrame(fid, date=date_text)

    def _getDate(self, fid):
        if fid in self.frame_set:
            return self.frame_set[fid][0].date
        else:
            return None

    @property
    def lyrics(self):
        return self._lyrics

    @property
    def disc_num(self):
        return self._splitNum(frames.DISCNUM_FID)
    @disc_num.setter
    def disc_num(self, val):
        self._setNum(frames.DISCNUM_FID, val)

    @property
    def objects(self):
        return self._objects

    @property
    def privates(self):
        return self._privates

    @property
    def popularities(self):
        return self._popularities

    def _getGenre(self):
        f = self.frame_set[frames.GENRE_FID]
        if f and f[0].text:
            return Genre.parse(f[0].text)
        else:
            return None
    def _setGenre(self, g):
        '''
        Set the genre. Four types are accepted for the ``g`` argument.
        A Genre object, an acceptable (see Genre.parse) genre string,
        or an integer genre ID all will set the value. A value of None will
        remove the genre.'''
        if g is None:
            if self.frame_set[frames.GENRE_FID]:
                del self.frame_set[frames.GENRE_FID]
            return

        if isinstance(g, unicode):
            g = Genre.parse(g)
        elif isinstance(g, int):
            g = Genre(id=g)
        elif not isinstance(g, Genre):
            raise TypeError("Invalid genre data type: %s" % str(type(g)))
        self.frame_set.setTextFrame(frames.GENRE_FID, unicode(g))
    genre = property(_getGenre, _setGenre)

    @property
    def user_text_frames(self):
        return self._user_texts

    def _setUrlFrame(self, fid, url):
        if fid not in frames.URL_FIDS:
            raise ValueError("Invalid URL frame-id")

        if self.frame_set[fid]:
            if not url:
                del self.frame_set[fid]
            else:
                self.frame_set[fid][0].url = url
        else:
            self.frame_set[fid] = frames.UrlFrame(fid, url)

    def _getUrlFrame(self, fid):
        if fid not in frames.URL_FIDS:
            raise ValueError("Invalid URL frame-id")
        f = self.frame_set[fid]
        return f[0].url if f else None

    @property
    def commercial_url(self):
        return self._getUrlFrame(frames.URL_COMMERCIAL_FID)
    @commercial_url.setter
    def commercial_url(self, url):
        self._setUrlFrame(frames.URL_COMMERCIAL_FID, url)

    @property
    def copyright_url(self):
        return self._getUrlFrame(frames.URL_COPYRIGHT_FID)
    @copyright_url.setter
    def copyright_url(self, url):
        self._setUrlFrame(frames.URL_COPYRIGHT_FID, url)

    @property
    def audio_file_url(self):
        return self._getUrlFrame(frames.URL_AUDIOFILE_FID)
    @audio_file_url.setter
    def audio_file_url(self, url):
        self._setUrlFrame(frames.URL_AUDIOFILE_FID, url)

    @property
    def audio_source_url(self):
        return self._getUrlFrame(frames.URL_AUDIOSRC_FID)
    @audio_source_url.setter
    def audio_source_url(self, url):
        self._setUrlFrame(frames.URL_AUDIOSRC_FID, url)

    @property
    def artist_url(self):
        return self._getUrlFrame(frames.URL_ARTIST_FID)
    @artist_url.setter
    def artist_url(self, url):
        self._setUrlFrame(frames.URL_ARTIST_FID, url)

    @property
    def internet_radio_url(self):
        return self._getUrlFrame(frames.URL_INET_RADIO_FID)
    @internet_radio_url.setter
    def internet_radio_url(self, url):
        self._setUrlFrame(frames.URL_INET_RADIO_FID, url)

    @property
    def payment_url(self):
        return self._getUrlFrame(frames.URL_PAYMENT_FID)
    @payment_url.setter
    def payment_url(self, url):
        self._setUrlFrame(frames.URL_PAYMENT_FID, url)

    @property
    def publisher_url(self):
        return self._getUrlFrame(frames.URL_PUBLISHER_FID)
    @publisher_url.setter
    def publisher_url(self, url):
        self._setUrlFrame(frames.URL_PUBLISHER_FID, url)

    @property
    def user_url_frames(self):
        return self._user_urls

    @property
    def unique_file_ids(self):
        return self._unique_file_ids

    @property
    def terms_of_use(self):
        if self.frame_set[frames.TOS_FID]:
            return self.frame_set[frames.TOS_FID][0].text

    @terms_of_use.setter
    @requireUnicode(1)
    def terms_of_use(self, tos):
        if self.frame_set[frames.TOS_FID]:
            self.frame_set[frames.TOS_FID][0].text = tos
        else:
            self.frame_set[frames.TOS_FID] = frames.TermsOfUseFrame(text=tos)

    def save(self, filename=None, version=None, encoding=None, backup=False):
        if not (filename or self.file_info):
            raise TagException("No file")
        elif filename:
            self.file_info = FileInfo(filename)

        version = version if version else self.version
        if version == ID3_V2_2:
            raise NotImplementedError("Unable to write ID3 v2.2")
        self.version = version

        if backup and os.path.isfile(self.file_info.name):
            backup_name = "%s.%s" % (self.file_info.name, "orig")
            i = 1
            while os.path.isfile(backup_name):
                backup_name = "%s.%s.%d" % (self.file_info.name, "orig", i)
                i += 1
            shutil.copyfile(self.file_info.name, backup_name)

        if version[0] == 1:
            self.__saveV1Tag(version)
        elif version[0] == 2:
            self.__saveV2Tag(version, encoding)
        else:
            assert(not "Version bug: %s" % str(version))

    def __saveV1Tag(self, version):
        assert(version[0] == 1)

        def pack(s, n):
            assert(type(s) is str)
            return s.ljust(n, '\x00')[:n]

        # Build tag buffer.
        tag = b"TAG"
        tag += pack(self.title.encode("latin_1") if self.title else b"", 30)
        tag += pack(self.artist.encode("latin_1") if self.artist else b"", 30)
        tag += pack(self.album.encode("latin_1") if self.album else b"", 30)

        release_date = self.best_release_date
        year = str(release_date.year) if release_date else b""
        tag += pack(year.encode("latin_1"), 4)

        cmt = ""
        for c in self.comments:
           if c.description == ID3_V1_COMMENT_DESC:
              cmt = c.text
              # We prefer this one over ""
              break
           elif c.description == "":
              cmt = c.text
              # Keep searching in case we find the description eyeD3 uses.
        cmt = pack(cmt.encode("latin_1"), 30)

        if version != ID3_V1_0:
           track = self.track_num[0]
           if track != None:
              cmt = cmt[0:28] + "\x00" + chr(int(track) & 0xff)
        tag += cmt

        if not self.genre or self.genre.id is None:
           genre = 0
        else:
           genre = self.genre.id
        tag += chr(genre & 0xff)

        assert(len(tag) == 128)

        mode = "rb+" if os.path.isfile(self.file_info.name) else "w+b"
        with open(self.file_info.name, mode) as tag_file:
            # Write the tag over top an original or append it.
            try:
                tag_file.seek(-128, 2)
                if tag_file.read(3) == "TAG":
                    tag_file.seek(-128, 2)
                else:
                    tag_file.seek(0, 2)
            except IOError:
               # File is smaller than 128 bytes.
               tag_file.seek(0, 2)

            tag_file.write(tag)
            tag_file.flush()

    def _render(self, version, curr_tag_size):
        std_frames = []
        converted_frames = []
        for f in self.frame_set.getAllFrames():
            try:
                _, fversion, _ = frames.ID3_FRAMES[f.id]
                if fversion in (version, ID3_V2):
                    std_frames.append(f)
                else:
                    converted_frames.append(f)
            except KeyError:
                # Not a standard frame (ID3_FRAMES)
                try:
                    _, fversion, _ = frames.NONSTANDARD_ID3_FRAMES[f.id]
                    # but it is one we can handle.
                    if fversion in (version, ID3_V2):
                        std_frames.append(f)
                    else:
                        converted_frames.append(f)
                except KeyError:
                    # Don't know anything about this pass it on for the error
                    # check there.
                    converted_frames.append(f)

        if converted_frames:
            # actually, they're not converted yet
            converted_frames = self._convertFrames(converted_frames, version)

        # Render all frames first so the data size is known for the tag header.
        frame_data = b""
        for f in std_frames + converted_frames:
            frame_header = frames.FrameHeader(f.id, version)
            if f.header:
                frame_header.copyFlags(f.header)
            f.header = frame_header

            log.debug("Rendering frame: %s" % frame_header.id)
            raw_frame = f.render()
            log.debug("Rendered %d bytes" % len(raw_frame))
            frame_data += raw_frame

        log.debug("Rendered %d total frame bytes" % len(frame_data))

        # eyeD3 never writes unsync'd data
        self.header.unsync = False

        pending_size = TagHeader.SIZE + len(frame_data)
        if self.header.extended:
            # Using dummy data and padding, the actual size of this header
            # will be the same regardless, it's more about the flag bits
            tmp_ext_header_data = self.extended_header.render(version,
                                                              b"\x00", 0)
            pending_size += len(tmp_ext_header_data)

        padding_size = 0
        if pending_size > curr_tag_size:
            # current tag (minus padding) larger than the current (plus padding)
            rewrite_required = True
            padding_size = DEFAULT_PADDING
        else:
            rewrite_required = False
            padding_size = curr_tag_size - pending_size

        assert(padding_size >= 0)
        log.debug("Using %d bytes of padding" % padding_size)

        # Extended header
        ext_header_data = b""
        if self.header.extended:
            log.debug("Rendering extended header")
            ext_header_data += self.extended_header.render(self.header.version,
                                                           frame_data,
                                                           padding_size)

        # Render the tag header.
        total_size = pending_size + padding_size
        log.debug("Rendering %s tag header with size %d" %
                  (versionToString(version),
                   total_size - TagHeader.SIZE))
        header_data = self.header.render(total_size - TagHeader.SIZE)

        # Assemble the entire tag.
        tag_data = b"%(tag_header)s%(ext_header)s%(frames)s" % \
                   {"tag_header": header_data,
                    "ext_header": ext_header_data,
                    "frames": frame_data,
                   }
        assert(len(tag_data) == (total_size - padding_size))
        return (rewrite_required, tag_data, "\x00" * padding_size)

    def __saveV2Tag(self, version, encoding):
        assert(version[0] == 2 and version[1] != 2)
        log.debug("Rendering tag version: %s" % versionToString(version))

        file_exists = os.path.exists(self.file_info.name)

        if encoding:
            # Any invalid encoding is going to get coersed to a valid value
            # when the frame is rendered.
            for f in self.frame_set.getAllFrames():
                f.encoding = frames.stringToEncoding(encoding)

        curr_tag_size = 0

        if file_exists:
            # We may be converting from 1.x to 2.x so we need to find any
            # current v2.x tag otherwise we're gonna hork the file.
            # This also resets all offsets, state, etc. and makes me feel safe.
            tmp_tag = Tag()
            if tmp_tag.parse(self.file_info.name, ID3_V2):
                log.debug("Found current v2.x tag:")
                curr_tag_size = tmp_tag.file_info.tag_size
                log.debug("Current tag size: %d" % curr_tag_size)

            rewrite_required, tag_data, padding = self._render(version,
                                                               curr_tag_size)
            log.debug("Writing %d bytes of tag data and %d bytes of "
                      "padding" % (len(tag_data), len(padding)))
            if rewrite_required:
                # Open tmp file
                tmp_name = tempfile.mktemp()
                with open(tmp_name, "wb") as tmp_file:
                    tmp_file.write(tag_data + padding)

                    # Copy audio data in chunks
                    with open(self.file_info.name, "rb") as tag_file:
                        if curr_tag_size != 0:
                            seek_point = curr_tag_size
                        else:
                            seek_point = 0
                        log.debug("Seeking to beginning of audio data, "
                                  "byte %d (%x)" % (seek_point, seek_point))
                        tag_file.seek(seek_point)
                        chunkCopy(tag_file, tmp_file)

                # Move tmp to orig.
                shutil.copyfile(tmp_name, self.file_info.name)
                os.unlink(tmp_name)

            else:
                with open(self.file_info.name, "r+b") as tag_file:
                    tag_file.write(tag_data + padding)

        else:
            _, tag_data, padding = self._render(version, 0)
            with open(self.file_info.name, "wb") as tag_file:
                tag_file.write(tag_data + padding)

        log.debug("Tag write complete. Updating FileInfo state.")
        self.file_info.tag_size = len(tag_data) + len(padding)

    def _convertFrames(self, flist, version):
        '''Maps frame imcompatibilies between ID3 v2.3 and v2.4'''
        from . import versionToString
        from .frames import (DATE_FIDS, DEPRECATED_DATE_FIDS,
                             DateFrame, TextFrame)
        converted_frames = []
        flist = list(flist)

        # Date frame conversions.
        date_frames = {f.id: f for f in flist if f.id in DEPRECATED_DATE_FIDS}\
                      if version == ID3_V2_4 else \
                      {f.id: f for f in flist if f.id in DATE_FIDS}
        if date_frames:
            if version == ID3_V2_4:
                if "TORY" in date_frames:
                    # TORY -> TDOR (year only)
                    date = self._v23OrignalReleaseDate()
                    if date:
                        converted_frames.append(DateFrame("TDOR", date))
                    flist.remove(date_frames["TORY"])
                    del date_frames["TORY"]

                if "TYER" in date_frames:
                    # TYER, TDAT, TIME -> TDRC
                    date = self._v23RecordingDate()
                    if date:
                        converted_frames.append(DateFrame("TDRC", date))
                    for fid in ["TYER", "TDAT", "TIME"]:
                        if fid in date_frames:
                            flist.remove(date_frames[fid])
                            del date_frames[fid]

                if "XDOR" in date_frames:
                    # XDOR -> TDRC
                    xdor = date_frames["XDOR"]
                    converted_frames.append(DateFrame("TDRC", xdor.text))

                    flist.remove(xdor)
                    del date_frames["XDOR"]

            elif version == ID3_V2_3:
                if "TDOR" in date_frames:
                    date = date_frames["TDOR"].date
                    if date:
                        converted_frames.append(DateFrame("TORY",
                                                          unicode(date.year)))
                    flist.remove(date_frames["TDOR"])
                    del date_frames["TDOR"]

                if "TDRL" in date_frames:
                    date = date_frames["TDRL"].date

                    if date:
                        converted_frames.append(DateFrame("TYER",
                                                          unicode(date.year)))
                        if None not in (date.month, date.day):
                            date_str = u"%s%s" %\
                                    (str(date.day).rjust(2, "0"),
                                     str(date.month).rjust(2, "0"))
                            converted_frames.append(TextFrame("TDAT", date_str))
                        if None not in (date.hour, date.minute):
                            date_str = u"%s%s" %\
                                    (str(date.hour).rjust(2, "0"),
                                     str(date.minute).rjust(2, "0"))
                            converted_frames.append(TextFrame("TIME", date_str))

                    flist.remove(date_frames["TDRL"])
                    del date_frames["TDRL"]

                if "TDRC" in date_frames:
                    # TDRC -> XDOR
                    date = date_frames["TDRC"].date
                    if date:
                        converted_frames.append(DateFrame("XDOR", str(date)))
                    flist.remove(date_frames["TDRC"])
                    del date_frames["TDRC"]

            # All other date frames have no conversion
            for fid in date_frames:
                log.warning("%s frame being dropped due to conversion to %s" %
                            (fid, versionToString(version)))

                flist.remove(date_frames[fid])

        # Convert sort order frames 2.3 (XSO*) <-> 2.4 (TSO*)
        prefix = "X" if version == ID3_V2_4 else "T"
        fids = ["%s%s" % (prefix, suffix) for suffix in ["SOA", "SOP", "SOT"]]
        soframes = [f for f in flist if f.id in fids]

        for frame in soframes:
            frame.id = ("X" if prefix == "T" else "T") + frame.id[1:]
            flist.remove(frame)
            converted_frames.append(frame)

        if len(flist) != 0:
            unconverted = ", ".join([f.id for f in flist])
            raise TagException("Unable to covert the following frames to "
                               "version %s: %s" % (versionToString(version),
                                                   unconverted))
        return converted_frames

    @staticmethod
    def remove(filename, version=ID3_ANY_VERSION):
        retval = False
        if version[0] & ID3_V1[0]:
            tag = Tag()
            with open(filename, "r+b") as tag_file:
                found = tag.parse(tag_file, ID3_V1)
                if found:
                    tag_file.seek(-128, 2)
                    log.debug("Removing ID3 v1.x Tag")
                    tag_file.truncate()
                    retval |= True

        if version[0] & ID3_V2[0]:
            tag = Tag()
            with open(filename, "rb") as tag_file:
                found = tag.parse(tag_file, ID3_V2)
                if found:
                    log.debug("Removing ID3 %s tag" %
                              versionToString(tag.version))
                    tag_file.seek(tag.file_info.tag_size)

                    # Open tmp file
                    tmp_name = tempfile.mktemp()
                    with open(tmp_name, "wb") as tmp_file:
                        chunkCopy(tag_file, tmp_file)

                    # Move tmp to orig
                    shutil.copyfile(tmp_name, filename)
                    os.unlink(tmp_name)

                    retval |= True

        return retval

    @property
    def chapters(self):
        return self._chapters


##
# This class is for storing information about a parsed file. It containts info 
# such as the filename, original tag size, and amount of padding all of which
# can make rewriting faster.
class FileInfo:
    def __init__(self, file_name):
        from .. import LOCAL_FS_ENCODING

        if type(file_name) is unicode:
            self.name = file_name
        else:
            try:
                self.name = unicode(file_name, LOCAL_FS_ENCODING)
            except UnicodeDecodeError:
                # Work around the local encoding not matching that of a mounted
                # filesystem
                log.warning(u"Mismatched file system encoding for file '%s'" %
                            file_name)
                self.name = file_name

        self.tag_size = 0  # This includes the padding byte count.
        self.tag_padding_size = 0

class AccessorBase(object):
    def __init__(self, fid, fs, match_func=None):
        self._fid = fid
        self._fs = fs
        self._match_func = match_func

    def __iter__(self):
        for f in self._fs[self._fid] or []:
            yield f

    def __len__(self):
        return len(self._fs[self._fid] or [])

    def __getitem__(self, i):
        frames = self._fs[self._fid]
        if not frames:
            raise IndexError("list index out of range")
        return frames[i]

    def get(self, *args, **kwargs):
        for frame in self._fs[self._fid] or []:
            if self._match_func(frame, *args, **kwargs):
                return frame
        return None

    def remove(self, *args, **kwargs):
        '''Returns the removed item or ``None`` if not found.'''
        fid_frames = self._fs[self._fid] or []
        for frame in fid_frames:
            if self._match_func(frame, *args, **kwargs):
                fid_frames.remove(frame)
                return frame
        return None


class DltAccessor(AccessorBase):
    def __init__(self, FrameClass, fid, fs):
        def match_func(frame, description, lang=DEFAULT_LANG):
            return frame.description == description and frame.lang == lang

        super(DltAccessor, self).__init__(fid, fs, match_func)
        self.FrameClass = FrameClass

    @requireUnicode(1, 2)
    def set(self, text, description=u"", lang=DEFAULT_LANG):
        for f in self._fs[self._fid] or []:
            if f.description == description and f.lang == lang:
                # Exists, update text
                f.text = text
                return f

        new_frame = self.FrameClass(description=description, lang=lang,
                                    text=text)
        self._fs[self._fid] = new_frame
        return new_frame

    @requireUnicode(1)
    def remove(self, description, lang=DEFAULT_LANG):
        return super(DltAccessor, self).remove(description, lang=lang)

    @requireUnicode(1)
    def get(self, description, lang=DEFAULT_LANG):
        return super(DltAccessor, self).get(description, lang=lang)

class CommentsAccessor(DltAccessor):
    def __init__(self, fs):
        super(CommentsAccessor, self).__init__(frames.CommentFrame,
                                               frames.COMMENT_FID, fs)

class LyricsAccessor(DltAccessor):
    def __init__(self, fs):
        super(LyricsAccessor, self).__init__(frames.LyricsFrame,
                                             frames.LYRICS_FID, fs)

class ImagesAccessor(AccessorBase):
    def __init__(self, fs):
        def match_func(frame, description):
            return frame.description == description
        super(ImagesAccessor, self).__init__(frames.IMAGE_FID, fs, match_func)

    @requireUnicode("description")
    def set(self, type, img_data, mime_type, description=u"", img_url=None):
        '''Add an image of ``type`` (a type constant from ImageFrame).
        The ``img_data`` is either bytes or ``None``. In the latter case
        ``img_url`` MUST be the URL to the image. In this case ``mime_type``
        is ignored and "-->" is used to signal this as a link and not data
        (per the ID3 spec).'''
        if not img_data and not img_url:
            raise ValueError("img_url MUST not be none when no image data")

        mime_type = mime_type if img_data else frames.ImageFrame.URL_MIME_TYPE

        images = self._fs[frames.IMAGE_FID] or []
        for img in images:
            if img.description == description:
                # update
                if not img_data:
                    img.image_url = img_url
                    img.image_data = None
                    img.mime_type = frames.ImageFrame.URL_MIME_TYPE
                else:
                    img.image_url = None
                    img.image_data = img_data
                    img.mime_type = mime_type
                img.picture_type = type
                return img

        img_frame = frames.ImageFrame(description=description,
                                      image_data=img_data,
                                      image_url=img_url,
                                      mime_type=mime_type,
                                      picture_type=type)
        self._fs[frames.IMAGE_FID] = img_frame
        return img_frame

    @requireUnicode(1)
    def remove(self, description):
        return super(ImagesAccessor, self).remove(description)

    @requireUnicode(1)
    def get(self, description):
        return super(ImagesAccessor, self).get(description)

class ObjectsAccessor(AccessorBase):
    def __init__(self, fs):

        def match_func(frame, description):
            return frame.description == description
        super(ObjectsAccessor, self).__init__(frames.OBJECT_FID, fs, match_func)

    @requireUnicode("description", "filename")
    def set(self, data, mime_type, description=u"", filename=u""):
        objects = self._fs[frames.OBJECT_FID] or []
        for obj in objects:
            if obj.description == description:
                # update
                obj.object_data = data
                obj.mime_type = mime_type
                obj.filename = filename
                return obj

        obj_frame = frames.ObjectFrame(description=description,
                                       filename=filename,
                                       object_data=data,
                                       mime_type=mime_type)
        self._fs[frames.OBJECT_FID] = obj_frame
        return obj_frame

    @requireUnicode(1)
    def remove(self, description):
        return super(ObjectsAccessor, self).remove(description)

    @requireUnicode(1)
    def get(self, description):
        return super(ObjectsAccessor, self).get(description)

class PrivatesAccessor(AccessorBase):
    def __init__(self, fs):

        def match_func(frame, owner_id):
            return frame.owner_id == owner_id
        super(PrivatesAccessor, self).__init__(frames.PRIVATE_FID, fs,
                                               match_func)

    def set(self, data, owner_id):
        priv_frames = self._fs[frames.PRIVATE_FID] or []
        for f in priv_frames:
            if f.owner_id == owner_id:
                # update
                f.owner_data = data
                return f

        priv_frame = frames.PrivateFrame(owner_id=owner_id,
                                         owner_data=data)
        self._fs[frames.PRIVATE_FID] = priv_frame
        return priv_frame

    def remove(self, owner_id):
        return super(PrivatesAccessor, self).remove(owner_id)

    def get(self, owner_id):
        return super(PrivatesAccessor, self).get(owner_id)

class UserTextsAccessor(AccessorBase):
    def __init__(self, fs):
        def match_func(frame, description):
            return frame.description == description
        super(UserTextsAccessor, self).__init__(frames.USERTEXT_FID, fs,
                                                match_func)

    @requireUnicode(1, "description")
    def set(self, text, description=u""):
        flist = self._fs[frames.USERTEXT_FID] or []
        for utf in flist:
            if utf.description == description:
                # update
                utf.text = text
                return utf

        utf = frames.UserTextFrame(description=description,
                                   text=text)
        self._fs[frames.USERTEXT_FID] = utf
        return utf

    @requireUnicode(1)
    def remove(self, description):
        return super(UserTextsAccessor, self).remove(description)

    @requireUnicode(1)
    def get(self, description):
        return super(UserTextsAccessor, self).get(description)

class UniqueFileIdAccessor(AccessorBase):
    def __init__(self, fs):
        def match_func(frame, owner_id):
            return frame.owner_id == owner_id
        super(UniqueFileIdAccessor, self).__init__(frames.UNIQUE_FILE_ID_FID,
                                                   fs, match_func)

    def set(self, data, owner_id):
        data = str(data)
        if len(data) > 64:
            raise TagException("UFID data must be 64 bytes or less")

        flist = self._fs[frames.UNIQUE_FILE_ID_FID] or []
        for f in flist:
            if f.owner_id == owner_id:
                # update
                f.uniq_id = data
                return f

        uniq_id_frame = frames.UniqueFileIDFrame(owner_id=owner_id,
                                                 uniq_id=data)
        self._fs[frames.UNIQUE_FILE_ID_FID] = uniq_id_frame
        return uniq_id_frame

    def remove(self, owner_id):
        return super(UniqueFileIdAccessor, self).remove(owner_id)

    def get(self, owner_id):
        return super(UniqueFileIdAccessor, self).get(owner_id)


class UserUrlsAccessor(AccessorBase):
    def __init__(self, fs):
        def match_func(frame, description):
            return frame.description == description
        super(UserUrlsAccessor, self).__init__(frames.USERURL_FID, fs,
                                               match_func)

    @requireUnicode("description")
    def set(self, url, description=u""):
        flist = self._fs[frames.USERURL_FID] or []
        for uuf in flist:
            if uuf.description == description:
                # update
                uuf.url = url
                return uuf

        uuf = frames.UserUrlFrame(description=description, url=url)
        self._fs[frames.USERURL_FID] = uuf
        return uuf

    @requireUnicode(1)
    def remove(self, description):
        return super(UserUrlsAccessor, self).remove(description)

    @requireUnicode(1)
    def get(self, description):
        return super(UserUrlsAccessor, self).get(description)

class PopularitiesAccessor(AccessorBase):
    def __init__(self, fs):
        def match_func(frame, email):
            return frame.email == email
        super(PopularitiesAccessor, self).__init__(frames.POPULARITY_FID, fs,
                                                   match_func)

    def set(self, email, rating, play_count):
        flist = self._fs[frames.POPULARITY_FID] or []
        for popm in flist:
            if popm.email == email:
                # update
                popm.rating = rating
                popm.count = play_count
                return popm

        popm = frames.PopularityFrame(email=email, rating=rating,
                                      count=play_count)
        self._fs[frames.POPULARITY_FID] = popm
        return popm

    def remove(self, email):
        return super(PopularitiesAccessor, self).remove(email)

    def get(self, email):
        return super(PopularitiesAccessor, self).get(email)


class ChaptersAccessor(AccessorBase):
    def __init__(self, fs):
        def match_func(frame, element_id):
            return frame.element_id == element_id
        super(ChaptersAccessor, self).__init__(frames.CHAPTER_FID, fs,
                                               match_func)
    def set(self, element_id, times, offsets=(None, None), sub_frames=None):
        flist = self._fs[frames.CHAPTER_FID] or []
        for chap in flist:
            if chap.element_id == element_id:
                # update
                chap.times, chap.offsets = times, offsets
                chap.sub_frames = sub_frames
                return chap

        chap = frames.ChapterFrame(element_id=element_id,
                                   times=times, offsets=offsets,
                                   sub_frames=sub_frames or [])
        self._fs[frames.CHAPTER_FID] = chap
        return chap

    def remove(self, element_id):
        return super(ChaptersAccessor, self).remove(element_id)

    def get(self, element_id):
        return super(ChaptersAccessor, self).get(element_id)

    def __getitem__(self, elem_id):
        '''Overiding the index based __getitem__ for one indexed with chapter
        element IDs. These are stored in the tag's table of contents frames.'''
        for chapter in (self._fs[frames.CHAPTER_FID] or []):
            if chapter.element_id == elem_id:
                return chapter
        raise IndexError("chapter '%s' not found" % elem_id)



import string
class TagTemplate(string.Template):
    idpattern = r'[_a-z][_a-z0-9:]*'

    def __init__(self, pattern, strict_release_date=False, path_friendly=True):
        super(TagTemplate, self).__init__(pattern)
        self._strict_date = strict_release_date
        self._path_friendly = path_friendly

    def substitute(self, tag, zeropad=True):
        mapping = self._makeMapping(tag, zeropad)

        # Helper function for .sub()
        def convert(mo):
            named = mo.group('named')
            if named is not None:
                try:
                    if type(mapping[named]) is tuple:
                        func, args = mapping[named][0], mapping[named][1:]
                        return '%s' % func(tag, named, *args)
                    # We use this idiom instead of str() because the latter
                    # will fail if val is a Unicode containing non-ASCII
                    return '%s' % (mapping[named],)
                except KeyError:
                    return self.delimiter + named
            braced = mo.group('braced')
            if braced is not None:
                try:
                    if type(mapping[braced]) is tuple:
                        func, args = mapping[braced][0], mapping[braced][1:]
                        return '%s' % func(tag, braced, *args)
                    return '%s' % (mapping[braced],)
                except KeyError:
                    return self.delimiter + '{' + braced + '}'
            if mo.group('escaped') is not None:
                return self.delimiter
            if mo.group('invalid') is not None:
                return self.delimiter
            raise ValueError('Unrecognized named group in pattern',
                             self.pattern)

        return self.pattern.sub(convert, self.template)

    safe_substitute = substitute

    def _release_date(self, tag, param):
        date = tag.best_release_date if not self._strict_date \
                                     else tag.release_date
        if param.endswith(":year"):
            return str(date.year)
        else:
            return str(date)

    def _track(self, tag, param, zeropad):
        tn, tt = (str(n) for n in tag.track_num)
        if zeropad:
            tt = tt.rjust(2, "0")
            tn = tn.rjust(len(tt), "0")

        if param.endswith(":num"):
            return tn
        elif param.endswith(":total"):
            return tt
        else:
            raise ValueError("Unknown template param: %s" % param)

    def _file(self, tag, param):
        assert(param.startswith("file"))

        if param.endswith(":ext"):
            return os.path.splitext(tag.file_info.name)[1][1:]
        else:
            return tag.file_info.name

    def _makeMapping(self, tag, zeropad):
        return {"artist": tag.artist if tag else None,
                "album": tag.album if tag else None,
                "title": tag.title if tag else None,
                "track:num": (self._track, zeropad) if tag else None,
                "track:total": (self._track, zeropad) if tag else None,
                "release_date": (self._release_date,) if tag else None,
                "release_date:year": (self._release_date,) if tag else None,
                "file": (self._file,) if tag else None,
                "file:ext": (self._file,) if tag else None,
               }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.