Changeset 4994


Ignore:
Timestamp:
18/02/09 16:19:23 (10 years ago)
Author:
cbyrom
Message:

Add keyword to getAtomFeed to allow proxies to be set explicitly. Also
deal with the case when these have been switched off; feedparser
will automatically pick up the underlying os env variable settings
and these may not match the keyword settings.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ndgCommon/trunk/ndg/common/src/clients/xmldb/eXist/feedclient.py

    r4983 r4994  
    55 @author: C Byrom - Tessella 09 
    66''' 
    7 import logging, httplib 
     7import logging, httplib, urllib2, os 
    88import feedparser 
    99from ndg.common.src.models.vocabtermdata import VocabTermData as VTD 
    1010from ndg.common.src.models.ndgObject import ndgObject 
     11import ndg.common.src.lib.utilities as utils 
    1112import ndg.common.src.clients.xmldb.eXist.dbconstants as dc 
    1213 
     
    150151 
    151152 
    152     def getAtomFeed(self, collection): 
     153    def getAtomFeed(self, collection, proxyServer = None): 
    153154        ''' 
    154155        Retrieve the contents of the feed for the specified eXist collection 
     
    156157        @param collection: eXist collection to retrieve the feed from 
    157158        @return feed: feed in a feedparser object 
     159        @keyword proxyServer: proxy server to use, if required  
    158160        ''' 
    159161        logging.info("Retrieving content of atom feed for collection, '%s'" %collection) 
    160         feed = feedparser.parse('http://%s/exist/atom/content/%s' \ 
    161                                 %(self.hostAndPort, collection)) 
    162          
     162         
     163        proxy = {} 
     164        # NB, there is slightly unhelpful default behaviour here: if there is a 
     165        # proxy specified in the environment variables, this will be used regardless 
     166        # of whether it is specified here - to temporarily remove this if no proxy 
     167        # is specified 
     168        oldProxy = None 
     169        if proxyServer: 
     170            proxy = urllib2.ProxyHandler( {"http": proxyServer} ) 
     171        elif os.environ.has_key(utils.PROXY_KEY): 
     172            oldProxy = os.environ[utils.PROXY_KEY] 
     173            del os.environ[utils.PROXY_KEY] 
     174             
     175        feed = feedparser.parse('http://%s/exist/atom/content/%s'  
     176                                %(self.hostAndPort, collection),  
     177                                handlers = [proxy]) 
     178 
     179        if oldProxy: 
     180            os.environ[utils.PROXY_KEY] = oldProxy 
     181             
    163182        # check we've got something valid back 
    164183        if feed.bozo: 
Note: See TracChangeset for help on using the changeset viewer.