Coverage for python/lsst/resources/packageresource.py: 100%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

11 statements  

1# This file is part of lsst-resources. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# Use of this source code is governed by a 3-clause BSD-style 

10# license that can be found in the LICENSE file. 

11 

12import logging 

13 

14import pkg_resources 

15 

16__all__ = ("PackageResourcePath",) 

17 

18from ._resourcePath import ResourcePath 

19 

20log = logging.getLogger(__name__) 

21 

22 

23class PackageResourcePath(ResourcePath): 

24 """URI referring to a Python package resource. 

25 

26 These URIs look like: ``resource://lsst.daf.butler/configs/file.yaml`` 

27 where the network location is the Python package and the path is the 

28 resource name. 

29 """ 

30 

31 def exists(self) -> bool: 

32 """Check that the python resource exists.""" 

33 return pkg_resources.resource_exists(self.netloc, self.relativeToPathRoot) 

34 

35 def read(self, size: int = -1) -> bytes: 

36 """Read the contents of the resource.""" 

37 with pkg_resources.resource_stream(self.netloc, self.relativeToPathRoot) as fh: 

38 return fh.read(size)