source: ndgCommon/trunk/ndg/common/src/lib/ndgXqueries.py @ 4810

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/lib/ndgXqueries.py@4810
Revision 4810, 4.2 KB checked in by cbyrom, 11 years ago (diff)

Create new class (and test suite) to do all the eXist DB setting up - and move this code from the
eXistdbclient to tidy things up.
Allow feed publishing to be done synch/asynch.
Improve loading of non-python resources - adding code to store the various schemata and indexes
required for setting up eXist
Improve parsing of atom URLs to allow browser host to change
Fix moles data in testconstants
Add tests for looking up associated atom data
Add indexing configuration files - to optimise atom processing in eXist + add code in initialiser class to set these up on eXist.
Add all the required schemata required for atom schema validation in eXist + add code in initialiser class to set these up on eXist;
this allows eXist to be ran without need of access beyond the firewall.

Line 
1'''
2Holds a set of ndg xqueries after having read them from disk
3
4 @author: B Lawrence?
5'''
6import pkg_resources
7import re
8
9class ndgXqueries(object):
10    '''
11    Holds a set of ndg xqueries after having read them from disk (which is
12    done in order to share the xqueries amongst different applications and
13    programming languages).
14    '''
15    # these are the different schemae needed to validate the atoms - NB, the main
16    # one is molesAtom1.0 - this imports the others
17    ATOM_MOLES_SCHEMA = "molesAtom1.0"
18    MOLES_SCHEMA = "moles2.0"
19    ATOM_SCHEMA = "atom1.0"
20   
21    def __init__(self, directory='common/xmldb/'):
22        ''' Loads up xqueries from the internal package directory '''
23
24        xqueryDir = directory + 'xquery'
25        files=pkg_resources.resource_listdir('ndg', xqueryDir)
26        # this next bit to get rid of .svn and anything else in testing
27        self.xq = {}
28        for f in files:
29            if f.endswith('.xq'):
30                self.xq[re.sub('.xq\Z','',f)] = \
31                    pkg_resources.resource_string('ndg','%s/%s'%(xqueryDir,f))
32
33        schemaDir = directory + 'schema'
34        files=pkg_resources.resource_listdir('ndg', schemaDir)
35        self.xsd = {}
36        for f in files:
37            if f.endswith('.xsd'):
38                self.xsd[re.sub('.xsd\Z','',f)] = \
39                    pkg_resources.resource_string('ndg','%s/%s'%(schemaDir, f))
40                         
41        # add the various xquery libraries, too
42        self.xqlib = {}
43        for dir in ['Vocabs', 'Utilities', 'StubB']:
44            xqueryLibDir = xqueryDir + '/lib/' + dir
45            files = pkg_resources.resource_listdir('ndg', xqueryLibDir)
46            for f in files:
47                if f.endswith('.xquery'): 
48                    self.xqlib[f] = \
49                        pkg_resources.resource_string('ndg','%s/%s'%(xqueryLibDir,f))
50       
51        # also add the organisation data files - NB, this class should be generalised
52        # to make more sense...
53        resourceDir = directory + 'resources'
54        files = pkg_resources.resource_listdir('ndg', resourceDir)
55        self.resources = {}
56        for f in files:
57            if f.endswith('.xml'): 
58                self.resources[f] = \
59                    pkg_resources.resource_string('ndg','%s/%s' %(resourceDir, f))
60
61        # lastly, add the indexes
62        self.indexes = {}
63        for dir in ['eXist']:
64            indexDir = directory + 'indexing' + '/' + dir
65            files = pkg_resources.resource_listdir('ndg', indexDir)
66            for f in files:
67                if f.endswith('.xconf'): 
68                    self.indexes[f] = \
69                        pkg_resources.resource_string('ndg','%s/%s'%(indexDir,f))
70       
71    def __setitem__(self,key,value):
72        ''' Add a new xquery to the xquery database dynamically'''
73        self.xq[key]=value
74    def __getitem__(self,key):
75        ''' Given a key, return an xquery '''
76        return self.xq[key]
77   
78    def getSchema(self, schemaName):
79        '''
80        Return the specified schema
81        @param schemaName: Name of schema to retrieve - NB, use the class constants to specify the schemaName
82        - e.g. self.ATOM_MOLES_SCHEMA
83        @return: schema in string format
84        '''
85        schema =  self.xsd.get(schemaName)
86        if not schema:
87            raise ValueError("Unrecognised schema, '%s'" %schemaName)
88       
89        return schema
90       
91       
92    def keys(self):
93        ''' Return a list of all the xqueries loaded into this instance '''
94        return self.xq.keys()
95    def help(self,key):
96        ''' Return the comment string at the top of the xquery (if it exists) '''
97        xq=self.xq[key]
98        s=xq.find('(:')
99        e=xq.find(':)')
100        if s!=-1 and e>s: 
101            return xq[s+2:e]
102        else: return 'No help available for [%s]'%key
103       
104       
105    def actual(self,key,targetCollection,repositoryID,localID):
106        ''' Return an actual xquery targetted for a specific localID, repositoryID and
107        targetCollection '''
108        xq=self.xq[key]
109        xq=xq.replace('RepositoryID',repositoryID)
110        xq=xq.replace('LocalID',localID)
111        xq=xq.replace('TargetCollection',targetCollection)
112        return xq
Note: See TracBrowser for help on using the repository browser.