Debian Codegraph @

envisage/resource/http_resource_protocol.py

raw
1 # (C) Copyright 2007-2025 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10 """ A resource protocol for HTTP documents. """
11
12 # Enthought library imports.
13 from traits.api import HasTraits, provides
14
15 # Local imports.
16 from .i_resource_protocol import IResourceProtocol
17 from .no_such_resource_error import NoSuchResourceError
18
19
20 @provides(IResourceProtocol)
21 class HTTPResourceProtocol(HasTraits):
22 """A resource protocol for HTTP documents."""
23
24 ###########################################################################
25 # 'IResourceProtocol' interface.
26 ###########################################################################
27
28 def file(self, address):
29 """Return a readable file-like object for the specified address."""
30
31 # Do the imports here 'cos I'm not sure how much this will actually
32 # be used.
33 from urllib.error import HTTPError
34 from urllib.request import urlopen
35
36 try:
37 f = urlopen("http://" + address)
38
39 except HTTPError:
40 raise NoSuchResourceError("http:://" + address)
41
42 return f