Newer
Older
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
# Copyright 2021 PrivateStorage.io, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Basic utilities related to the Tahoe configuration file.
"""
from __future__ import (
division,
absolute_import,
print_function,
unicode_literals,
)
def _merge_dictionaries(dictionaries):
"""
Collapse a sequence of dictionaries into one, with collisions resolved by
taking the value from later dictionaries in the sequence.
:param [dict] dictionaries: The dictionaries to collapse.
:return dict: The collapsed dictionary.
"""
result = {}
for d in dictionaries:
result.update(d)
return result
def _tahoe_config_quote(text):
"""
Quote **%** in a unicode string.
:param unicode text: The string on which to perform quoting.
:return unicode: The string with ``%%`` replacing ``%``.
"""
return text.replace("%", "%%")
def config_string_from_sections(divided_sections):
"""
Get the .ini-syntax unicode string representing the given configuration
values.
:param [dict] divided_sections: The configuration to use to generate the
string. Each ``dict`` maps a top-level section name to a ``dict`` of
key/value pairs. Dictionaries may have overlapping top-level
sections, in which case the section items are merged (for collisions,
last value wins).
"""
sections = _merge_dictionaries(divided_sections)
return "".join(list(
"[{name}]\n{items}\n".format(
name=name,
items="\n".join(
"{key} = {value}".format(key=key, value=_tahoe_config_quote(value))
for (key, value)
in contents.items()
)
)
for (name, contents) in sections.items()
))