使用ConfigParser将字符串作为配置值读取

时间:2014-10-07 18:23:11

标签: python parsing configparser

我试图编写一个实用程序,让我根据配置文件设置在unix shell中运行的命令。从本质上讲,我希望能够提供[Command List]和预期[Response List] ..这是我的一个配置文件的示例:

# Command File
# List of commands to be carried out for each host listed in `veri.serv.cfg`

[Command List]
; T server
nc -zw1 159.1.1.1 9988 | gawk '{print $7}'

; P Server
nc -zw1 sup.serv.com 8050 | gawk '{print $7}'

; G L Service
nc -zw1 hi.serv.com 80 | gawk '{print $7}'

; L and N Hosts
nc -zw1 l.serv.com 80 | gawk '{print $7}'
nc -zw1 ln.serv.com 443 | gawk '{print $7}'
nc -zw1 llnn.serv.com 443 | gawk '{print $7}'

但是,当我尝试使用parser.items('Command List')解析它时,我得到:

  File "veri.py", line 22, in <module>
    print subConfigParser.read(os.path.join(relativeRunPath, 'veri.cfg'))
  File "/usr/lib/python2.7/ConfigParser.py", line 305, in read
    self._read(fp, filename)
  File "/usr/lib/python2.7/ConfigParser.py", line 546, in _read
    raise e
ConfigParser.ParsingError: File contains parsing errors

[line  6]: "nc -zw1 159.1.1.1 9988 | gawk '{print $7}'\n"
[line  9]: "nc -zw1 sup.serv.com 8050 | gawk '{print $7}'\n"
[line 12]: "nc -zw1 hi.serv.com 80 | gawk '{print $7}'\n"
[line 15]: "nc -zw1 l.serv.com 80 | gawk '{print $7}'\n"
[line 16]: "nc -zw1 ln.serv.com 443 | gawk '{print $7}'\n"
[line 17]: "nc -zw1 llnn.serv.com 443 | gawk '{print $7}'\n"

我是否可以使用SafeConfigParser将完整字符串作为值读取?

完整档案:

#! /usr/bin/python25
import os
import ConfigParser

# --- sudo consts --- #
# relative path, regardless of where we're executing from
RELATIVE_PATH = os.path.abspath(os.path.dirname(__file__))
BASE_SERVER_FILE = os.path.join(RELATIVE_PATH, 'cfg', 'veri.serv.cfg')
BASE_CONFIG_FILE = os.path.join(RELATIVE_PATH, 'cfg', 'veri.base.cfg')
# --- end consts --- #

baseConfigParser = ConfigParser.SafeConfigParser()
baseConfigParser.read(BASE_CONFIG_FILE)
runlist = baseConfigParser.get('Config List', 'runlist').strip().split("\n")
print runlist

for subDir in runlist:
    print subDir
    relativeRunPath = os.path.join(RELATIVE_PATH, subDir)
    subConfigParser = ConfigParser.SafeConfigParser()
    subConfigParser.read(os.path.join(relativeRunPath, 'veri.cfg'))
    print subConfigParser.items('Command List')            ###### <<< Error Here
    if os.path.isfile(os.path.join(relativeRunPath, 'veri.serv.cfg')):
        subServSpecified = true
        subServConfigParser = ConfigParser.SafeConfigParser()
        subServConfigParser.read(os.path.join(relativeRunPath, 'veri.serv.cfg'))
        print subServConfigParser.get('Config List', 'runlist')

1 个答案:

答案 0 :(得分:1)

您的配置文件没有使用ConfigParser的有效格式。它必须是一系列部分,后跟命名选项,例如

[T server]
commands = nc -zw1 159.1.1.1 9988 | gawk '{print $7}'

[P Server]
commands = nc -zw1 sup.serv.com 8050 | gawk '{print $7}'

[G L Service]
commands = nc -zw1 hi.serv.com 80 | gawk '{print $7}'

[L and N Hosts]
commands = nc -zw1 l.serv.com 80 | gawk '{print $7}'
    nc -zw1 ln.serv.com 443 | gawk '{print $7}'
    nc -zw1 llnn.serv.com 443 | gawk '{print $7}'

然后您可以获取值

>>> from ConfigParser import SafeConfigParser
>>> parser = SafeConfigParser()
>>> parser.read('myconfig.ini')
['myconfig.ini']
>>> for section in parser.sections():
...     print 'section', section
...     for option in parser.options(section):
...             print parser.get(section, option)
... 
section T server
nc -zw1 159.1.1.1 9988 | gawk '{print $7}'
section P Server
nc -zw1 sup.serv.com 8050 | gawk '{print $7}'                                                                                                                                  
section G L Service                                                                                                                                                            
nc -zw1 hi.serv.com 80 | gawk '{print $7}'                                                                                                                                     
section L and N Hosts                                                                                                                                                          
nc -zw1 l.serv.com 80 | gawk '{print $7}'                                                                                                                                      
nc -zw1 ln.serv.com 443 | gawk '{print $7}'                                                                                                                                    
nc -zw1 llnn.serv.com 443 | gawk '{print $7}'                                                                                                                                  
>>>