Python 7.0pl5 Benutzerhandbuch

Seite von 124
Program Setup
20
import netsvc
import os
netsvc.loadConfig("database.cfg")
netsvc.mergeConfig("PWD",os.getcwd())
print netsvc.lookupConfig("PWD")
A single configuration item can be removed from the database using the "
removeConfig()
" func-
tion. The configuration database can be completely emptied using the function "
removeAllCon-
fig()
". The contents of the configuration database can be saved to a file using the
"
saveConfig()
" function.
netsvc.removeConfig("PWD")
netsvc.saveConfig("database.cfg")
Configuration File
The only real restrictions in regard to naming is that the colon character should not be used anywhere
in a name, a name should not being with an exclamation mark and whitespace should not be used at
the start or end of a name. The colon character cannot be used as it used in a configuration file to sep-
arate the name from the value. A leading exclamation mark should not be used as it is used to denote
a comment.
If these characters are used in a name and the configuration database is saved to a file, the results when
that configuration file is read back in will not be the same. The only other special character when used
in a configuration file is a back slash, which when used at the end of the line, indicates the following
line is part of the same value. Note that the leading whitespace and the whitespace either side of the
colon will be ignored when the configuration file is read in.
! comment
single-line-value : value
multi-line-value : value\
value
When reading in a configuration file using "
loadConfig()
", an exception is raised only if the file
doesn’t exist or the file couldn’t be opened. If there are no errors in the file, the value
None
is returned.
If there are errors in the file, a string is returned which contains details of the errors and what action
has been taken. By default, the details of the errors are also output via the logging system on the default
log channel.
If details of any errors should be output on a specific log channel, an optional second argument can be
supplied to the "
loadConfig()
" function giving the name of the log channel. If the value
None
is
supplied in place of the name of the log channel, the details of the errors will not be output via the log-
ger at all. The value
None
could be used if you wish to amend the details of the errors before they are
logged.