Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
649685ad
Commit
649685ad
authored
Jul 14, 2000
by
Eric S. Raymond
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ConfigParser enhancements to edit existing configs, part 2
parent
23adc9f3
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
5 deletions
+41
-5
Lib/ConfigParser.py
Lib/ConfigParser.py
+41
-5
No files found.
Lib/ConfigParser.py
View file @
649685ad
...
...
@@ -36,6 +36,9 @@ ConfigParser -- responsible for for parsing a list of
has_section(section)
return whether the given section exists
has_option(section, option)
return whether the given option exists in the given section
options(section)
return list of configuration options for the named section
...
...
@@ -68,6 +71,18 @@ ConfigParser -- responsible for for parsing a list of
getboolean(section, options)
like get(), but convert value to a boolean (currently defined as 0 or
1, only)
remove_section(section)
remove the given file section and all its options
remove_option(section, option)
remove the given option from the given section
set(section, option, value)
set the given option
write(fp)
write the configuration state in .ini format
"""
import
sys
...
...
@@ -310,18 +325,40 @@ class ConfigParser:
"""Write an .ini-format representation of the configuration state."""
if
self
.
__defaults
:
fp
.
write
(
"[DEFAULT]
\
n
"
)
for
key
in
self
.
__defaults
.
key
s
():
fp
.
write
(
key
+
" = "
+
self
.
__defaults
[
key
]
+
"
\
n
"
)
for
(
key
,
value
)
in
self
.
__defaults
.
item
s
():
fp
.
write
(
"%s = %s
\
n
"
%
(
key
,
value
)
)
fp
.
write
(
"
\
n
"
)
for
section
in
self
.
sections
():
fp
.
write
(
"["
+
section
+
"]
\
n
"
)
sectdict
=
self
.
__sections
[
section
]
for
key
in
sectdict
.
key
s
():
for
(
key
,
value
)
in
sectdict
.
item
s
():
if
key
==
"__name__"
:
continue
fp
.
write
(
key
+
" = "
+
str
(
sectdict
[
key
])
+
"
\
n
"
)
fp
.
write
(
"%s = %s
\
n
"
%
(
key
,
value
)
)
fp
.
write
(
"
\
n
"
)
def
remove_option
(
section
,
option
):
"""Remove an option."""
if
not
section
or
section
==
"DEFAULT"
:
sectdict
=
self
.
__defaults
else
:
try
:
sectdict
=
self
.
__sections
[
section
]
except
KeyError
:
raise
NoSectionError
(
section
)
existed
=
sectdict
.
has_key
(
key
)
if
existed
:
del
sectdict
[
key
]
return
existed
def
remove_section
(
section
):
"""Remove a file section."""
if
self
.
__sections
.
has_key
(
section
):
del
self
.
__sections
[
section
]
return
1
else
:
return
0
#
# Regular expressions for parsing section headers and options. Note a
# slight semantic change from the previous version, because of the use
...
...
@@ -393,7 +430,6 @@ class ConfigParser:
mo = self.OPTCRE.match(line)
if mo:
optname, vi, optval = mo.group('option', 'vi', 'value')
optname = string.lower(optname)
if vi in ('=', ':') and ';' in optval:
# ';' is a comment delimiter only if it follows
# a spacing character
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment