Changeset 3161 for exist/trunk


Ignore:
Timestamp:
18/12/07 15:30:24 (12 years ago)
Author:
lawrence
Message:

Modifications to ndgDirectory to expose filenames, entries and both more easily

Location:
exist/trunk/python/ndgUtils
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • exist/trunk/python/ndgUtils/ndgDirectory.py

    r3158 r3161  
    44class ndgDirectory: 
    55    ''' Treats an NDG directory as something that can be counted,listed, and dated ''' 
    6     def __init__(self,targetCollection,existHost,docType='DIF'): 
     6    def __init__(self,targetCollection,existHost,docType='DIF',fast=0): 
    77        ''' Initialise a targetCollection directory of a particular docType. 
    88        Valid docTypes are: 
    99            dgObservationStation, dgDataEntity, dgActivity, dgDataProductionTool 
    1010            DIF, MDIP 
     11            (Not all supported ...yet ) 
     12            Note that if fast is set true, the members attribute is not populated 
     13            at instantiation. 
    1114        ''' 
    1215         
    1316        self.members=[] 
     17        self.entries=[] 
    1418        self.lastUpdated=None 
    1519        self.targetCollection=targetCollection 
     
    2125        queries=ndgXqueries.ndgXqueries() 
    2226        xqName={'DIF':'DIFList'}[docType] 
     27        self.docType=docType 
    2328        xq=queries[xqName] 
    24         xq=xq.replace('TargetCollection',self.targetCollection) 
     29        self.xq=xq.replace('TargetCollection',self.targetCollection) 
     30        
     31        #make sure we have a db available 
     32        self.xmldb=ndg_eXist(db=existHost) 
     33       
     34        if not fast: m=self.getMembers() 
    2535         
    26         #issue the query 
    27         self.xmldb=ndg_eXist(db=existHost) 
    28         id,s=self.xmldb.executeQuery(xq) 
    29         if s['hits']==0: return 
    30          
    31         #ok loop over the members and load a list of them 
     36    def getMembers(self): 
     37        ''' Build the membership of the directory by URI (based on the FILENAME) ''' 
     38        id,s=self.xmldb.executeQuery(self.xq) 
     39        if s['hits']==0: return [] 
    3240        indices=range(s['hits']) 
    3341        self.members=[self.__loadxml(id,i) for i in indices] 
    3442        self.xmldb.release(id) 
     43        self.files=[i['fileName'] for i in self.members] 
     44        self.entries=[i['EntryID'] for i in self.members] 
     45        return self.members 
    3546         
    3647    def __loadxml(self,id,i): 
     
    3950        e=ET.fromstring(r) 
    4051        fn=e.findtext('{%s}fileName'%self.ns) 
    41         return fn.split('.xml')[0] 
     52        eid=e.findtext('{%s}repositoryID'%self.ns) 
     53        return {'fileName':fn,'EntryID':eid} 
    4254    
    43     def get(self,uri): 
     55    def get(self,fileName): 
    4456        ''' Return the actual document corresponding to uri in this directory ''' 
    4557        #Use the fact that we know its document name ... 
    46         fn='%s.xml'%uri 
    47         return self.xmldb.getDoc(self.targetCollection,fn) 
     58        return self.xmldb.getDoc(self.targetCollection,fileName) 
    4859     
    49     def created(self,uri=None): 
     60    def created(self,fileName=None): 
    5061        ''' Return the creation date of the collection (if uri is None), or 
    5162        of the specific member within the collection ''' 
    5263        q="'%s'"%self.targetCollection 
    53         if uri is not None: q+=",'%s.xml'"%uri 
     64        if fileName is not None: q+=",'%s'"%fileName 
    5465        xq="<created>{xmldb:created(%s)}</created>"%q 
    5566        r=self.xmldb.executeChunkedQuery(xq,1,1) 
    5667        e=ET.fromstring(str(r)) 
    5768        return e.findtext('created') 
    58         
    59          
     69     
     70 
    6071         
    6172        
  • exist/trunk/python/ndgUtils/test_ndgDirectory.py

    r3158 r3161  
    1313        ''' Test the ability to get members of a directory and the first one of them ''' 
    1414        print self.ndgDir.members 
    15         doc=self.ndgDir.get(self.ndgDir.members[0]) 
     15        doc=self.ndgDir.get(self.ndgDir.members[0]['fileName']) 
    1616        print doc 
    1717         
     
    1919        '''Test the creation information''' 
    2020        print self.ndgDir.created() 
    21         print self.ndgDir.created(self.ndgDir.members[0]) 
     21        print self.ndgDir.created(self.ndgDir.members[0]['fileName']) 
    2222         
    2323 
Note: See TracChangeset for help on using the changeset viewer.