Ignore:
Timestamp:
03/02/09 22:00:34 (3 years ago)
Author:
Peter Peterson
Message:

Added ability for the types to be supplied in openpath. Refs #101.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bindings/python/nxs/napi.py

    r1188 r1189  
    315315        self.isopen = True 
    316316 
    317     def _getpath(self):  
    318         return '/'+'/'.join(self._path) 
     317    def _getpath(self): 
     318        mypath = [level[0] for level in self._path] 
     319        return '/'+'/'.join(mypath) 
    319320    path = property(_getpath,doc="Unix-style path to node") 
    320321 
     
    421422    def openpath(self, path): 
    422423        """ 
    423         Open a particular group '/path/to/group'.  Paths can 
    424         be absolute or relative to the currently open group. 
    425         If openpath fails, then currently open path may not 
    426         be different from the starting path. 
     424        Open a particular group '/path/to/group'.  Paths can be 
     425        absolute or relative to the currently open group.  If openpath 
     426        fails, then currently open path may not be different from the 
     427        starting path. For better performation the types can be 
     428        specified as well using '/path:type1/to:type2/group:type3' 
     429        which will prevent searching the file for the types associated 
     430        with the supplied names. 
    427431 
    428432        Raises ValueError. 
     
    437441        if path == '/': 
    438442            target = [] 
    439         elif path.startswith('/'): 
    440             target = path[1:].split('/') 
    441443        else: 
    442             target = self._path + path.split('/') 
     444            if path.endswith("/"): 
     445                path = path[:-1] 
     446            if path.startswith('/'): 
     447                target = path[1:].split('/') 
     448            else: 
     449                target = self._path + path.split('/') 
    443450 
    444451        # Remove relative path indicators from target 
     
    455462                L.append(t) 
    456463        target = L 
     464 
     465        # split out nxclass from each level if available 
     466        L = [] 
     467        for t in target: 
     468            try: 
     469                item = t.split(":") 
     470                if len(item) == 1: 
     471                    L.append((item[0], None)) 
     472                else: 
     473                    L.append(tuple(item)) 
     474            except AttributeError: 
     475                L.append(t) 
     476        target = L 
     477 
    457478        #print "current path",self._path 
    458479        #print "%s"%path,target 
     
    476497            up = self._path[len(target):] 
    477498            down = [] 
    478         up.reverse() 
     499 
     500        # add more information to the down path 
     501        for i in xrange(len(down)): 
     502            try: 
     503                (name, nxclass) = down[i] 
     504            except ValueError: 
     505                down[i] = (down[i], None) 
    479506        #print "close,open",up,down 
    480507 
     
    488515        # Open groups on the way down 
    489516        for target in down: 
    490             # Find target name in current group.  We need to do this because 
    491             # we can't open the group without knowing the class.  We also 
    492             # need the class so that we can handle SDS specially. 
    493             n,_,_ = self.getgroupinfo() 
    494             self.initgroupdir() 
    495             for i in range(n): 
    496                 name,nxclass = self.getnextentry() 
    497                 if name != target: continue 
    498                 if nxclass != 'SDS': 
    499                     self.opengroup(name,nxclass) 
    500                 elif opendata:  
    501                     self.opendata(name) 
    502                 break 
     517            (name, nxclass) = target 
     518            if nxclass is None: 
     519                entries = self.getentries() 
     520                if not entries.has_key(name): 
     521                    raise KeyError("Failed to find entry with name \"%s\"" \ 
     522                                   % name) 
     523                nxclass = entries[name] 
     524            if nxclass != "SDS": 
     525                self.opengroup(name, nxclass) 
     526            elif opendata: 
     527                self.opendata(name) 
    503528            else: 
    504529                raise ValueError("node %s not in %s"%(name,self.path)) 
     
    540565            raise ValueError,\ 
    541566                "Could not open %s:%s in %s"%(nxclass,name,self._loc()) 
    542         self._path.append(name) 
     567        self._path.append((name,nxclass)) 
    543568 
    544569    nxlib.nxiclosegroup_.restype = c_int 
     
    738763        if status == ERROR: 
    739764            raise ValueError, "Could not open data %s: %s"%(name, self._loc()) 
    740         self._path.append(name) 
     765        self._path.append((name,"SDS")) 
    741766        self._indata = True 
    742767 
Note: See TracChangeset for help on using the changeset viewer.