source: exist/trunk/python/ndgUtils/vocabtermdata.py @ 4301

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

Add convenience methods to determine whether a data item is a granule +
selected atom related links according to their type - to allow granules + deployments
to be associated with data entities.

Line 
1#!/usr/bin/env python
2'''
3 Class to store and access the various vocab term data
4 
5 @author: C Byrom, Tessella Jul 2008
6'''
7import sys, logging, commands, string, os, time, re
8import urllib
9
10class VocabTermItem(object):
11    '''
12    Class representing single vocab term item
13    '''
14    def __init__(self, vocabURL, termID, title=None):
15        self.vocabURL = vocabURL
16        self.termID = termID
17        self.title = title
18
19
20class VocabTermDataError(ValueError):
21    """
22    Exception handling for VocabTermData class.
23    """
24    def __init__(self, msg):
25        logging.error(msg)
26        ValueError.__init__(self, msg)
27
28
29class VocabTermData(object):
30    '''
31    Class representing vocab term data - including
32    methods to look these up to ensure they are current
33    '''
34
35    OBJECT_PAGE_TERM = 'ObjectPage'
36    DATA_URL_TERM = 'DataURL'
37    DATA_PAGE_TERM = 'DataPage'
38    CURATOR_PAGE_TERM = 'CuratorPage'
39    EXT_METADATA_TERM = 'ExtMetadata'
40    METADATA_SOURCE_TERM = 'MetadataSource'
41    URI_TERM = 'URI'
42    LOGO_TERM = 'LOGO'
43    NUM_SIM_TERM = 'NumSim'
44    OPENDAP_TERM = 'OPENDAP'
45    THREDDS_TERM = 'THREDDS'
46    WMS_TERM = 'WMS'
47    WCS_TERM = 'WCS'
48    WFS_TERM = 'WFS'
49    LAS_TERM = 'LAS'
50    DATA_EXTRACTOR_TERM = 'DataExtractor' 
51    FILE_BROWSER_TERM = 'FileBrowser'   
52    CSML_TERM = 'CSML'
53   
54    ACTIVITY_TERM = 'ACTIVITY'
55    DPT_TERM = 'DPT'
56    OBS_TERM = 'OBS'
57    GRANULE_TERM = 'GRANULE'
58    DE_TERM = "DE"
59   
60    # dpt subtypes
61    LIDAR_TERM = "dgLidar"
62    RADAR_TERM = "dgRadar"
63    SONDE_TERM = "dgSonde"
64    NAVIGATION_TERM = "dgNavigation"
65    GAS_CHROMATOGRAPH_TERM = "dgGasChromatograph"
66    SPECTROMETER_TERM = "dgSpectrometer"
67    MASS_SPECTROMETER_TERM = "dgMassSpectrometer"
68    MET_SENSOR_TERM = "dgMetSensor"
69    DOAS_TERM = "dgDOAS"
70    ASOZ_TERM = "dgASOZ"
71    RADIOMETER_TERM = "dgRadiometer"
72    FAGE_TERM = "dgFAGE"
73    IMAGER_TERM = "dgImager"
74    FILTER_TERM = "dgFilter"
75    PARTICLE_COUNTER_TERM = "dgParticleCounter"
76    SAMPLER_TERM = "dgSampler"
77    OTHER_INSTRUMENT_TYPE_TERM = "dgOtherInstrumentType"
78    MODEL_TERM = "dgModel"
79    INSTRUMENT_TERM = "dgInstrument"
80   
81    # de subtypes
82    SIMULATION_TERM = "dgSimulation"
83    ANALYSIS_TERM = "dgAnalysis"
84    MEASUREMENT_TERM = "dgMeasurement"
85   
86    # activity subtypes
87    DATA_COLLECTION_TERM = "dgActivityDataCollection"
88    DATA_PROJECT_TERM = "dgActivityDataProject"
89    DATA_CAMPAIGN_TERM = "dgActivityDataCampaign"
90    DATA_INVESTIGATION_TERM = "dgActivityDataInvestigation"
91    FLIGHT_TERM = "dgFlight"
92    CRUISE_TERM = "dgCruise"
93    FUNDING_PROGRAM_TERM = "dgFundingProgram"
94    DEPLOYMENT_TERM = "Deployment"
95    # NB, this is a specialised activity with the subtype deployment pre-set
96    ACTIVITY_DEPLOYMENT_TERM = "ActivityDeployment"
97   
98    # obs subtypes
99    STATIONARY_PLATFORM_TERM = "dgStationaryPlatform"
100    MOVING_PLATFORM_TERM = "dgMovingPlatform"
101    LAND_STATION_TERM = "dgLandStation"
102    MOORING_TERM = "dgMooring"
103    STATION_GROUP_TERM = "dgStationGroup"
104    SHIP_TERM = "dgShip"
105    AIRCRAFT_TERM = "dgAircraft"
106    SATELLITE_TERM = "dgSatellite"
107    COMPUTER_TERM = "dgComputer"
108
109    # provider types
110    BADC_TERM = 'badc.nerc.ac.uk'
111    NEODC_TERM = 'neodc.nerc.ac.uk'
112   
113    TERM_DATA = {
114                 OBJECT_PAGE_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/N041', '1', title = 'Object Home Page'),
115                 DATA_URL_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/N041', '2', title = 'Data URL'),
116                 DATA_PAGE_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/N041', '3', title = 'Data Home Page'),
117                 CURATOR_PAGE_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/N041', '4', title = 'Curator Home Page'),
118                 EXT_METADATA_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/N041', '5', title = 'Extended Metadata'),
119                 METADATA_SOURCE_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/N041', '6', title = 'Original Metadata Source'),
120                 #URI_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/N041', '3URI', title = 'Data Home Page'),
121                 
122                 BADC_TERM:VocabTermItem('NOT YET SET UP', BADC_TERM, title = 'British Atmospheric Data Centre'),
123                 NEODC_TERM:VocabTermItem('NOT YET SET UP', NEODC_TERM, title = 'NERC Earth Observation Data Centre'),
124                 
125                 LOGO_TERM:VocabTermItem('LOGO', 'LOGO', title = 'Logo'),
126                 NUM_SIM_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/N041', '7', title = 'NumSim description'),
127                 OPENDAP_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/P201', 'GCMDU010', title = 'GET DATA > OPENDAP DATA (DODS)'),
128                 THREDDS_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/P201', 'GCMDU013', title = 'GET DATA > THREDDS DATA'),
129                 WMS_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/P201', 'GCMDU026', title = 'GET SERVICE > GET WEB MAP SERVICE (WMS)'),
130                 WCS_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/P201', 'GCMDU024', title = 'GET SERVICE > GET WEB COVERAGE SERVICE (WCS)'),
131                 WFS_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/P201', 'GCMDU025', title = 'GET SERVICE > GET WEB FEATURE SERVICE (WFS)'),
132                 LAS_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/P201', 'GCMDU006', title = 'GET DATA > LAS'),
133                 DATA_EXTRACTOR_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/N041', '8', title = 'NDG DataExtractor'),
134                 FILE_BROWSER_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/N041', '9', title = 'File Browser'),
135                 CSML_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/N021', '1'),
136                 ACTIVITY_TERM:VocabTermItem('Activity - NOT YET SET UP', ACTIVITY_TERM, title = 'Activity'),
137                 ACTIVITY_DEPLOYMENT_TERM:VocabTermItem('Activity Deployment - NOT YET SET UP', ACTIVITY_DEPLOYMENT_TERM, title = 'Activity Deployment'),
138                 DPT_TERM:VocabTermItem('DPT - NOT YET SET UP', DPT_TERM, title = 'Data Production Tool'),
139                 OBS_TERM:VocabTermItem('OBS - NOT YET SET UP', OBS_TERM, title = 'Observation Station'),
140                 GRANULE_TERM:VocabTermItem('GRAN - NOT YET SET UP', GRANULE_TERM, title = 'Data Granule'),
141                 DE_TERM:VocabTermItem('DE - NOT YET SET UP', DE_TERM, title = 'Data Entity'),
142                 
143                 LIDAR_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG02', title = "Lidar"),
144                 RADAR_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG03', title = "Radar"),
145                 SONDE_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG04', title = "Sonde"),
146                 NAVIGATION_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG05', title = "Navigation"),
147                 GAS_CHROMATOGRAPH_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG06', title = "Gas Chromatograph"),
148                 SPECTROMETER_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG07', title = SPECTROMETER_TERM),
149                 MASS_SPECTROMETER_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG08', title = MASS_SPECTROMETER_TERM),
150                 MET_SENSOR_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG09', title = MET_SENSOR_TERM),
151                 DOAS_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG10', title = DOAS_TERM),
152                 ASOZ_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG11', title = ASOZ_TERM),
153                 RADIOMETER_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG12', title = RADIOMETER_TERM),
154                 FAGE_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG13', title = FAGE_TERM),
155                 IMAGER_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG14', title = IMAGER_TERM),
156                 FILTER_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG15', title = FILTER_TERM),
157                 PARTICLE_COUNTER_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG16', title = PARTICLE_COUNTER_TERM),
158                 SAMPLER_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG17', title = SAMPLER_TERM),
159                 OTHER_INSTRUMENT_TYPE_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG99', title = OTHER_INSTRUMENT_TYPE_TERM),
160                 MODEL_TERM:VocabTermItem('dgModel - NOT YET SET UP', MODEL_TERM, title = MODEL_TERM),
161                 INSTRUMENT_TERM:VocabTermItem('dgInstrument - NOT YET SET UP', INSTRUMENT_TERM, title = INSTRUMENT_TERM),
162                 
163                 SIMULATION_TERM:VocabTermItem('NOT YET SET UP', SIMULATION_TERM, title = SIMULATION_TERM),
164                 ANALYSIS_TERM:VocabTermItem('NOT YET SET UP', ANALYSIS_TERM, title = ANALYSIS_TERM),
165                 MEASUREMENT_TERM:VocabTermItem('NOT YET SET UP', MEASUREMENT_TERM, title = MEASUREMENT_TERM),
166                 DATA_COLLECTION_TERM:VocabTermItem('NOT YET SET UP', DATA_COLLECTION_TERM, title = DATA_COLLECTION_TERM),
167                 DATA_PROJECT_TERM:VocabTermItem('NOT YET SET UP', DATA_PROJECT_TERM, title = DATA_PROJECT_TERM),
168                 DATA_CAMPAIGN_TERM:VocabTermItem('NOT YET SET UP', DATA_CAMPAIGN_TERM, title = DATA_CAMPAIGN_TERM),
169                 DATA_INVESTIGATION_TERM:VocabTermItem('NOT YET SET UP', DATA_INVESTIGATION_TERM, title = DATA_INVESTIGATION_TERM),
170                 FLIGHT_TERM:VocabTermItem('NOT YET SET UP', FLIGHT_TERM, title = FLIGHT_TERM),
171                 CRUISE_TERM:VocabTermItem('NOT YET SET UP', CRUISE_TERM, title = CRUISE_TERM),
172                 FUNDING_PROGRAM_TERM:VocabTermItem('NOT YET SET UP', FUNDING_PROGRAM_TERM, title = FUNDING_PROGRAM_TERM),
173                 DEPLOYMENT_TERM:VocabTermItem('NOT YET SET UP', DEPLOYMENT_TERM, title = DEPLOYMENT_TERM),
174                 
175                 STATIONARY_PLATFORM_TERM:VocabTermItem('NOT YET SET UP', STATIONARY_PLATFORM_TERM, title = STATIONARY_PLATFORM_TERM),
176                 MOVING_PLATFORM_TERM:VocabTermItem('NOT YET SET UP', MOVING_PLATFORM_TERM, title = MOVING_PLATFORM_TERM),
177                 LAND_STATION_TERM:VocabTermItem('NOT YET SET UP', LAND_STATION_TERM, title = LAND_STATION_TERM),
178                 MOORING_TERM:VocabTermItem('NOT YET SET UP', MOORING_TERM, title = MOORING_TERM),
179                 STATION_GROUP_TERM:VocabTermItem('NOT YET SET UP', STATION_GROUP_TERM, title = STATION_GROUP_TERM),
180                 SHIP_TERM:VocabTermItem('NOT YET SET UP', SHIP_TERM, title = SHIP_TERM),
181                 AIRCRAFT_TERM:VocabTermItem('NOT YET SET UP', AIRCRAFT_TERM, title = AIRCRAFT_TERM),
182                 SATELLITE_TERM:VocabTermItem('NOT YET SET UP', SATELLITE_TERM, title = SATELLITE_TERM),
183                 COMPUTER_TERM:VocabTermItem('NOT YET SET UP', COMPUTER_TERM, title = COMPUTER_TERM)
184                 }
185
186    ATOM_TYPES = [ACTIVITY_TERM, DE_TERM, DPT_TERM, GRANULE_TERM, OBS_TERM]
187   
188    DEPLOYABLE_ATOM_TYPES = [ACTIVITY_TERM, DPT_TERM, OBS_TERM]
189
190    PROVIDER_TYPES = [BADC_TERM, NEODC_TERM]
191
192    ONLINE_REF_TYPES = [ OBJECT_PAGE_TERM, DATA_URL_TERM, DATA_PAGE_TERM, \
193                         CURATOR_PAGE_TERM, EXT_METADATA_TERM, METADATA_SOURCE_TERM, \
194                         NUM_SIM_TERM, \
195                         OPENDAP_TERM, THREDDS_TERM, WMS_TERM, WCS_TERM, WFS_TERM, \
196                         DATA_EXTRACTOR_TERM, FILE_BROWSER_TERM, LAS_TERM, LOGO_TERM]
197#URI_TERM, \
198
199    ATOM_CATEGORY = "atom"
200    DEPLOYABLE_ATOM_CATEGORY = "deployableatom"
201    ONLINE_REF_CATEGORY = "onlineref"
202    PROVIDER_CATEGORY = "provider"
203   
204    # A dictionary to group the various valid subtypes of atoms - grouped by their
205    # main type
206    SUBTYPE_TERMS = {
207                     DPT_TERM: [
208                        LIDAR_TERM, RADAR_TERM, \
209                        SONDE_TERM, NAVIGATION_TERM, \
210                        GAS_CHROMATOGRAPH_TERM, SPECTROMETER_TERM, \
211                        MASS_SPECTROMETER_TERM,
212                        MET_SENSOR_TERM, DOAS_TERM,
213                        ASOZ_TERM, RADIOMETER_TERM,
214                        FAGE_TERM, IMAGER_TERM,
215                        FILTER_TERM, PARTICLE_COUNTER_TERM,
216                        SAMPLER_TERM, OTHER_INSTRUMENT_TYPE_TERM,
217                        MODEL_TERM, INSTRUMENT_TERM
218                        ],
219                     DE_TERM: [
220                        SIMULATION_TERM, ANALYSIS_TERM, \
221                        MEASUREMENT_TERM       
222                        ],
223                     ACTIVITY_TERM: [
224                        DATA_COLLECTION_TERM, DATA_PROJECT_TERM, \
225                        DATA_CAMPAIGN_TERM, DATA_INVESTIGATION_TERM, \
226                        FLIGHT_TERM, CRUISE_TERM, \
227                        FUNDING_PROGRAM_TERM, DEPLOYMENT_TERM
228                        ],
229                     OBS_TERM: [
230                        STATIONARY_PLATFORM_TERM, MOVING_PLATFORM_TERM, \
231                        LAND_STATION_TERM, MOORING_TERM, \
232                        STATION_GROUP_TERM, SHIP_TERM, \
233                        AIRCRAFT_TERM, SATELLITE_TERM, \
234                        COMPUTER_TERM
235                        ]
236                     }
237   
238    ONE_HOUR = 3600.0
239   
240    VOCAB_SERVER_URL = 'http://vocab.ndg.nerc.ac.uk/clients/whatLists'
241
242    BROWSE_ROOT_URL = "http://localhost:5000/view/"
243
244    def __init__(self):
245        logging.info("Setting up VocabTermData object")
246        self.VOCAB_DATA_PAGE = None
247        # introduce slight delay here
248        self.REFRESH_TIME = time.time() - 2.0
249        self.latestTermVersion = {}
250        logging.info("VocabTermData object set up")
251
252
253    def isValidSubType(self, mainType, subType):
254        '''
255        Determine whether a specified subtype is valid for a particular
256        main type
257        @param mainType: term ID of the main type of the data
258        @param subType: term ID of the subtype of the data
259        @return: True if the subtype is valid
260        @raise ValueError: if the mainType is not recognised or the subType is not valid
261        '''
262        if not self.SUBTYPE_TERMS.has_key(mainType):
263            errorMessage = "Error: unrecognised data type: '%s'" %mainType
264            logging.error(errorMessage)
265            raise ValueError(errorMessage)
266
267        if subType in self.SUBTYPE_TERMS[mainType]:
268            return True
269           
270        errorMessage = "Error: subtype, '%s' is not valid for data type, '%s'" \
271            %(subType, mainType)
272        logging.error(errorMessage)
273        raise ValueError(errorMessage)
274
275
276    def getValidTypes(self, category):
277        '''
278        Return a list of the valid types available for a particular data category
279        @param category: type of term info to look up - e.g. atoms or providers
280        - this should be specified using the ..._CATEGORY instance variables
281        defined above
282        @return list of VocabTermInfo objects for the specified category
283        '''
284        logging.debug("Lookup up list of valid %s types" %category)
285        catList = []
286        if category == self.ATOM_CATEGORY:
287            catList = self.ATOM_TYPES
288        elif category == self.DEPLOYABLE_ATOM_CATEGORY:
289            catList = self.DEPLOYABLE_ATOM_TYPES
290        elif category == self.PROVIDER_CATEGORY:
291            catList = self.PROVIDER_TYPES
292        elif category == self.ONLINE_REF_CATEGORY:
293            catList = self.ONLINE_REF_TYPES
294        else:
295            errorMessage = "Unrecognised data category, '%s'" %category
296            logging.info(errorMessage)
297            raise ValueError(errorMessage)
298           
299        types = []
300        for st in catList:
301            types.append(self.TERM_DATA[st])
302        return types
303
304
305    def tidySubTypeTitle(self, title):
306        '''
307        Tidy up the title of subtypes slightly, if need be
308        - NB, these are inherited from original moles format so are
309        not really 'human readable'
310        '''
311        if title and title.startswith('dg'):
312            title = title[2:]
313            # and fix any camelcase - putting into words
314            title = re.sub('([A-Z])', r" \1", title).strip()
315
316        return title
317
318    def getValidSubTypes(self, atomType):
319        '''
320        Get list of subtypes that are valid wrt a specified atom type
321        @param atomType: term ID for the atom type whose subtypes need to
322        be looked up
323        @return: list of valid subtypes
324        '''
325        logging.debug("Lookup up subtypes for atom type, '%s'" %atomType)
326        subTypes = self.SUBTYPE_TERMS.get(atomType) or []
327        types = []
328        for st in subTypes:
329            item = self.TERM_DATA[st]
330            item.title = self.tidySubTypeTitle(item.title)
331            types.append(item)
332        logging.debug("Found subtypes: %s" %subTypes)
333        return types
334
335       
336    def getTermFromTitle(self, title):
337        '''
338        Given a term title/label, get back the related term id
339        @param title: title/label of term id to retrieve
340        @raise ValueError: if more than one title or no title is returned 
341        '''
342        termID = []
343        for val in self.TERM_DATA.itervalues():
344            if val.title == title:
345                termID.append(val.termID)
346       
347        if len(termID) != 1:
348            errorMessage = "Error: could not accurately determine the vocab term \
349                ID for the label, '%s' - %s values returned" %(title, len(termID))
350            logging.error(errorMessage)
351            raise ValueError(errorMessage)
352
353        return termID[0]
354       
355
356    def _getVocabDataPage(self):
357        '''
358        Getter method to allow regular refreshing of data
359        '''
360        if self.REFRESH_TIME < time.time():
361            self.getVocabServerData()
362       
363        return self.VOCAB_DATA_PAGE
364
365   
366    def getVocabServerData(self):
367        '''
368        Retrieve the web page containing the versioning info for the various vocab terms
369        '''
370        logging.info("Retrieving vocab server data page")
371        f = urllib.urlopen(self.VOCAB_SERVER_URL)
372        self.VOCAB_DATA_PAGE = f.read()
373        f.close()
374        self.REFRESH_TIME = time.time() + self.ONE_HOUR
375        logging.info("Vocab server data retrieved")
376
377
378    def getLatestTermVersions(self):
379        '''
380        Retrieve the latest versions of the vocab terms required
381        - NB, refreshes the data on an hourly basis, if necessary
382        '''
383        # now need to parse the returned data to get the current version number
384        for termName in self.TERM_DATA:
385            if termName not in self.latestTermVersion:
386                self.getLatestTermVersion(termName)
387
388                   
389    def getLatestTermVersion(self, termName):
390        '''
391        Parse the vocab server data and determine the latest version number of the term with the specified name
392        @param termName: name of term whose current version needs to be established
393        '''
394        logging.info("Getting latest term version for term, '%s'" %termName)
395        # check for recent data - NB, the term will probably share a base url with other terms
396        # so may have already have the correct URL set up
397        if termName not in self.latestTermVersion or self.REFRESH_TIME < time.time():
398   
399            uri = self.TERM_DATA[termName].vocabURL
400            currentVersion = self._getURIVersion(uri)
401   
402            self.latestTermVersion[termName] = str(currentVersion)
403        logging.info("Latest term version for term, '%s' retrieved" %termName)
404
405
406    def _getURIVersion(self, uri):
407        '''
408        Parse the server data for a specified uri and return the latest version number of it
409        @param uri: uri to look for
410        @raise ValueError: if specified uri not found in vocab server
411        @return version of vocab uri, '' if uri contains latest version and None if uri not found
412        '''
413        # allow the data to be refreshed on an hourly basis
414        pageData = self._getVocabDataPage()
415       
416        uri = uri.replace('/term/','/list/')
417        if uri.endswith('/'):
418            uri = uri.rstrip('/')
419        # NB, the uri may already have a version number included in it - so allow this in the search
420        regExp = re.compile(r'(' + uri + '(/(\d{1,}))?)')
421        currentVersion = None
422        foundTerm = False
423        for termVals in regExp.findall(pageData):
424            foundTerm = True
425            version = termVals[2]
426            # version number must be included in original uri, if match found without a version number
427            # being split out - so ignore this - since the original uri is already specified in full
428            if not version:
429                currentVersion = ''
430                break
431
432            # TODO: check how the versioning system will be done - i.e. 3.2.4 type versioning will cause this to break
433            if not currentVersion:
434                currentVersion = int(version)
435            elif int(version) > currentVersion:
436                currentVersion = int(version)
437
438        if currentVersion == None:
439            errorMessage = "Could not find information in vocab server for uri, '%s' - exiting" %uri
440            logging.error(errorMessage)
441            # TODO: uncomment the ValueError once all the vocab terms have been defined
442            #raise ValueError(errorMessage)
443
444        return currentVersion
445
446       
447    def getTermCurrentVocabURL(self, termName):
448        '''
449        Get the current URL on the vocab server to the specified term
450        @param termName: name of term whose URL to return
451        '''
452        logging.debug("Looking up vocab data for term: '%s'" %termName)
453       
454        if termName not in self.TERM_DATA:
455            errorMessage = "Could not find term, '%s' in defined list of valid vocab terms - exiting" %termName
456            logging.error(errorMessage)
457            raise ValueError(errorMessage)
458       
459        if termName not in self.latestTermVersion:
460            self.getLatestTermVersion(termName)
461
462        uri = self.TERM_DATA[termName].vocabURL + \
463            "/" + self.latestTermVersion[termName] + "/" + \
464            self.TERM_DATA[termName].termID
465        logging.debug("Returning vocab URL: '%s'" %uri)
466        return  uri
467   
468   
469    def getCurrentVocabURI(self, uri):
470        '''
471        Look up a specified URI and return the current version of it
472        '''
473        logging.debug("Looking up current version of uri: '%s'" %uri)
474        currentVersion = self._getURIVersion(uri)
475        logging.debug("URI version looked up")
476        if currentVersion is not None:
477            if not uri.endswith('/'):
478                uri += '/'
479            return uri + str(currentVersion)
480        return uri
481   
482    def getVTI(self, term):
483        '''
484        Return the vocab term item for the specified term
485        @param term: term ID - one of the constants defined above
486        @return VocabTermItem corresponding to term
487        @raise VocabTermDataError if term not found
488        '''
489        vti = self.TERM_DATA.get(term)
490        if not vti:
491            raise VocabTermDataError("No info for term, '%s', found" %term)
492   
493        return vti
494   
495   
496    def getTermItemfromFullVocabURI(self, uri):
497        '''
498        Given a full term id - i.e. with vocab uri + version + term ID
499        determine the correct vocab data term item
500        @param uri: full term ID with version + vocab uri
501        @return vocab term item corresponding to the input uri
502        @raise ValueError if term ID not found
503        '''
504        logging.debug("Determining term ID for uri, '%s'" %uri)
505        termID = uri.split('/')[-1]
506        vti = None
507        if self.TERM_DATA.get(termID) and uri.startswith(self.TERM_DATA[termID].vocabURL):
508            vti = self.TERM_DATA[termID]
509        else:
510            for item in self.TERM_DATA.itervalues():
511                if item.termID == termID and uri.startswith(item.vocabURL):
512                    vti = item
513                    break
514       
515        if not vti:
516            raise VocabTermDataError("Unrecognised term URI: '%s'" %uri)
517
518        logging.debug("Found matching term - '%s'" %vti.termID)
519        return vti
520   
521   
522    def isDeployable(self, vti):
523        '''
524        Determines whether a vocab term item can be used as part of a deployment
525        @param vti: vocab term item to check
526        @return True if allowed as part of deployment, false otherwise
527        '''
528        logging.debug("Checking if item is deployable")
529        if vti:
530            if vti.termID == self.ACTIVITY_TERM or \
531                vti.termID == self.DPT_TERM or \
532                vti.termID == self.OBS_TERM:
533                logging.debug("- item is deployable")
534                return True
535       
536        logging.debug("- item is not deployable")
537        return False
538   
539   
540    def isDeployment(self, vti):
541        '''
542        Determines whether a vocab term item is a deployment item
543        @param vti: vocab term item to check
544        @return True if a deployment, false otherwise
545        '''
546        logging.debug("Checking if item is a deployment")
547        if vti:
548            if vti.termID == self.DEPLOYMENT_TERM:
549                logging.debug("- item is a deployment")
550                return True
551       
552        logging.debug("- item is not a deployment")
553        return False
554   
555   
556    def isGranule(self, vti):
557        '''
558        Determines whether a vocab term item is a granule item
559        @param vti: vocab term item to check
560        @return True if a granule, false otherwise
561        '''
562        logging.debug("Checking if item is a granule")
563        if vti:
564            if vti.termID == self.GRANULE_TERM:
565                logging.debug("- item is a granule")
566                return True
567       
568        logging.debug("- item is not a granule")
569        return False
570               
Note: See TracBrowser for help on using the repository browser.