Esempio n. 1
0
    def getUrlByExt(self, extList=None):
        '''Given a request for an extension, find a best match for a URL from
        the list of known URLs. If ext is None, return the first URL.
        '''
        if not common.isListLike(extList):
            extList = [extList]
        if extList is None or extList == [None]:
            return [self.urlList[0]]  # return a list of all

        post = []
        for ext in extList:
            for url in self.urlList:
                unused_format, extFound = common.findFormatExtURL(url)
                # environLocal.printDebug([extFound, ext])
                if extFound == ext:
                    post.append(url)
        return post  # no match
Esempio n. 2
0
    def getUrlByExt(self, extList=None):
        '''Given a request for an extension, find a best match for a URL from 
        the list of known URLs. If ext is None, return the first URL.
        '''
        if not common.isListLike(extList):
            extList = [extList]
        if extList == None or extList == [None]:
            return [self.urlList[0]] # return a list of all 

        post = []
        for ext in extList:
            for url in self.urlList:
                unused_format, extFound = common.findFormatExtURL(url)
                #environLocal.printDebug([extFound, ext])
                if extFound == ext:
                    post.append(url)
        return post # no match
Esempio n. 3
0
    def parseURL(self, url):
        '''Given a url, download and parse the file into a music21 Stream.

        Note that this checks the user Environment `autoDownlaad` setting before downloading. 
        '''
        autoDownload = environLocal['autoDownload']
        if autoDownload == 'allow':
            pass
        elif autoDownload == 'deny':
            raise ConverterException('automatic downloading of URLs is presently set to "%s"; configure your Environment "autoDownload" setting to "allow" to permit automatic downloading.' % autoDownload)
        elif autoDownload == 'ask':
            raise ConverterException('automatic downloading of URLs is presently set to "%s"; configure your Environment "autoDownload" setting to "allow" to permit automatic downloading.' % autoDownload)

        #url = urllib.quote(url) may need?
        format, ext = common.findFormatExtURL(url)
        if format == None: # cannot figure out what it is
            raise ConverterException('cannot determine file format of url: %s' % url)
        dir = environLocal.getRootTempDir()
        #dst = environLocal.getTempFile(ext)

        dst = self._getDownloadFp(dir, ext, url)

        if not os.path.exists(dst):
            try:
                environLocal.printDebug(['downloading to:', dst])
                fp, headers = urllib.urlretrieve(url, filename=dst)
            except IOError:
                raise ConverterException('cannot access file: %s' % url)
        else:
            environLocal.printDebug(['using already downloaded file:', dst])
            fp = dst

        # update format based on downloaded fp
        format = common.findFormatFile(fp) 
        self._setConverter(format, forceSource=False)
        self._converter.parseFile(fp)
Esempio n. 4
0
def getWorkReferences(sort=True):
    '''
    Return a data dictionary for all works in the corpus and (optionally) the
    virtual corpus. Returns a list of reference dictionaries, each each
    dictionary for a each composer. A 'works' dictionary for each composer
    provides references to dictionaries for all associated works.

    This is used in the generation of corpus documentation

    >>> post = corpus.getWorkReferences()

    '''
    # from music21 import corpus; corpus.getWorkReferences()
    # TODO: update this to use metadata
    results = []
    for composerDirectory, composer in corpora.CoreCorpus._composers:
        ref = {}
        ref['composer'] = composer
        ref['composerDir'] = composerDirectory
        ref['works'] = {}  # store by keys of name/dirname
        works = getComposer(composerDirectory)
        for path in works:
            # split by the composer dir to get relative path
            #environLocal.printDebug(['dir composer', composerDirectory, path])
            junk, fileStub = path.split(composerDirectory)
            if fileStub.startswith(os.sep):
                fileStub = fileStub[len(os.sep):]
            # break into file components
            fileComponents = fileStub.split(os.sep)
            # the first is either a directory for containing components
            # or a top-level name
            m21Format, ext = common.findFormatExtFile(fileComponents[-1])
            if ext is None:
                #environLocal.printDebug([
                #    'file that does not seem to have an extension',
                #    ext, path])
                continue
            # if not a file w/ ext, we will get None for format
            if m21Format is None:
                workStub = fileComponents[0]
            else:  # remove the extension
                workStub = fileComponents[0].replace(ext, '')
            # create list location if not already added
            if workStub not in ref['works']:
                ref['works'][workStub] = {}
                ref['works'][workStub]['files'] = []
                title = common.spaceCamelCase(workStub).title()
                ref['works'][workStub]['title'] = title
                ref['works'][workStub]['virtual'] = False
            # last component is name
            m21Format, ext = common.findFormatExtFile(fileComponents[-1])
            fileDict = {}
            fileDict['format'] = m21Format
            fileDict['ext'] = ext
            # all path parts after corpus
            fileDict['corpusPath'] = os.path.join(composerDirectory, fileStub)
            fileDict['fileName'] = fileComponents[-1]  # all after
            title = None
            # this works but takes a long time!
