Coverage for /opt/conda/envs/apienv/lib/python3.10/site-packages/daiquiri/core/resources/resource_provider.py: 67%

24 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-14 02:13 +0000

1import typing 

2import contextlib 

3import abc 

4 

5 

6class ResourceNotAvailable(RuntimeError): 

7 pass 

8 

9 

10class ResourceProvider(abc.ABC): 

11 @abc.abstractmethod 

12 def get_resource_path(self, resourcetype: str, resourcename: str) -> str: 

13 """Get the path of a resource from its name from this resource provider. 

14 

15 Arguments: 

16 resourcetype: subdirectory 

17 resourcename: filename 

18 

19 Raises: 

20 ResourceNotAvailable: resource does not exist 

21 """ 

22 raise NotImplementedError 

23 

24 @abc.abstractmethod 

25 def list_resource_names( 

26 self, resourcetype: str, pattern: str = "*" 

27 ) -> typing.List[str]: 

28 """List resource names by pattern from the resource provider.""" 

29 raise NotImplementedError 

30 

31 @abc.abstractmethod 

32 def resource(self, resourcetype: str, resourcename: str) -> object: 

33 """Returns a resource descriptor 

34 

35 Arguments: 

36 resourcetype: subdirectory 

37 resourcename: filename 

38 """ 

39 raise NotImplementedError 

40 

41 def abs_resource(self, resource_name: str): 

42 """Returns a resource descriptor from the absolute path. 

43 

44 Arguments: 

45 resourcename: filename 

46 """ 

47 elements = resource_name.split("/", 1) 

48 if len(elements) == 1: 

49 return self.resource("", elements[0]) 

50 return self.resource(elements[0], elements[1]) 

51 

52 @contextlib.contextmanager 

53 @abc.abstractmethod 

54 def open_resource(self, resource: object, mode="t"): 

55 """Context manager with the resource as a stream 

56 

57 Argument: 

58 resource: Reference to the resource 

59 mode: 't' or 'b' for text or binary 

60 """ 

61 raise NotImplementedError