def parse(lines): """returns a TOCFile object from an iterator of lines raises TOCException if some problem occurs parsing the file""" TRACKLINE = re.compile(r'TRACK AUDIO') lines = list(lines) if ('CD_DA' not in [line.strip() for line in lines]): raise TOCException(_(u"No CD_DA TOC header found")) lines = iter(lines) toc = TOCFile() track = None track_number = 0 line_number = 0 try: while (True): line_number += 1 line = lines.next().strip() if (len(line) == 0): pass elif (TRACKLINE.match(line)): if (track is not None): toc.tracks[track.number] = track track_number += 1 track = Track(track_number) else: if (track is not None): track.lines.append(line) if (line.startswith('FILE') or line.startswith('AUDIOFILE')): if ('"' in line): track.indexes = map( parse_timestamp, re.findall(r'\d+:\d+:\d+|\d+', line[line.rindex('"') + 1:])) else: track.indexes = map( parse_timestamp, re.findall(r'\d+:\d+:\d+|\d+', line)) elif (line.startswith('START')): track.start = parse_timestamp(line[len('START '):]) else: toc.lines.append(line) except StopIteration: if (track is not None): toc.tracks[track.number] = track return toc
def parse(lines): """Returns a TOCFile object from an iterator of lines. Raises TOCException if some problem occurs parsing the file.""" TRACKLINE = re.compile(r'TRACK AUDIO') lines = list(lines) if ('CD_DA' not in [line.strip() for line in lines]): raise TOCException(_(u"No CD_DA TOC header found")) lines = iter(lines) toc = TOCFile() track = None track_number = 0 line_number = 0 try: while (True): line_number += 1 line = lines.next().strip() if (len(line) == 0): pass elif (TRACKLINE.match(line)): if (track is not None): toc.tracks[track.number] = track track_number += 1 track = Track(track_number) else: if (track is not None): track.lines.append(line) if (line.startswith('FILE') or line.startswith('AUDIOFILE')): if ('"' in line): track.indexes = map( parse_timestamp, re.findall(r'\d+:\d+:\d+|\d+', line[line.rindex('"') + 1:])) else: track.indexes = map( parse_timestamp, re.findall(r'\d+:\d+:\d+|\d+', line)) elif (line.startswith('START')): track.start = parse_timestamp(line[len('START '):]) else: toc.lines.append(line) except StopIteration: if (track is not None): toc.tracks[track.number] = track return toc
def parse(lines): """Returns a TOCFile object from an iterator of lines. Raises TOCException if some problem occurs parsing the file.""" TRACKLINE = re.compile(r"TRACK AUDIO") lines = list(lines) if "CD_DA" not in [line.strip() for line in lines]: raise TOCException(_(u"No CD_DA TOC header found")) lines = iter(lines) toc = TOCFile() track = None track_number = 0 line_number = 0 try: while True: line_number += 1 line = lines.next().strip() if len(line) == 0: pass elif TRACKLINE.match(line): if track is not None: toc.tracks[track.number] = track track_number += 1 track = Track(track_number) else: if track is not None: track.lines.append(line) if line.startswith("FILE") or line.startswith("AUDIOFILE"): if '"' in line: track.indexes = map( parse_timestamp, re.findall(r"\d+:\d+:\d+|\d+", line[line.rindex('"') + 1 :]) ) else: track.indexes = map(parse_timestamp, re.findall(r"\d+:\d+:\d+|\d+", line)) elif line.startswith("START"): track.start = parse_timestamp(line[len("START ") :]) else: toc.lines.append(line) except StopIteration: if track is not None: toc.tracks[track.number] = track return toc
def __parse__(lines): """returns a TOCFile object from an iterator of lines raises TOCException if some problem occurs parsing the file""" import re from fractions import Fraction from audiotools import parse_timestamp, Sheet, SheetTrack, SheetIndex CATALOG = re.compile(r'CATALOG\s*"(.*?)"') AUDIOFILE = re.compile( r'(AUDIO)?FILE\s*".*?"\s*(\d+:\d+:\d+|\d+)\s*(\d+:\d+:\d+|\d+)?') ISRC = re.compile(r'ISRC\s*"(.*)"') START = re.compile(r'START\s*(\d+:\d+:\d+|\d+)') INDEX = re.compile(r'INDEX\s*(\d+:\d+:\d+|\d+)') def make_indexes(index_start, index_offsets): for (number, offset) in enumerate(index_offsets, index_start): yield SheetIndex(number, Fraction(offset, 75)) lines = list(lines) #FIXME - make this less stupid if ('CD_DA' not in [line.strip() for line in lines]): from .text import ERR_TOC_NO_HEADER raise TOCException(ERR_TOC_NO_HEADER) lines = iter(lines) cuesheet_tracks = [] cuesheet_catalog_number = None track_number = None track_audio = True track_ISRC = None index_start = 1 index_offsets = [] line_number = 0 try: while (True): line_number += 1 line = lines.next().strip() if (len(line) == 0): pass elif (line == "TRACK AUDIO"): #start a new track if (track_number is not None): cuesheet_tracks.append( SheetTrack(track_number, list(make_indexes(index_start, index_offsets)), track_audio, track_ISRC)) track_number += 1 track_indexes = [] track_audio = True track_ISRC = None else: track_number = 1 elif (track_number is not None): #first index, which may be 1 or 0 first_index = AUDIOFILE.match(line) if (first_index is not None): index_start = 1 index_offsets = [parse_timestamp(first_index.group(2))] continue start = START.match(line) if (start is not None): #first index point is number 0 #and add a new index point number 1 assert(len(index_offsets) == 1) index_start = 0 index_offsets.append( index_offsets[0] + parse_timestamp(start.group(1))) continue index = INDEX.match(line) if (index is not None): #add a new index point index_offsets.append(parse_timestamp(index.group(1))) continue isrc = ISRC.match(line) if (isrc is not None): #add ISRC to track track_ISRC = isrc.group(1) else: catalog = CATALOG.match(line) if (catalog is not None): cuesheet_catalog_number = catalog.group(1) continue except StopIteration: if (track_number is not None): cuesheet_tracks.append( SheetTrack(track_number, list(make_indexes(index_start, index_offsets)), track_audio, track_ISRC)) return Sheet(cuesheet_tracks, cuesheet_catalog_number)
def __parse__(lines): """returns a TOCFile object from an iterator of lines raises TOCException if some problem occurs parsing the file""" import re from fractions import Fraction from audiotools import parse_timestamp, Sheet, SheetTrack, SheetIndex CATALOG = re.compile(r'CATALOG\s*"(.*?)"') AUDIOFILE = re.compile( r'(AUDIO)?FILE\s*".*?"\s*(\d+:\d+:\d+|\d+)\s*(\d+:\d+:\d+|\d+)?') ISRC = re.compile(r'ISRC\s*"(.*)"') START = re.compile(r'START\s*(\d+:\d+:\d+|\d+)') INDEX = re.compile(r'INDEX\s*(\d+:\d+:\d+|\d+)') def make_indexes(index_start, index_offsets): for (number, offset) in enumerate(index_offsets, index_start): yield SheetIndex(number, Fraction(offset, 75)) lines = list(lines) #FIXME - make this less stupid if ('CD_DA' not in [line.strip() for line in lines]): from .text import ERR_TOC_NO_HEADER raise TOCException(ERR_TOC_NO_HEADER) lines = iter(lines) cuesheet_tracks = [] cuesheet_catalog_number = None track_number = None track_audio = True track_ISRC = None index_start = 1 index_offsets = [] line_number = 0 try: while (True): line_number += 1 line = lines.next().strip() if (len(line) == 0): pass elif (line == "TRACK AUDIO"): #start a new track if (track_number is not None): cuesheet_tracks.append( SheetTrack( track_number, list(make_indexes(index_start, index_offsets)), track_audio, track_ISRC)) track_number += 1 track_indexes = [] track_audio = True track_ISRC = None else: track_number = 1 elif (track_number is not None): #first index, which may be 1 or 0 first_index = AUDIOFILE.match(line) if (first_index is not None): index_start = 1 index_offsets = [parse_timestamp(first_index.group(2))] continue start = START.match(line) if (start is not None): #first index point is number 0 #and add a new index point number 1 assert (len(index_offsets) == 1) index_start = 0 index_offsets.append(index_offsets[0] + parse_timestamp(start.group(1))) continue index = INDEX.match(line) if (index is not None): #add a new index point index_offsets.append(parse_timestamp(index.group(1))) continue isrc = ISRC.match(line) if (isrc is not None): #add ISRC to track track_ISRC = isrc.group(1) else: catalog = CATALOG.match(line) if (catalog is not None): cuesheet_catalog_number = catalog.group(1) continue except StopIteration: if (track_number is not None): cuesheet_tracks.append( SheetTrack(track_number, list(make_indexes(index_start, index_offsets)), track_audio, track_ISRC)) return Sheet(cuesheet_tracks, cuesheet_catalog_number)