在RHEL5中填充.ini中的Python词典

时间:2015-06-24 19:12:09

标签: python dictionary ini rhel6

我正在为dumpcap.exe创建一个脚本,以便为其添加更多功能。我将其编写为多平台,大部分MSWindows版本都已完成。

目前我正在将我所拥有的内容移至REHL5,以便开始调试并更改所需内容,以便使转换更加简单。 MSWindows版本已使用Python 2.7编写,而RHEL5运行Python 2.5

该程序大约1200行,所以我将删除不必要的东西。 我会尽量提供尽可能丰富的信息,因为我不知道哪里出错了

我目前的问题是我的Variables{}字典没有填充。

Variables = {}                     

choice = ""                        
MAX_CHOICE = 0                     

DUMPCAP = None                     

section = None                     

dc_envfile = "cfg.ini"             
EVENTBAT = "eventaction.bat"       
FINALBAT = "finalaction.bat"       
DISPLAY_IFACES = None              
DC_PID = None                      

dc_priority = None                 
dc_mode =  None                    

#Dumpcap settings:                 
dc_interface = None                
dc_capfile = None                  
dc_capfilter = None                
dc_snaplen = None                  
dc_promisc = None                  
dc_bufsize = None                  
dc_pcapng =  None                  

def readFile(filename):                                   
    global section                                        
    myfile = open(filename, 'r+')                         
    #with open(filename) as myfile:                       
    for line in myfile:                                   
        raw_data, _, comments = line.partition("#")       
        raw_data = raw_data.strip()                       

        if raw_data[0:1] == '[':                          
            section = line[0:-2]                          
            Variables.setdefault(section, {})             
        else:                                             
            var, _, val = raw_data.partition("=")         
            var = var.strip(' ')                          
            val = val.strip(' ')                          
            Variables.setdefault(section, {})[var] = [val]
    myfile.close()           

readFile(dc_envfile)

readFile()中注释掉的行是Python2.7代码,在RHEL5中不起作用。 Virtualenv不是一个选项。

正在读取的.ini文件如下所示

[DC]
dc_capfile = test
dc_interface = 2
dc_ringtime = 
dc_priority = NORMAL
dc_maxtime = 
dc_snaplen = 
dc_bufsize = 20
dc_promisc = Y
dc_capfilter = icmp
dc_maxfiles = 
dc_maxsize = 
dc_mode = Dumpcap+Event
dc_pcapng = N
dc_ringsize = 
dc_maxpackets = 
dc_ringfiles =       

运行此功能后,我会收到error

File "PythonDumpCap25.py", line 992, in setVariables
    if ''.join(map(str, Variables['DC']["dc_priority"])) is '':
KeyError: 'DC'

setVariables()函数包含的内容不止于此,但错误发生在它的前几行

def setVariables():
    global dc_mode, dc_priority
            ####### Dumpcap settings: #######
    global dc_interface, dc_capfile, dc_capfilter, dc_snaplen
    global dc_promisc, dc_bufsize, dc_pcapng
            ####### Stop conditions: #######
    global dc_maxpackets, dc_maxtime, dc_maxsize, dc_maxfiles
            ####### Ringbuffer settings: #######
    global dc_ringtime, dc_ringsize, dc_ringfiles
            ####### Capture event settings: #######
    global dc_ev_interface, dc_ev_capfilter, dc_ev_count
    global dc_ev_kill, dc_ev_delay
            ####### Mailsend settings: #######
    global ms, ms_smtp_server, ms_smtp_port, ms_sendto
    global ms_cc, ms_bcc, ms_rrr, ms_from_name, ms_from
    global ms_replyto, ms_importance, ms_sensitivity
    global ms_security, ms_user, ms_attach, ms_maxattach
    global ms_subject, ms_pm

    global SMTP_USER_PASS

    if ''.join(map(str, Variables['DC']["dc_priority"])) is '':
        dc_priority = "NORMAL"
    else:
        dc_priority = ''.join(map(str, Variables['DC']["dc_priority"]))

当我运行我的printVariables()函数时,它会打印一个空字典。

我的Variables{} dict为什么不填充?

0 个答案:

没有答案