source: ndgCommon/trunk/ndg/common/src/dal/DocumentRetrieve.py @ 4930

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/dal/DocumentRetrieve.py@4930
Revision 4930, 3.6 KB checked in by cbyrom, 11 years ago (diff)

Remove main DocumentRetrieve? functionality - since this is now fully
handled by the various new clients created. Retain the http objects
incase they are actually needed.

Line 
1'''
2 Extend eXistInterface class - to add document retrieval functionality
3 
4 @author: B Lawrence?
5'''
6from xml.etree import ElementTree as ET
7import urllib2, logging, socket
8
9
10def httpify(url):
11    '''
12    Ensure a url has an http prefix
13    '''
14    if url[0:4] != 'http':
15        url = 'http://' + url
16    return url
17
18           
19class genericHTTP(object):
20    ''' Provides a generic HTTP request class '''
21    def __init__(self,proxyServer=None):
22        if proxyServer is None:
23            proxyHandler=urllib2.ProxyHandler({})
24        else:
25            proxy=httpify(proxyServer)
26            proxyHandler=urllib2.ProxyHandler({'http':proxy})
27        self.opener=urllib2.build_opener(proxyHandler)
28       
29    def get(self,url):
30        url = httpify(url)
31        request=urllib2.Request(url)
32        logging.info("Getting data from url: %s" %url)
33        response='Cannot obtain remote file: '
34        try:
35            f = self.opener.open(request)
36            response=''
37        except urllib2.URLError,e:
38            if hasattr(e,'reason'):
39                response+='No access to server [%s]'%e.reason
40            elif hasattr(e,'code'):
41                response+='Response code [%s]'%e.code
42        except socket.error:
43            response+='Network Socket problem'
44        except Exception,e:
45            response+='[%s]'%str(e)
46           
47        if response=='':
48            return f.read()
49        else:
50            raise IOError(response) 
51
52           
53class ndgHTTP(genericHTTP):
54    ''' Provides a get method to obtain an xml document from a remote NDG repository '''
55    def __init__(self,remoteHost,proxyServer=None):
56        self.remoteHost=remoteHost
57        genericHTTP.__init__(self,proxyServer)
58       
59    def uriget(self,uri):
60        # NB, having this import at the module level can cause problems
61        # with resolving imports when using this module - e.g. from the
62        # ndgObject level
63        from ndg.common.src.models.ndgObject import ndgObject
64        n = ndgObject(uri)
65        return self.getNDGDoc(n.repository,n.schema,n.localID)
66
67    def getNDGDoc(self,repository,schema,localID,**kw):
68        ''' Return a remote ndg document '''
69        #TODO what about security? Probably means we need to get the headers of our responses sorted ...
70        url='%s/retrieve/%s__%s__%s'%(self.remoteHost,repository,schema,localID)
71        return genericHTTP.get(self,url)
72       
73    def setSecurity(self,location,usercode,password):
74        ''' Use a usercode password to set security credentials at a specific location '''
75        pass
76
77   
78class ndgVocabPOX(genericHTTP):
79    ''' Provides a POX interface to the vocab server '''
80    def __init__(self,path="http://vocab.ndg.nerc.ac.uk/axis2/services/vocab/",proxyServer=None):
81        genericHTTP.__init__(self,proxyServer)
82        self.path="http://vocab.ndg.nerc.ac.uk/axis2/services/vocab/"
83        self.ns="http://vocab.ndg.nerc.ac.uk/"
84    def getRelated(self,subject):
85        ''' Get a related record '''
86        url='%sgetRelatedRecordByCriteria?subjectText=%s&predicate=255&inferences=True&objectList=%slist/P211/current'%(self.path,subject,self.ns)
87        self.url=url
88        doc=genericHTTP.get(self,url)
89        x=ET.fromstring(doc)
90        b=x.findall('*/{urn:vocab/types}broadMatch')
91        n=x.findall('*/{urn:vocab/types}narrowMatch')
92        s=x.findall('*/{urn:vocab/types}exactMatch')
93        self.broader=[(i.find('{urn:vocab/types}entryTerm').text or '') for i in b]
94        self.narrower=[(i.find('{urn:vocab/types}entryTerm').text or '') for i in n]
95        self.synonyms=[(i.find('{urn:vocab/types}entryTerm').text or '') for i in s]
96        return [self.broader,self.narrower,self.synonyms]
97
Note: See TracBrowser for help on using the repository browser.