Coverage for pygeoapi/provider/__init__.py: 95%

22 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 08:15 +0000

1# ================================================================= 

2# 

3# Authors: Tom Kralidis <tomkralidis@gmail.com> 

4# 

5# Copyright (c) 2026 Tom Kralidis 

6# 

7# Permission is hereby granted, free of charge, to any person 

8# obtaining a copy of this software and associated documentation 

9# files (the "Software"), to deal in the Software without 

10# restriction, including without limitation the rights to use, 

11# copy, modify, merge, publish, distribute, sublicense, and/or sell 

12# copies of the Software, and to permit persons to whom the 

13# Software is furnished to do so, subject to the following 

14# conditions: 

15# 

16# The above copyright notice and this permission notice shall be 

17# included in all copies or substantial portions of the Software. 

18# 

19# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 

20# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 

21# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 

22# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 

23# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 

24# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 

25# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 

26# OTHER DEALINGS IN THE SOFTWARE. 

27# 

28# ================================================================= 

29 

30import logging 

31 

32from pygeoapi.provider.base import ProviderTypeError 

33 

34LOGGER = logging.getLogger(__name__) 

35 

36 

37def filter_providers_by_type(providers: list, type: str) -> dict: 

38 """ 

39 helper function to filter a list of providers by type 

40 

41 :param providers: ``list`` 

42 :param type: str 

43 

44 :returns: filtered ``dict`` provider 

45 """ 

46 

47 providers_ = {provider['type']: provider for provider in providers} 

48 return providers_.get(type) 

49 

50 

51def get_provider_by_type(providers: list, provider_type: str) -> dict: 

52 """ 

53 helper function to load a provider by a provider type 

54 

55 :param providers: ``list`` of providers 

56 :param provider_type: type of provider (e.g. feature) 

57 

58 :returns: provider based on type 

59 """ 

60 

61 LOGGER.debug(f'Searching for provider type {provider_type}') 

62 try: 

63 p = (next(d for i, d in enumerate(providers) 

64 if d['type'] == provider_type)) 

65 except (RuntimeError, StopIteration): 

66 raise ProviderTypeError('Invalid provider type requested') 

67 

68 return p 

69 

70 

71def get_provider_default(providers: list) -> dict: 

72 """ 

73 helper function to get a resource's default provider 

74 

75 :param providers: ``list`` of providers 

76 

77 :returns: filtered ``dict`` 

78 """ 

79 

80 try: 

81 default = (next(d for i, d in enumerate(providers) if 'default' in d 

82 and d['default'])) 

83 LOGGER.debug('found default provider type') 

84 except StopIteration: 

85 LOGGER.debug('no default provider type. Returning first provider') 

86 default = providers[0] 

87 

88 LOGGER.debug(f"Default provider: {default['type']}") 

89 return default