-
Notifications
You must be signed in to change notification settings - Fork 0
/
stanza.py
65 lines (56 loc) · 2.25 KB
/
stanza.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
from configparser import _default_dict
from configparser import _UNSET
from configparser import ConfigParser
from configparser import DEFAULTSECT
import re
class AIXStanzaParser(ConfigParser):
"""
Parse AIX stanza files. Modifies ConfigParser to handle unique
characteristics of AIX stanza files.
"""
def __init__(self, *args, **kwargs):
try:
# Grab parameters unique to AIXStanzaParser
self._sectionBegin = kwargs.pop("sectionBegin", "")
self._sectionEnd = kwargs.pop("sectionBegin", ":")
self._keyValuePrefix = kwargs.pop("keyValuePrefix", "\t")
# If delimiters is specified, use it, otherwise fill in with the
# default appropriate for AIX stanza files.
if "delimiters" not in kwargs:
kwargs["delimiters"] = "="
# AIX stanza options allow no values
if "allow_no_value" not in kwargs:
kwargs["allow_no_value"] = True
except KeyError:
pass
super().__init__(*args, **kwargs)
self.SECTCRE = re.compile(
r"""
"""
+ self._sectionBegin
+ r"(?P<header>[^"
+ self._sectionEnd
+ r"]*)"
+ self._sectionEnd
+ r"""
""",
re.VERBOSE,
)
#
# Override _write_section() method. This presumes that
# RawConfigParser.write() calls self._write_section(). If that changes
# we'll want to override() the write() method instead.d We want to
# - preserve stanza style
# - add white space (_keyValuePrefix) before key-value pairs
#
def _write_section(self, fp, section_name, section_items, delimiter):
"""Write a single section to the specified `fp'."""
fp.write(f"{self._sectionBegin}{section_name}{self._sectionEnd}\n")
for key, value in section_items:
value = self._interpolation.before_write(self, section_name, key, value)
if value is not None or not self._allow_no_value:
value = delimiter + str(value).replace("\n", "\n\t")
else:
value = ""
fp.write(f"{self._keyValuePrefix}{key}{value}\n")
fp.write("\n")