def fullLoadValidationStore(topObj): """hard load ValidationStore from old location """ from memops.format.xml import Util as xmlUtil from memops.universal import Io as uniIo from memops.format.xml import XmlIO root = topObj.memopsRoot locator = ( root.findFirstPackageLocator(targetName='ccp.molecule.Validation') or root.findFirstPackageLocator(targetName='any')) repository = locator.findFirstRepository() #repository = topObj.activeRepositories[0] fileLocation = repository.getFileLocation( 'ccp.molecule.StructureValidation') filePath = uniIo.joinPath(fileLocation, xmlUtil.getTopObjectFile(topObj)) XmlIO.loadFromStream(open(filePath), topObject=topObj, topObjId=topObj.guid, partialLoad=False)
def setupProject(self, fileName, projectName): self.projectFileName = fileName try: self.ccpnProject = XmlIO.loadProjectFile(fileName) print "Read existing CCPN project..." except: print "Creating new CCPN project..." projectName = returnMemopsWord(projectName) self.ccpnMemopsRoot = Implementation.MemopsRoot(name=projectName) self.nmrProject = Nmr.NmrProject(self.ccpnProject, name=self.ccpnProject.name) # # Set the location... # (path, file) = splitPath(fileName) self.ccpnProject.path = file self.ccpnProject.url.path = path outputPath = os.path.join(path, projectName) try: os.stat(outputPath) except: os.mkdir(outputPath) for url in self.ccpnProject.urls: if url != self.ccpnProject.url: url.path = outputPath # TODO: check name of project: if not same CRASH OUT! return self.ccpnProject
def loadProject(path, projectName=None, showWarning=None, askFile=None, askDir=None, suppressGeneralDataDir=False): """ Loads a project file and checks and deletes unwanted project repositories and changes the project repository path if the project has moved. Returns the project. (The project repository path is effectively the userData repository.) showWarning (if not None) has signature showWarning(title, message) askFile (if not None) has signature askFile(title, message, initial_value = '') askDir (if not None) has signature askDir(title, message, initial_value = '') Throws an IOError if there is an I/O error. Throws an ApiError if there is an API exception. """ from memops.format.xml import XmlIO if not showWarning: showWarning = printWarning path = normalisePath(path, makeAbsolute=True) # check if path exists and is directory if not os.path.exists(path): raise IOError('path "%s" does not exist' % path) if not os.path.isdir(path): raise IOError('path "%s" is not a directory' % path) projectFile = xmlUtil.getProjectFile(path, projectName) if projectName: # projectName was specified so projectFile better exist if not os.path.exists(projectFile): raise IOError('project file "%s" does not exist' % projectFile) else: # projectName was not specified so default projectFile might not exist if not os.path.exists(projectFile): projectFiles = xmlUtil.getPossibleProjectFiles(path) if len(projectFiles) == 0: raise IOError('"%s" contains no project file' % path) elif len(projectFiles) == 1: projectFile = projectFiles[0] elif askFile: projectFile = askFile('Select project file', 'Select project file', initial_value=projectFiles[0]) if not projectFile: # cancelled raise IOError('Cancelled') else: raise IOError( '"%s" contains %d project files, not sure which to use' % (path, len(projectFiles))) # TBD: should projectName be based on projectFile or on path??? # the way it is set here do not need to change project.name # but if you used the path then you would need to change it projectName = xmlUtil.getTopObjIdFromFileName(projectFile) # doing the loadProject twice has a bit of an overhead, but not much try: # below assumes TopObjects exist where stated, so might fail project = XmlIO.loadProject(path, projectName, partialLoad=False) except: print("\nFirst loading attempt failed - compatibility problem?.") project = None def isGeneralDataWriteable(generalDataRepository): oldPath = generalDataRepository.url.path return (isWindowsOS() or os.access(oldPath, os.W_OK | os.X_OK)) def isGeneralDataOk(project): if suppressGeneralDataDir: return True generalDataRepository = project.findFirstRepository(name='generalData') if generalDataRepository: return isGeneralDataWriteable(generalDataRepository) return True if project is not None and (not xmlUtil.areAllTopObjectsPresent(project) or not isGeneralDataOk(project)): # if not all loaded (shell) TopObjects can be found, try again project = None ###print "\nSome files unfindable - has project moved?." if project is None: ###print "Re-trying, skipping cached TopObjects:" project = XmlIO.loadProject(path, projectName, partialLoad=True) warningMessages = [] # try and fix project repository path, if needed packageLocator = project.packageLocator repositories = packageLocator.repositories for repository in repositories: if repository.url.path == path: oldPath = path break else: # change first repository path to point to this one, and also change backup repository = repositories[0] oldPath = repository.url.path warningMessages.append('Project file has moved from\n"%s"\nto\n"%s"' % (oldPath, path)) repository.url = Implementation.Url(path=path) # Necessary because activeRepositories are not set right # if file names do not match: project.__dict__['activeRepositories'].append(repository) projectRepository = repository # check backup path backupRepository = project.findFirstRepository(name="backup") if backupRepository: backupUrl = backupRepository.url oldBackupPath = backupUrl.path newBackupPath = path + '_backup' if oldBackupPath.startswith(oldPath): # hopefully true if path != oldPath: warningMessages.append( 'Backup is being changed from\n"%s"\nto\n"%s"' % (oldBackupPath, newBackupPath)) backupRepository.url = Implementation.Url(path=newBackupPath) elif not os.path.exists(oldBackupPath): warningMessages.append( 'Backup is being changed from\n"%s"\nto\n"%s"' % (oldBackupPath, newBackupPath)) backupRepository.url = Implementation.Url(path=newBackupPath) # check if project repository is called 'userData' if projectRepository.name != 'userData': warningMessages.append( 'Project has non-standard repository name "%s"' % projectRepository.name) # repoint dataStores that are in same directory as project # (but only if old path does not exist and new one does) if path != oldPath: oldDirectory = os.path.dirname(oldPath) newDirectory = os.path.dirname(path) for dataLocationStore in project.dataLocationStores: for dataStore in dataLocationStore.dataStores: fullPath = dataStore.fullPath if not os.path.exists(fullPath): dataUrl = dataStore.dataUrl dataLocation = dataUrl.url.dataLocation if dataLocation == oldDirectory or \ (dataLocation.startswith(oldDirectory) and dataLocation[len(oldDirectory)] == dirsep): newDataUrlPath = newDirectory + dataLocation[ len(oldDirectory):] newPath = joinPath(newDataUrlPath, dataStore.path) if os.path.exists(newPath): warningMessages.append( 'DataStore %s:%s path has been changed from\n"%s"\nto\n"%s"' % (dataStore.dataLocationStore.name, dataStore.serial, oldDirectory, newDataUrlPath)) dataUrl.url = dataUrl.url.clone( path=newDataUrlPath) # change refData to current one if need be refDataRepository = project.findFirstRepository(name='refData') if refDataRepository: oldPath = refDataRepository.url.path newPath = normalisePath(getDataDirectory()) if newPath != oldPath: warningMessages.append( 'refData has been changed from\n"%s"\nto\n"%s"' % (oldPath, newPath)) refDataRepository.url = Implementation.Url(path=newPath) else: warningMessages.append('Project has no repository with name "refData"') # change generalData to current one if need be generalDataRepository = project.findFirstRepository(name='generalData') if generalDataRepository and not suppressGeneralDataDir: oldPath = generalDataRepository.url.path if not os.path.exists(oldPath) or not isGeneralDataWriteable( generalDataRepository): newPath = normalisePath(os.path.expanduser('~/.ccpn/data')) if not os.path.exists(newPath): os.makedirs(newPath) warningMessages.append( 'generalData has been changed from\n"%s"\nto\n"%s"' % (oldPath, newPath)) generalDataRepository.url = Implementation.Url(path=newPath) # check other repository paths for repository in project.repositories: if repository not in (projectRepository, refDataRepository, backupRepository, generalDataRepository): oldPath = repository.url.path if not repository.stored: title = 'Repository being deleted' msg = 'Repository "%s" with path "%s" has no packageLocators, deleting' % ( repository.name, oldPath) repository.delete() elif not os.path.exists(oldPath): title = 'Repository path does not exist' if showWarning is printWarning: tt = '' else: tt = ' (see console for packages)' msg = 'Repository "%s" path "%s" does not exist%s' % ( repository.name, oldPath, tt) print('List of packageLocators for repository "%s":' % repository.name) for packageLocator in repository.stored: print(' %s' % packageLocator.targetName) if askDir: newPath = askDir(title, msg + ': enter new path', initial_value=oldPath) while newPath and not os.path.exists(newPath): msg = 'Path "%s" does not exist' % newPath newPath = askDir(title, msg + ': enter new path', initial_value=newPath) if newPath: repository.url = Implementation.Url( path=normalisePath(newPath)) else: warningMessages.append(msg) if warningMessages: showWarning('Warnings', '\n\n'.join(warningMessages)) return project
addText = moleculeText + ':' + chainText + ':' + resCodesText fout.write(" %-15s -> %-15s %s" % ("'" + resonanceName + "'", string.join(atomNames, ','), addText)) else: fout.write(" %-15s %-15s" % ("'" + resonanceName + "'", 'NOT LINKED')) fout.write(newline) return True ################### # Main of program # ################### if __name__ == "__main__": inFile = sys.argv[1] outFile = sys.argv[2] # Read xml file from memops.format.xml import XmlIO proj = XmlIO.loadProjectFile(inFile) writeMappingFile(proj, outFile)
def getChemComp(project, molType, ccpCode, download=True, showError=None, partialLoad=False, chemCompArchiveDir=None, copyFile=True): """ get ChemComp corresponding to molType,ccpCode, looking 1) in memory, 2) in Repositories on lookup path, 3) in chemCompArchiveDir directory, 4) downloading from msd ChemComp server. For 3) and 4) save new ChemComp in first Repository on PAckageLocator lookup path Do 4) only if download==True showError is an optional GUI error handler that can be passed in. partialLoad controls if only the TopObject (default) or the entire file is loaded copyFile can be set to False to avoid copying file from central archive (good for testing) Optimised to avoid mass reading. """ # First get it if already loaded chemComp = project.getByNavigation(('chemComps', (molType, ccpCode))) if chemComp is None: # try to load it from an existing repository - avoiding mass loading packageName = 'ccp.molecule.ChemComp' chemCompFileSearchString = "%s+%s+*.xml" % (molType, getCcpFileString(ccpCode)) chemCompXmlFile = findCcpXmlFile(project, packageName, chemCompFileSearchString) if chemCompXmlFile: chemComp = XmlIO.loadFromFile(project, chemCompXmlFile, partialLoad=partialLoad) if chemComp is None: # try to get it from chemCompArchiveDir directory, if any, or to download it. # Use custom directory if it was passed in!!!! if not chemCompArchiveDir: chemCompPath = getChemCompArchiveDataDir() else: chemCompPath = chemCompArchiveDir ccLocator = ( project.findFirstPackageLocator(targetName=packageName) or project.findFirstPackageLocator(targetName='any')) repository = ccLocator.findFirstRepository() fileFound = getChemCompXmlFile(repository, chemCompPath, molType, ccpCode, showError=showError, copyFile=copyFile) if not fileFound and download: fileFound = downloadChemCompXmlFile(repository, molType, ccpCode, showError=showError) if fileFound: chemComp = XmlIO.loadFromFile(project, fileFound, partialLoad=partialLoad) # return chemComp
from memops.format.xml import Util as xmlUtil from memops.format.xml import XmlIO from memops.universal import Io as uniIo from memops.api import Implementation import Tkinter # standard project name projectName = 'refIsotopeEditor' # get or make project repDir = uniIo.joinPath(uniIo.normalisePath(uniIo.os.getcwd()), projectName) projectFile = xmlUtil.getProjectFile(repDir, projectName=projectName) if os.path.isfile(projectFile): project = XmlIO.loadProjectFile(projectFile) else: project = Implementation.MemopsRoot(name=projectName) # reorder to make sure data are saved in refData labelingSchemeLocator = project.findFirstPackageLocator( targetName='ccp.molecule.ChemCompLabel') repositories = list(labelingSchemeLocator.repositories) for rr in repositories: if rr.name == 'refData': refRepository = rr break else: refRepository = None if refRepository is not None: repositories.remove(refRepository)
def loadProject(path, projectName=None, showWarning=None, askFile=None, askDir=None, suppressGeneralDataDir = False): """ Loads a project file and checks and deletes unwanted project repositories and changes the project repository path if the project has moved. Returns the project. (The project repository path is effectively the userData repository.) showWarning (if not None) has signature showWarning(title, message) askFile (if not None) has signature askFile(title, message, initial_value = '') askDir (if not None) has signature askDir(title, message, initial_value = '') Throws an IOError if there is an I/O error. Throws an ApiError if there is an API exception. """ from memops.format.xml import XmlIO if not showWarning: showWarning = printWarning path = normalisePath(path, makeAbsolute=True) # check if path exists and is directory if not os.path.exists(path): raise IOError('path "%s" does not exist' % path) if not os.path.isdir(path): raise IOError('path "%s" is not a directory' % path) projectFile = xmlUtil.getProjectFile(path, projectName) if projectName: # projectName was specified so projectFile better exist if not os.path.exists(projectFile): raise IOError('project file "%s" does not exist' % projectFile) else: # projectName was not specified so default projectFile might not exist if not os.path.exists(projectFile): projectFiles = xmlUtil.getPossibleProjectFiles(path) if len(projectFiles) == 0: raise IOError('"%s" contains no project file' % path) elif len(projectFiles) == 1: projectFile = projectFiles[0] elif askFile: projectFile = askFile('Select project file', 'Select project file', initial_value=projectFiles[0]) if not projectFile: # cancelled raise IOError('Cancelled') else: raise IOError('"%s" contains %d project files, not sure which to use' % (path, len(projectFiles))) # TBD: should projectName be based on projectFile or on path??? # the way it is set here do not need to change project.name # but if you used the path then you would need to change it projectName = xmlUtil.getTopObjIdFromFileName(projectFile) # doing the loadProject twice has a bit of an overhead, but not much try: # below assumes TopObjects exist where stated, so might fail project = XmlIO.loadProject(path, projectName, partialLoad=False) # check whether all topObject directories exist where they are said to exist # if not then reload project with partialLoad = True for repository in project.repositories: if not os.path.exists(repository.getFileLocation()): project = XmlIO.loadProject(path, projectName, partialLoad=True) break except: # this can happen if Implementation file is inconsistent with data model project = XmlIO.loadProject(path, projectName, partialLoad=True) # try and fix project repository path, if needed packageLocator = project.packageLocator repositories = packageLocator.repositories for repository in repositories: if repository.url.path == path: oldPath = path break else: # change first repository path to point to this one, and also change backup repository = repositories[0] oldPath = repository.url.path showWarning('Project moved', 'Project file has moved from "%s" to "%s", updating path' \ % (oldPath, path)) repository.url = Implementation.Url(path=path) projectRepository = repository # check backup path backupRepository = project.findFirstRepository(name="backup") if backupRepository: backupUrl = backupRepository.url oldBackupPath = backupUrl.path newBackupPath = path + '_backup' if oldBackupPath.startswith(oldPath): # hopefully true if path != oldPath: showWarning('Backup changed', 'Backup is being changed from "%s" to "%s", updating path' \ % (oldBackupPath, newBackupPath)) backupRepository.url = Implementation.Url(path=newBackupPath) elif not os.path.exists(oldBackupPath): showWarning('Backup changed', 'Backup is being changed from "%s" to "%s", updating path' \ % (oldBackupPath, newBackupPath)) backupRepository.url = Implementation.Url(path=newBackupPath) # check if project repository is called 'userData' if projectRepository.name != 'userData': showWarning('Project non-standard', 'Project has non-standard repository name "%s"' % projectRepository.name) # change refData to current one if need be refDataRepository = project.findFirstRepository(name='refData') if refDataRepository: oldPath = refDataRepository.url.path newPath = normalisePath(getDataDirectory()) if newPath != oldPath: showWarning('refData changed', 'refData has been changed from "%s" to "%s", updating paths' % (oldPath, newPath)) refDataRepository.url = Implementation.Url(path=newPath) else: showWarning('No refData repository', 'Project has no repository with name "refData"') # change generalData to current one if need be generalDataRepository = project.findFirstRepository(name='generalData') if generalDataRepository and not suppressGeneralDataDir: oldPath = generalDataRepository.url.path if not os.path.exists(oldPath): newPath = normalisePath(os.path.expanduser('~/.ccpn/data')) if not os.path.exists(newPath): os.makedirs(newPath) showWarning('generalData changed', 'generalData has been changed from "%s" to "%s", updating paths' % (oldPath, newPath)) generalDataRepository.url = Implementation.Url(path=newPath) # check other repository paths for repository in project.repositories: if repository not in (projectRepository, refDataRepository, backupRepository, generalDataRepository): oldPath = repository.url.path if not repository.stored: title = 'Repository being deleted' msg = 'Repository "%s" with path "%s" has no packageLocators, deleting' % (repository.name, oldPath) repository.delete() elif not os.path.exists(oldPath): title = 'Repository path does not exist' if showWarning is printWarning: tt = '' else: tt = ' (see console for packages)' msg = 'Repository "%s" path "%s" does not exist%s' % (repository.name, oldPath, tt) print 'List of packageLocators for repository "%s":' % repository.name for packageLocator in repository.stored: print ' %s' % packageLocator.targetName if askDir: newPath = askDir(title, msg + ': enter new path', initial_value=oldPath) while newPath and not os.path.exists(newPath): msg = 'Path "%s" does not exist' % newPath newPath = askDir(title, msg + ': enter new path', initial_value=newPath) if newPath: repository.url = Implementation.Url(path=normalisePath(newPath)) else: showWarning(title, msg) return project