Exemplo n.º 1
0
def launchApplication(projectDir=None):

  global top

  root = Tkinter.Tk()
  root.withdraw()
  top  = EntryCompletionGui(root)
 
  project = None
  if projectDir:
    projectDir = normalisePath(projectDir)
    askdir  = lambda title, prompt, initial_value, default_dir: askDir(title, prompt,
              initial_value, parent=top, extra_dismiss_text='Skip')
    askfile = lambda title, prompt, initial_value: askFile(title, prompt,
              initial_value, parent=top, extra_dismiss_text='Skip')
    try:
      project = loadProject(path=projectDir, showWarning=showWarning, askDir=askdir, askFile=askfile)

      userRepos = project.findFirstRepository(name='userData')
      chemCompPackLoc = project.findFirstPackageLocator(targetName='ccp.molecule.ChemComp')

      if userRepos not in chemCompPackLoc.repositories:
        chemCompPackLoc.addRepository(userRepos)

    except ApiError, e:
      showError('Reading project', e.error_msg, parent=top)
Exemplo n.º 2
0
def launchApplication(projectDir=None):

    global top

    root = Tkinter.Tk()
    root.withdraw(
    )  # get rid of the root window Tkinter would otherwise put up
    top = ApplicationPopup(
        root)  # creates our controlled window (this class in fact)

    project = None
    if projectDir:
        projectDir = normalisePath(projectDir)
        askdir = lambda title, prompt, initial_value, default_dir: askDir(
            title,
            prompt,
            initial_value,
            parent=top,
            extra_dismiss_text='Skip')
        askfile = lambda title, prompt, initial_value: askFile(
            title,
            prompt,
            initial_value,
            parent=top,
            extra_dismiss_text='Skip')
        try:
            project = loadProject(path=projectDir,
                                  showWarning=showWarning,
                                  askDir=askdir,
                                  askFile=askfile)
        except ApiError, e:
            showError('Reading project', e.error_msg, parent=top)
Exemplo n.º 3
0
    def determineDir(self, directory):

        directory = normalisePath(directory)

        assert os.path.isdir(directory), '"%s" is not a directory' % directory

        self.prev_directory = self.directory

        if not os.path.isabs(directory):
            if self.directory is None:  # first time around
                directory = joinPath(normalisePath(os.getcwd()), directory)
            else:
                directory = joinPath(self.directory, directory)

        self.directory = directory
        self.dirs = self.directory.split(dirsep)
Exemplo n.º 4
0
def launchApplication(filename=None):

    global top

    root = Tkinter.Tk()
    root.withdraw()
    top = ApplicationPopup(root)

    project = None
    if filename:
        file = normalisePath(filename)
        askdir = lambda title, prompt, initial_value, default_dir: askDir(
            title,
            prompt,
            initial_value,
            parent=top,
            extra_dismiss_text='Skip',
            default_dir=default_dir)
        askfile = lambda title, prompt, initial_value: askFile(
            title,
            prompt,
            initial_value,
            parent=top,
            extra_dismiss_text='Skip')
        try:
            project = loadProject(path=file,
                                  showWarning=showWarning,
                                  askDir=askdir,
                                  askFile=askfile)
        except ApiError, e:
            showError('Reading project', e.error_msg)
Exemplo n.º 5
0
def setRepositoryPath(project, repositoryName, path):

    from memops.api.Implementation import Url

    repository = project.findFirstRepository(name=repositoryName)
    if repository:
        if path != repository.url.path:
            # TBD: should we copy anything over from old url?
            url = Url(path=normalisePath(path))
            repository.url = url
Exemplo n.º 6
0
  def getDrives(self):

    if isWindowsOS():
      import win32api
      drives = win32api.GetLogicalDriveStrings()
      drives = drives.split('\x00')
      drives = [normalisePath(drive) for drive in drives if drive]
      drives.sort()
    else:
      drives = []

    return drives
Exemplo n.º 7
0
    def __init__(self, projectFile):

        # attributes
        self.projectFile = normalisePath(projectFile, makeAbsolute=True)
        self.sequenceFile = None
        self.spectraFile = None
        self.crosspeaksFile = None
        self.binaryformat = 'bigendian'

        # links
        self.sequence = None
        self.spectra = []
        self.crosspeaks = []
        self.resonances = set()
Exemplo n.º 8
0
    def addFile(self):

        spectrum = self.spectrum
        if not spectrum:
            return

        dataStore = spectrum.dataStore
        if not dataStore:
            showError('No dataStore',
                      'Spectrum does not have associated dataStore',
                      parent=self)
            return

        if not isinstance(dataStore, BlockedBinaryMatrix):
            showError('No blockedBinaryMatrix',
                      'Spectrum dataStore is not a blockedBinaryMatrix',
                      parent=self)
            return

        if not dataStore.blockSizes:
            showError('No blockSize',
                      'Spectrum dataStore does not have blockSize set',
                      parent=self)
            return

        blockSize = list(dataStore.blockSizes)

        fileName = self.file_entry.get()
        if not fileName:
            showError('No filename', 'No filename given', parent=self)
            return

        fileName = normalisePath(fileName, makeAbsolute=True)

        contourDir = spectrum.analysisSpectrum.contourDir.dataLocation
        if fileName.startswith(contourDir):
            path = fileName[len(contourDir) + 1:]
        else:
            path = os.path.basename(fileName)
            if not os.path.exists(contourDir):
                os.makedirs(contourDir)
            print 'Copying %s to %s' % (fileName, contourDir)
            shutil.copy(fileName, contourDir)

        try:
            header = getStoredContourHeader(fileName)
        except Exception, e:
            showError('File error', str(e), parent=self)
            return
Exemplo n.º 9
0
def launchApplication(projectDir=None):

    global top

    root = Tkinter.Tk()
    root.withdraw()
    top = ApplicationPopup(root)

    project = None
    if projectDir:
        projectDir = normalisePath(projectDir)
        try:
            project = loadProject(top, path=projectDir)
        except ApiError, e:
            showError('Reading project', e.error_msg, parent=top)
Exemplo n.º 10
0
def modifyPackageLocators(project,
                          repositoryName,
                          repositoryPath,
                          packageNames,
                          resetPackageLocator=True,
                          resetRepository=False):
    """
  Resets package locators for specified packages to specified repository.
  
  Use as, for example:
  
  modifyPackageLocators(project,'newChemComps','/mydir/data/chemComps/',('ccp.molecule.ChemComp','ccp.molecule.ChemCompCoord'))
  
  Additional args:
  
  - resetPackageLocator:  True   will reset the package locator completely, removing old info
                          False  will add the repository to the package locator.
  
  - resetRepository:      True   will reset url for the repository, even if it already exists
                          False  will not reset the url for the repository if it already exists
  
  Returns the relevant repository.
  
  """

    repository = project.findFirstRepository(name=repositoryName)
    ss = normalisePath(repositoryPath)

    if not repository:
        repository = project.newRepository(name=repositoryName,
                                           url=Implementation.Url(path=ss))
    elif resetRepository and repository.url.path != repositoryPath:
        repository.url = Implementation.Url(path=ss)

    for packageName in packageNames:
        packageLocator = project.findFirstPackageLocator(
            targetName=packageName)

        if not packageLocator:
            raise ApiError("Cannot modify repository 'any' for package %s" %
                           packageName)

        if resetPackageLocator:
            packageLocator.repositories = (repository, )
        elif not repository in packageLocator.repositories:
            packageLocator.addRepository(repository)

    return repository
