Question

I have an python script that needs to be run in the Windows cmd.exe for some testing, it has three options to choose

such as

config A

config B

config C

Hence the user can choose different config by running xx.py configA, the script will use the config A as options until the user specify another config.

Due to some reasons, my program does not use for loop to keep track of the state of the config, hence I have three options to choose based on my research

  1. Delete A single file that A use, but B C does not, and similar process is done with B and C (config A, B and C will use difference executable file to run the same things)

sample code like this:

if os.environ.get('xx'):
    path_a = os.path.join('xx','xx.dll')
    if os.path.isfile(path_a):
        os.remove(path_a)

2.Another idea is to use a local file to keep track of the configuration by checking the specific text in the text file and decide which executable file to choose

3.Third idea is to create an registry key for this script and keep track of that

My question is which way should I go with and is any other better way I can achieve the same results.

Was it helpful?

Solution

From what I can understand you are asking the following:

What is the best way for my python script to handle running with one of three different arguments that points to the configuration settings to use during that run.

If that is the case, then I think you should look into xml.etree as an option to store and access configuration data in a config file. you should only need 1 config file this way since you can utilize different nodes with configuration settings as children for each config option.

you could save your configuration in a file in this format;

<configroot>
    <configsettings1>
        <option1>foo</option1>
        <option2>bar</option2>
    </configsettings1>
    <configsettings2>
        <option1>foo</option1>
        <option2>foo</option2>

and so on. you can even customize it a bit more by adding more data to each option:

<option1 disabled=True>

or

<option2 active=1 type='foo'>

then you can extract the nodes from that depending on what args are used with getopt

import getopt
import xml.etree as et

INSERTMAINCODEHERE

if __name__ == '__main__':
    try:
        opts, args = getopt.getopt(sys.argv[1:], "f:", ["configoption="])


    except getopt.GetoptError, e:
        print "getopt.GetoptError: %s" % e
        sys.exit(1)

    for o, a in opts:
        if o in ("-f","--configoption="):
            b=et.ElementTree.parse('configfilename')
            options=b.getroot().find(a)
            for s in list(options):
                print s, s.tag, s.tail, s.attrib #ETC. ETC.

you can see more information and some detail about the specifics here

I hope this is what you were asking. if not please let me know more and I'll try to help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top