def __init__(self, name=None): if not isinstance(name, (six.string_types, type(None))): raise CorpusException("Name must be a string or None") if name is not None and not name: raise CorpusException("Name cannot be blank") if name == 'local': self._name = None else: self._name = name
def __init__(self, name=None): if not isinstance(name, (str, unicode, type(None))): raise CorpusException("Name must be a string or None") if name is not None and len(name) == 0: raise CorpusException("Name cannot be blank") if name == 'local': self._name = None else: self._name = name
def __init__(self, name=None): if not isinstance(name, (str, type(None))): raise CorpusException('Name must be a string or None') if name is not None and not name: raise CorpusException('Name cannot be blank') if name == 'local': self._name = None elif name in ('core', 'virtual'): raise CorpusException("The name '{}' is reserved.".format(name)) else: self._name = name
def getWork( workName, movementNumber=None, fileExtensions=None, ): ''' this parse method is called from `corpus.parse()` and does nothing differently from it. Searches all corpora for a file that matches the name and returns it parsed. ''' addXMLWarning = False workNameJoined = workName mxlWorkName = workName if workName in (None, ''): raise CorpusException('a work name must be provided as an argument') if not common.isListLike(fileExtensions): fileExtensions = [fileExtensions] if common.isIterable(workName): workNameJoined = os.path.sep.join(workName) if workNameJoined.endswith(".xml"): # might be compressed MXL file mxlWorkName = os.path.splitext(workNameJoined)[0] + ".mxl" addXMLWarning = True filePaths = None for corpusObject in iterateCorpora(): workList = corpusObject.getWorkList(workName, movementNumber, fileExtensions) if not workList and addXMLWarning: workList = corpusObject.getWorkList(mxlWorkName, movementNumber, fileExtensions) if not workList: continue if len(workList) >= 1: filePaths = workList break if filePaths is None: warningMessage = 'Could not find a' if addXMLWarning: warningMessage += 'n xml or mxl' warningMessage += ' work that met this criterion: {0};'.format( workName) warningMessage += ' if you are searching for a file on disk, ' warningMessage += 'use "converter" instead of "corpus".' raise CorpusException(warningMessage) else: if len(filePaths) == 1: return filePaths[0] else: return filePaths
def getMetadataBundleByCorpus(corpusObject): ''' Return the metadata bundle for a single Corpus object >>> cc = corpus.corpora.CoreCorpus() >>> mdb1 = corpus.manager.getMetadataBundleByCorpus(cc) >>> mdb1 <music21.metadata.bundles.MetadataBundle 'core': {... entries}> This is the same as calling `metadataBundle` on the corpus itself, but this is the routine that actually does the work. In other words, it's the call on the object that is redundant, not this routine. >>> mdb1 is cc.metadataBundle True Non-existent corpus... >>> lc = corpus.corpora.LocalCorpus('junk') >>> mdb1 = corpus.manager.getMetadataBundleByCorpus(lc) >>> mdb1 <music21.metadata.bundles.MetadataBundle 'junk': {0 entries}> ''' cacheMetadataBundleFromDisk(corpusObject) corpusName = corpusObject.name if corpusName in _metadataBundles: return _metadataBundles[corpusName] else: # pragma: no cover raise CorpusException( 'No metadata bundle found for corpus {0} with name {1}'.format( corpusObject, corpusName))
def manualCoreCorpusPath(self, expr): # pragma: no cover userSettings = environment.UserSettings() if expr is not None: path = common.cleanpath(expr) if not os.path.isdir(path) or not os.path.exists(path): raise CorpusException('path needs to be a path to an existing directory') userSettings['manualCoreCorpusPath'] = path else: userSettings['manualCoreCorpusPath'] = None environment.Environment().write()
def delete(self): r''' Delete a non-default local corpus from the user settings. ''' if self.name is None or self.name in ('core', 'virtual', 'local'): raise CorpusException('Cannot delete this corpus') elif not self.existsInSettings: return if os.path.exists(self.metadataBundle.filePath): os.remove(self.metadataBundle.filePath) userSettings = environment.UserSettings() del(userSettings['localCorporaSettings'][self.name]) environment.Environment().write()
def cacheFilePath(self, value): ''' Set the path to the file path that stores the .json file. ''' if not self.existsInSettings: raise CorpusException('Save this corpus before changing the cacheFilePath') localCorpusSettings = self._getSettings() localCorpusSettings.cacheFilePath = common.cleanpath(value, returnPathlib=True) en = environment.Environment() if self.name == 'local': en['localCorpusSettings'] = localCorpusSettings else: en['localCorporaSettings'][self.name] = localCorpusSettings en.write()
def fromCacheName(name): ''' Instantiate a specific corpus based on its `cacheName`: These are the same as `fromName`. >>> corpus.manager.fromCacheName('core') <music21.corpus.corpora.CoreCorpus> >>> corpus.manager.fromCacheName('virtual') <music21.corpus.corpora.VirtualCorpus> >>> corpus.manager.fromCacheName('local') <music21.corpus.corpora.LocalCorpus: 'local'> >>> corpus.manager.fromCacheName(None) <music21.corpus.corpora.LocalCorpus: 'local'> Other local corpora are different and prefaced by "local-": >>> corpus.manager.fromCacheName('local-testDummy') <music21.corpus.corpora.LocalCorpus: 'testDummy'> Raises a corpus exception if it is not an allowable cache name. >>> corpus.manager.fromCacheName('testDummy') Traceback (most recent call last): music21.exceptions21.CorpusException: Cannot parse a cacheName of 'testDummy' ''' if name == 'core': return corpora.CoreCorpus() elif name == 'virtual': return corpora.VirtualCorpus() elif name == 'local' or name is None: return corpora.LocalCorpus() elif name.startswith('local-'): return corpora.LocalCorpus(name=name[6:]) else: raise CorpusException("Cannot parse a cacheName of '{0}'".format(name))