def test_lines(): shortLine = 'A C' assert isChordLine(shortLine) notQuiteChords = 'A carrot' assert not isChordLine(notQuiteChords) line = 'Dm C Dm C Dm C ' assert isChordLine(line)
def test_lines(): shortLine = "A C" assert isChordLine(shortLine) notQuiteChords = "A carrot" assert not isChordLine(notQuiteChords) line = "Dm C Dm C Dm C " assert isChordLine(line)
def test_complex_chords(): complex = ['Cmaj7', 'Caug', 'Bbsus2', 'Dbdim', 'Gadd9', 'Dm', 'Emadd9'] for chord in complex: assert isChord(chord), chord line = ' '.join(complex) assert isChordLine(line)
def test_complex_chords(): complexChords = [ "Cmaj7", "Caug", "Bbsus2", "Dbdim", "Gadd9", "Dm", "Emadd9" ] for chord in complexChords: assert isChord(chord), chord line = " ".join(complexChords) assert isChordLine(line)
def toJSON(self, fileName): """ Text file of a song --> JSON file of a song """ textFile = os.path.join(self.textFolder, fileName) f = open(textFile, "r") lines = f.readlines() lines = [x.rstrip() for x in lines] data = {} songID = nameToID(fileName) [title, artist] = idToData(songID) data["title"] = title data["artist"] = artist data["id"] = songID data["lines"] = [] print title allChords = [] def updateAllChords(line): for chord in line.split(): # Chords with a bass note if "/" in chord: chord = chord.split("/")[0] if chord not in allChords: allChords.append(chord) linesIter = iter(lines) # Putting meta data in the file is pretty sketchy # Move to a db eventually #36 firstLine = lines[0] capo = "CAPO " if firstLine.startswith(capo): data["capo"] = firstLine.split(capo)[1] next(linesIter) for line in linesIter: if isLabel(line): data["lines"].append({"label": line}) elif isChordLine(line): while True: next_line = next(linesIter) lyrics = next_line if isLyricLine(next_line) else "" data["lines"].append({"lyrics": lyrics, "chord": line}) updateAllChords(line) line = next_line if isLabel(line): data["lines"].append({"label": line}) break elif not isChordLine(line): break elif line: data["lines"].append({"lyrics": line, "chord": ""}) # FIXME: should we preserve blank lines? data["allChords"] = allChords title = clean(title) artist = clean(artist) fileName = dataToName(title, artist, JSON) jsonFile = os.path.join(JSON_FOLDER, fileName) with open(jsonFile, "w") as outfile: json.dump(data, outfile, indent=2, sort_keys=True)
def toJSON(self, fileName): """ Text file of a song --> JSON file of a song """ textFile = os.path.join(textFolder, fileName) f = open(textFile, 'r') lines = f.readlines() lines = [x.rstrip() for x in lines] data = {} songID = nameToID(fileName) [title, artist] = idToData(songID) data['title'] = title data['artist'] = artist data['id'] = songID data['lines'] = [] print title allChords = [] def updateAllChords(line): for chord in line.split(): # Chords with a bass note if "/" in chord: chord = chord.split("/")[0] if chord not in allChords: allChords.append(chord) lines_iter = iter(lines) first_line = lines[0] capo = "CAPO " if first_line.startswith(capo): data["capo"] = first_line.split(capo)[1] next(lines_iter) for line in lines_iter: if isLabel(line): data['lines'].append({'label': line}) elif isChordLine(line): while True: next_line = next(lines_iter) lyrics = next_line if isLyricLine(next_line) else '' data['lines'].append({'lyrics': lyrics, 'chord': line}) updateAllChords(line) line = next_line if isLabel(line): data['lines'].append({'label': line}) break elif not isChordLine(line): break elif line: data['lines'].append({'lyrics': line, 'chord': ''}) # FIXME: should we preserve blank lines? data['allChords'] = allChords title = clean(title) artist = clean(artist) fileName = dataToName(title, artist, JSON) jsonFile = os.path.join(JSON_FOLDER, fileName) with open(jsonFile, 'w') as outfile: json.dump(data, outfile, indent=2, sort_keys=True)