/usr/lib/python3/dist-packages/dcos/config.py is in python3-dcos 0.2.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | import collections
import toml
from dcos import util
from dcos.errors import DCOSException
def load_from_path(path, mutable=False):
"""Loads a TOML file from the path
:param path: Path to the TOML file
:type path: str
:param mutable: True if the returned Toml object should be mutable
:type mutable: boolean
:returns: Map for the configuration file
:rtype: Toml | MutableToml
"""
util.ensure_file_exists(path)
with util.open_file(path, 'r') as config_file:
try:
toml_obj = toml.loads(config_file.read())
except Exception as e:
raise DCOSException(
'Error parsing config file at [{}]: {}'.format(path, e))
return (MutableToml if mutable else Toml)(toml_obj)
def save(toml_config):
"""
:param toml_config: TOML configuration object
:type toml_config: MutableToml or Toml
"""
serial = toml.dumps(toml_config._dictionary)
path = util.get_config_path()
with util.open_file(path, 'w') as config_file:
config_file.write(serial)
def _get_path(config, path):
"""
:param config: Dict with the configuration values
:type config: dict
:param path: Path to the value. E.g. 'path.to.value'
:type path: str
:returns: Value stored at the given path
:rtype: double, int, str, list or dict
"""
for section in path.split('.'):
config = config[section]
return config
def _iterator(parent, dictionary):
"""
:param parent: Path to the value parameter
:type parent: str
:param dictionary: Value of the key
:type dictionary: collection.Mapping
:returns: An iterator of tuples for each property and value
:rtype: iterator of (str, any) where any can be str, int, double, list
"""
for key, value in dictionary.items():
new_key = key
if parent is not None:
new_key = "{}.{}".format(parent, key)
if not isinstance(value, collections.Mapping):
yield (new_key, value)
else:
for x in _iterator(new_key, value):
yield x
class Toml(collections.Mapping):
"""Class for getting value from TOML.
:param dictionary: configuration dictionary
:type dictionary: dict
"""
def __init__(self, dictionary):
self._dictionary = dictionary
def __getitem__(self, path):
"""
:param path: Path to the value. E.g. 'path.to.value'
:type path: str
:returns: Value stored at the given path
:rtype: double, int, str, list or dict
"""
config = _get_path(self._dictionary, path)
if isinstance(config, collections.Mapping):
return Toml(config)
else:
return config
def __iter__(self):
"""
:returns: Dictionary iterator
:rtype: iterator
"""
return iter(self._dictionary)
def property_items(self):
"""Iterator for full-path keys and values
:returns: Iterator for pull-path keys and values
:rtype: iterator of tuples
"""
return _iterator(None, self._dictionary)
def __len__(self):
"""
:returns: The length of the dictionary
:rtype: int
"""
return len(self._dictionary)
class MutableToml(collections.MutableMapping):
"""Class for managing CLI configuration through TOML.
:param dictionary: configuration dictionary
:type dictionary: dict
"""
def __init__(self, dictionary):
self._dictionary = dictionary
def __getitem__(self, path):
"""
:param path: Path to the value. E.g. 'path.to.value'
:type path: str
:returns: Value stored at the given path
:rtype: double, int, str, list or dict
"""
config = _get_path(self._dictionary, path)
if isinstance(config, collections.MutableMapping):
return MutableToml(config)
else:
return config
def __iter__(self):
"""
:returns: Dictionary iterator
:rtype: iterator
"""
return iter(self._dictionary)
def property_items(self):
"""Iterator for full-path keys and values
:returns: Iterator for pull-path keys and values
:rtype: iterator of tuples
"""
return _iterator(None, self._dictionary)
def __len__(self):
"""
:returns: The length of the dictionary
:rtype: int
"""
return len(self._dictionary)
def __setitem__(self, path, value):
"""
:param path: Path to set
:type path: str
:param value: Value to store
:type value: double, int, str, list or dict
"""
config = self._dictionary
sections = path.split('.')
for section in sections[:-1]:
config = config.setdefault(section, {})
config[sections[-1]] = value
def __delitem__(self, path):
"""
:param path: Path to delete
:type path: str
"""
config = self._dictionary
sections = path.split('.')
for section in sections[:-1]:
config = config[section]
del config[sections[-1]]
|