1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
24 """Global configuration managed by environment variables."""
32 logger
= logging
.getLogger(__name__
)
36 def __init__(self
, config_file
: str = ''):
38 self
._read
_config
_file
(config_file
)
41 def _read_config_file(self
, config_file
):
44 config_file
= pkg_resources
.resource_filename(__name__
, path
)
45 with
open(config_file
) as f
:
46 self
.conf
= yaml
.load(f
)
48 def get(self
, section
, field
=None):
50 return self
.conf
[section
]
51 return self
.conf
[section
][field
]
53 def set(self
, section
, field
, value
):
54 if section
not in self
.conf
:
55 self
.conf
[section
] = {}
56 self
.conf
[section
][field
] = value
59 for env
in os
.environ
:
60 if not env
.startswith("OSMMON_"):
62 elements
= env
.lower().split("_")
65 "Environment variable %s=%s does not comply with required format. Section and/or field missing.",
69 field
= '_'.join(elements
[2:])
70 value
= os
.getenv(env
)
71 if section
not in self
.conf
:
72 self
.conf
[section
] = {}
73 self
.conf
[section
][field
] = value