source: ndgCommon/trunk/ndg/common/src/dal/ndgDirectory.py @ 4793

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/dal/ndgDirectory.py@4834
Revision 4793, 3.8 KB checked in by cbyrom, 11 years ago (diff)

Checking in initial codebase for ndgUtils restructure.

Line 
1'''
2 Treats an NDG directory as something that can be counted,listed, and dated
3 
4 @author: B Lawrence?
5'''
6from ndg.common.src.lib.ndgXqueries import ndgXqueries
7from ndg.common.src.clients.xmldb.eXist.eXistInterface import ndg_eXist
8from xml.etree import ElementTree as ET
9
10
11class ndgDirectory:
12    ''' Treats an NDG directory as something that can be counted,listed, and dated '''
13    def __init__(self,targetCollection,existHost,docType='DIF',fast=0,
14                 passwordFile=None):
15        ''' Initialise a targetCollection directory of a particular docType.
16        Valid docTypes are:
17            DIF, MDIP, ATOM, MOLES
18            (Not all supported ...yet )
19            Note that if fast is set true, the members attribute is not populated
20            at instantiation.
21        '''
22       
23        self.members=[]
24        self.entries=[]
25        self.lastUpdated=None
26        self.targetCollection=targetCollection
27        self.docType = docType
28       
29        inputs = {}
30        if passwordFile:
31            inputs['passwordFile'] = passwordFile
32           
33        #get the right namespace
34        self.ns={'DIF':'http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/',\
35                 'MOLES':'http://ndg.nerc.ac.uk/moles',\
36                 'MDIP':'http://www.oceannet.org/mdip/xml',\
37                 'ATOM': 'http://www.w3.org/2005/Atom'}[docType]
38        #load all queries
39        queries = ndgXqueries()
40        #select the right query according to the docType
41        xqName={'DIF':'DIFList','MOLES':'molesList', \
42                'MDIP':'MDIPList', 'moles':'molesList', \
43                'ATOM': 'atomList'}[docType]
44        self.docType=docType
45        xq=queries[xqName]
46        # set the query up with the correct target collection
47        self.xq=xq.replace('TargetCollection',self.targetCollection)
48       
49        #make sure we have a db available
50        self.xmldb=ndg_eXist(db=existHost, **inputs)
51     
52        if not fast: m=self.getMembers()
53
54       
55    def getMembers(self):
56        ''' Build the membership of the directory by URI (based on the FILENAME) '''
57        id,s=self.xmldb.executeQuery(self.xq)
58        if s['hits']==0: return []
59        indices=range(s['hits'])
60       
61        doc = self.xmldb.retrieve(id, 0)
62        et = ET.fromstring(doc)
63        for member in et:
64            fn = member.findtext('{%s}fileName'%self.ns)
65            eid = member.findtext('{%s}repositoryID'%self.ns)
66            c = member.findtext('{%s}created'%self.ns)
67            self.members.append({'fileName':fn,'EntryID':eid,'created':c})
68       
69        self.xmldb.release(id)
70        self.files=[i['fileName'] for i in self.members]
71        self.entries=[i['EntryID'] for i in self.members]
72        return self.members
73
74   
75    def get(self,fileName):
76        ''' Return the actual document corresponding to uri in this directory '''
77        #Use the fact that we know its document name ...
78        return self.xmldb.getDoc(self.targetCollection,fileName)
79
80   
81    def created(self,fileName=None):
82        ''' Return the creation date of the collection (if uri is None), or
83        of the specific member within the collection '''
84        q="'%s'"%self.targetCollection
85        if fileName:
86            # if we're dealing with atom docs, these include the full collection
87            # path already - so strip this off
88            if fileName.startswith(self.targetCollection):
89                fileBits = fileName.split('/')
90                fileName = fileBits[-1]
91                tc = '/' + '/'.join(fileBits[0:-1])
92                q = "'%s', '%s'" %(tc, fileName)
93            else:
94                q+=",'%s'"%fileName
95        xq="<created>{xmldb:created(%s)}</created>"%q
96        r=self.xmldb.executeChunkedQuery(xq,1,1)
97        e=ET.fromstring(str(r))
98        return e.findtext('created')
99   
100
101       
102       
103       
Note: See TracBrowser for help on using the repository browser.