1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
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 #######################################################################################
17 """Global configuration managed by environment variables."""
25 logger
= logging
.getLogger(__name__
)
29 def __init__(self
, config_file
: str = ""):
31 self
._read
_config
_file
(config_file
)
34 def _read_config_file(self
, config_file
):
37 config_file
= pkg_resources
.resource_filename(__name__
, path
)
38 with
open(config_file
) as f
:
39 self
.conf
= yaml
.safe_load(f
)
41 def get(self
, section
, field
=None):
43 return self
.conf
[section
]
44 return self
.conf
[section
].get(field
)
46 def set(self
, section
, field
, value
):
47 if section
not in self
.conf
:
48 self
.conf
[section
] = {}
49 self
.conf
[section
][field
] = value
52 for env
in os
.environ
:
53 if not env
.startswith("OSMMON_"):
55 elements
= env
.lower().split("_")
58 "Environment variable %s=%s does not comply with required format. Section and/or field missing.",
64 field
= "_".join(elements
[2:])
65 value
= os
.getenv(env
)
66 if section
not in self
.conf
:
67 self
.conf
[section
] = {}
68 self
.conf
[section
][field
] = value