Exemplo n.º 11
0
def logMemoryStats(project, path):
    """
  Logs stats for a project.
  It appends information to the file given by path.
  """

    path = normalisePath(path)
    fp = open(path, 'a')
    if fp:
        t = time.ctime(time.time())
        m = gc.collect()
        n = len(gc.get_objects())
        s = '%s: %d collected, %d objects' % (t, m, n)
        fp.write('%s\n' % s)
        print('logging: %s' % s)
        fp.close()
Exemplo n.º 12
0
    def setDir(self):

        directory = self.dir_entry.get()
        if not directory:
            showWarning('No directory',
                        'No directory specified, not setting backup directory',
                        parent=self)
            return

        if not os.path.abspath(directory):
            directory = joinPath(os.getcwd(), directory)

        if os.path.exists(directory):
            if not os.path.isdir(directory):
                showWarning('Path not directory',
                            'Path "%s" exists but is not a directory' %
                            directory,
                            parent=self)
                return
        else:
            if showYesNo(
                    'Directory does not exist',
                    'Directory "%s" does not exist, should it be created?' %
                    directory,
                    parent=self):
                os.mkdir(directory)
            else:
                showWarning(
                    'Directory not created',
                    'Directory "%s" not created so not setting backup directory'
                    % directory,
                    parent=self)
                return

        repository = self.project.findFirstRepository(name='backup')
        if not repository:
            showWarning(
                'No backup repository',
                'No backup repository found (something wrong somewhere)',
                parent=self)
            return

        url = repository.url
        if url.path != directory:
            repository.url = Url(path=normalisePath(directory))
Exemplo n.º 13
0
    def __init__(self,
                 parent,
                 file=None,
                 files=None,
                 component=None,
                 selectionText='Select',
                 title=None,
                 format=None,
                 multiSelect=False):

        #
        # Also get format (from self?)
        #

        self.file = None  # Use this to return single file info, for backward compat

        if not multiSelect and file:
            self.files = [file]

        elif files:
            self.files = files

        else:
            self.files = []

        if self.files:
            for i in range(len(self.files)):
                self.files[i] = normalisePath(self.files[i])

        self.component = component
        self.fileSelected = False
        self.selectionText = selectionText
        self.format = format

        self.multiSelect = multiSelect

        if not title:
            title = 'Select %s file' % component

        BasePopup.__init__(self,
                           parent=parent,
                           title=title,
                           modal=True,
                           transient=True)
Exemplo n.º 14
0
def newProject(projectName, path=None, removeExisting=False, showYesNo=None):
    """
  Create, and return, a new project using a specified path (directory).
  If path is not specified it takes the current working directory.
  The path can be either absolute or relative.
  The 'userData' repository is pointed to the path.
  The 'backup' repository is pointed to the path + '_backup'.
  If either of these paths already exist (either as files or as directories):
    If removeExisting:
      Delete the path
    Else if showYesNo:
      Ask the user if it is ok to delete the path
      If yes, delete.  If no return None.
    Else:
      Raise an IOError
  """

    # relies on knowing that repositories to move have these names, and these values for path suffix
    repositoryNameMap = {'userData': '', 'backup': '_backup'}

    if not path:
        path = os.getcwd()

    for name in repositoryNameMap.keys():
        fullPath = joinPath(path, projectName) + repositoryNameMap[name]
        answer = checkRemoveIfExists(fullPath, removeExisting, showYesNo)
        if answer is None:
            return None

    project = Implementation.MemopsRoot(name=projectName)

    for name in repositoryNameMap.keys():
        fullPath = normalisePath(joinPath(path, projectName) +
                                 repositoryNameMap[name],
                                 makeAbsolute=True)
        repository = project.findFirstRepository(name=name)
        repository.url = Implementation.Url(path=fullPath)

    return project
Exemplo n.º 15
0
    def getDrives(self):

        if isWindowsOS():
            #import win32api
            #drives = win32api.GetLogicalDriveStrings()
            #drives = drives.split('\x00')
            #drives = [normalisePath(drive) for drive in drives if drive]
            #drives.sort()
            # 19 Mar 2012: do not use win32api because not standard
            from ctypes import windll
            import string
            drives = []
            bitmask = windll.kernel32.GetLogicalDrives()
            for letter in string.ascii_uppercase:
                if bitmask & 1:
                    drives.append(letter)
                bitmask >>= 1
            drives = [drive + ':\\' for drive in drives]
            drives = [normalisePath(drive) for drive in drives]
        else:
            drives = []

        return drives
Exemplo n.º 16
0
def saveProject(project, newPath = None, newProjectName = None, changeBackup = True,
                createFallback = False, removeExisting = False, showYesNo = None,
                checkValid = False):
  """
  Save the userData for a project to a location given by newPath (the url.path
  of the userData repository) if set, or the existing location if not. 
  If userData does not exist then throws IOError.
  If newPath is not specified then it is set to oldPath.
  If newProjectName is not specified then it is set to oldProjectName if
    newPath==oldPath, otherwise it is set to basename(newPath).
  If changeBackup, then also changes backup URL path for project.
  If createFallback, then makes copy of existing modified topObjects
    files (in newPath, not oldPath) before doing save.
  If newPath != oldPath and newPath exists (either as file or as directory):
    If removeExisting:
      Delete the newPath.
    Else if showYesNo:
      Ask the user if it is ok to delete the newPath
      If yes, delete.  If no, return without saving.
    Else:
      Raise an IOError
  Elif newProjectName != oldProjectName and there exists corresponding path (file/directory):
    If removeExisting:
      Delete the path.
    Else if showYesNo:
      Ask the user if it is ok to delete the path.
      If yes, delete.  If no, return without saving.
    Else:
      Raise an IOError
  if checkValid then does checkAllValid on project
  If there is no exception or early return then at end userData is pointing to newPath.
  Return True if save done, False if not (unless there is an exception)
  """

  # check project valid (so don't save an obviously invalid project)
  if checkValid:
    project.checkAllValid()

  # only want to change path for userData
  userData = project.findFirstRepository(name='userData')
  if not userData:
    raise IOError('Problem: userData not found')

  oldPath = userData.url.path
  oldProjectName = project.name

  if newPath:
    # normalise newPath
    newPath = normalisePath(newPath, makeAbsolute=True)
  else:
    newPath = oldPath

  # if newProjectName isn't specified then use default
  if not newProjectName:
    if newPath == oldPath:
      newProjectName = oldProjectName
    else:
      newProjectName = os.path.basename(newPath)

  # change project name
  if newProjectName != oldProjectName:
    project.override = True # TBD: for now name is frozen so change this way
    try:
      # below constraint is not checked in setName() if override is True so repeat here
      isValid = newProjectName.isalnum()  # superfluous but faster in most cases
      if not isValid:
        for cc in newProjectName:
          if cc != '_' and not cc.isalnum():
            isValid = False
            break
        else:
          isValid = True
      if (not (isValid)):
        raise ApiError('project name must only have characters that are alphanumeric or underscore')

      # below checks for length of name as well
      project.name = newProjectName
    finally:
      project.override = False

  # if newPath same as oldPath, check if newProjectName already exists if it's not same as oldProjectName
  if newPath == oldPath:
    if newProjectName != oldProjectName:
      location = xmlUtil.getTopObjectPath(project)
      if os.path.exists(location):
        answer = checkRemoveIfExists(location, removeExisting, showYesNo)
        if answer is None:
          project.__dict__['name'] = oldProjectName  # TBD: for now name is frozen so change this way
          return False
  else: # check instead if newPath already exists
    if os.path.exists(newPath):
      answer = checkRemoveIfExists(newPath, removeExisting, showYesNo)
      if answer is None:
        if newProjectName != oldProjectName:
          project.__dict__['name'] = oldProjectName  # TBD: for now name is frozen so change this way
        return False
      # TBD: should we be removing it?
      removePath(newPath)
    else:
      # NBNB 2008/04/03 Rasmus Fogh. Added because it otherwise fell over when
      # target path did not exist
      upDir = os.path.dirname(newPath)
      if not os.path.exists(upDir):
        os.makedirs(upDir)

    # check if any topObject activeRepository is not either of above
    refData = project.findFirstRepository(name='refData')
    genData = project.findFirstRepository(name='generalData')
    topObjects = []
    repositories = set()
    for topObject in project.topObjects:
      repository = topObject.findFirstActiveRepository()
      if repository and repository not in (userData, refData, genData):
        topObjects.append(topObject)
        repositories.add(repository)
    if topObjects:
      print 'Warning, topObjects %s, in repositories %s, being left in original locations' % (topObjects, repositories)

  try:
    oldUrl = userData.url
    if changeBackup:
      # change project backup url to point to new path
      backupRepository = project.findFirstRepository(name="backup")
      if backupRepository:
        oldBackupUrl = backupRepository.url
      else:
        changeBackup = False

    # copy userData files over
    if newPath != oldPath:
      # if os.path.exists(oldPath):  # only copy if this is a directory
      if os.path.isdir(oldPath):
        # just copy everything from oldPath to newPath
        shutil.copytree(oldPath, newPath)

        # but need to remove all implementation files
        implPath = joinPath(newPath, ImpConstants.modellingPackageName,
                            ImpConstants.implementationPackageName)
        #implPath = pathImplDirectory(newPath)
        removePath(implPath)

      # change userData url to point to new path
      userData.url = Implementation.Url(path=newPath)
      # above will set project.isModified = True

      if changeBackup:
        # change project backup repository url to point to new path
        backupRepository.url = Implementation.Url(path=newPath+'_backup')

    # change project name
    if newProjectName != oldProjectName:
      if not project.isModified: # if it isModified it will be saved below
        if createFallback:
          createTopObjectFallback(project)
        project.save()

    # create fallbacks
    if createFallback:
      for topObject in (project,)+tuple(project.topObjects):
        if not topObject.isDeleted and topObject.isModified:
          createTopObjectFallback(topObject)

    # save modifications
    project.saveModified()

    if not isWindowsOS():
      os.system('touch %s' % newPath)  # so that user can see which are most recent

    return True

  except:
    # saveModified failed so revert to old values
    if newProjectName != oldProjectName:
      project.__dict__['name'] = oldProjectName  # TBD: for now name is frozen so change this way

    if newPath != oldPath:
      userData.url = oldUrl
      if changeBackup:
        backupRepository.url = oldBackupUrl
      try:
        removePath(newPath)
      except:
        pass

    raise
Exemplo n.º 17
0
  def __init__(self, parent, file_types = None, directory = None,
               single_callback = None, double_callback = None,
               prompt = None, show_file = True, file = '', multiSelect=False,
               default_dir = None, getRowColor = None,
               getExtraCell = None, extraHeadings = None, extraJustifies = None,
               displayExtra = True, *args, **kw):

    if file_types is None:
      file_types = [ FileType("All", ["*"]) ]

    if directory is None:
      directory = normalisePath(os.getcwd())

    if extraHeadings is None:
      extraHeadings = ()
    else:
      extraHeadings = tuple(extraHeadings)

    if extraJustifies is None:
      extraJustifies = ()
    else:
      extraJustifies = tuple(extraJustifies)

    if extraHeadings or extraJustifies:
      assert len(extraHeadings) == len(extraJustifies)
      assert getExtraCell

    self.extraHeadings = extraHeadings
    self.extraJustifies = extraJustifies
    self.displayExtra = displayExtra

    Frame.__init__(self, parent, *args, **kw)

    self.file_types = file_types
    self.fileType = file_types[0]
    self.single_callback = single_callback
    self.double_callback = double_callback
    self.show_file = show_file
    self.directory = None
    self.historyBack = []
    self.historyFwd = []
    self.determineDir(directory)
    self.default_dir = default_dir
    self.getRowColor = getRowColor
    self.getExtraCell = getExtraCell

    
    self.grid_columnconfigure(0, weight=1)

    row = 0
    if prompt:
      label = Label(self, text=prompt, grid=(row,0))
      row += 1

    self.grid_rowconfigure(row, weight=1)
    if show_file:
      headings = ('Name', 'Size', 'Date')
      justifies = ('left','right','right')
    else:
      headings = ('Directory',)
      justifies = ('left',)
    self.normalHeadings = headings
    self.normalJustifies = justifies
    headings = headings + extraHeadings
    justifies = justifies + extraJustifies

    self.fileList = ScrolledMatrix(self, headingList=headings, justifyList=justifies,
                                    initialRows=10, callback=self.singleCallback,
                                    doubleCallback=self.doubleCallback,
                                    multiSelect=multiSelect, grid=(row,0))

    row += 1
    texts = ['Back', 'Up', 'Forward', 'Top', 'Home', 'New', 'Refresh']
    commands = [self.backDir, self.fwdDir, self.upDir, self.topDir,
                self.homeDir, self.createDir, self.updateFileList ]

    if self.default_dir:
      texts.append('Default')
      commands.append(self.setDefaultDir)
      
    self.icons = []
    for name in ICON_NAMES:
      icon = Tkinter.PhotoImage(file=os.path.join(GFX_DIR,name+'.gif'))
      self.icons.append(icon)
      
    self.buttons = ButtonList(self, texts=texts, commands=commands,
                              images=self.icons, grid=(row,0))


    if show_file:
      row += 1
      self.file_entry = LabeledEntry(self, label='File name',
                              label_width=10, entry_width=40,
                              returnCallback=self.setSelectedFile)
      self.file_entry.grid(row=row, column=0, sticky=Tkinter.EW)
    else:
      self.file_entry = None

    row += 1
    self.directory_entry = LabeledEntry(self, label='Directory',
                                        entry = directory,
                                        label_width=10, entry_width=40,
                                        returnCallback=self.entryDir)
    self.directory_entry.grid(row=row, column=0, sticky=Tkinter.EW)


    row += 1
    subFrame = Frame(self, grid=(row,0))
    subFrame.expandGrid(None,6)

    if show_file:

      label = Label(subFrame, text='File type:', grid=(0,0))

      type_labels = self.determineTypeLabels()
      self.fileType_menu = PulldownList(subFrame, callback=self.typeCallback,
                                        texts=type_labels, objects=self.file_types,
                                        grid=(0,1))

    label = Label(subFrame, text=' Show hidden:', grid=(0,2))

    self.hidden_checkbutton = CheckButton(subFrame, text='', selected=False,
                                          callback=self.updateFileList, grid=(0,3))

    label = Label(subFrame, text='Dir path:', grid=(0,4))

    self.pathMenu = PulldownList(subFrame, callback=self.dirCallback,
                                 objects=range(len(self.dirs)),
                                 texts=self.dirs, index=len(self.dirs)-1,
                                 indent='  ', prefix=dirsep, grid=(0,5))

    self.updateFileList()
    self.updateButtons()

    if file:
      self.setFile(file)
Exemplo n.º 18
0
  def parseFields(self, fields):
  
    #
    # This is taken almost directly from Wayne's AzaraParams.py file
    #

    key = fields[0]
    assert key in self.allParKeys, 'key = %s' % key
    
    parType = None
    numFields = None

    for tempParType in self.allPars.keys():
      if key in self.parKeys[tempParType]:
        for parInfo in self.allPars[tempParType]:
          if key == parInfo[0]:
            (origName,analysisName,numFields,default) = parInfo
            parType = tempParType
            break
            
      if parType:
        break
      
    if not numFields:
      return

    if numFields == -1: # Basically any number of parameters!
      valueList = []
      for field in fields[1:]:
        try:
          x = int(field)
        except:
          x = float(field)
        valueList.append(x) 
      if key == 'params':
        dim = self.aPars.get('dim')
        if dim is None:
          raise Exception("'params' set before 'dim'")
        npts = self.aPars.get('npts')
        if not npts or len(npts) <= dim:
          raise Exception("'params' set before 'npts'")
        npts = npts[dim]
      if len(valueList) != npts:
        raise Exception("'params' set before 'npts'")
        
      if parType == 'mainPars':
        self.setValue(parInfo, tuple(valueList))
      elif parType == 'dimPars':
        assert self.aPars['dim'] >= 0, '"%s" must be after "dim"' % key
        dim = self.aPars['dim']
        self.setListValue(parInfo,dim,tuple(valueList))
      
      # TBD: should this be done with a setValue somehow??
      self.aPars['dimType'][dim] = self.dimTypeSampled
      
      return

    assert len(fields) == numFields, 'len(fields) = %d, should be %d' % (len(fields),numFields)

    if numFields == 1:
      if (key == 'little_endian'):  # Int is now taken care of by dictionary!
        self.setValue(parInfo,False)
      else:
        self.setValue(parInfo, True)
       
    elif numFields == 2:

      value = fields[1]

      if parType == 'mainPars':
      
        if (key == 'ndim'):
          assert self.aPars['ndim'] == 0, 'repeated "ndim"'
          self.setValue(parInfo, returnInt(value))
          assert self.aPars['ndim'] > 0, 'ndim <= 0'
          self.initDims()
        elif (key == 'file'):
          assert not self.aPars['file'], 'repeated "file"'
          path = normalisePath(value)
          if (path and (not os.path.isabs(path)) and os.path.isabs(self.name)):
            path = joinPath(splitPath(self.name)[0], path)
          self.setValue(parInfo,path)
        elif (key == 'head'):
          assert self.aPars['head'] == 0, 'repeated "head"'
          self.setValue(parInfo, 4*int(value))
          assert self.aPars['head'] >= 0, 'head < 0'
          
      elif parType == 'dimPar':
      
        if (key == 'dim'):
          assert self.aPars['ndim'] > 0, '"dim" must be after "ndim"'
          dim = self.aPars['dim']
          self.setValue(parInfo, int(value) - 1)
          assert self.aPars['dim'] != dim, 'repeated dim %d' % (dim+1)
          assert self.aPars['dim'] >= 0, 'dim = %d <= 0' % (self.aPars['dim']+1)
          assert self.aPars['dim'] < self.aPars['ndim'], 'dim = %d > ndim = %d' % (self.aPars['dim']+1, self.aPars['dim'])
          
      elif parType == 'dimPars':

        assert self.aPars['dim'] >= 0, '"%s" must be after "dim"' % key
        dim = self.aPars['dim']
        # can no longer do below because initDims() sets up default values
        #assert getattr(self, key)[dim] is None, '%s repeated for dim %d' % (key, dim+1)
        if (key  in ('npts','block')):
          v = int(value)
          assert v > 0, '%s <= 0 for dim %d' % (key, dim+1)
          self.setListValue(parInfo,dim,v)
        elif (key == 'nuc'):
          self.setListValue(parInfo,dim,standardNucleusName(value))
        else:
          self.setListValue(parInfo,dim,float(value))
Exemplo n.º 19
0
    def __init__(self,
                 parent,
                 file_types=None,
                 directory=None,
                 single_callback=None,
                 double_callback=None,
                 select_dir_callback=None,
                 change_dir_callback=None,
                 should_change_dir_callback=None,
                 prompt=None,
                 show_file=True,
                 file='',
                 multiSelect=False,
                 default_dir=None,
                 getRowColor=None,
                 getExtraCell=None,
                 extraHeadings=None,
                 extraJustifies=None,
                 displayExtra=True,
                 manualFileFilter=False,
                 extraTipTexts=None,
                 *args,
                 **kw):

        if file_types is None:
            file_types = [FileType("All", ["*"])]

        if manualFileFilter:
            self.manualFilter = FileType("Manual")
            file_types.append(self.manualFilter)
        else:
            self.manualFilter = None

        if directory is None:
            directory = normalisePath(os.getcwd())

        if extraHeadings is None:
            extraHeadings = ()
        else:
            extraHeadings = tuple(extraHeadings)

        if extraJustifies is None:
            extraJustifies = ()
        else:
            extraJustifies = tuple(extraJustifies)

        if extraHeadings or extraJustifies:
            assert len(extraHeadings) == len(extraJustifies)
            assert getExtraCell

        self.extraHeadings = extraHeadings
        self.extraJustifies = extraJustifies
        self.extraTipTexts = extraTipTexts or []
        self.displayExtra = displayExtra

        Frame.__init__(self, parent, *args, **kw)

        self.file_types = file_types
        self.fileType = file_types[0]
        self.single_callback = single_callback
        self.double_callback = double_callback
        self.select_dir_callback = select_dir_callback
        self.change_dir_callback = change_dir_callback
        self.should_change_dir_callback = should_change_dir_callback
        self.show_file = show_file
        self.directory = None
        self.historyBack = []
        self.historyFwd = []
        self.determineDir(directory)
        self.default_dir = default_dir
        self.getRowColor = getRowColor
        self.getExtraCell = getExtraCell

        self.grid_columnconfigure(0, weight=1)

        row = 0
        if prompt:
            label = Label(self, text=prompt, grid=(row, 0))
            row += 1

        self.grid_rowconfigure(row, weight=1)
        if show_file:
            headings = ('Name', 'Size', 'Date')
            justifies = ('left', 'right', 'right')
            tipTexts = [
                'Name of file, directory or link', 'Size of file in bytes',
                'Date of last modification'
            ]
        else:
            headings = ('Directory', )
            justifies = ('left', )
            tipTexts = ['Name of directory']

        self.normalHeadings = headings
        self.normalJustifies = justifies
        self.normalTipTexts = tipTexts
        headings = headings + extraHeadings
        justifies = justifies + extraJustifies
        tipTexts += [None] * len(extraHeadings)

        self.fileList = ScrolledMatrix(self,
                                       headingList=headings,
                                       justifyList=justifies,
                                       initialRows=10,
                                       callback=self.singleCallback,
                                       doubleCallback=self.doubleCallback,
                                       tipTexts=tipTexts,
                                       multiSelect=multiSelect,
                                       grid=(row, 0))

        row += 1
        tipTexts = [
            'Go to previous location in history',
            'Go forward in location history', 'Go up one directory level',
            'Go to root directory', 'Go to home directory',
            'Make a new directory', 'Refresh directory listing'
        ]

        texts = ['Back', 'Forward', 'Up', 'Top', 'Home', 'New', 'Refresh']
        commands = [
            self.backDir, self.fwdDir, self.upDir, self.topDir, self.homeDir,
            self.createDir, self.updateFileList
        ]

        if self.default_dir:
            texts.append('Default')
            commands.append(self.setDefaultDir)
            tipTexts.append('Set the current directory as the default')

        self.icons = []
        for name in ICON_NAMES:
            icon = Tkinter.PhotoImage(
                file=os.path.join(GFX_DIR, name + '.gif'))
            self.icons.append(icon)

        self.buttons = ButtonList(self,
                                  texts=texts,
                                  commands=commands,
                                  images=self.icons,
                                  grid=(row, 0),
                                  tipTexts=tipTexts)

        if show_file:
            row += 1
            self.file_entry = LabeledEntry(self,
                                           label='File name',
                                           label_width=10,
                                           entry_width=40,
                                           returnCallback=self.setSelectedFile)
            self.file_entry.grid(row=row, column=0, sticky=Tkinter.EW)
        else:
            self.file_entry = None

        row += 1
        self.directory_entry = LabeledEntry(self,
                                            label='Directory',
                                            entry=directory,
                                            label_width=10,
                                            entry_width=40,
                                            returnCallback=self.entryDir)
        self.directory_entry.grid(row=row, column=0, sticky=Tkinter.EW)

        row += 1
        subFrame = Frame(self, grid=(row, 0))
        subFrame.expandGrid(None, 6)

        if show_file:

            label = Label(subFrame, text='File type:', grid=(0, 0))

            type_labels = self.determineTypeLabels()
            self.fileType_menu = PulldownList(
                subFrame,
                callback=self.typeCallback,
                texts=type_labels,
                objects=self.file_types,
                grid=(0, 1),
                tipText='Restrict listed files to selected suffix')

        label = Label(subFrame, text=' Show hidden:', grid=(0, 2))

        self.hidden_checkbutton = CheckButton(
            subFrame,
            text='',
            selected=False,
            callback=self.updateFileList,
            grid=(0, 3),
            tipText='Show hidden files beginning with "." etc.')

        label = Label(subFrame, text='Dir path:', grid=(0, 4))

        self.pathMenu = PulldownList(
            subFrame,
            callback=self.dirCallback,
            objects=range(len(self.dirs)),
            texts=self.dirs,
            index=len(self.dirs) - 1,
            indent='  ',
            prefix=dirsep,
            grid=(0, 5),
            tipText='Directory navigation to current location')

        if show_file and self.manualFilter is not None:
            row += 1
            self.manual_filter_entry = LabeledEntry(
                self,
                label='Manual Select',
                entry=defaultFilter,
                label_width=13,
                entry_width=40,
                returnCallback=self.entryFilter,
                tipText=
                'Path specification with wildcards to select multiple files')
            self.manual_filter_entry.grid(row=row, column=0, sticky=Tkinter.EW)

        self.updateFileList()
        self.updateButtons()

        if file:
            self.setFile(file)