#             if format == 'musicxml':
#                 mxDocument = musicxml.Document()
#                 mxDocument.open(path)
#                 title = mxDocument.getBestTitle()
            if title is None:
                title = common.spaceCamelCase(
                    fileComponents[-1].replace(ext, ''))
                title = title.title()
            fileDict['title'] = title
            ref['works'][workStub]['files'].append(fileDict)
            # add this path
        results.append(ref)
    # get each VirtualWork object
    for vw in corpora.VirtualCorpus._virtual_works:
        composerDir = vw.corpusPath.split('/')[0]
        match = False
        for ref in results:
            # check composer reference or first part of corpusPath
            if (ref['composer'] == vw.composer or
                composerDir == ref['composerDir']):
                match = True
                break  # use this ref
        if not match:  # new composers, create a new ref
            ref = {}
            ref['composer'] = vw.composer
            ref['composerDir'] = composerDir
            ref['works'] = {}  # store by keys of name/dirname
        # work stub should be everything other than top-level
        workStub = vw.corpusPath.replace(composerDir + '/', '')
        ref['works'][workStub] = {}
        ref['works'][workStub]['virtual'] = True
        ref['works'][workStub]['files'] = []
        ref['works'][workStub]['title'] = vw.title
        for url in vw.urlList:
            m21Format, ext = common.findFormatExtURL(url)
            fileDict = {}
            fileDict['format'] = m21Format
            fileDict['ext'] = ext
            # all path parts after corpus
            fileDict['corpusPath'] = vw.corpusPath
            fileDict['title'] = vw.title
            fileDict['url'] = url
            ref['works'][workStub]['files'].append(fileDict)
        if not match:  # not found already, need to add
            results.append(ref)
    if sort:
        sortGroup = []
        for ref in results:
            sortGroupSub = []
            for workStub in ref['works']:
                # add title first for sorting
                sortGroupSub.append([
                    ref['works'][workStub]['title'],
                    workStub,
                    ])
            sortGroupSub.sort()
            ref['sortedWorkKeys'] = [y for unused_x, y in sortGroupSub]
            # prepare this sort group
            sortGroup.append([ref['composerDir'], ref])
        sortGroup.sort()
        results = [ref for junk, ref in sortGroup]
    return results
