-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
80 lines (65 loc) · 2.72 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
import os
import configparser
from collections import UserDict
from configparser import NoSectionError, NoOptionError
class ESMConfig(UserDict):
"""A class to find and contain ESM creds and config."""
def __init__(self, path=None, esm='esm', *args):
"""
Initializes the ESMConfig.
Credentials for the ESM(s) live in an .ini file that should be
located in a user directory with properly restrictive permissions.
This class will check for environemental variables for a home dir
and check there and then check in the local dir.
Args:
path (str): path to a mfesaw ini file.
esm (str): section name of the esm settings in the ini file.
default is 'default'.
"""
self.data = {}
config = configparser.ConfigParser()
if path:
config.read(path)
else:
home_vars = ['HOME', 'APPDATA', 'XDG_HOME']
ini_paths = _find_envs(home_vars)
ini_paths.append('.')
ini_names = ['.mfe_saw.ini', '.mfesaw.ini', '.mfesaw2.ini']
ini_files = _find_files(ini_names, ini_paths)
config.read(ini_files)
if not config:
raise FileNotFoundError('.mfesaw2.ini file not found.')
if not config.has_section(esm):
raise NoSectionError('Section not found in INI file: {}'
.format(esm))
options = ['esmhost', 'esmuser', 'esmpass']
for opt in options:
if not config.has_option(esm, opt):
raise NoOptionError('Missing opt in ini file.'.format(opt))
self.__dict__[opt] = config[esm][opt]
self[opt] = config[esm][opt]
def _find_envs(env_vars):
"""Searches env for given variables.
Args:
vars (str, list): variable(s) to be checked.
Returns: list of values for any variables found.
"""
if isinstance(env_vars, str):
env_vars = [env_vars]
return [val for var, val in os.environ.items() if var in env_vars]
def _find_files(files, paths):
"""Detect ini files in a list of given paths.
Args:
files (list): list of filenames to search.
paths (list): list of paths to search for the list of files.
Returns:
List of file paths verified to exist from given files/paths.
"""
found_files = []
for path in paths:
for file in files:
file_path = os.path.join(path, file)
if os.path.isfile(file_path):
found_files.append(file_path)
return found_files