LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sourcing a python config file? (https://www.linuxquestions.org/questions/programming-9/sourcing-a-python-config-file-351840/)

shanenin 08-10-2005 10:06 AM

sourcing a python config file?
 
I wrote simple python program that rips, encodes, and gets song data from the cddb. I would like to give the user the option of customizing it to there taste. So I want to make a config file that I can store in /etc/pythonriprc or in ~/.pythonriprc. for example it would look like this

config file
Code:

# if you would like to specify the location of you music directory uncomment the following
#music_dir = '/home/shane/location/of/directory'
# if you would like to permantey set the bitrate used please unmcooment the following
#bit_rate = 192

is their a way to direct my script to use the variables set is this file?

Matir 08-10-2005 11:21 AM

You could use something like this section of the python tutorial. (As a shortcut, look into execfile()).

Crashed_Again 08-10-2005 11:57 AM

Something like this would work:

Code:

import re

pattern = "\'(.*?)\'"

file = open('/path/to/config.conf', 'r')
for line in file.readlines():
    if line.startswith('music_dir'):
        music_dir = re.findall(pattern, line)[0]
        print music_dir
    elif line.startswith('bit_rate'):
        bit_rate = re.findall(pattern, line)[0]
        print bit_rate
       
file.close()

Basically all this does is check each line to see if it starts with either 'music_dir' or 'bit_rate'. If it does then it extracts the string that is between the single quotes and sets that to the variable. So everything would have to be inside single quotes. The config would look like this:

Code:

# if you would like to specify the location of you music directory uncomment the following
music_dir = '/home/shane/location/of/directory'
# if you would like to permantey set the bitrate used please unmcooment the following
bit_rate = '19'

This is pretty limited but it does the trick.

shanenin 08-10-2005 12:11 PM

@Crashed_Again

that would work :-) No need to reinvent the wheel, which I do all to often. I usually spend half a day working on an algorythm, then find out later there was aready a module made for the task.

shanenin 08-10-2005 12:22 PM

thanks Matir for the link, it seems something as simple as
Code:

execfile('/etc/pythonrip.rc')
would work.

Matir 08-10-2005 12:49 PM

No problem. You might want to check if the file exists first, just to be clean about it. :)

shanenin 08-10-2005 12:58 PM

I will do something like this
Code:

import os
if os.path.isfile('/etc/pythonriprc.py'):
    execfile('/etc/pythonriprc.py')


Matir 08-10-2005 12:59 PM

Yep, that makes sense. Might want to spit an error/warning if it doesn't exist, depending on how badly you want them to have such a config file :)

shanenin 08-10-2005 02:19 PM

Ideally I wnat to use Distutils to install my modules, main script and config file. I looked at the docs, it semed a little involved , but not to hard.


All times are GMT -5. The time now is 04:04 AM.