source: DPPP/kml/csml2kml/python/csml2kml/csml2kml/utils.py @ 3684

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DPPP/kml/csml2kml/python/csml2kml/csml2kml/utils.py@3684
Revision 3684, 767 bytes checked in by mkochan, 11 years ago (diff)

Changed wget to return urllib2.HTTPError on retrieval error; changed setup.py to include config files in the csml2kml distribution.

RevLine 
[3612]1'''
2Utility/convenience functions.
3'''
4
[3464]5import urllib2
[3304]6
[3684]7class UrlRetrievalError(Exception):
8    def __init__(self, value):
9        self.value = value
10    def __repr__(self):
11        return repr(self.value)
12
[3304]13def wget(url):
14    '''
[3464]15    Read data from an URL.
16    @param url: The URL.
[3612]17    @type url: C{str}
[3684]18    @return: The returned data as a string, or an C{urllib2.HTTPError} on retrieval error.
[3612]19    @rtype: C{str}
[3304]20    '''
21    content = None
22    try:
[3684]23        f = urllib2.urlopen(url)         # open a file-like object with the data
24        content = f.read()               # read the text in
[3304]25    finally:
26        try:
[3464]27            f.close()
[3304]28        except NameError:
[3684]29            pass                         # in case socket has not even been opened
[3304]30    return content
Note: See TracBrowser for help on using the repository browser.