0

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Denny Crane
  • 637
  • 6
  • 19
  • Use a `load_configuration()` function that takes the filename as an argument. – Martijn Pieters Jul 29 '13 at 08:10
  • I'm not sure if I can follow you. Could you explain me that briefly? – Denny Crane Jul 29 '13 at 08:20
  • Create a function in your options module that you import and call, passing in the filename of the configuration file. No global variable required. – Martijn Pieters Jul 29 '13 at 08:23
  • you can use the a python file as configuration and just import it. see: http://stackoverflow.com/questions/8225954/python-configuration-file-any-file-format-recommendation-ini-format-still-appr – Udy Jul 29 '13 at 08:27
  • @Martin, I get what you mean. But my script contains a lot of files. And I would like to add this on the fly. – Denny Crane Jul 29 '13 at 08:35

2 Answers2

0

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
eri
  • 3,133
  • 1
  • 23
  • 35
0

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
[...]
Denny Crane
  • 637
  • 6
  • 19