Exemplo n.º 20
0
def saveProject(project,
                newPath=None,
                newProjectName=None,
                changeBackup=True,
                createFallback=False,
                removeExisting=False,
                showYesNo=None,
                checkValid=False,
                changeDataLocations=False,
                showWarning=None,
                revertToOriginal=False):
    """
  Save the userData for a project to a location given by newPath (the url.path
  of the userData repository) if set, or the existing location if not. 
  If userData does not exist then throws IOError.
  If newPath is not specified then it is set to oldPath.
  If newProjectName is not specified then it is set to oldProjectName if
    newPath==oldPath, otherwise it is set to basename(newPath).
  If changeBackup, then also changes backup URL path for project.
  If createFallback, then makes copy of existing modified topObjects
    files (in newPath, not oldPath) before doing save.
  If newPath != oldPath and newPath exists (either as file or as directory):
    If removeExisting:
      Delete the newPath.
    Else if showYesNo:
      Ask the user if it is ok to delete the newPath
      If yes, delete.  If no, return without saving.
    Else:
      Raise an IOError
  Elif newProjectName != oldProjectName and there exists corresponding path (file/directory):
    If removeExisting:
      Delete the path.
    Else if showYesNo:
      Ask the user if it is ok to delete the path.
      If yes, delete.  If no, return without saving.
    Else:
      Raise an IOError
  If checkValid then does checkAllValid on project
  If changeDataLocations then copy to project directory
  If revertToOriginal then after the save changes paths back to as they were originally
  If there is no exception or early return then at end userData is pointing to newPath.
  Return True if save done, False if not (unless there is an exception)
  """

    # check project valid (so don't save an obviously invalid project)
    if checkValid:
        project.checkAllValid()

    # only want to change path for userData
    userData = project.findFirstRepository(name='userData')
    if not userData:
        raise IOError('Problem: userData not found')

    oldPath = userData.url.path
    oldProjectName = project.name

    if newPath:
        # normalise newPath
        newPath = normalisePath(newPath, makeAbsolute=True)
    else:
        newPath = oldPath

    # if newProjectName isn't specified then use default
    if not newProjectName:
        if newPath == oldPath:
            newProjectName = oldProjectName
        else:
            newProjectName = os.path.basename(newPath)
            # below is because of data model limit
            newProjectName = newProjectName[:32]

    renameProject(project, newProjectName)

    # if newPath same as oldPath, check if newProjectName already exists if it's not same as oldProjectName
    if newPath == oldPath:
        if newProjectName != oldProjectName:
            location = xmlUtil.getTopObjectPath(project)
            if os.path.exists(location):
                answer = checkRemoveIfExists(location, removeExisting,
                                             showYesNo)
                if answer is None:
                    project.__dict__[
                        'name'] = oldProjectName  # TBD: for now name is frozen so change this way
                    return False
    else:  # check instead if newPath already exists
        if os.path.exists(newPath):
            answer = checkRemoveIfExists(newPath, removeExisting, showYesNo)
            if answer is None:
                if newProjectName != oldProjectName:
                    project.__dict__[
                        'name'] = oldProjectName  # TBD: for now name is frozen so change this way
                return False
            # TBD: should we be removing it?
            removePath(newPath)
        else:
            # NBNB 2008/04/03 Rasmus Fogh. Added because it otherwise fell over when
            # target path did not exist
            upDir = os.path.dirname(newPath)
            if not os.path.exists(upDir):
                os.makedirs(upDir)

        # check if any topObject activeRepository is not either of above
        refData = project.findFirstRepository(name='refData')
        genData = project.findFirstRepository(name='generalData')
        topObjects = []
        repositories = set()
        for topObject in project.topObjects:
            repository = topObject.findFirstActiveRepository()
            if repository and repository not in (userData, refData, genData):
                topObjects.append(topObject)
                repositories.add(repository)
        if topObjects:
            print(
                'Warning, topObjects %s, in repositories %s, being left in original locations'
                % (topObjects, repositories))

    try:
        oldUrl = userData.url
        if changeBackup:
            # change project backup url to point to new path
            backupRepository = project.findFirstRepository(name="backup")
            if backupRepository:
                oldBackupUrl = backupRepository.url
            else:
                changeBackup = False

        # copy userData files over
        if newPath != oldPath:
            # if os.path.exists(oldPath):  # only copy if this is a directory
            if os.path.isdir(oldPath):
                # just copy everything from oldPath to newPath
                print(
                    'Copying directory %s to %s (this might take some time if there are big files)'
                    % (oldPath, newPath))
                shutil.copytree(oldPath, newPath)

                # but need toz remove all implementation files
                implPath = joinPath(newPath, ImpConstants.modellingPackageName,
                                    ImpConstants.implementationPackageName)
                #implPath = pathImplDirectory(newPath)
                removePath(implPath)

                # and need to repoint dataUrl's that were copied over
                oldPathP = oldPath + '/'
                for dataLocationStore in project.dataLocationStores:
                    for dataStore in dataLocationStore.dataStores:
                        oldDataPath = dataStore.fullPath
                        if oldDataPath.startswith(oldPathP):
                            dataUrl = dataStore.dataUrl
                            oldDataUrlPath = dataUrl.url.dataLocation
                            if oldDataUrlPath.startswith(
                                    oldPathP):  # normally true
                                newDataUrlPath = newPath + oldDataUrlPath[
                                    len(oldPath):]
                                dataUrl.url = Implementation.Url(
                                    path=newDataUrlPath)
                            else:  # path split awkwardly between absolute and relative
                                newDataUrlPath = newPath
                                dataUrl.url = Implementation.Url(
                                    path=newDataUrlPath)
                                dataStore.path = oldDataPath[len(oldPath):]

            # change userData url to point to new path
            userData.url = Implementation.Url(path=newPath)
            # above will set project.isModified = True

            if changeBackup:
                # change project backup repository url to point to new path
                backupRepository.url = Implementation.Url(path=newPath +
                                                          '_backup')

        # change project name
        if newProjectName != oldProjectName:
            if not project.isModified:  # if it isModified it will be saved below
                if createFallback:
                    createTopObjectFallback(project)
                project.save()

        # create fallbacks and keep track of modified topObjects
        modifiedTopObjects = []
        if createFallback:
            for topObject in (project, ) + tuple(project.topObjects):
                if not topObject.isDeleted and topObject.isModified:
                    createTopObjectFallback(topObject)
                    modifiedTopObjects.append(topObject)

        if changeDataLocations:
            dataLocationStores = project.sortedDataLocationStores()

            userRepository = project.findFirstRepository(name='userData')
            userPath = userRepository.url.dataLocation
            # 2010 Aug 11: remove data directory from path
            #dataPath = joinPath(userPath, 'data')
            dataPath = userPath

            # 2010 Aug 11: change name
            #dataStorePrefix = 'dataStore'
            dataStorePrefix = 'spectra'
            if os.path.exists(dataPath):
                files = [
                    xx for xx in os.listdir(dataPath)
                    if xx.startswith(dataStorePrefix)
                ]
                offset = len(files)
            else:
                offset = 0

            copyingList = []
            dataUrlDict = {}
            for dataLocationStore in dataLocationStores:
                for dataStore in dataLocationStore.sortedDataStores():

                    # wb104: 24 Mar 2010: below check is a complete kludge
                    # we should check whether dataStore is instance of
                    # NumericMatrix, etc., but those are in ccp so should
                    # not be imported here
                    # in any case, there is no proper way to find out if
                    # a dataStore is used without explicit knowledge of class
                    knt = 0
                    # hicard = 1
                    for attr in ('nmrDataSourceImage', ):
                        if hasattr(dataStore, attr):
                            if getattr(dataStore, attr):
                                knt += 1
                    # hicard > 1
                    for attr in ('externalDatas', 'nmrDataSources'):
                        if hasattr(dataStore, attr):
                            knt += len(getattr(dataStore, attr))
                    if knt == 0:
                        continue

                    oldFullPath = dataStore.fullPath
                    if not oldFullPath.startswith(userPath + '/'):
                        # first figure out new dataUrl path
                        dataUrl = dataStore.dataUrl
                        oldPath = dataUrl.url.dataLocation
                        if dataUrl in dataUrlDict:
                            newUrl = dataUrlDict[dataUrl]
                        else:
                            offset += 1
                            newUrlPath = '%s%d' % (dataStorePrefix, offset)
                            newUrlPath = joinPath(dataPath, newUrlPath)
                            newUrl = dataUrlDict[dataUrl] = Implementation.Url(
                                path=newUrlPath)
                        # then add to list to copy over if original data exists
                        if os.path.exists(oldFullPath):
                            newFullPath = joinPath(newUrl.dataLocation,
                                                   dataStore.path)
                            copyingList.append((oldFullPath, newFullPath))

            # now copy data files over
            nfilesToCopy = len(copyingList)
            for n, (oldFullPath, newFullPath) in enumerate(copyingList):
                dirName = os.path.dirname(newFullPath)
                if not os.path.exists(dirName):
                    os.makedirs(dirName)
                print('Copying file %s to %s (%d of %d)' %
                      (oldFullPath, newFullPath, n + 1, nfilesToCopy))
                shutil.copy(oldFullPath, newFullPath)

            # finally change dataUrl paths
            for dataUrl in dataUrlDict:
                dataUrl.url = dataUrlDict[dataUrl]

        # save modifications
        # change way doing save in case exception is thrown
        if createFallback:
            for topObject in modifiedTopObjects:
                try:
                    topObject.save()
                except:
                    location = xmlUtil.getTopObjectPath(topObject)
                    print('Exception working on topObject %s, file %s' %
                          (topObject, location))
                    raise
            # be safe and do below in case new modifications after
            # modifiedTopObjects has been created
            project.saveModified()
        else:
            project.saveModified()

        if not isWindowsOS():
            os.system('touch "%s"' %
                      newPath)  # so that user can see which are most recent

        badTopObjects = []
        for topObject in modifiedTopObjects:
            if not checkFileIntegrity(topObject):
                badTopObjects.append(topObject)

        if badTopObjects:
            if showWarning:
                showWarning(
                    'Incomplete save',
                    'It looks like one or more files did not save completely, see console for list'
                )
            print(
                'It looks like one or more files did not save completely, you should check them:'
            )
            for topObject in badTopObjects:
                print
                print('%s, path:' % topObject)
                print(xmlUtil.getTopObjectPath(topObject))
            return False

        if revertToOriginal:  # TBD: the below does not change back dataUrl paths
            if newProjectName != oldProjectName:
                project.__dict__[
                    'name'] = oldProjectName  # TBD: for now name is frozen so change this way

            if newPath != oldPath:
                userData.url = oldUrl
                if changeBackup:
                    backupRepository.url = oldBackupUrl

        return True

    except:
        # saveModified failed so revert to old values
        if newProjectName != oldProjectName:
            project.__dict__[
                'name'] = oldProjectName  # TBD: for now name is frozen so change this way

        if newPath != oldPath:
            userData.url = oldUrl
            if changeBackup:
                backupRepository.url = oldBackupUrl
            try:
                removePath(newPath)
            except:
                pass

        raise
Exemplo n.º 21
0
    def createFullProject(self, fileName):

        if self.verbose == 1:
            print "Writing %s project from file %s" % (self.formatLabel,
                                                       fileName)

        self.file = self.projectIO.MarsProjectFile(fileName)

        (fileDir, fileName) = os.path.split(fileName)
        fileDir = normalisePath(fileDir)
        marsInputDir = normalisePath(self.inputDirectory)
        if marsInputDir != fileDir and marsInputDir.count(fileDir):
            marsInputDir = marsInputDir.replace(fileDir, '')
            if marsInputDir[0] == os.sep:
                marsInputDir = marsInputDir[1:]

        if not os.path.exists(marsInputDir):
            os.mkdir(marsInputDir)

        #
        # Set tag for export/import cycle
        #

        #self.setDateTimeStamp(self.project,setTag,isUnique = False)
        #self.setDateTimeStamp(self.chain,setTag,isUnique = True) # Only one export per chain!

        #
        # FASTA type sequence file
        #

        sequenceFileName = 'fastaSequence.tab'
        self.file.files['sequence'] = os.path.join(marsInputDir,
                                                   sequenceFileName)
        self.writeSequence(os.path.join(self.inputDirectory, sequenceFileName),
                           chains=[self.chain],
                           minimalPrompts=self.minimalPrompts)

        #
        # Chemical shift tab file - also used to set up spin system info
        #

        chemShiftFile = 'cs.tab'
        self.file.files['csTab'] = os.path.join(marsInputDir, chemShiftFile)

        if self.useFixedConnection:
            ignoreFixedConnection = True
        else:
            ignoreFixedConnection = False

        # Need to keep track of info in measurement file
        self.writeShifts(os.path.join(self.inputDirectory, chemShiftFile),
                         measurementList=self.shiftList,
                         chains=[self.chain],
                         ignoreFixedConnection=ignoreFixedConnection,
                         noWrite=True,
                         minimalPrompts=self.minimalPrompts)
        self.measurementFile.write(**self.writeKeywds)

        #
        # Fixed connection info - TODO make a ccp/format level file for this? Or not worth it?
        #

        if self.useFixedConnection:
            fixedConnectionFile = 'fix_con.tab'
            self.file.files['fixConn'] = os.path.join(marsInputDir,
                                                      fixedConnectionFile)

            fixConnOut = open(
                os.path.join(self.inputDirectory, fixedConnectionFile), 'w')

            # Ignore info written in chemical shift file - is already filtered to not include chain-linked spin systems
            ignoreSsSerials = []
            for ssSerial in self.measurementFile.spinSystemLinks.keys():
                ignoreSsSerials.append(ssSerial)
                ignoreSsSerials.append(
                    self.measurementFile.spinSystemLinks[ssSerial][-1])

            # Now go over spins systems, and print out links
            serialConnections = []
            for spinSystem in self.spinSystemList:
                ssSerial = spinSystem.serial
                if ssSerial in ignoreSsSerials:
                    continue

                # Only report spin systems that are part of the chemical shift tab file!!
                if not self.spinSystemToPrevId.has_key(
                        spinSystem) or not self.spinSystemToPrevId[spinSystem]:
                    continue

                refResonanceToSpinSystem = self.spinSystemToResonanceDict[
                    spinSystem][0]

                serialKey = None
                if refResonanceToSpinSystem.linkedSpinSystems.has_key(1):
                    serialKey = (
                        spinSystem.serial,
                        refResonanceToSpinSystem.linkedSpinSystems[1].serial)
                elif refResonanceToSpinSystem.linkedSpinSystems.has_key(-1):
                    serialKey = (
                        refResonanceToSpinSystem.linkedSpinSystems[-1].serial,
                        spinSystem.serial)

                if serialKey and serialKey not in serialConnections:
                    serialConnections.append(serialKey)

            for serialKey in serialConnections:
                fixConnOut.write("%-4d  %-4d" % (serialKey[0], serialKey[1]) +
                                 newline)
            fixConnOut.close()

        #
        # Fixed assignment info - TODO make a ccp/format level file for this? Or not worth it?
        #

        if self.useFixedAssignments:
            fixedAssignmentFile = 'fix_ass.tab'
            self.file.files['fixAss'] = os.path.join(marsInputDir,
                                                     fixedAssignmentFile)

            fixAssOut = open(
                os.path.join(self.inputDirectory, fixedAssignmentFile), 'w')

            # Go over spins systems, and print out info if available
            ssInfo = []
            for spinSystem in self.spinSystemList:

                # Only report spin systems that are part of the chemical shift tab file!!
                if not self.spinSystemToPrevId.has_key(
                        spinSystem) or not self.spinSystemToPrevId[spinSystem]:
                    continue

                seqIdList = []
                resCodeList = []

                # First use 'clean' information
                if spinSystem.residue:
                    if spinSystem.residue.chain == self.chain:
                        seqIdList.append(spinSystem.residue.seqId)
                    else:
                        # Don't use if residue in wrong chain
                        continue

                else:
                    if spinSystem.residueProbs:
                        for residueProb in spinSystem.residueProbs:
                            residue = residueProb.possibility
                            if residue.chain == self.chain:
                                seqIdList.append(residue.seqId)

                        # Don't use if all residues are in wrong chain
                        if not seqIdList:
                            continue

                    elif spinSystem.ccpCode:
                        resCodeList.append(
                            self.getCode1Letter(spinSystem.ccpCode))

                    else:
                        for residueTypeProb in spinSystem.residueTypeProbs:
                            code1Letter = self.getCode1Letter(
                                residueTypeProb.possibility.ccpCode)
                            if not code1Letter in resCodeList:
                                resCodeList.append(code1Letter)

                if seqIdList or resCodeList:
                    ssInfo.append((spinSystem.serial, seqIdList, resCodeList))

            for (ssSerial, seqIdList, resCodeList) in ssInfo:
                fixAssOut.write("%-4d  " % ssSerial)
                if seqIdList:
                    for seqId in seqIdList:
                        fixAssOut.write("%d " % seqId)
                if resCodeList:
                    fixAssOut.write("%s" % string.join(resCodeList, ''))
                fixAssOut.write(newline)

            fixAssOut.close()

        #
        # Finish up
        #

        self.file.write()

        return self.file
Exemplo n.º 22
0
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
Exemplo n.º 23
0
    def body(self, master):

        master.grid_columnconfigure(0, weight=1)
        master.grid_rowconfigure(0, weight=1)
        master.grid_rowconfigure(1, weight=1)

        self.geometry('500x500')

        fileNames = []

        # All files have to be in same directory!
        if self.files and self.files[0].find(dirsep) > -1:
            (path, fileName) = splitPath(self.files[0])
            directory = path
            fileNames.append(fileName)

            for file in self.files[1:]:
                (path, fileName) = splitPath(file)
                fileNames.append(fileName)

        else:
            path = None
            directory = normalisePath(os.path.abspath(os.curdir))
            fileNames = []

        #print directory
        #print fileName

        file_types = []

        if fileTypeDict.has_key(self.component):

            if self.format and fileTypeDict[self.component].has_key(
                    self.format):

                file_types.extend([
                    FileType(self.format,
                             fileTypeDict[self.component][self.format])
                ])

            if fileTypeDict[self.component].has_key('generic'):

                file_types.extend([
                    FileType('generic',
                             fileTypeDict[self.component]['generic'])
                ])

        file_types.extend([FileType('All', ['*'])])

        self.file_select = FileSelect(master,
                                      file_types=file_types,
                                      directory=directory,
                                      double_callback=self.ok,
                                      multiSelect=self.multiSelect)

        if self.multiSelect:
            self.file_select.setFiles(fileNames)
        else:
            if fileNames:
                fileName = fileNames[0]
            else:
                fileName = ''
            self.file_select.setFile(fileName)

        self.file_select.grid(row=0, column=0, sticky=Tkinter.NSEW)

        texts = [self.selectionText]
        commands = [self.ok]
        buttons = createDismissHelpButtonList(master,
                                              texts=texts,
                                              commands=commands)
        buttons.grid(row=1, column=0, sticky=Tkinter.EW)
Exemplo n.º 24
0
def saveRepository(repository,
                   newPath=None,
                   createFallback=False,
                   removeExisting=False,
                   showYesNo=None):
    """
  Save the repository to a location given by newPath (the url.path
  of the repository) if set, or the existing location if not. 
  For the userData repository this just calls saveProject instead.
  If createFallback, then makes copy of existing modified repository
    files (in newPath, not oldPath) before doing save.
  If newPath != oldPath and newPath exists (either as file or as directory):
    If removeExisting:
      Delete the newPath.
    Else if showYesNo:
      Ask the user if it is ok to delete the newPath
      If yes, delete.  If no, return without saving.
    Else:
      Raise an IOError
  If there is no exception or early return then at end repository is pointing to newPath.
  Return True if save done, False if not (unless there is an exception)
  """

    project = repository.root
    if repository.name == 'userData':
        saveProject(project,
                    newPath=newPath,
                    createFallback=createFallback,
                    removeExisting=removeExisting,
                    showYesNo=showYesNo)
        return False

    oldPath = repository.url.path

    if newPath:
        # normalise path
        newPath = normalisePath(newPath, makeAbsolute=True)
    else:
        newPath = oldPath

    if newPath != oldPath:
        answer = checkRemoveIfExists(newPath, removeExisting, showYesNo)
        if answer is None:
            return False

    # check which topObjects have this repository as activeRepositories[0]
    # and are modified but not deleted
    topObjects = set()
    for topObject in project.topObjects:
        if topObject.isModified and not topObject.isDeleted and \
               repository == topObject.findFirstActiveRepository():
            topObjects.add(topObject)

    # TBD: should this be done here?
    for topObject in topObjects:
        topObject.checkAllValid()

    # copy repository files over
    if newPath != oldPath:
        if os.path.exists(oldPath):
            # just copy everything from oldPath to newPath
            shutil.copytree(oldPath, newPath)

        # change repository url to point to new newPath
        oldUrl = repository.url
        repository.url = Implementation.Url(path=newPath)
        # above will set project.isModified = True

    # save modifications
    try:
        if createFallback:
            for topObject in topObjects:
                createTopObjectFallback(topObject)

            for topObject in topObjects:
                topObject.save()

        # the paths have changed so also need to save project
        project.save()

        return True

    except:
        # something failed so revert to old path
        if newPath != oldPath:
            repository.url = oldUrl
            try:
                removePath(newPath)
            except:
                pass

        raise
Exemplo n.º 25
0
        print 'must specify project directory'
        sys.exit()

    path = sys.argv[1]

    import Tkinter
    from memops.gui.DataEntry import askDir, askFile
    from memops.gui.MessageReporter import showWarning
    from memops.general.Io import loadProject
    from memops.universal.Io import normalisePath

    # This needs to be above loadProject because that can pop up
    # dialogs, and it will create a root if one has not been created
    # already, and that will lead to the later code crashing
    r = Tkinter.Tk()

    path = normalisePath(path)
    askdir = lambda title, prompt, initial_value: askDir(
        title, prompt, initial_value, parent=top, extra_dismiss_text='Skip')
    askfile = lambda title, prompt, initial_value: askFile(
        title, prompt, initial_value, parent=top, extra_dismiss_text='Skip')
    project = loadProject(path,
                          showWarning=showWarning,
                          askDir=askdir,
                          askFile=askfile)

    popup = DataLocationPopup(r, project)

    r.withdraw()
    r.mainloop()
Exemplo n.º 26
0
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
Exemplo n.º 27
0
    def parseFields(self, fields):

        key = fields[0]

        if (not (key in self.keys1 or key in self.keys2 or key in self.keys3)):
            raise ApiError(
                'unknown keyword (first few characters = "%s") in par file "%s"'
                % (key[:10], self.parFile))

        if (key in self.keys3):
            return

        if (key in self.keys1):
            if (key != 'params' and len(fields) != 2):
                raise ApiError(
                    'for key "%s", number of args = %d, should be 1' %
                    (key, len(fields) - 1))
        elif (key in self.keys2):
            if (len(fields) != 1):
                raise ApiError(
                    'for key "%s", number of args = %d, should be 0' %
                    (key, len(fields) - 1))

        if (key in self.keys2):
            if (key == 'int'):
                self.integer = True
            elif (key == 'little_endian'):
                self.big_endian = False
            else:
                setattr(self, key, True)
            return

        if len(fields) == 2:
            value = fields[1]
        else:
            value = fields[1:]

        if (key == 'ndim'):
            if (self.ndim != 0):
                raise ApiError('repeated "ndim"')
            try:
                self.ndim = int(value)
            except:
                raise ApiError('"ndim" value not an int')
            if (self.ndim <= 0):
                raise ApiError('ndim <= 0')
            self.initDims()
        elif (key == 'file'):
            if (self.dataFile):
                raise ApiError('repeated "file"')
            path = normalisePath(value)
            if not os.path.isabs(path):
                directory = os.path.dirname(self.parFile)
                if not directory:
                    directory = os.getcwd()
                path = joinPath(directory, path)
            self.dataFile = path
        elif (key == 'head'):
            if (self.head != 0):
                raise ApiError('repeated "head"')
            try:
                self.head = 4 * int(value)
            except:
                raise ApiError('"head" value not an int')
            if (self.head < 0):
                raise ApiError('head < 0')
        elif (key == 'dim'):
            if (self.ndim <= 0):
                raise ApiError('"dim" must be after "ndim"')
            dim = self.dim
            try:
                self.dim = int(value) - 1
            except:
                raise ApiError('"dim" value not an int')
            if (self.dim == dim):
                raise ApiError('repeated dim %d' % (dim + 1))
            if (self.dim < 0):
                raise ApiError('dim = %d <= 0' % (self.dim + 1))
            if (self.dim >= self.ndim):
                raise ApiError('dim = %d > ndim = %d' %
                               (self.dim + 1, self.ndim))
        else:
            if (self.dim < 0):
                raise ApiError('keyword "%s" must be after "dim"' % key)
            dim = self.dim
            # can no longer do below because initDims() sets up default values
            #assert getattr(self, key)[dim] is None, '%s repeated for dim %d' % (key, dim+1)
            if (key == 'npts'):
                try:
                    v = int(value)
                except:
                    raise ApiError('"npts" value not an int')
                if (v <= 0):
                    raise ApiError('npts <= 0 for dim %d' % (key, dim + 1))
                self.npts[dim] = v
            elif (key == 'block'):
                try:
                    v = int(value)
                except:
                    raise ApiError('"block" value not an int')
                if (v <= 0):
                    raise ApiError('block <= 0 for dim %d' % (key, dim + 1))
                self.block[dim] = v
            elif (key == 'nuc'):
                self.nuc[dim] = self.standardNucleusName(value)
            elif (key in ('sw', 'sf', 'refppm', 'refpt')):
                try:
                    v = float(value)
                except:
                    raise ApiError('"%s" value not a float' % key)
                if (key == 'sw'):
                    self.sw[dim] = v
                elif (key == 'sf'):
                    self.sf[dim] = v
                elif (key == 'refppm'):
                    self.refppm[dim] = v
                elif (key == 'refpt'):
                    self.refpt[dim] = v
            elif (key == 'params'):
                self.dimType[dim] = self.sampledDimType
                try:
                    self.pointValues[dim] = [float(v) for v in value]
                except:
                    raise ApiError('%s input %s not valid input to float' %
                                   (key, value))