| 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 |
""" The default resource manager. """ |
| 11 |
|
| 12 |
|
| 13 |
# Enthought library imports. |
| 14 |
from traits.api import Dict, HasTraits, provides, Str |
| 15 |
|
| 16 |
# Local imports. |
| 17 |
from .i_resource_manager import IResourceManager |
| 18 |
from .i_resource_protocol import IResourceProtocol |
| 19 |
|
| 20 |
|
| 21 |
@provides(IResourceManager) |
| 22 |
class ResourceManager(HasTraits): |
| 23 |
"""The default resource manager.""" |
| 24 |
|
| 25 |
#### 'IResourceManager' interface ######################################### |
| 26 |
|
| 27 |
# The protocols used by the manager to resolve resource URLs. |
| 28 |
resource_protocols = Dict(Str, IResourceProtocol) |
| 29 |
|
| 30 |
########################################################################### |
| 31 |
# 'IResourceManager' interface. |
| 32 |
########################################################################### |
| 33 |
|
| 34 |
#### Trait initializers ################################################### |
| 35 |
|
| 36 |
def _resource_protocols_default(self): |
| 37 |
"""Trait initializer.""" |
| 38 |
|
| 39 |
# We do the import(s) here in case somebody wants a resource manager |
| 40 |
# that doesn't use the default protocol(s). |
| 41 |
from .file_resource_protocol import FileResourceProtocol |
| 42 |
from .http_resource_protocol import HTTPResourceProtocol |
| 43 |
from .package_resource_protocol import PackageResourceProtocol |
| 44 |
|
| 45 |
resource_protocols = { |
| 46 |
"file": FileResourceProtocol(), |
| 47 |
"http": HTTPResourceProtocol(), |
| 48 |
"pkgfile": PackageResourceProtocol(), |
| 49 |
} |
| 50 |
|
| 51 |
return resource_protocols |
| 52 |
|
| 53 |
#### Methods ############################################################## |
| 54 |
|
| 55 |
def file(self, url): |
| 56 |
"""Return a readable file-like object for the specified url.""" |
| 57 |
|
| 58 |
protocol_name, address = url.split("://") |
| 59 |
|
| 60 |
protocol = self.resource_protocols.get(protocol_name) |
| 61 |
if protocol is None: |
| 62 |
raise ValueError("unknown protocol in URL %s" % url) |
| 63 |
|
| 64 |
return protocol.file(address) |