source: ndgCommon/trunk/ndg/common/src/lib/ndgresources.py @ 4991

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

Move generic 'httpify' method to utilities + adjust utilities method
to retrive default proxy to cope with standard formatting + set this
up on each call to urlopen - since different urls may use different
proxies + tidyup and extend test suite.

Line 
1'''
2Utility class providing access to various static resource files - e.g.
3xqueries and xsd schema docs
4
5 @author: C Byrom, Tessella, Feb 09
6'''
7import pkg_resources
8import re, logging
9from ndg.common.src.models.ndgObject import ndgObject as no
10
11# these are the different schemas needed to validate atom docs - NB, the main
12# one is molesAtom1.0 - this imports the others
13ATOM_MOLES_SCHEMA = "molesAtom1.0"
14MOLES_SCHEMA = "moles2.0"
15ATOM_SCHEMA = "atom1.0"
16
17class ndgResources(object):
18   
19    def __init__(self, directory='common/xmldb/'):
20        '''
21        Loads up resources from the internal package directory
22        @keyword directory: root directory from which to retrieve docs from.
23        Default is 'common/xmldb/'
24        '''
25        logging.debug("Initialising ndgResources")
26        self.__loadResources(directory)
27
28        # provide mapping between ndgObject URI doc type name and xquery name
29        # TODO: if we're going to replace the old moles1.3 entirely with the
30        # atom based format, will need to implement atom2dif, atom2dc and atom2iso
31        # queries
32        self.knownQueries={no.DIF_DOC_TYPE:'moles2dif',
33                           no.DC_DOC_TYPE:'moles2DC',
34                           no.ISO_DOC_TYPE:'moles2iso19139',
35                           no.NDGB0_DOC_TYPE:'moles',
36                           no.NDGB1_DOC_TYPE:'molesObjectType',
37                           no.MDIP_DOC_TYPE:'moles2mdip', 
38                           no.NDGA0_DOC_TYPE:'csml',
39                           no.NUMSIM_DOC_TYPE:'numsim', 
40                           no.ATOM_DOC_TYPE:'atom', 
41                           no.ATOM_BACKUP_DOC_TYPE:'atom', 
42                           no.ASSOCIATED_ATOM_DOC_TYPE:'atomTypeList',
43                           no.BROWSE_DIF_DOC_TYPE:'dif'}
44        logging.debug("ndgResources initialised")
45
46
47    def __loadResources(self, directory):
48        '''
49        Load the various resources
50        @keyword directory: root directory from which to retrieve docs from.
51        '''
52        xqueryDir = directory + 'xquery'
53        files=pkg_resources.resource_listdir('ndg', xqueryDir)
54        # this next bit to get rid of .svn and anything else in testing
55        self.xq = {}
56        for f in files:
57            if f.endswith('.xq'):
58                self.xq[re.sub('.xq\Z','',f)] = \
59                    pkg_resources.resource_string('ndg','%s/%s'%(xqueryDir,f))
60
61        schemaDir = directory + 'schema'
62        files=pkg_resources.resource_listdir('ndg', schemaDir)
63        self.xsd = {}
64        for f in files:
65            if f.endswith('.xsd'):
66                self.xsd[re.sub('.xsd\Z','',f)] = \
67                    pkg_resources.resource_string('ndg','%s/%s'%(schemaDir, f))
68                         
69        # add the various xquery libraries, too
70        self.xqlib = {}
71        for dir in ['Vocabs', 'Utilities', 'StubB']:
72            xqueryLibDir = xqueryDir + '/lib/' + dir
73            files = pkg_resources.resource_listdir('ndg', xqueryLibDir)
74            for f in files:
75                if f.endswith('.xquery'): 
76                    fullPath = '%s/%s'%(xqueryLibDir,f)
77                    partPath = '/%s/%s' %(dir, f)
78                    self.xqlib[partPath] = pkg_resources.resource_string('ndg', fullPath)
79       
80        # also add the organisation data files - NB, this class should be generalised
81        # to make more sense...
82        resourceDir = directory + 'resources'
83        files = pkg_resources.resource_listdir('ndg', resourceDir)
84        self.resources = {}
85        for f in files:
86            if f.endswith('.xml'): 
87                self.resources[f] = \
88                    pkg_resources.resource_string('ndg','%s/%s' %(resourceDir, f))
89
90        # lastly, add the indexes
91        self.indexes = {}
92        for dir in ['eXist']:
93            indexDir = directory + 'indexing' + '/' + dir
94            files = pkg_resources.resource_listdir('ndg', indexDir)
95            for f in files:
96                if f.endswith('.xconf'): 
97                    self.indexes[f] = \
98                        pkg_resources.resource_string('ndg','%s/%s'%(indexDir,f))
99       
100       
101    def createXQuery(self, key, targetCollection, repositoryID, localID):
102        '''
103        Return an actual xquery targetted for a specific localID, repositoryID and
104        targetCollection
105        @param key: name of xquery to use
106        @param targetCollection: target collection to use with xquery
107        @param repositoryID: ID of the repository to use with the xquery
108        @param localID: local ID of doc to lookup with xquery
109        @raise ValueError: if key is not a valid xquery
110        @return xquery as a string
111        '''
112        logging.debug("Setting up %s xquery" %key)
113        if not self.xq.has_key(key):
114            raise ValueError("Unrecognised xquery type: '%s'" %key)
115       
116        xq=self.xq[key]
117        xq=xq.replace('RepositoryID',repositoryID)
118        xq=xq.replace('LocalID',localID)
119        xq=xq.replace('TargetCollection',targetCollection)
120        logging.debug("Xquery set up - returning")
121        return xq
Note: See TracBrowser for help on using the repository browser.