Esempio n. 5
0
def getWorkReferences(sort=True):
    '''
    Return a data dictionary for all works in the corpus and (optionally) the
    virtual corpus. Returns a list of reference dictionaries, each each
    dictionary for a each composer. A 'works' dictionary for each composer
    provides references to dictionaries for all associated works.

    This is used in the generation of corpus documentation

    ::

        >>> from music21 import corpus
        >>> post = corpus.getWorkReferences()

    '''
    # from music21 import corpus; corpus.getWorkReferences()
    # TODO: update this to use metadata
    results = []
    for composerDirectory, composer in corpora.CoreCorpus._composers:
        ref = {}
        ref['composer'] = composer
        ref['composerDir'] = composerDirectory
        ref['works'] = {}  # store by keys of name/dirname
        works = getComposer(composerDirectory)
        for path in works:
            # split by the composer dir to get relative path
            #environLocal.printDebug(['dir composer', composerDirectory, path])
            junk, fileStub = path.split(composerDirectory)
            if fileStub.startswith(os.sep):
                fileStub = fileStub[len(os.sep):]
            # break into file components
            fileComponents = fileStub.split(os.sep)
            # the first is either a directory for containing components
            # or a top-level name
            m21Format, ext = common.findFormatExtFile(fileComponents[-1])
            if ext is None:
                #environLocal.printDebug([
                #    'file that does not seem to have an extension',
                #    ext, path])
                continue
            # if not a file w/ ext, we will get None for format
            if m21Format is None:
                workStub = fileComponents[0]
            else:  # remove the extension
                workStub = fileComponents[0].replace(ext, '')
            # create list location if not already added
            if workStub not in ref['works']:
                ref['works'][workStub] = {}
                ref['works'][workStub]['files'] = []
                title = common.spaceCamelCase(workStub).title()
                ref['works'][workStub]['title'] = title
                ref['works'][workStub]['virtual'] = False
            # last component is name
            m21Format, ext = common.findFormatExtFile(fileComponents[-1])
            fileDict = {}
            fileDict['format'] = m21Format
            fileDict['ext'] = ext
            # all path parts after corpus
            fileDict['corpusPath'] = os.path.join(composerDirectory, fileStub)
            fileDict['fileName'] = fileComponents[-1]  # all after
            title = None
            # this works but takes a long time!
            #             if format == 'musicxml':
            #                 mxDocument = musicxml.Document()
            #                 mxDocument.open(path)
            #                 title = mxDocument.getBestTitle()
            if title is None:
                title = common.spaceCamelCase(fileComponents[-1].replace(
                    ext, ''))
                title = title.title()
            fileDict['title'] = title
            ref['works'][workStub]['files'].append(fileDict)
            # add this path
        results.append(ref)
    # get each VirtualWork object
    for vw in corpora.VirtualCorpus._virtual_works:
        composerDir = vw.corpusPath.split('/')[0]
        match = False
        for ref in results:
            # check composer reference or first part of corpusPath
            if (ref['composer'] == vw.composer
                    or composerDir == ref['composerDir']):
                match = True
                break  # use this ref
        if not match:  # new composers, create a new ref
            ref = {}
            ref['composer'] = vw.composer
            ref['composerDir'] = composerDir
            ref['works'] = {}  # store by keys of name/dirname
        # work stub should be everything other than top-level
        workStub = vw.corpusPath.replace(composerDir + '/', '')
        ref['works'][workStub] = {}
        ref['works'][workStub]['virtual'] = True
        ref['works'][workStub]['files'] = []
        ref['works'][workStub]['title'] = vw.title
        for url in vw.urlList:
            m21Format, ext = common.findFormatExtURL(url)
            fileDict = {}
            fileDict['format'] = m21Format
            fileDict['ext'] = ext
            # all path parts after corpus
            fileDict['corpusPath'] = vw.corpusPath
            fileDict['title'] = vw.title
            fileDict['url'] = url
            ref['works'][workStub]['files'].append(fileDict)
        if not match:  # not found already, need to add
            results.append(ref)
    if sort:
        sortGroup = []
        for ref in results:
            sortGroupSub = []
            for workStub in ref['works']:
                # add title first for sorting
                sortGroupSub.append([
                    ref['works'][workStub]['title'],
                    workStub,
                ])
            sortGroupSub.sort()
            ref['sortedWorkKeys'] = [y for unused_x, y in sortGroupSub]
            # prepare this sort group
            sortGroup.append([ref['composerDir'], ref])
        sortGroup.sort()
        results = [ref for junk, ref in sortGroup]
    return results