1 # -*- coding: utf-8 -*-
2 # Copyright 2020 ArctosLabs Scandinavia AB
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 """Global configuration managed by environment variables."""
24 logger
= logging
.getLogger(__name__
)
28 def __init__(self
, config_file
: str = ''):
30 self
._read
_config
_file
(config_file
)
33 def _read_config_file(self
, config_file
):
36 config_file
= pkg_resources
.resource_filename(__name__
, path
)
37 with
open(config_file
) as f
:
38 self
.conf
= yaml
.load(f
)
41 for env
in os
.environ
:
42 if not env
.startswith("OSMPLA_"):
44 elements
= env
.lower().split("_")
47 "Environment variable %s=%s does not comply with required format. Section and/or field missing.",
51 field
= '_'.join(elements
[2:])
52 value
= os
.getenv(env
)
53 if section
not in self
.conf
:
54 self
.conf
[section
] = {}
55 self
.conf
[section
][field
] = value
57 def get(self
, section
, field
=None):
59 return self
.conf
[section
]
60 return self
.conf
[section
][field
]
62 def set(self
, section
, field
, value
):
63 if section
not in self
.conf
:
64 self
.conf
[section
] = {}
65 self
.conf
[section
][field
] = value