Debian Codegraph @

envisage/resource/file_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 a local file system. """
11
12
13 # Standard library imports.
14 import errno
15
16 # Enthought library imports.
17 from traits.api import HasTraits, provides
18
19 # Local imports.
20 from .i_resource_protocol import IResourceProtocol
21 from .no_such_resource_error import NoSuchResourceError
22
23
24 @provides(IResourceProtocol)
25 class FileResourceProtocol(HasTraits):
26 """A resource protocol for a local file system."""
27
28 ###########################################################################
29 # 'IResourceProtocol' interface.
30 ###########################################################################
31
32 def file(self, address):
33 """Return a readable file-like object for the specified address."""
34
35 # Opened in binary mode to be consistent with package resources. This
36 # means, for example, that line-endings will not be converted.
37 try:
38 f = open(address, "rb")
39
40 except IOError as e:
41 if e.errno == errno.ENOENT:
42 raise NoSuchResourceError(address)
43
44 else:
45 raise
46
47 return f