1 | ''' |
---|
2 | Utility class providing access to various static resource files - e.g. |
---|
3 | xqueries and xsd schema docs |
---|
4 | |
---|
5 | @author: C Byrom, Tessella, Feb 09 |
---|
6 | ''' |
---|
7 | import pkg_resources |
---|
8 | import re, logging |
---|
9 | from 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 |
---|
13 | ATOM_MOLES_SCHEMA = "molesAtom1.0" |
---|
14 | MOLES_SCHEMA = "moles2.0" |
---|
15 | ATOM_SCHEMA = "atom1.0" |
---|
16 | |
---|
17 | class ndgResources(object): |
---|
18 | |
---|
19 | def __init__(self, directory='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 '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 | ''' |
---|
33 | no.DIF2stubISO:'dif2stubISO', |
---|
34 | no.ISO2DIF:'ISO2DIF', |
---|
35 | no.ISO2DC:'ISO2DC' |
---|
36 | ''' |
---|
37 | self.knownQueries={no.DIF_DOC_TYPE:'moles2dif', |
---|
38 | no.DC_DOC_TYPE:'moles2DC', |
---|
39 | no.ISO_DOC_TYPE:'moles2iso19139', |
---|
40 | no.NDGB0_DOC_TYPE:'moles', |
---|
41 | no.NDGB1_DOC_TYPE:'molesObjectType', |
---|
42 | no.MDIP_DOC_TYPE:'moles2mdip', |
---|
43 | no.NDGA0_DOC_TYPE:'csml', |
---|
44 | no.NUMSIM_DOC_TYPE:'numsim', |
---|
45 | no.ATOM_DOC_TYPE:'atom', |
---|
46 | no.ATOM_BACKUP_DOC_TYPE:'atom', |
---|
47 | no.ASSOCIATED_ATOM_DOC_TYPE:'atomTypeList', |
---|
48 | no.BROWSE_DC_DOC_TYPE:'DIF2DC', |
---|
49 | no.BROWSE_DIF_DOC_TYPE:'dif' |
---|
50 | |
---|
51 | } |
---|
52 | logging.debug("ndgResources initialised") |
---|
53 | |
---|
54 | |
---|
55 | def __loadResources(self, directory): |
---|
56 | ''' |
---|
57 | Load the various resources |
---|
58 | @keyword directory: root directory from which to retrieve docs from. |
---|
59 | ''' |
---|
60 | xqueryDir = directory + 'xquery' |
---|
61 | files=pkg_resources.resource_listdir('ndg.common', xqueryDir) |
---|
62 | # this next bit to get rid of .svn and anything else in testing |
---|
63 | self.xq = {} |
---|
64 | for f in files: |
---|
65 | if f.endswith('.xq'): |
---|
66 | self.xq[re.sub('.xq\Z','',f)] = \ |
---|
67 | pkg_resources.resource_string('ndg.common','%s/%s'%(xqueryDir,f)) |
---|
68 | |
---|
69 | schemaDir = directory + 'schema' |
---|
70 | files=pkg_resources.resource_listdir('ndg.common', schemaDir) |
---|
71 | self.xsd = {} |
---|
72 | for f in files: |
---|
73 | if f.endswith('.xsd'): |
---|
74 | self.xsd[re.sub('.xsd\Z','',f)] = \ |
---|
75 | pkg_resources.resource_string('ndg.common','%s/%s'%(schemaDir, f)) |
---|
76 | |
---|
77 | # add the various xquery libraries, too |
---|
78 | self.xqlib = {} |
---|
79 | for dir in ['Vocabs', 'Utilities', 'StubB']: |
---|
80 | xqueryLibDir = xqueryDir + '/lib/' + dir |
---|
81 | files = pkg_resources.resource_listdir('ndg.common', xqueryLibDir) |
---|
82 | for f in files: |
---|
83 | if f.endswith('.xquery'): |
---|
84 | fullPath = '%s/%s'%(xqueryLibDir,f) |
---|
85 | partPath = '/%s/%s' %(dir, f) |
---|
86 | self.xqlib[partPath] = pkg_resources.resource_string('ndg.common', fullPath) |
---|
87 | |
---|
88 | # also add the organisation data files - NB, this class should be generalised |
---|
89 | # to make more sense... |
---|
90 | resourceDir = directory + 'resources' |
---|
91 | files = pkg_resources.resource_listdir('ndg.common', resourceDir) |
---|
92 | self.resources = {} |
---|
93 | for f in files: |
---|
94 | if f.endswith('.xml'): |
---|
95 | self.resources[f] = \ |
---|
96 | pkg_resources.resource_string('ndg.common','%s/%s' %(resourceDir, f)) |
---|
97 | |
---|
98 | # lastly, add the indexes |
---|
99 | self.indexes = {} |
---|
100 | for dir in ['eXist']: |
---|
101 | indexDir = directory + 'indexing' + '/' + dir |
---|
102 | files = pkg_resources.resource_listdir('ndg.common', indexDir) |
---|
103 | for f in files: |
---|
104 | if f.endswith('.xconf'): |
---|
105 | self.indexes[f] = \ |
---|
106 | pkg_resources.resource_string('ndg.common','%s/%s'%(indexDir,f)) |
---|
107 | |
---|
108 | |
---|
109 | def createXQuery(self, key, targetCollection, repositoryID, localID): |
---|
110 | ''' |
---|
111 | Return an actual xquery targetted for a specific localID, repositoryID and |
---|
112 | targetCollection |
---|
113 | @param key: name of xquery to use |
---|
114 | @param targetCollection: target collection to use with xquery |
---|
115 | @param repositoryID: ID of the repository to use with the xquery |
---|
116 | @param localID: local ID of doc to lookup with xquery |
---|
117 | @raise ValueError: if key is not a valid xquery |
---|
118 | @return xquery as a string |
---|
119 | ''' |
---|
120 | logging.debug("Setting up %s xquery" %key) |
---|
121 | if not self.xq.has_key(key): |
---|
122 | raise ValueError("Unrecognised xquery type: '%s'" %key) |
---|
123 | |
---|
124 | xq=self.xq[key] |
---|
125 | xq=xq.replace('RepositoryID',repositoryID) |
---|
126 | xq=xq.replace('LocalID',localID) |
---|
127 | xq=xq.replace('TargetCollection',targetCollection) |
---|
128 | logging.debug("Xquery set up - returning") |
---|
129 | return xq |
---|