Coverage for pygeoapi/config.py: 57%

38 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# Francesco Bartoli <xbartolone@gmail.com> 

5# 

6# Copyright (c) 2025 Tom Kralidis 

7# Copyright (c) 2025 Francesco Bartoli 

8# 

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

10# obtaining a copy of this software and associated documentation 

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

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

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

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

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

16# conditions: 

17# 

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

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

20# 

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

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

23# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 

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

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

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

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

28# OTHER DEALINGS IN THE SOFTWARE. 

29# 

30# ================================================================= 

31 

32import click 

33import json 

34from jsonschema import validate as jsonschema_validate 

35import logging 

36import os 

37import yaml 

38 

39from pygeoapi.util import SCHEMASDIR, to_json, yaml_load 

40 

41LOGGER = logging.getLogger(__name__) 

42 

43 

44def get_config(raw: bool = False) -> dict: 

45 """ 

46 Get pygeoapi configurations 

47 

48 :param raw: `bool` over interpolation during config loading 

49 

50 :returns: `dict` of pygeoapi configuration 

51 """ 

52 

53 if not os.environ.get('PYGEOAPI_CONFIG'): 53 ↛ 54line 53 didn't jump to line 54 because the condition on line 53 was never true

54 raise RuntimeError('PYGEOAPI_CONFIG environment variable not set') 

55 

56 with open(os.environ.get('PYGEOAPI_CONFIG'), encoding='utf8') as fh: 

57 if raw: 57 ↛ 58line 57 didn't jump to line 58 because the condition on line 57 was never true

58 CONFIG = yaml.safe_load(fh) 

59 else: 

60 CONFIG = yaml_load(fh) 

61 

62 return CONFIG 

63 

64 

65def load_schema() -> dict: 

66 """ Reads the JSON schema YAML file. """ 

67 

68 schema_file = SCHEMASDIR / 'config' / 'pygeoapi-config-0.x.yml' 

69 

70 with schema_file.open() as fh2: 

71 return yaml_load(fh2) 

72 

73 

74def validate_config(instance_dict: dict) -> bool: 

75 """ 

76 Validate pygeoapi configuration against pygeoapi schema 

77 

78 :param instance_dict: dict of configuration 

79 

80 :returns: `bool` of validation 

81 """ 

82 

83 jsonschema_validate(json.loads(to_json(instance_dict)), load_schema()) 

84 

85 return True 

86 

87 

88@click.group() 

89def config(): 

90 """Configuration management""" 

91 pass 

92 

93 

94@click.command() 

95@click.pass_context 

96@click.option('--config', '-c', 'config_file', help='configuration file') 

97def validate(ctx, config_file): 

98 """Validate configuration""" 

99 

100 if config_file is None: 

101 raise click.ClickException('--config/-c required') 

102 

103 with open(config_file) as ff: 

104 click.echo(f'Validating {config_file}') 

105 instance = yaml_load(ff) 

106 validate_config(instance) 

107 click.echo('Valid configuration') 

108 

109 

110config.add_command(validate)