简体   繁体   中英

Passing config file as argument to config parse module

My script should be called like thiks:

# python start.py --config=start.cfg

So I've got three files:

start.py

import argparse                                                                 
import options                                                                  

parser = argparse.ArgumentParser(prog='mystart')                                
parser.add_argument("-f", "--file", dest="filename",                            
                    help="write report to FILE", metavar="FILE")                

args = parser.parse_args()                                                      
ConfigFile = args.filename                                                      

test = options.getTest()                                                        

print test

options.py

from ConfigReader import getConfigValue as getVal                               


def getTest():                                                                  
    Test = getVal('main', 'test')                                               
    return Test 

ConfigReader.py

import os                                                                       
import ConfigParser                                                             


def config():                                                                   
    cfg = ConfigParser.ConfigParser()                                           
    for f in ('default.cfg',):                                                  
        if os.path.exists(f):                                                   
            cfg.read(f)                                                         
            return cfg                                                          
    return None                                                                 


def getConfigValue(Section, Option):                                            
    cfg = config()                                                              
    if cfg.has_option(Section, Option):                                         
        return cfg.get(Section, Option)                                         
    else:                                                                       
        return None

As you can see my config file is hard coded in my module ConfigReader.py. How can I pass now my var ConfigFile into my module?

This is just a simple example. I have a hole bunch of files which should access this variable also.

Is a global variable an option? Is there another way?

start.py

...
options.ConfigFile = args.filename
... 

options.py

from ConfigReader import getConfigValue as getVal, ConfigFile                               

def getTest():                                                                  
    Test = getVal('main', 'test')                                               
    return Test 

ConfigReader.py

import os                                                                       
import ConfigParser                                                             

ConfigFile='default.cfg'  
def config():                                                                   
    cfg = ConfigParser.ConfigParser()                                           
    for f in set(['default.cfg',ConfigFile]):                                                  
        if os.path.exists(f):                                                   
            cfg.read(f)                                                         
            return cfg                                                          
    return None                                                                 


def getConfigValue(Section, Option):                                            
    cfg = config()                                                              
    if cfg.has_option(Section, Option):                                         
        return cfg.get(Section, Option)                                         
    else:                                                                       
        return None

I found a solution for me.

start.py

[...]
import ConfigRader
[...]
ConfigReader.LoadConfigFile(args.filename)

ConfigReader.py

[...]
_ConfigFile = None                                                              
def LoadConfigFile(File):                                                          
    if not File:                                                                
        File = 'default.cfg'                                                    
    global _ConfigFile                                                          
    _ConfigFile = File
[...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM