Commits

Travis Shirk committed a0c6eb9

Accessors for Chapters table of contents, etc.

Comments (0)

Files changed (3)

examples/chapters.py

 def printChapter(chapter):
     # The element ID is the unique key for this chapter
     print("== Chapter '%s'" % chapter.element_id)
+    # TIT2 sub frame
+    print("-- Title:", chapter.title)
+    # TIT333ub frame
+    print("-- subtitle:", chapter.subtitle)
     # Start and end time - tuple
     print("-- Start time: %d; End time: %d" % chapter.times)
     # Start and end offset - tuple. None is used to set to "no offset"
     print("-- Start offset: %s; End offset: %s" %
           tuple((str(o) for o in chapter.offsets)))
+    print("-- Sub frames:", str(chapter.sub_frames.keys()))
 
 tag = Tag()
 if len(sys.argv) > 1:
     tag.parse(sys.argv[1])
 
+if tag.toc:
+    print("=== Table of contents:", tag.toc.element_id)
+    print("--- description:", tag.toc.description)
+    print("--- toplevel:", tag.toc.toplevel)
+    print("--- ordered:", tag.toc.ordered)
+    print("--- child_ids:", tag.toc.child_ids)
+
 tag.chapters.set("a brand new chapter", (16234, 21546))
 tag.chapters.set("another brand new chapter", (21567, 30000), (654221, 765543))
 tag.chapters.set("final chapter", (40000, 50000))
     print(chap)
     printChapter(chap)
 print("-" * 80)
+
+# Given a list of chapter IDs from the table of contents access each chapter
+print("+" * 80)
+for chap_id in tag.toc.child_ids:
+    print(chap_id)
+    printChapter(tag.chapters[chap_id])
+print("+" * 80)
+

src/eyed3/id3/frames.py

 
 
 TITLE_FID          = "TIT2"
+SUBTITLE_FID       = "TIT3"
 ARTIST_FID         = "TPE1"
 ALBUM_FID          = "TALB"
 TRACKNUM_FID       = "TRCK"
 
         return super(ChapterFrame, self).render()
 
+    @property
+    def title(self):
+        if TITLE_FID in self.sub_frames:
+            return self.sub_frames[TITLE_FID][0].text
+        return None
+
+    @title.setter
+    def title(self, title):
+        self.sub_frames.setTextFrame(TITLE_FID, title)
+
+    @property
+    def subtitle(self):
+        if SUBTITLE_FID in self.sub_frames:
+            return self.sub_frames[SUBTITLE_FID][0].text
+        return None
+
+    @subtitle.setter
+    def subtitle(self, subtitle):
+        self.sub_frames.setTextFrame(TITLE_FID, subtitle)
+
 
 class FrameSet(dict):
     def __init__(self):

src/eyed3/id3/tag.py

                     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 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:]*'