Changeset 4580


Ignore:
Timestamp:
09/12/08 16:15:06 (11 years ago)
Author:
cbyrom
Message:

Add logic to cope with changes to the Atom structure - to allow granulite files and logos data to be stored + improve error handling.

Location:
exist/trunk/python/ndgUtils/lib
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • exist/trunk/python/ndgUtils/lib/existdbclient.py

    r4555 r4580  
    9696        ''' 
    9797        logging.info("Looking up atom with id, '%s'" %(id)) 
    98         doc = self.xmldb.get('', DR.ATOM, id, targetCollection = ec.BASE_COLLECTION_PATH) 
     98        doc = self.xmldb.get('', DR.ATOM, id, \ 
     99                             targetCollection = ec.BASE_COLLECTION_PATH) 
    99100        logging.info("Atom retrieved") 
    100101        return doc 
  • exist/trunk/python/ndgUtils/lib/granulite.py

    r4571 r4580  
    1919    # expected sections in a granulite file 
    2020    AUTHORS_SECTION = "authors" 
    21     FILE_SECTION = "file" 
     21    FILE_SECTION = "files" 
    2222    PARAMETERS_SECTION = "parameters" 
    2323    COVERAGE_SECTION = "coverage" 
     
    3636 
    3737    # group the data together as either single valued or array valued - to ease setting attributes on Atom 
    38     singleVals = {LOGO_SECTION:'logo'}   
    39     arrayVals = {AUTHORS_SECTION:'authors', FILE_SECTION:'files', GRANULE_AUTHORS_SECTION:'atomAuthors', \ 
    40                  SUMMARY_SECTION:'summary', RELATED_LINKS_SECTION:'relatedLinks'} 
     38    singleVals = {}   
     39    arrayVals = {AUTHORS_SECTION:'authors', \ 
     40                 FILE_SECTION:'files', GRANULE_AUTHORS_SECTION:'atomAuthors', \ 
     41                 SUMMARY_SECTION:'summary'} 
    4142         
    4243    # config file with eXist DB details 
     
    144145         
    145146        # assume we've already read in the file if it is multi-line 
    146         if len(self._granulite) > 1: 
    147             granuliteData = self._granulite.split('\n') 
     147        newLine = None 
     148        # cope with unix and dos end of lines 
     149        if self._granulite.find('\n') > -1: 
     150            newLine = '\n' 
     151        elif self._granulite.find('\r') > -1: 
     152            newLine = '\r' 
     153         
     154        if newLine: 
     155            granuliteData = self._granulite.split(newLine) 
    148156        else: 
    149157            # Check this file exists 
     
    158166        # create a dictionary of all data in file - then use this to get the required data 
    159167        granulite_data = {} 
     168         
     169        # initialise vocab term data lookup object 
     170        self.VTD = VTD() 
    160171 
    161172        sectionName = "" # variable to indicate what section we're currently parsing in the granulite file data 
     
    210221                self._atom.setAttribute(self.singleVals[attribute], granulite_data[attribute][0]) 
    211222 
     223        # NB, explicitly set the related links before running the arrayVals loop, since 
     224        # this will replace any file Links created if it occurs after these have been 
     225        # created (was originally in arrayVals, but this doesn't enforce any specific ordering) 
     226        if self.RELATED_LINKS_SECTION in granulite_data: 
     227            self._atom.setAttribute('relatedLinks', \ 
     228                                    granulite_data[self.RELATED_LINKS_SECTION]) 
     229 
    212230        # now add the arrays data 
    213231        for attribute in self.arrayVals: 
     
    226244        self._atom.setDatasetID(data[1]) 
    227245        self._atom.title = data[2] 
     246         
     247        if self.LOGO_SECTION in granulite_data: 
     248            for logo in granulite_data[self.LOGO_SECTION]: 
     249                self._atom.addRelatedLinks(logo + " | Logo | " + \ 
     250                                           self.VTD.getTermCurrentVocabURL(VTD.LOGO_TERM)) 
    228251 
    229252        # add the parameters data via the method - since this does some tidying up of the data 
     
    235258        if not self._csmlFileName: 
    236259            if self.CSML_SECTION in granulite_data: 
    237                 self._csmlFileName = granulite_data[self.CSML_SECTION][0] 
     260                self.__addCSMLOrCDMLData(granulite_data[self.CSML_SECTION][0], None) 
    238261 
    239262        if not self._cdmlFileName: 
     
    244267             
    245268                data = utilities.getTripleData(granulite_data[self.CDML_SECTION][0]) 
    246                 self._cdmlFileName = data[0] 
     269                self.__addCSMLOrCDMLData(data[0], None) 
    247270                self._datasetID = data[1] 
    248271                self._cdmlTimeAxis = data[2] 
     
    256279        if self.TEMPORAL_DATA in granulite_data: 
    257280            self._extractTemporalData(granulite_data[self.TEMPORAL_DATA][0]) 
    258              
     281 
    259282        logging.info("All input data set up") 
    260283         
     
    348371         
    349372        logging.debug("Now, save the updated DE atom back to eXist") 
    350         self._eXist.createOrUpdateEXistFile(de.toPrettyXML(), \ 
    351                                             de.getDefaultCollectionPath(), \ 
    352                                             de.atomName) 
     373        self._eXist.createAtomInExist(de) 
    353374        logging.debug("DE atom updated") 
    354375 
     
    363384    def __processCSMLFile(self): 
    364385        logging.info("Processing CSML file") 
     386        # only keep file name, if full path specified 
     387        fileName = self._csmlFileName 
     388        fileName = fileName.split('/')[-1] 
     389        fileName = fileName.split('\\')[-1] 
     390         
    365391        csmlDoc = self._atom.addCSMLData(self._csmlFileName, \ 
    366392                                         self._csmlContent, useCSMLID = self.useCSMLID) 
     
    369395                                        eXistConnector.NDG_A_COLLECTION_PATH + \ 
    370396                                        self._atom.ME.providerID + '/', \ 
    371                                         self._csmlFileName) 
     397                                        fileName) 
    372398        logging.info("CSML file added to eXist") 
    373399        logging.info("Finished processing CSML file") 
     
    433459            except IOError, e: 
    434460                logging.error(e.message) 
     461                 
    435462            if not fileContent: 
    436                 logging.info("Could not load any data from file, '%s' - skipping") 
    437                 return None 
     463                raise ValueError("Could not load data from file, '%s'" %fileName) 
    438464 
    439465        # if we're dealing with a CDML file, process this to produce a CSML file 
     
    509535        logging.info("granulite processing complete") 
    510536        return self._atom 
     537 
     538 
     539if __name__=="__main__": 
     540    opts, args = getopt.getopt(sys.argv[1:], '-oxuvd') 
     541    if len(args) < 1: 
     542        print "Error: Please specify a granulite data file to process" 
     543        sys.exit(2) 
     544     
     545    logging.basicConfig(level = logging.DEBUG, 
     546                        format='%(asctime)s %(filename)s:%(lineno)d %(levelname)s %(message)s') 
     547    g = granulite(args[0]) 
     548    g.processGranulite() 
Note: See TracChangeset for help on using the changeset viewer.