Code Coverage

Cobertura Coverage Report > osm_policy_module.core >

config.py

Trend

File Coverage summary

NameClassesLinesConditionals
config.py
100%
1/1
72%
28/39
100%
0/0

Coverage Breakdown by Class

NameLinesConditionals
config.py
72%
28/39
N/A

Source

osm_policy_module/core/config.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12
13 #         http://www.apache.org/licenses/LICENSE-2.0
14
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 ##
24 1 """Global configuration managed by environment variables."""
25
26 1 import logging
27 1 import os
28
29 1 import pkg_resources
30 1 import yaml
31
32 1 logger = logging.getLogger(__name__)
33
34
35 1 class Config:
36 1     def __init__(self, config_file: str = ""):
37 1         self.conf = {}
38 1         self._read_config_file(config_file)
39 1         self._read_env()
40
41 1     def _read_config_file(self, config_file):
42 1         if not config_file:
43 1             path = "pol.yaml"
44 1             config_file = pkg_resources.resource_filename(__name__, path)
45 1         with open(config_file) as f:
46 1             self.conf = yaml.safe_load(f)
47
48 1     def get(self, section, field=None):
49 1         if not field:
50 1             return self.conf[section]
51 1         return self.conf[section][field]
52
53 1     def set(self, section, field, value):
54 1         if section not in self.conf:
55 0             self.conf[section] = {}
56 1         self.conf[section][field] = value
57
58 1     def _read_env(self):
59 1         for env in os.environ:
60 1             if not env.startswith("OSMPOL_"):
61 1                 continue
62 0             elements = env.lower().split("_")
63 0             if len(elements) < 3:
64 0                 logger.warning(
65                     "Environment variable %s=%s does not comply with required format. Section and/or field missing.",
66                     env,
67                     os.getenv(env),
68                 )
69 0                 continue
70 0             section = elements[1]
71 0             field = "_".join(elements[2:])
72 0             value = os.getenv(env)
73 0             if section not in self.conf:
74 0                 self.conf[section] = {}
75 0             self.conf[section][field] = value