def getPaths(self): documentationDirectoryPath = os.path.join(common.getSourceFilePath(), 'documentation') self.documentationDirectoryPath = documentationDirectoryPath self.autogenDirectoryPath = os.path.join( documentationDirectoryPath, 'autogenerated', ) self.buildDirectoryPath = os.path.join( documentationDirectoryPath, 'build', ) self.doctreesDirectoryPath = os.path.join( self.buildDirectoryPath, 'doctrees', ) self.buildDirectories = { 'html': os.path.join( self.buildDirectoryPath, 'html', ), 'latex': os.path.join( self.buildDirectoryPath, 'latex', ), # could reuse latex, but too much rewriting 'latexpdf': os.path.join( self.buildDirectoryPath, 'latex', ), }
def __init__(self): self.outputDirectory = None self.docBasePath = os.path.join(common.getSourceFilePath(), 'documentation') self.docSourcePath = os.path.join(self.docBasePath, 'source') self.docGeneratedPath = os.path.join(self.docBasePath, 'autogenerated')
def testComplete(self): ci = CapellaImporter() #ci.readCapellaXMLFile(r'd:/desktop/achsorgd.capx') capellaDirPath = common.getSourceFilePath() / 'capella' oswaldPath = capellaDirPath / r'Nu_rue_mit_sorgen.capx' partScore = ci.scoreFromFile(oswaldPath) partScore.show()
def testComplete(self): nwcTranslatePath = common.getSourceFilePath() / 'noteworthy' complete = nwcTranslatePath / 'NWCTEXT_Really_complete_example_file.nwctxt' #'Part_OWeisheit.nwctxt' # myScore = NoteworthyTranslator().parseFile(complete) myScore.show()
def testMIDIParse(): from music21 import converter, common from music21 import freezeThaw #a = 'https://github.com/ELVIS-Project/vis/raw/master/test_corpus/prolationum-sanctus.midi' #c = converter.parse(a) # c = corpus.parse('bwv66.6', forceSource=True) # v = freezeThaw.StreamFreezer(c) # v.setupSerializationScaffold() # return v.writeStr() # returns a string import os a = os.path.join(common.getSourceFilePath(), 'midi', 'testPrimitive', 'test03.mid') #a = 'https://github.com/ELVIS-Project/vis/raw/master/test_corpus/prolationum-sanctus.midi' c = converter.parse(a) v = freezeThaw.StreamFreezer(c) v.setupSerializationScaffold() mockType = lambda x: x.__class__.__name__ == 'weakref' ty = TreeYielder(mockType) for val in ty.run(c): print(val, ty.currentLevel())
def testPaert(self): import os nwcTranslatePath = common.getSourceFilePath() + os.path.sep + 'noteworthy' paertPath = nwcTranslatePath + os.path.sep + 'NWCTEXT_Really_complete_example_file.nwctxt' #'Part_OWeisheit.nwctxt' # #paertPath = r'D:\docs\research\music21\elvis\Wegman Masses\01\Caput\01 Kyrie.nwctxt' myScore = NoteworthyTranslator().parseFile(paertPath) myScore.show()
def readExcerpts(self): for languageCode in self.languageCodes: thisExcerpt = os.path.join(common.getSourceFilePath(), 'languageExcerpts', languageCode + '.txt') f = open(thisExcerpt) self.trigrams[languageCode] = Trigram(f.read().split()) f.close()
def readExcerpts(self): for languageCode in self.languageCodes: thisExcerpt = (common.getSourceFilePath() / 'languageExcerpts' / (languageCode + '.txt')) with thisExcerpt.open(encoding='utf-8') as f: excerptWords = f.read().split() self.trigrams[languageCode] = Trigram(excerptWords)
def testTransposingInstruments(self): import os from music21 import converter, common fpDir = os.path.join(common.getSourceFilePath(), 'musedata', 'testPrimitive', 'test01') s = converter.parse(fpDir) p = s.parts['Clarinet in A'] self.assertEqual(str(p.getElementsByClass('Measure')[0].keySignature), '<music21.key.KeySignature of 3 sharps>') self.assertEqual(str(p.flat.notesAndRests[0]), '<music21.note.Note A>')
def allFiles(): thisDir = common.getSourceFilePath() / 'musicxml' / 'lilypondTestSuite' allOut = [] for f in thisDir.iterdir(): if f.name.startswith('__'): continue allOut.append(f) return allOut
def testComplete(self): ci = CapellaImporter() #ci.readCapellaXMLFile(r'd:/desktop/achsorgd.capx') import os from music21 import common capellaDirPath = common.getSourceFilePath() + os.path.sep + 'capella' oswaldPath = capellaDirPath + os.path.sep + r'Nu_rue_mit_sorgen.capx' partScore = ci.scoreFromFile(oswaldPath) partScore.show()
def testBasic(self): import os from music21 import common nwcTranslatePath = common.getSourceFilePath() + os.path.sep + 'noteworthy' simplePath = nwcTranslatePath + os.path.sep + 'verySimple.nwctxt'#'NWCTEXT_Really_complete_example_file.nwctxt' # ## #'Part_OWeisheit.nwctxt' # myScore = NoteworthyTranslator().parseFile(simplePath) self.assertEqual(len(myScore.flat.notes), 1) self.assertEqual(str(myScore.flat.notes[0].name), "E") self.assertEqual(str(myScore.flat.getElementsByClass('Clef')[0]), "<music21.clef.BassClef>")
def getDirBuildHtml(): '''Return the html directory ''' from music21 import common cwdir = common.getSourceFilePath() parentDir = os.path.dirname(cwdir) dirBuild = os.path.join(parentDir, 'music21', 'documentation', 'build') dirBuildHtml = os.path.join(dirBuild, 'html') return dirBuildHtml
def readExcerpts(self): for languageCode in self.languageCodes: thisExcerpt = os.path.join(common.getSourceFilePath(), 'languageExcerpts', languageCode + '.txt') with open(thisExcerpt) as f: excerptWords = f.read().split() self.trigrams[languageCode] = Trigram(excerptWords)
def getDirBuildHtml(): """Return the html directory """ from music21 import common cwdir = common.getSourceFilePath() parentDir = os.path.dirname(cwdir) dirBuild = os.path.join(parentDir, "music21", "documentation", "build") dirBuildHtml = os.path.join(dirBuild, "html") return dirBuildHtml
def allFiles(): thisDir = os.sep.join([common.getSourceFilePath(), 'musicxml', 'lilypondTestSuite']) allOut = [] for f in os.listdir(thisDir): if f.startswith('__'): continue allOut.append(thisDir + os.sep + f) return allOut
def testBackBasic(self): from music21 import converter, common fpDir = common.getSourceFilePath() / 'musedata' / 'testPrimitive' / 'test01' s = converter.parse(fpDir) # note: this is a multi-staff work, but presently gets encoded # as multiple voices measures = s.parts[0].measures(1, 5) self.assertEqual(len(measures[0].flat.notesAndRests), 2) self.assertEqual(len(measures[1].flat.notesAndRests), 5) self.assertEqual(len(measures[2].flat.notesAndRests), 5) self.assertEqual(len(measures[3].flat.notesAndRests), 6) self.assertEqual(len(measures[4].flat.notesAndRests), 4)
def testPickleMidi(self): from music21 import converter a = str(common.getSourceFilePath() / 'midi' / 'testPrimitive' / 'test03.mid') #a = 'https://github.com/ELVIS-Project/vis/raw/master/test_corpus/prolationum-sanctus.midi' c = converter.parse(a) f = converter.freezeStr(c) d = converter.thawStr(f) self.assertEqual(d[1][20].volume._client.__class__.__name__, 'weakref')
def getPaths(self): documentationDirectoryPath = os.path.join(common.getSourceFilePath(), "documentation") self.documentationDirectoryPath = documentationDirectoryPath self.autogenDirectoryPath = os.path.join(documentationDirectoryPath, "autogenerated") self.buildDirectoryPath = os.path.join(documentationDirectoryPath, "build") self.doctreesDirectoryPath = os.path.join(self.buildDirectoryPath, "doctrees") self.buildDirectories = { "html": os.path.join(self.buildDirectoryPath, "html"), "latex": os.path.join(self.buildDirectoryPath, "latex"), # could reuse latex, but too much rewriting "latexpdf": os.path.join(self.buildDirectoryPath, "latex"), }
def __iter__(self): rootFilesystemPath = common.getSourceFilePath() for directoryPath, directoryNames, fileNames in os.walk( rootFilesystemPath): directoryNamesToRemove = [] for directoryName in directoryNames: if directoryName in self._ignoredDirectoryNames: directoryNamesToRemove.append(directoryName) for directoryName in directoryNamesToRemove: directoryNames.remove(directoryName) if '__init__.py' in fileNames: strippedPath = directoryPath.partition(rootFilesystemPath)[2] pathParts = [x for x in strippedPath.split(os.path.sep) if x] pathParts.insert(0, 'music21') packagesystemPath = '.'.join(pathParts) try: module = __import__(packagesystemPath, fromlist=['*']) if getattr(module, '_DOC_IGNORE_MODULE_OR_PACKAGE', False): # Skip examining any other file or directory below # this directory. if self.verbose: print('\tIGNORED {0}/*'.format( common.relativepath(directoryPath))) directoryNames[:] = [] continue except ImportError: pass for fileName in fileNames: if fileName in self._ignoredFileNames: continue if not fileName.endswith('.py'): continue if fileName.startswith('_') and not fileName.startswith('__'): continue filePath = os.path.join(directoryPath, fileName) strippedPath = filePath.partition(rootFilesystemPath)[2] pathParts = [x for x in os.path.splitext( strippedPath)[0].split(os.path.sep)[1:] if x] pathParts = ['music21'] + pathParts packagesystemPath = '.'.join(pathParts) try: module = __import__(packagesystemPath, fromlist=['*']) if getattr(module, '_DOC_IGNORE_MODULE_OR_PACKAGE', False): if self.verbose: print('\tIGNORED {0}'.format( common.relativepath(filePath))) continue yield module except ImportError: pass raise StopIteration
def testBackBasic(self): import os from music21 import converter, common fpDir = os.path.join(common.getSourceFilePath(), 'musedata', 'testPrimitive', 'test02') s = converter.parse(fpDir) # note: this is a multi-staff work, but presently gets encoded # as multiple voices measures = s.parts[0].measures(1,5) self.assertEqual(len(measures[0].flat.notesAndRests), 6) self.assertEqual(len(measures[1].flat.notesAndRests), 12) self.assertEqual(len(measures[2].flat.notesAndRests), 5) self.assertEqual(len(measures[3].flat.notesAndRests), 8) self.assertEqual(len(measures[4].flat.notesAndRests), 7) #s.show() # alternative test # note: this encoding has many parts in a single staff # not sure how to translate fpDir = os.path.join(common.getSourceFilePath(), 'musedata', 'testPrimitive', 'test03') s = converter.parse(fpDir)
def xtestImportSorgen(self): ci = CapellaImporter() capellaDirPath = common.getSourceFilePath() / 'capella' oswaldPath = capellaDirPath / r'Nu_rue_mit_sorgen.capx' ci.readCapellaXMLFile(oswaldPath) ci.parseXMLText() #firstSystemObject = ci.mainDom.documentElement.getElementsByTagName('system')[0] #m21SystemObj = ci.systemFromSystem(firstSystemObject) #m21SystemObj.show('text') #scoreElement = ci.mainDom.documentElement.getElementsByTagName('score')[0] scoreObj = ci.systemScoreFromScore(ci.mainDom.documentElement) partScore = ci.partScoreFromSystemScore(scoreObj) partScore.show()
def getDocumentationFiles(runOne=False): ''' returns a list of namedtuples for each module that should be run >>> from music21.test import testDocumentation >>> testDocumentation.getDocumentationFiles() [ModTuple(module='index.rst', fullModulePath='...music21/documentation/autogenerated/index.rst', moduleNoExtension='index', autoGen=False), ..., ModTuple(module='usersGuide_03_pitches.rst', fullModulePath='...music21/documentation/autogenerated/usersGuide/usersGuide_03_pitches.rst', moduleNoExtension='usersGuide_03_pitches', autoGen=True), ...] ''' from music21 import common music21basedir = common.getSourceFilePath() builddocRstDir = os.path.join(music21basedir, 'documentation', 'source') if not os.path.exists(builddocRstDir): raise Music21Exception( "Cannot run tests on documentation because the rst files " + "in documentation/source do not exist") allModules = [] for root, unused_dirnames, filenames in os.walk(builddocRstDir): for module in filenames: fullModulePath = os.path.join(root, module) if not module.endswith('.rst'): continue if module.startswith('module'): # we have this already... continue if module in skipModules: continue if runOne is not False: if not module.endswith(runOne): continue with io.open(fullModulePath, 'r', encoding='utf-8') as f: incipit = f.read(1000) if 'AUTOMATICALLY GENERATED' in incipit: autoGen = True else: autoGen = False moduleNoExtension = module[:-4] modTuple = ModTuple(module, fullModulePath, moduleNoExtension, autoGen) allModules.append(modTuple) return allModules
def argRun(): parser = argparse.ArgumentParser( description='Run pylint on music21 according to style guide.') parser.add_argument('files', metavar='filename', type=str, nargs='*', help='Files to parse (default nearly all)') parser.add_argument('--strict', action='store_true', help='Run the file in strict mode') args = parser.parse_args() #print(args.files) #print(args.strict) files = args.files if args.files else None if files: sfp = common.getSourceFilePath() files = [common.relativepath(f, sfp) for f in files] main(files, args.strict)
def _runNBConvert(self, ipythonNotebookFilePath): # import music21 #runDirectoryPath = common.getBuildDocFilePath() nbconvertPath = os.path.join(os.path.dirname(common.getSourceFilePath()), 'ext', 'nbconvert', 'nbconvert.py') #print nbconvertPath # pathParts = os.path.dirname(getSourceFilePath())music21.__path__ + [ # 'ext', # 'nbconvert', # 'nbconvert.py', # ] # nbconvertPath = os.path.join(*pathParts) nbconvertCommand = '{executable} rst {notebook}'.format( executable=nbconvertPath, notebook=ipythonNotebookFilePath, ) #subprocess.call(nbconvertCommand, shell=True, cwd=runDirectoryPath) subprocess.call(nbconvertCommand, shell=True)
def testBasic(self): from music21 import musedata from music21 import common fp1 = (common.getSourceFilePath() / 'musedata' / 'testPrimitive' / 'test01' /'01.md') mdw = musedata.MuseDataWork() mdw.addFile(str(fp1)) # remove str in Py3.6 s = museDataWorkToStreamScore(mdw) #post = s.musicxml #s.show() self.assertEqual(len(s.parts), 1) self.assertEqual(s.parts[0].id, 'Clarinet in A') self.assertEqual(len(s.parts[0].flat.notesAndRests), 54)
def _runNBConvert(self, ipythonNotebookFilePath): # import music21 #runDirectoryPath = common.getBuildDocFilePath() nbconvertPath = os.path.join( os.path.dirname(common.getSourceFilePath()), 'ext', 'nbconvert', 'nbconvert.py') #print nbconvertPath # pathParts = os.path.dirname(getSourceFilePath())music21.__path__ + [ # 'ext', # 'nbconvert', # 'nbconvert.py', # ] # nbconvertPath = os.path.join(*pathParts) nbconvertCommand = '{executable} rst {notebook}'.format( executable=nbconvertPath, notebook=ipythonNotebookFilePath, ) #subprocess.call(nbconvertCommand, shell=True, cwd=runDirectoryPath) subprocess.call(nbconvertCommand, shell=True)
def corpusPathToKey(filePath, number=None): r''' Given a file path or corpus path, return the metadata key: >>> from music21 import metadata >>> mb = metadata.bundles.MetadataBundle() >>> key = mb.corpusPathToKey('bach/bwv1007/prelude') >>> key.endswith('bach_bwv1007_prelude') True >>> key = mb.corpusPathToKey('corelli/opus3no1/1grave.xml') >>> key.endswith('corelli_opus3no1_1grave_xml') True ''' if isinstance(filePath, pathlib.Path): try: filePath = filePath.relative_to(common.getSourceFilePath() / 'corpus') except ValueError: pass parts = filePath.parts if parts[0] == '/' and len(parts) > 1: parts = parts[1:] corpusPath = '_'.join(parts) else: if 'corpus' in filePath: # get filePath after corpus corpusPath = filePath.split('corpus')[-1] else: corpusPath = filePath if corpusPath.startswith(os.sep): corpusPath = corpusPath[1:] corpusPath = corpusPath.replace('/', '_') corpusPath = corpusPath.replace(os.sep, '_') corpusPath = corpusPath.replace('.', '_') # append name to metadata path if number is not None: return '{0}_{1}'.format(corpusPath, number) return corpusPath
def __init__(self, **keywords): self.iterIndex = 2 if ("filename" in keywords): self.filename = keywords["filename"] if self.filename: try: xbook = xlrd.open_workbook(self.filename) except IOError: xbook = xlrd.open_workbook(os.path.join(common.getSourceFilePath(), 'alpha', 'trecento', self.filename)) if ("sheetname" in keywords): self.sheetname = keywords["sheetname"] self.sheet = xbook.sheet_by_name(self.sheetname) self.totalRows = self.sheet.nrows self.rowDescriptions = self.sheet.row_values(0)
def __init__(self, **keywords): self.iterIndex = 2 if ("filename" in keywords): self.filename = keywords["filename"] if self.filename: try: xbook = xlrd.open_workbook(self.filename) except IOError: xbook = xlrd.open_workbook(os.path.join(common.getSourceFilePath(), 'alpha', 'trecento', self.filename)) if ("sheetname" in keywords): self.sheetname = keywords["sheetname"] self.sheet = xbook.sheet_by_name(self.sheetname) self.totalRows = self.sheet.nrows self.rowDescriptions = self.sheet.row_values(0)
def testImportMei4(self): ''' For the sake of completeness, this is the same as testImportMei3() but with a UTF-8 file. ''' try: # this works in Python 3.3+ from unittest import mock # pylint: disable=no-name-in-module except ImportError: try: # system library overrides the built-in import mock except ImportError: # last resort from music21.ext import mock with mock.patch('music21.mei.MeiToM21Converter') as mockConv: testPath = common.getSourceFilePath() / 'mei' / 'test' / 'notes_in_utf8.mei' testConverter = ConverterMEI() testConverter.parseFile(str(testPath)) # remove str in Py3.6 self.assertEqual(1, mockConv.call_count)
def testBasic(self): from music21 import musedata import os from music21 import common fp1 = os.path.join(common.getSourceFilePath(), 'musedata', 'testPrimitive', 'test01', '01.md') mdw = musedata.MuseDataWork() mdw.addFile(fp1) s = museDataWorkToStreamScore(mdw) #post = s.musicxml #s.show() self.assertEqual(len(s.parts), 1) self.assertEqual(s.parts[0].id, 'Clarinet in A') self.assertEqual(len(s.parts[0].flat.notesAndRests), 54)
def argRun(): parser = argparse.ArgumentParser( description='Run pylint on music21 according to style guide.') parser.add_argument('files', metavar='filename', type=str, nargs='*', help='Files to parse (default nearly all)') parser.add_argument('--strict', action='store_true', help='Run the file in strict mode') args = parser.parse_args() #print(args.files) #print(args.strict) files = args.files if args.files else None if files: sfp = common.getSourceFilePath() files = [common.relativepath(f, sfp) for f in files] main(files, args.strict)
def testBasic(self): from music21 import musedata import os from music21 import common fp1 = os.path.join(common.getSourceFilePath(), 'musedata', 'testPrimitive', 'test01', '01.md') mdw = musedata.MuseDataWork() mdw.addFile(fp1) s = museDataWorkToStreamScore(mdw) #post = s.musicxml #s.show() self.assertEqual(len(s.parts), 1) self.assertEqual(s.parts[0].id, 'Clarinet in A') self.assertEqual(len(s.parts[0].flat.notesAndRests), 54)
def corpusPathToKey(filePath, number=None): r''' Given a file path or corpus path, return the metadata key: >>> from music21 import metadata >>> mb = metadata.bundles.MetadataBundle() >>> key = mb.corpusPathToKey('bach/bwv1007/prelude') >>> key.endswith('bach_bwv1007_prelude') True >>> key = mb.corpusPathToKey('corelli/opus3no1/1grave.xml') >>> key.endswith('corelli_opus3no1_1grave_xml') True ''' if isinstance(filePath, pathlib.Path): try: filePath = filePath.relative_to(common.getSourceFilePath() / 'corpus') except ValueError: pass parts = filePath.parts if parts[0] == '/' and len(parts) > 1: parts = parts[1:] corpusPath = '_'.join(parts) else: if 'corpus' in filePath: # get filePath after corpus corpusPath = filePath.split('corpus')[-1] else: corpusPath = filePath if corpusPath.startswith(os.sep): corpusPath = corpusPath[1:] corpusPath = corpusPath.replace('/', '_') corpusPath = corpusPath.replace(os.sep, '_') corpusPath = corpusPath.replace('.', '_') # append name to metadata path if number is not None: return '{0}_{1}'.format(corpusPath, number) return corpusPath
def testTsvHandler(self): name = 'tsvEg.tsv' # A short and improbably complicated test case complete with: # '@none' (rest entry), '/' relative root, and time signature changes. path = common.getSourceFilePath() / 'romanText' / name handler = TsvHandler(path) # Raw self.assertEqual(handler.tsvData[0][0], '.C.I6') self.assertEqual(handler.tsvData[1][0], '#viio6/ii') # Chords handler.tsvToChords() testTabChord1 = handler.chordList[0] # Also tests makeTabChord() testTabChord2 = handler.chordList[1] self.assertIsInstance(testTabChord1, TabChord) self.assertEqual(testTabChord1.combinedChord, '.C.I6') self.assertEqual(testTabChord1.numeral, 'I') self.assertEqual(testTabChord2.combinedChord, '#viio6/ii') self.assertEqual(testTabChord2.numeral, '#vii') # Change Representation self.assertEqual(testTabChord1.representationType, 'DCML') testTabChord1._changeRepresentation() self.assertEqual(testTabChord1.numeral, 'I') testTabChord2._changeRepresentation() self.assertEqual(testTabChord2.numeral, 'vii') # M21 RNs m21Chord1 = testTabChord1.tabToM21() m21Chord2 = testTabChord2.tabToM21() self.assertEqual(m21Chord1.figure, 'I') self.assertEqual(m21Chord2.figure, 'viio6/ii') self.assertEqual(m21Chord1.key.name, 'C major') self.assertEqual(m21Chord2.key.name, 'C major') # M21 stream strm = handler.toM21Stream() self.assertEqual(strm.parts[0].measure(1)[0].figure, 'I') # First item in measure 1
def testImportMei4(self): ''' For the sake of completeness, this is the same as testImportMei3() but with a UTF-8 file. ''' try: # this works in Python 3.3+ from unittest import mock # pylint: disable=no-name-in-module except ImportError: try: # system library overrides the built-in import mock except ImportError: # last resort from music21.ext import mock with mock.patch('music21.mei.MeiToM21Converter') as mockConv: from os import path testPath = path.join(common.getSourceFilePath(), 'mei', 'test', 'notes_in_utf8.mei') testConverter = ConverterMEI() testConverter.parseFile(testPath) self.assertEqual(1, mockConv.call_count)
def __iter__(self): rootFilesystemPath = common.getSourceFilePath() documentationPath = os.path.join( rootFilesystemPath, 'documentation', 'source', ) for pathParts in os.walk(documentationPath): directoryPath, fileNames = pathParts[0], pathParts[2] if directoryPath.endswith('.ipynb_checkpoints'): continue for fileName in fileNames: if '-checkpoint' in fileName: continue if fileName.endswith('.ipynb'): filePath = os.path.join( directoryPath, fileName, ) yield filePath
def getDocumentationFiles(runOne=False): ''' returns a list of namedtuples for each module that should be run >>> from music21.test import testDocumentation >>> testDocumentation.getDocumentationFiles() [ModTuple(module='index.rst', fullModulePath='...music21/documentation/source/index.rst', moduleNoExtension='index', isIPYNB=False), ...] ''' from music21 import common music21basedir = common.getSourceFilePath() builddocRstDir = os.path.join(music21basedir, 'documentation', 'source') if not os.path.exists(builddocRstDir): raise Exception( "Cannot run tests on documentation because the rst files in documentation/source do not exist" ) allModules = [] for root, unused_dirnames, filenames in os.walk(builddocRstDir): for module in filenames: fullModulePath = os.path.join(root, module) if not module.endswith('.rst'): continue if module.startswith('module'): continue if module in skipModules: continue if runOne is not False: if not module.endswith(runOne): continue moduleNoExtension = module[:-4] if moduleNoExtension + '.ipynb' in filenames: isIPYNB = True else: isIPYNB = False modTuple = ModTuple(module, fullModulePath, moduleNoExtension, isIPYNB) allModules.append(modTuple) return allModules
def testImportMei3(self): ''' When the file uses UTF-16 encoding rather than UTF-8 (which happens if it was exported from the "sibmei" plug-in for Sibelius. ''' try: # this works in Python 3.3+ from unittest import mock # pylint: disable=no-name-in-module except ImportError: try: # system library overrides the built-in import mock except ImportError: # last resort from music21.ext import mock with mock.patch('music21.mei.MeiToM21Converter') as mockConv: testPath = common.getSourceFilePath() / 'mei' / 'test' / 'notes_in_utf16.mei' testConverter = ConverterMEI() testConverter.parseFile(str(testPath)) self.assertEqual(1, mockConv.call_count)
def testCompositeLyrics(self): xmlDir = common.getSourceFilePath() / 'musicxml' / 'lilypondTestSuite' fp = xmlDir / '61l-Lyrics-Elisions-Syllables.xml' s = converter.parse(fp) n1 = s[note.NotRest].first() xmlOut = self.getXml(n1) self.assertIn('<lyric name="1" number="1">', xmlOut) self.assertIn('<syllabic>begin</syllabic>', xmlOut) self.assertIn('<text>a</text>', xmlOut) tree = self.getET(s) mxLyrics = tree.findall('part/measure/note/lyric') ly0 = mxLyrics[0] self.assertEqual(ly0.get('number'), '1') self.assertEqual(len(ly0), 2) self.assertEqual(ly0[0].tag, 'syllabic') self.assertEqual(ly0[1].tag, 'text') # contents already checked above ly1 = mxLyrics[1] self.assertEqual(len(ly1), 5) tags = [child.tag for child in ly1] self.assertEqual(tags, ['syllabic', 'text', 'elision', 'syllabic', 'text']) self.assertEqual(ly1.find('elision').text, ' ') self.assertEqual(ly1.findall('syllabic')[0].text, 'middle') self.assertEqual(ly1.findall('text')[0].text, 'b') self.assertEqual(ly1.findall('syllabic')[1].text, 'middle') self.assertEqual(ly1.findall('text')[1].text, 'c') ly2 = mxLyrics[2] self.assertEqual(len(ly2), 5) tags = [child.tag for child in ly2] self.assertEqual(tags, ['syllabic', 'text', 'elision', 'syllabic', 'text']) self.assertIsNone(ly2.find('elision').text) self.assertEqual(ly2.findall('syllabic')[0].text, 'middle') self.assertEqual(ly2.findall('text')[0].text, 'd') self.assertEqual(ly2.findall('syllabic')[1].text, 'end') self.assertEqual(ly2.findall('text')[1].text, 'e')
def testMIDIParse(): from music21 import converter, common from music21 import freezeThaw # a = 'https://github.com/ELVIS-Project/vis/raw/master/test_corpus/prolationum-sanctus.midi' # c = converter.parse(a) # c = corpus.parse('bwv66.6', forceSource=True) # v = freezeThaw.StreamFreezer(c) # v.setupSerializationScaffold() # return v.writeStr() # returns a string a = common.getSourceFilePath() / 'midi' / 'testPrimitive' / 'test03.mid' #a = 'https://github.com/ELVIS-Project/vis/raw/master/test_corpus/prolationum-sanctus.midi' c = converter.parse(a) v = freezeThaw.StreamFreezer(c) v.setupSerializationScaffold() mockType = lambda x: x.__class__.__name__ == 'weakref' ty = TreeYielder(mockType) for val in ty.run(c): print(val, ty.currentLevel())
def testSpannersWritePartStaffs(self): ''' Test that spanners are gathered on the PartStaffs that need them. Multi-staff instruments are separated on import into distinct PartStaff objects, where usually all the spanners will remain on the first object. ''' xmlDir = common.getSourceFilePath() / 'musicxml' / 'lilypondTestSuite' s = converter.parse(xmlDir / '43e-Multistaff-ClefDynamics.xml') # StaffGroup spanner stored on the score self.assertEqual(len(s.spanners), 1) self.assertIsInstance(s.spanners[0], layout.StaffGroup) # Crescendo in LH actually stored in first PartStaff object self.assertEqual(len(s.parts[0].spanners), 1) self.assertEqual(len(s.parts[1].spanners), 0) self.assertIsInstance(s.parts[0].spanners[0], dynamics.Crescendo) # Will it be found by coreGatherMissingSpanners without being inserted? s.makeNotation(inPlace=True) self.assertEqual(len(s.parts[1].spanners), 0) # and written after the backup tag, i.e. on the LH? xmlOut = self.getXml(s) xmlAfterFirstBackup = xmlOut.split('</backup>\n')[1] def stripInnerSpaces(txt): return re.sub(r'\s+', ' ', txt) self.assertIn( stripInnerSpaces(''' <direction placement="below"> <direction-type> <wedge number="1" spread="0" type="crescendo" /> </direction-type> <staff>2</staff> </direction>'''), stripInnerSpaces(xmlAfterFirstBackup))
def __init__(self, useExtended=False, autoWalk=True): self.dirParent = str(common.getSourceFilePath()) self.useExtended = useExtended self.modulePaths = [] self.moduleSkip = [ 'testSingleCoreAll.py', 'testExternal.py', 'testDefault.py', 'testInstallation.py', 'testLint.py', 'testPerformance.py', 'timeGraphs.py', 'timeGraphImportStar.py', 'multiprocessTest.py', 'setup.py', # somehow got committed once and now screwing things up... # 'corpus/virtual.py', # offline for v.4+ 'figuredBass/examples.py', # 40 seconds and runs fine ] self.moduleSkipExtended = self.moduleSkip + [ 'configure.py', # runs oddly... 'testSerialization.py', 'mptCurses.py', 'memoryUsage.py', 'test/treeYield.py', 'test/toggleDebug.py', 'musicxml/testPrimitive.py', 'musicxml/testFiles.py', 'musedata/testPrimitive/test01/__init__.py', 'musedata/testPrimitive/__init__.py', 'mei/test_base.py', 'humdrum/questions.py', 'documentation/upload.py', 'documentation/source/conf.py', 'documentation/extensions.py', 'corpus/testCorpus.py', 'audioSearch/scoreFollower.py', 'audioSearch/repetitionGame.py', 'abcFormat/testFiles.py', ] # run these first... self.slowModules = [ 'metadata/caching', 'metadata/bundles', 'features', 'graph', 'graph/plot', 'graph/axis', 'graph/primitives', 'freezeThaw', 'figuredBass/realizer', 'features/jSymbolic', 'features/native', 'figuredBass/examples', 'braille/test', 'test/testStream', 'analysis/windowed', 'converter/__init__', 'musicxml/m21ToXml', 'musicxml/xmlToM21', 'romanText/translate', 'alpha/theoryAnalysis/theoryAnalyzer', ] # skip any path that contains this string self.pathSkip = [ 'music21/ext', # not just 'ext' because of 'text!' ] self.pathSkipExtended = self.pathSkip + [] self.moduleSkip = [x.replace('/', os.sep) for x in self.moduleSkip] self.moduleSkipExtended = [ x.replace('/', os.sep) for x in self.moduleSkipExtended ] self.pathSkip = [x.replace('/', os.sep) for x in self.pathSkip] self.pathSkipExtended = [ x.replace('/', os.sep) for x in self.pathSkipExtended ] self.slowModules = [x.replace('/', os.sep) for x in self.slowModules] # search on init if autoWalk: self.walk()
music += '\n' with open(fp, 'w') as f: f.write(music) return fp if __name__ == '__main__': from music21 import common import os converter.registerSubconverter(QMConverter) print('\nFILE') print('+++++++++++++++++++++++++') parserPath = common.getSourceFilePath() + os.path.sep + 'converter' testPath = parserPath + os.path.sep + 'quarterMusicTestIn.qm' a = converter.parse(testPath) a.show('text') print('\nIn-Line') print('+++++++++++++++++++++++++') b = converter.parse('quarterMusic: G C G') b.show('text') print(b.write('qm'))
def __init__(self, useExtended=False): self.dirParent = common.getSourceFilePath() self.useExtended = useExtended self.modulePaths = [] self.moduleSkip = [ 'testSingleCoreAll.py', 'testExternal.py', 'testDefault.py', 'testInstallation.py', 'testLint.py', 'testPerformance.py', 'timeGraphs.py', 'exceldiff.py', 'multiprocessTest.py', 'figuredBass/examples.py', # 40 seconds and runs fine ] self.moduleSkipExtended = self.moduleSkip + [ 'exceldiff.py', 'mrjobaws.py', # takes too long. 'configure.py', # runs oddly... 'timeGraphImportStar.py', 'testSerialization.py', 'mptCurses.py', 'memoryUsage.py', 'dedent.py', 'testPerformance.py', 'multiprocessTest.py', 'timeGraphs.py', 'alpha/trecento/quodJactatur.py', 'alpha/trecento/find_vatican1790.py', 'alpha/trecento/findSevs.py', 'alpha/trecento/correlations.py', 'alpha/trecento/contenanceAngloise.py', 'alpha/trecento/capuaProbabilities.py', 'alpha/theoryAnalysis/wwnortonMGTA.py', 'test/treeYield.py', 'test/toggleDebug.py', 'musicxml/testPrimitive.py', 'musicxml/testFiles.py', 'musedata/testPrimitive/test01/__init__.py', 'musedata/testPrimitive/__init__.py', 'mei/test_base.py', 'humdrum/questions.py', 'documentation/upload.py', 'documentation/source/conf.py', 'documentation/extensions.py', 'corpus/testCorpus.py', 'composition/seeger.py', 'composition/aug30.py', 'audioSearch/scores/__init__.py', 'audioSearch/scoreFollower.py', 'audioSearch/repetitionGame.py', 'audioSearch/omrfollow.py', 'audioSearch/humanVScomputer.py', 'audioSearch/graphicalInterfaceTranscriber.py', 'audioSearch/graphicalInterfaceSF.py', 'audioSearch/graphicalInterfaceGame.py', 'analysis/phrasing.py', 'abcFormat/testFiles.py', ] # run these first... self.slowModules = [ 'graph', 'figuredBass/realizer', 'features/jSymbolic', 'features/native', 'figuredBass/examples', 'braille/test', 'test/testStream', 'analysis/windowed', 'converter/__init__', 'metadata/bundles', 'musicxml/m21ToXml', 'musicxml/xmlToM21', 'musicxml/fromMxObjects', 'musicxml/m21ToString', 'romanText/translate', 'alpha/theoryAnalysis/theoryAnalyzer', ] # skip any path that contains this string self.pathSkip = [ 'obsolete', 'music21/ext', # not just "ext" because of "text!" 'alpha/webapps/server', 'alpha/webapps/archive', ] self.pathSkipExtended = self.pathSkip + [ 'demos', ] self.moduleSkip = [x.replace('/', os.sep) for x in self.moduleSkip] self.moduleSkipExtended = [ x.replace('/', os.sep) for x in self.moduleSkipExtended ] self.pathSkip = [x.replace('/', os.sep) for x in self.pathSkip] self.pathSkipExtended = [ x.replace('/', os.sep) for x in self.pathSkipExtended ] self.slowModules = [x.replace('/', os.sep) for x in self.slowModules] # search on init self._walk()
def testComplete(self): ci = CapellaImporter() capellaDirPath = common.getSourceFilePath() / 'capella' oswaldPath = capellaDirPath / r'Nu_rue_mit_sorgen.capx' partScore = ci.scoreFromFile(oswaldPath) partScore.show()
def main(fnAccept=None): ''' `fnAccept` is a list of one or more files to test. ''' sourceFolder = common.getSourceFilePath() mg = test.ModuleGather() print( "If you get an error, make sure that 'sudo pip install pylint' is there" ) # only accept a few file names for now if fnAccept in (None, []): fnAccept = ['stream'] fnPathReject = [ '/ext/', 'bar.py', # crashes pylint... 'repeat.py', # hangs pylint... 'spanner.py', # hangs pylint... ] #fnAccept = ['stream.py', 'note.py', 'chord.py'] disable = [ #'C0301', 'C0302', 'C0103', 'C0330', 'C0324', #'W0621', 'W0511', #'W0404', 'R0201', 'R0904', 'E1101', 'R0914', 'R0903', #'R0911', 'R0902', 'unnecessary-pass', # nice, but not really a problem... 'locally-disabled', # test for this later, but hopefully will know what they're doing 'arguments-differ', # someday... 'abstract-class-instantiated', # this trips on the fractions.Fraction() class. 'redefined-builtin', # remove when Eclipse tags are parsed @ReservedAssignment = pylint: disable=W0622 'fixme', # known... 'superfluous-parens', # next... 'too-many-statements', # someday 'no-member', # important, but too many false positives 'too-many-arguments', # definitely! but takes too long to get a fix now... 'too-many-public-methods', # maybe, look 'too-many-branches', # yes, someday 'too-many-locals', # no 'too-many-lines', # yes, someday. 'bad-whitespace', # maybe later, but "bad" isn't something I necessarily agree with 'bad-continuation', # never remove -- this is a good thing many times. 'line-too-long', # maybe later 'too-many-return-statements', # we'll see 'unpacking-non-sequence', # gets it wrong too often. 'too-many-instance-attributes', # maybe later 'invalid-name', # never remove -- these are good music21 names; fix the regexp instead... 'no-self-use', # maybe later 'too-few-public-methods', # never remove or set to 1 'trailing-whitespace', # should ignore blank lines with tabs 'missing-docstring', # gets too many well-documented properties 'star-args', # no problem with them... 'protected-access', # this is an important one, but for now we do a lot of # x = copy.deepcopy(self); x._volume = ... which is not a problem... 'unused-argument', 'import-self', # fix is either to get rid of it or move away many tests... ] cmd = [ '/usr/bin/env pylint -f colorized ' + '--dummy-variables-rgx="_|dummy|unused|i|j|junk" ' + '--docstring-min-length=3 ' + '--max-args=7 ' + # should be 5 later, but baby steps '--bad-name="foo,shit,f**k,stuff" ' # definitely allow "bar" for barlines ] for pyLintId in disable: cmd.append('--disable=%s' % pyLintId) # add entire package for fp in mg.modulePaths: rejectIt = False for rejectPath in fnPathReject: if rejectPath in fp: rejectIt = True break if rejectIt: continue fpRelative = fp.replace(sourceFolder, '') unused_dir, fn = os.path.split(fpRelative) fnNoExt = fn.replace('.py', '') fpRelativeNoSlash = fpRelative[1:] if fn in fnAccept or fnNoExt in fnAccept or fpRelative in fnAccept or fpRelativeNoSlash in fnAccept: cmdFile = cmd + [fp] print(' '.join(cmdFile)) if common.getPlatform() != 'win': os.system(' '.join(cmdFile))
def printSummary(summaryOutput, timeStart, pathsToRun): outStr = "" summaryOutputTwo = [i[1] for i in summaryOutput] for fp in pathsToRun: if fp not in summaryOutputTwo: failLog = ("NoResult", fp) summaryOutput.append(failLog) totalTests = 0 skippedSummary = [] successSummary = [] errorsFoundSummary = [] otherSummary = [] for l in summaryOutput: (returnCode, fp) = (l[0], l[1]) if returnCode == 'Skipped': skippedSummary.append("Skipped: %s" % fp) elif returnCode == 'NoResult': otherSummary.append("Silent test fail for %s: Run separately!" % fp) elif returnCode == 'UntrappedException': otherSummary.append("Untrapped Exception for unknown module: %s" % fp) elif returnCode == 'TrappedException': (moduleName, excp) = (l[2], l[3]) otherSummary.append("Trapped Exception for module %s, at %s: %s" % (moduleName, fp, excp)) elif returnCode == 'LargeException': excp = l[2] otherSummary.append("Large Exception for file %s: %s" % (fp, excp)) elif returnCode == 'ImportError': otherSummary.append("Import Error for %s" % fp) elif returnCode == 'NotInTree': otherSummary.append("Not in Tree Error: %s " % l[2]) elif returnCode == 'TestsRun': (moduleName, textTestResultObj) = (l[2], l[3]) testsRun = textTestResultObj.testsRun totalTests += testsRun if textTestResultObj.wasSuccessful(): successSummary.append("%s successfully ran %d tests" % (moduleName, testsRun)) else: errorsList = textTestResultObj.errors # not the original errors list! see pickle note above failuresList = textTestResultObj.failures errorsFoundSummary.append( "\n-----------\n%s had %d ERRORS and %d FAILURES in %d tests:" % (moduleName, len(errorsList), len(failuresList), testsRun)) for e in errorsList: outStr += e + "\n" errorsFoundSummary.append('%s' % (e)) for f in failuresList: outStr += f + "\n" errorsFoundSummary.append('%s' % (f)) # for e in errorsList: # print e[0], e[1] # errorsFoundSummary.append('%s: %s' % (e[0], e[1])) # for f in failuresList: # print f[0], f[1] # errorsFoundSummary.append('%s: %s' % (f[0], f[1])) else: otherSummary.append("Unknown return code %s" % l) outStr += "\n\n---------------SUMMARY---------------------------------------------------\n" for l in skippedSummary: outStr += l + "\n" for l in successSummary: outStr += l + "\n" for l in otherSummary: outStr += l + "\n" for l in errorsFoundSummary: outStr += l + "\n" outStr += "-------------------------------------------------------------------------\n" elapsedTime = time.time() - timeStart outStr += "Ran %d tests in %.4f seconds\n" % (totalTests, elapsedTime) sys.stdout.flush() print(outStr) sys.stdout.flush() from music21 import common import datetime with open( os.path.join(common.getSourceFilePath(), 'test', 'lastResults.txt'), 'w') as f: f.write(outStr) f.write("Run at " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
.. code-block:: python assert obj.name == thawed.name If you will never need to load (regenerate the Python class from JSON), you can pass in the keyword unpicklable=False to prevent extra information from being added to JSON:: oneway = jsonpickle.encode(obj, unpicklable=False) result = jsonpickle.decode(oneway) assert obj.name == result['name'] == 'Awesome' """ import sys, os from music21 import common sys.path.append(common.getSourceFilePath() + os.path.sep + 'ext') from jsonpickle import pickler from jsonpickle import unpickler from jsonpickle.backend import JSONBackend from jsonpickle.version import VERSION # ensure built-in handlers are loaded __import__('jsonpickle.handlers') __all__ = ('encode', 'decode') __version__ = VERSION json = JSONBackend() # Export specific JSONPluginMgr methods into the jsonpickle namespace
def __init__(self): self.outputDirectory = None self.docBasePath = os.path.join(common.getSourceFilePath(), 'documentation') self.docSourcePath = os.path.join(self.docBasePath, 'source') self.docGeneratedPath = os.path.join(self.docBasePath, 'autogenerated')
# -*- coding: utf-8 -*- import os import io from music21 import common if __name__ == '__main__': directory = common.getSourceFilePath() for root, dirs, files in os.walk(directory): for f in files: if f.endswith('.py') is not True: continue fullf = root + os.sep + f if 'ext' in root: continue with io.open(fullf, encoding='latin-1') as fh: data = fh.read() head = data[0:200] if 'utf-8' not in head: print(fullf, ' is not utf-8 compliant')