Changeset 4219


Ignore:
Timestamp:
23/09/08 14:22:51 (11 years ago)
Author:
cbyrom
Message:

Simplify vocab data by only returning termIDs for the subtypes + fix
a typo + extend Atom, allowing dicts of values to be set in the constructor +
add method to determine the eXist collection relating to the atom state.

Location:
exist/trunk/python/ndgUtils
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • exist/trunk/python/ndgUtils/models/Atom.py

    r4217 r4219  
    120120 
    121121    def __init__(self, atomType = None, vocabTermData = None, ndgObject = None, \ 
    122                  xmlString = None): 
     122                 xmlString = None, state = eXistConnector.WORKING_COLLECTION_PATH, **inputs): 
    123123        ''' 
    124124        Constructor - initialise the atom variables 
     
    162162        self.t1 = None 
    163163        self.t2 = None 
     164 
     165        self.ME = ME.MolesEntity() 
     166         
     167        # date when the atom was first ingested 
     168        self.publishedDate = None 
     169 
     170        # last update date 
     171        self.updateDate = None 
     172 
     173        # assume atom in working state by default - this is used to define what collection 
     174        # in eXist the atom is stored in 
     175        self.state = state 
    164176         
    165177        # additional, non standard atom data can be included in the molesExtra element 
     
    168180        else: 
    169181            self.VTD = VTD() 
     182         
     183        if xmlString: 
     184            self.fromString(xmlString) 
     185 
     186        # if inputs passed in as dict, add these now 
     187        self.__dict__.update(inputs) 
    170188 
    171189        if self.atomTypeID: 
    172190            self.atomTypeName = self.VTD.TERM_DATA[self.atomTypeID].title 
    173191 
    174         self.ME = ME.MolesEntity() 
    175          
    176         # date when the atom was first ingested 
    177         self.publishedDate = None 
    178  
    179         # last update date 
    180         self.updateDate = None 
    181  
    182         # assume atom in working state by default - this is used to define what collection 
    183         # in eXist the atom is stored in 
    184         self.state = eXistConnector.WORKING_COLLECTION_PATH 
    185          
    186         if xmlString: 
    187             self.fromString(xmlString) 
    188              
    189192        logging.info("Atom initialised") 
     193 
     194 
     195    def getEXistCollectionPath(self): 
     196        ''' 
     197        Determine the correct collection to use for the atom in eXist 
     198        ''' 
     199        collectionPath = eXistConnector.BASE_COLLECTION_PATH + self.state 
     200         
     201        if self.atomTypeID == VTD.DE_TERM: 
     202            collectionPath += eXistConnector.DE_COLLECTION_PATH 
     203        elif self.atomTypeID == VTD.GRANULE_TERM: 
     204            collectionPath += eXistConnector.GRANULE_COLLECTION_PATH 
     205        else: 
     206            collectionPath += eXistConnector.DEPLOYMENT_COLLECTION_PATH 
     207         
     208        if self.ME: 
     209            collectionPath += self.ME.providerID + "/" 
     210        return collectionPath 
     211 
    190212 
    191213 
     
    196218        logging.debug("Lookup up subtypes for type, '%s'" %self.atomTypeID) 
    197219        subTypes = self.VTD.SUBTYPE_TERMS.get(self.atomTypeID) or [] 
    198         logging.debug(subTypes) 
    199         return subTypes 
     220        types = [] 
     221        for st in subTypes: 
     222            types.append(self.VTD.TERM_DATA[st]) 
     223        logging.debug("Found subtypes: %s" %subTypes) 
     224        return types 
    200225         
    201226             
  • exist/trunk/python/ndgUtils/models/testatom.py

    r4209 r4219  
    4444            '<link href="http://badc.nerc.ac.uk/a-real-wms-endpoint" rel="http://vocab.ndg.nerc.ac.uk/term/P201/1/GCMDU026" title="WMS"/>' + \ 
    4545            '<link href="http://badc.nerc.ac.uk/alternate" rel="alternate" title="alternate page"/>' + \ 
    46             '<link href="' + logo + '" rel="' + VTD.TERM_DATA[VTD.LOGO_TERM].vocabURL + '" title="logo"/>' + \ 
     46            '<link href="' + logo + '" title="' + VTD.TERM_DATA[VTD.LOGO_TERM].title + '" rel="logo"/>' + \ 
    4747            '<author><name>Tiddeman, David</name><uri>www.cb.org</uri></author>' + \ 
    4848            '<contributor><name>Brian Bandy</name><uri>www.cb.org</uri></contributor>' + \ 
     
    9696    def testAddValidParameters(self): 
    9797        self.atom.addParameters(self.validParams) 
    98         self.assertEqual(self.atom.parameters[0].term, getTripleData(self.validParams[0])[0]) 
    99         self.assertEqual(self.atom.parameters[1].term, getTripleData(self.validParams[1])[0]) 
    100         self.assertEqual(self.atom.parameters[2].term, getTripleData(self.validParams[2])[0]) 
     98        self.assertEqual(self.atom.parameters[0].term, getTripleData(self.validParams[0])[2]) 
     99        self.assertEqual(self.atom.parameters[1].term, getTripleData(self.validParams[1])[2]) 
     100        self.assertEqual(self.atom.parameters[2].term, getTripleData(self.validParams[2])[2]) 
    101101         
    102102         
    103103    def testAddMessyParameters(self): 
    104104        self.atom.addParameters(self.messyParams) 
    105         self.assertEqual(self.atom.parameters[0].term, 'OZONE MIXING RATIO &amp; (PPB )')# | http://vocab.ndg.nerc.ac.uk/blah | OZONE') 
    106         self.assertEqual(self.atom.parameters[1].term, 'ERROR FLAG (OZONE MIXING &gt; RATIO )')# | http://vocab.ndg.nerc.ac.uk/blah | ERROR') 
    107         self.assertEqual(self.atom.parameters[2].term, 'HYDROGEN PEROXIDE &lt; PPBV') 
     105        self.assertEqual(self.atom.parameters[0].term, 'OZONE') 
     106        self.assertEqual(self.atom.parameters[1].term, 'ERROR') 
     107        self.assertEqual(self.atom.parameters[2].term, 'HYDROGEN &amp;') 
    108108        self.assertEqual(self.atom.parameters[2].scheme, 'http://vocab.ndg.nerc.ac.uk/blah') 
    109         self.assertEqual(self.atom.parameters[2].label, 'HYDROGEN &amp;') 
     109        self.assertEqual(self.atom.parameters[2].label.lower(), 'Hydrogen Peroxide &lt; ppbv'.lower()) 
    110110         
    111111    def testFromString(self): 
     
    172172        #self.assertEqual(xml, self.xmlString) 
    173173 
    174  
     174    def testCreateWithInputDict(self): 
     175        inputs = { 
     176                  'atomTypeID': VTD.ACTIVITY_TERM, 
     177                  'title': self.title 
     178                  } 
     179        a = Atom.Atom(**inputs) 
     180        self.assertEqual(self.title, a.title) 
     181        self.assertEqual(VTD.ACTIVITY_TERM, a.atomTypeID) 
     182        self.assertEqual(VTD.TERM_DATA[VTD.ACTIVITY_TERM].title, a.atomTypeName) 
    175183 
    176184 
  • exist/trunk/python/ndgUtils/vocabtermdata.py

    r4218 r4219  
    6969     
    7070    # activity subtypes 
    71     DATA_COLLECTION_TERM = "dgDataCollection" 
    72     DATA_PROJECT_TERM = "dgDataProject" 
    73     DATA_CAMPAIGN_TERM = "dgDataCampaignProject" 
    74     DATA_INVESTIGATION_TERM = "dgDataInvestigation" 
     71    DATA_COLLECTION_TERM = "dgActivityDataCollection" 
     72    DATA_PROJECT_TERM = "dgActivityDataProject" 
     73    DATA_CAMPAIGN_TERM = "dgActivityDataCampaign" 
     74    DATA_INVESTIGATION_TERM = "dgActivityDataInvestigation" 
    7575    FLIGHT_TERM = "dgFlight" 
    7676    CRUISE_TERM = "dgCruise" 
     
    114114                 SPECTROMETER_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG07', title = SPECTROMETER_TERM), 
    115115                 MASS_SPECTROMETER_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG08', title = MASS_SPECTROMETER_TERM), 
    116                  MET_SENSOR_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG09', title = _TERM), 
     116                 MET_SENSOR_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG09', title = MET_SENSOR_TERM), 
    117117                 DOAS_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG10', title = DOAS_TERM), 
    118118                 ASOZ_TERM:VocabTermItem('http://vocab.ndg.nerc.ac.uk/term/C330', 'NG11', title = ASOZ_TERM), 
     
    155155    SUBTYPE_TERMS = { 
    156156                     DPT_TERM: [ 
    157                         TERM_DATA[LIDAR_TERM], TERM_DATA[RADAR_TERM], \ 
    158                         TERM_DATA[SONDE_TERM], TERM_DATA[NAVIGATION_TERM], \ 
    159                         TERM_DATA[GAS_CHROMATOGRAPH_TERM], TERM_DATA[SPECTROMETER_TERM], \ 
    160                         TERM_DATA[MASS_SPECTROMETER_TERM], 
    161                         TERM_DATA[MET_SENSOR_TERM], TERM_DATA[DOAS_TERM], 
    162                         TERM_DATA[ASOZ_TERM], TERM_DATA[RADIOMETER_TERM], 
    163                         TERM_DATA[FAGE_TERM], TERM_DATA[IMAGER_TERM], 
    164                         TERM_DATA[FILTER_TERM], TERM_DATA[PARTICLE_COUNTER_TERM], 
    165                         TERM_DATA[SAMPLER_TERM], TERM_DATA[OTHER_INSTRUMENT_TYPE_TERM], 
    166                         TERM_DATA[MODEL_TERM], TERM_DATA[INSTRUMENT_TERM] 
     157                        LIDAR_TERM, RADAR_TERM, \ 
     158                        SONDE_TERM, NAVIGATION_TERM, \ 
     159                        GAS_CHROMATOGRAPH_TERM, SPECTROMETER_TERM, \ 
     160                        MASS_SPECTROMETER_TERM, 
     161                        MET_SENSOR_TERM, DOAS_TERM, 
     162                        ASOZ_TERM, RADIOMETER_TERM, 
     163                        FAGE_TERM, IMAGER_TERM, 
     164                        FILTER_TERM, PARTICLE_COUNTER_TERM, 
     165                        SAMPLER_TERM, OTHER_INSTRUMENT_TYPE_TERM, 
     166                        MODEL_TERM, INSTRUMENT_TERM 
    167167                        ], 
    168168                     DE_TERM: [ 
    169                         TERM_DATA[SIMULATION_TERM], TERM_DATA[ANALYSIS_TERM], \ 
    170                         TERM_DATA[MEASUREMENT_TERM]        
     169                        SIMULATION_TERM, ANALYSIS_TERM, \ 
     170                        MEASUREMENT_TERM        
    171171                        ], 
    172172                     ACTIVITY_TERM: [ 
    173                         TERM_DATA[DATA_COLLECTION_TERM], TERM_DATA[DATA_PROJECT_TERM], \ 
    174                         TERM_DATA[DATA_CAMPAIGN_TERM], TERM_DATA[DATA_INVESTIGATION_TERM], \ 
    175                         TERM_DATA[FLIGHT_TERM], TERM_DATA[CRUISE_TERM], \ 
    176                         TERM_DATA[FUNDING_PROGRAM_TERM], 
     173                        DATA_COLLECTION_TERM, DATA_PROJECT_TERM, \ 
     174                        DATA_CAMPAIGN_TERM, DATA_INVESTIGATION_TERM, \ 
     175                        FLIGHT_TERM, CRUISE_TERM, \ 
     176                        FUNDING_PROGRAM_TERM, 
    177177                        ], 
    178178                     OBS_TERM: [ 
    179                         TERM_DATA[STATIONARY_PLATFORM_TERM], TERM_DATA[MOVING_PLATFORM_TERM], \ 
    180                         TERM_DATA[LAND_STATION_TERM], TERM_DATA[MOORING_TERM], \ 
    181                         TERM_DATA[STATION_GROUP_TERM], TERM_DATA[SHIP_TERM], \ 
    182                         TERM_DATA[AIRCRAFT_TERM], TERM_DATA[SATELLITE_TERM], \ 
    183                         TERM_DATA[COMPUTER_TERM]  
     179                        STATIONARY_PLATFORM_TERM, MOVING_PLATFORM_TERM, \ 
     180                        LAND_STATION_TERM, MOORING_TERM, \ 
     181                        STATION_GROUP_TERM, SHIP_TERM, \ 
     182                        AIRCRAFT_TERM, SATELLITE_TERM, \ 
     183                        COMPUTER_TERM  
    184184                        ] 
    185185                     } 
     
    203203 
    204204 
     205    def isValidSubType(self, mainType, subType): 
     206        ''' 
     207        Determine whether a specified subtype is valid for a particular 
     208        main type 
     209        @param mainType: term ID of the main type of the data 
     210        @param subType: term ID of the subtype of the data 
     211        @return: True if the subtype is valid 
     212        @raise ValueError: if the mainType is not recognised or the subType is not valid  
     213        ''' 
     214        if not self.SUBTYPE_TERMS.has_key(mainType): 
     215            errorMessage = "Error: unrecognised data type: '%s'" %mainType 
     216            logging.error(errorMessage) 
     217            raise ValueError(errorMessage) 
     218 
     219        if subType in self.SUBTYPE_TERMS[mainType]: 
     220            return True 
     221             
     222        errorMessage = "Error: subtype, '%s' is not valid for data type, '%s'" \ 
     223            %(subType, mainType) 
     224        logging.error(errorMessage) 
     225        raise ValueError(errorMessage) 
     226 
     227         
    205228    def getTermFromTitle(self, title): 
    206229        ''' 
Note: See TracChangeset for help on using the changeset viewer.