source: exist/trunk/python/ndgUtils/ndgDirectory.py @ 4229

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgUtils/ndgDirectory.py@4229
Revision 4229, 3.5 KB checked in by cbyrom, 11 years ago (diff)

Add support for doing lists and summaries of atom docs via ndgDirectory and the existInterface class + add necessary xquery files for this + add new method to
allow retrieval of the collection to which an atom with a specified ID belongs - including the associated xquery file + extend tests to exercise these new features
+ add custom Atom error + improve preservation of key atom attributes when loading existing atoms into an Atom object + improve determining default atom collection

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