Changeset 4196


Ignore:
Timestamp:
15/09/08 10:41:44 (11 years ago)
Author:
cbyrom
Message:

Extend xmlHandler to allow specification of different namespaces when
using the _distrbutens method to decorate xpath expressions with namespaces
+ add logging + add namespace info to ndgObject - for NSs used in the
various doc types + fix bug in ndgSearch to check response Hits properly.

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

Legend:

Unmodified
Added
Removed
  • exist/trunk/python/ndgUtils/ndgObject.py

    r4187 r4196  
    66                (1) a downloadable xml representation from a repository, 
    77                (2) a printable xml representation ''' 
    8      
     8 
    99    # The various different document types: 
    1010    MOLES_DOC_TYPE = 'NDG-B0' 
     
    1919    ATOM_DOC_TYPE = 'ATOM' 
    2020    ATOM_BACKUP_DOC_TYPE = 'ATOM-BACKUP' 
     21 
     22        # various namespaces used in the docs 
     23    ATOM_NS = 'http://www.w3.org/2005/Atom' 
     24    MOLES_NS = 'http://ndg.nerc.ac.uk/schema/moles2alpha' 
     25    GEOSS_NS = 'http://www.georss.org/georss' 
     26    GML_NS = 'http://www.opengis.net/gml' 
    2127         
    2228    # Group the doc types according to the source they should be retrieved from 
     
    2632                      NUMSIM_DOC_TYPE, ATOM_DOC_TYPE, \ 
    2733                      ATOM_BACKUP_DOC_TYPE] 
    28  
     34     
    2935    def __init__(self,uri,config=None): 
    3036        ''' Parse the uri and prepare for obtaining the actual content''' 
     
    145151        #now, we'll build a stub-B url as well, in case that comes in handy  
    146152        if self.schema!='NumSim' and self.gettable<>-1: 
    147             self.BURL=discoveryBASE.replace(self.schema,'NDG-B1') 
     153            if self.schema: 
     154                self.BURL=discoveryBASE.replace(self.schema,'NDG-B1') 
    148155            if server: 
    149156                self.BURL=self.BURL.replace(server,servicehost) 
  • exist/trunk/python/ndgUtils/ndgSearch.py

    r4187 r4196  
    3434# 
    3535 
    36 default_HostAndPort="http://localhost:8080/axis2/services/DiscoveryService" 
     36default_HostAndPort="http://ndg.badc.rl.ac.uk:8080/axis2/services/DiscoveryService" 
    3737 
    3838class ndgSearch: 
     
    9898        etime=time.time()-itime 
    9999        logging.debug('Search Request [%s] took [%ss]'%(term,etime)) 
    100          
     100 
     101        # NB, set default and only change this if response lists Hits - this 
     102        # can return 'None' - so isn't reliable var to set to without checking 
     103        self.hits = 0 
     104        if response.Hits: 
     105            self.hits = response.Hits 
     106             
    101107        if response._status: 
    102108            self.serverSessionID=response._resultId 
    103             self.hits=response.Hits 
    104109            self.documents=response.Documents.Document 
    105         else: 
    106             self.hits=response.Hits 
    107110         
    108111        logging.info("Search returned %s results: %s" %(self.hits, self.documents)) 
     
    225228        #so we know that the following call is the problem ... 
    226229        responses=self.getAllDocs(format) 
     230         
    227231        i=len(self.documents) 
    228232        j=len(responses) 
  • exist/trunk/python/ndgUtils/xmlHandler2.py

    r4050 r4196  
    1313         
    1414from xml.parsers.expat import ExpatError 
    15 import StringIO, re 
     15import StringIO, re, logging 
    1616XMLHDR='<?xml version="1.0"' 
    1717from ETxmlView import subAI 
     
    2424           - load up an element-tree 
    2525           - collect a namespace map ''' 
    26          
     26        logging.info("Reading in XML file - to create an elementtree object") 
    2727        self.cleanup=subAI() 
    2828 
     
    5252        self.__getns() 
    5353         
    54          
    5554        self.tree=ET.XML(self.xmls) 
    5655        self.__updatens() 
     56        logging.info("XML file read into elementtree object") 
    5757     
    5858    def __getns(self): 
     
    6161        #ought to do this with a regular expression, but needs must 
    6262        # or bettter yet, use iterparse in the first place, but that seemed slow. 
    63          
    64  
    6563        self.defns=None 
    6664        if self.xmls[0:19]==XMLHDR: 
     
    176174        return h 
    177175      
    178     def _distributens(self,xpathExpression): 
    179         ''' Actually we only support tag finding in this ''' 
    180         if self.defns is None: return xpathExpression 
     176    def _distributens(self, xpathExpression, alternativeNS=None): 
     177        '''  
     178        Update xpath expression to include namespaces.  NB, the 
     179        default doc NS is used if available and if the keyword, 'alternativeNS' 
     180        has not been set to specify a different namespace 
     181        @param xpathExpression: xpath expression to update with namespace 
     182        @type xpathExpression: str 
     183        @keyword alternativeNS: alternative namespace to decorate the xpath 
     184        expression with 
     185        @type alternativeNS: str 
     186        @return: xpath expression with name space decoration   
     187        ''' 
     188        logging.debug("Adding namespace to xpath expression, '%s'" \ 
     189                      %xpathExpression) 
     190        if self.defns is None and not alternativeNS: 
     191            logging.debug("- no namespace specified - returning") 
     192            return xpathExpression 
     193         
    181194        tags=xpathExpression.split('/') 
    182195        new='' 
     196        ns = (alternativeNS or self.defns) 
    183197        for t in tags:  
    184198            if t[1]<>'{':  
    185                 new+='{%s}%s/'%(self.defns,t) 
     199                new+='{%s}%s/'%(ns, t) 
    186200            else: 
    187201                new+=t+'/' 
    188202        new=new[0:-1] 
     203        logging.debug("- added namespace, '%s'" %ns) 
     204        logging.debug("- final xpath expression, '%s'" %new) 
    189205        return new 
    190206         
Note: See TracChangeset for help on using the changeset viewer.