Ejemplo n.º 1
0
    def status(self):
        '''Given a file path specified with __init__, look for an up to date pickled version of this file path. If it exists, return its fp, other wise return the original file path.

        Return arguments are file path to load, boolean whether to write a pickle, and the file path of the pickle.
        '''
        fpScratch = environLocal.getRootTempDir()
        format = common.findFormatFile(self.fp)

        if format == 'pickle': # do not pickle a pickle
            if self.forceSource:
                raise PickleFilterException('cannot access source file when only given a file path to a pickled file.')
            writePickle = False # cannot write pickle if no scratch dir
            fpLoad = self.fp
            fpPickle = None                    
        elif fpScratch == None or self.forceSource:
            writePickle = False # cannot write pickle if no scratch dir
            fpLoad = self.fp
            fpPickle = None
        else: # see which is more up to doate
            fpPickle = self._getPickleFp(fpScratch)
            if not os.path.exists(fpPickle):
                writePickle = True # if pickled file does not exist
                fpLoad = self.fp
            else:
                post = common.sortFilesRecent([self.fp, fpPickle])
                if post[0] == fpPickle: # pickle is most recent
                    writePickle = False
                    fpLoad = fpPickle
                elif post[0] == self.fp: # file is most recent
                    writePickle = True
                    fpLoad = self.fp
        return fpLoad, writePickle, fpPickle
Ejemplo n.º 2
0
 def parseFile(self, fp, forceSource=False):
     '''Given a file path, parse and store a music21 Stream.
     '''
     #environLocal.printDebug(['attempting to parseFile', fp])
     if not os.path.exists(fp):
         raise ConverterFileException('no such file eists: %s' % fp)
     # TODO: no extension matching for tinyNotation
     format = common.findFormatFile(fp) 
     self._setConverter(format, forceSource=forceSource)
     self._converter.parseFile(fp)
Ejemplo 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)
Ejemplo n.º 4
0
    def parseFile(self, fp):
        '''Open from a file path; check to see if there is a pickled
        version available and up to date; if so, open that, otherwise
        open source.
        '''
        # return fp to load, if pickle needs to be written, fp pickle
        # this should be able to work on a .mxl file, as all we are doing
        # here is seeing which is more recent

        pfObj = PickleFilter(fp, self.forceSource)
        # fpDst here is the file path to load, which may or may not be
        # a pickled file 
        fpDst, writePickle, fpPickle = pfObj.status() # get status

        formatSrc = common.findFormatFile(fp)
        # here we determine if we have pickled file or a musicxml file
        format = common.findFormatFile(fpDst)
        pickleError = False

        c = musicxml.Document()
        if format == 'pickle':
            environLocal.printDebug(['opening pickled file', fpDst])
            try:
                c.openPickle(fpDst)
            except (ImportError, EOFError):
                msg = 'pickled file (%s) is damaged; a new file will be created.' % fpDst
                pickleError = True
                writePickle = True
                if formatSrc == 'musicxml':
                    environLocal.printDebug([msg], environLocal)
                    fpDst = fp # set to orignal file path
                else:
                    raise ConverterException(msg)
            # check if this pickle is up to date
            if (hasattr(c.score, 'm21Version') and 
                c.score.m21Version >= musicxml.VERSION_MINIMUM):
                environLocal.printDebug(['pickled file version is compatible',
                c.score.m21Version])
            else:
                try:
                    environLocal.printDebug(['pickled file version is not compatible', c.score.m21Version])
                except AttributeError:
                    ## some old pickles have no versions
                    pass
                pickleError = True
                writePickle = True
                fpDst = fp # set to orignal file path

        if format == 'musicxml' or (formatSrc == 'musicxml' and pickleError):
            environLocal.printDebug(['opening musicxml file', fpDst])

            # here, we can see if this is a mxl or similar archive
            arch = ArchiveFilter(fpDst)
            if arch.isArchive():
                c.read(arch.getData())
            else: # its a file path
                c.open(fpDst)

        if writePickle:
            if fpPickle == None: # if original file cannot be found
                raise ConverterException('attempting to write pickle but no file path is given')
            environLocal.printDebug(['writing pickled file', fpPickle])
            c.writePickle(fpPickle)

        self._mxScore = c.score
        if len(self._mxScore) == 0:
            raise ConverterException('score from file path (...%s) no parts defined' % fp[-10:])
        self.load()