Coverage for python/lsst/daf/butler/core/_butlerUri.py: 32%

26 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-14 22:50 +0000

1# Temporarily alias ResourcePath as ButlerURI. 

2__all__ = ["ButlerURI"] 

3 

4from typing import Any, Type 

5 

6from deprecated.sphinx import deprecated 

7from lsst.resources import ResourcePath, ResourcePathExpression 

8from lsst.resources.file import FileResourcePath 

9 

10 

11def _add_base(cls: Type) -> None: 

12 """Update the class to use ButlerURI as the base. 

13 

14 Notes 

15 ----- 

16 Care must be taken with SchemelessResourcePath since that class inherits 

17 from FileResourcePath. 

18 """ 

19 if cls.__bases__ == (ResourcePath,): 

20 cls.__bases__ = (ButlerURI,) 

21 elif cls.__bases__ == (FileResourcePath,) and FileResourcePath.__bases__ == (ResourcePath,): 

22 FileResourcePath.__bases__ = (ButlerURI,) 

23 

24 

25def _reset_base(cls: Type) -> None: 

26 """Reset the base class to be ResourcePath.""" 

27 if cls.__bases__ == (ButlerURI,): 

28 cls.__bases__ = (ResourcePath,) 

29 elif issubclass(cls, FileResourcePath) and FileResourcePath.__bases__ == (ButlerURI,): 

30 FileResourcePath.__bases__ = (ResourcePath,) 

31 

32 

33@deprecated( 

34 reason="Please use lsst.resources.ResourcePath instead. Will be removed after v24.", 

35 version="v24.0", 

36 category=FutureWarning, 

37) 

38class ButlerURI(ResourcePath): 

39 """Class for handling URIs to file resources. 

40 

41 All users should instead use `lsst.resources.ResourcePath`. 

42 """ 

43 

44 def __new__(cls, uri: ResourcePathExpression, **kwargs: Any) -> ResourcePath: 

45 if cls is not ButlerURI: 

46 # This is a subclass trying to create an updated version of 

47 # itself without wanting to change the class. The ButlerURI 

48 # __new__ forces ResourcePath() to be used every time and so 

49 # the knowledge of the subclass is lost. This can result in a 

50 # Schemeless becoming a File URI. The simplest approach is to 

51 # remove ButlerURI from the class hierarchy and add it back again. 

52 # This seems inefficient but without it the subclass will 

53 # always try to call ButlerURI.__new__. 

54 _reset_base(cls) 

55 new = cls(uri, **kwargs) 

56 _add_base(cls) 

57 return new 

58 

59 new_uri = ResourcePath(uri, **kwargs) 

60 _add_base(type(new_uri)) 

61 

62 return new_uri