Changeset 5371


Ignore:
Timestamp:
09/06/09 12:34:05 (8 years ago)
Author:
lawrence
Message:

Fixes to the underlying utils for handling the vocab server interface.
Note that by moving this stuff to it's own module, there might be
consequential changes in calling code.

Location:
exist/trunk/python/ndgUtils
Files:
1 added
1 edited

Legend:

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

    r4638 r5371  
    11from eXistInterface import ndg_eXist 
    22from ndgXqueries import ndgXqueries 
     3 
    34 
    45import urllib2, logging, socket 
     
    1415 
    1516debug=0 
    16  
    17  
    18 def httpify(url): 
    19     ''' 
    20     Ensure a url has an http prefix 
    21     ''' 
    22     if url[0:4] != 'http': 
    23         url = 'http://' + url 
    24     return url 
    25      
    26          
     17  
    2718class DocumentRetrieve(ndg_eXist): 
    2819    '''  
     
    10596 
    10697             
    107 class genericHTTP(object): 
    108     ''' Provides a generic HTTP request class ''' 
    109     def __init__(self,proxyServer=None): 
    110         if proxyServer is None: 
    111             proxyHandler=urllib2.ProxyHandler({}) 
    112         else: 
    113             proxy=httpify(proxyServer) 
    114             proxyHandler=urllib2.ProxyHandler({'http':proxy}) 
    115         self.opener=urllib2.build_opener(proxyHandler) 
    116          
    117     def get(self,url): 
    118         url = httpify(url) 
    119         request=urllib2.Request(url) 
    120         logging.info("Getting data from url: %s" %url) 
    121         response='Cannot obtain remote file: ' 
    122         try: 
    123             f = self.opener.open(request) 
    124             response='' 
    125         except urllib2.URLError,e: 
    126             if hasattr(e,'reason'): 
    127                 response+='No access to server [%s]'%e.reason 
    128             elif hasattr(e,'code'): 
    129                 response+='Response code [%s]'%e.code 
    130         except socket.error: 
    131             response+='Network Socket problem' 
    132         except Exception,e: 
    133             response+='[%s]'%str(e) 
    134              
    135         if response=='': 
    136             return f.read() 
    137         else: 
    138             raise IOError(response)   
    139  
    140              
    141 class ndgHTTP(genericHTTP): 
    142     ''' Provides a get method to obtain an xml document from a remote NDG repository ''' 
    143     def __init__(self,remoteHost,proxyServer=None): 
    144         self.remoteHost=remoteHost 
    145         genericHTTP.__init__(self,proxyServer) 
    146          
    147     def uriget(self,uri): 
    148         # NB, having this import at the module level can cause problems 
    149         # with resolving imports when using this module - e.g. from the 
    150         # ndgObject level 
    151         import ndgObject 
    152         n=ndgObject.ndgObject(uri) 
    153         return self.get(n.repository,n.schema,n.localID) 
    154  
    155     def get(self,repository,schema,localID,**kw): 
    156         ''' Return a remote ndg document ''' 
    157         #TODO what about security? Probably means we need to get the headers of our responses sorted ... 
    158         url='%s/retrieve/%s__%s__%s'%(self.remoteHost,repository,schema,localID) 
    159         return genericHTTP.get(self,url) 
    160          
    161     def setSecurity(self,location,usercode,password): 
    162         ''' Use a usercode password to set security credentials at a specific location ''' 
    163         pass 
    164  
    165      
    166 class ndgVocabPOX(genericHTTP): 
    167     ''' Provides a POX interface to the vocab server ''' 
    168     def __init__(self,path="http://vocab.ndg.nerc.ac.uk/axis2/services/vocab/",proxyServer=None): 
    169         genericHTTP.__init__(self,proxyServer) 
    170         self.path="http://vocab.ndg.nerc.ac.uk/axis2/services/vocab/" 
    171         self.ns="http://vocab.ndg.nerc.ac.uk/" 
    172     def getRelated(self,subject): 
    173         ''' Get a related record ''' 
    174         url='%sgetRelatedRecordByCriteria?subjectText=%s&predicate=255&inferences=True&objectList=%slist/P211/current'%(self.path,subject,self.ns) 
    175         self.url=url 
    176         doc=genericHTTP.get(self,url) 
    177         x=ET.fromstring(doc) 
    178         b=x.findall('*/{urn:vocab/types}broadMatch') 
    179         n=x.findall('*/{urn:vocab/types}narrowMatch') 
    180         s=x.findall('*/{urn:vocab/types}exactMatch') 
    181         self.broader=[(i.find('{urn:vocab/types}entryTerm').text or '') for i in b] 
    182         self.narrower=[(i.find('{urn:vocab/types}entryTerm').text or '') for i in n] 
    183         self.synonyms=[(i.find('{urn:vocab/types}entryTerm').text or '') for i in s] 
    184         return [self.broader,self.narrower,self.synonyms] 
    185  
Note: See TracChangeset for help on using the changeset viewer.