LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-10-2005, 11:06 AM   #1
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
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?
 
Old 08-10-2005, 12:21 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 128Reputation: 128
You could use something like this section of the python tutorial. (As a shortcut, look into execfile()).
 
Old 08-10-2005, 12:57 PM   #3
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
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.
 
Old 08-10-2005, 01:11 PM   #4
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Original Poster
Rep: Reputation: 30
@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.

Last edited by shanenin; 08-10-2005 at 01:23 PM.
 
Old 08-10-2005, 01:22 PM   #5
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Original Poster
Rep: Reputation: 30
thanks Matir for the link, it seems something as simple as
Code:
execfile('/etc/pythonrip.rc')
would work.

Last edited by shanenin; 08-10-2005 at 01:44 PM.
 
Old 08-10-2005, 01:49 PM   #6
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 128Reputation: 128
No problem. You might want to check if the file exists first, just to be clean about it.
 
Old 08-10-2005, 01:58 PM   #7
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Original Poster
Rep: Reputation: 30
I will do something like this
Code:
import os
if os.path.isfile('/etc/pythonriprc.py'):
    execfile('/etc/pythonriprc.py')
 
Old 08-10-2005, 01:59 PM   #8
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 128Reputation: 128
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
 
Old 08-10-2005, 03:19 PM   #9
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Original Poster
Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sourcing .bashrc shifty_eyes Linux - Newbie 3 09-25-2005 10:43 PM
build redhat-config-packages no python neo_cool_XIIK Fedora 0 12-30-2004 09:04 AM
.bashrc error upon sourcing psiakr3w Linux - General 2 08-07-2004 01:44 AM
Error when sourcing file to finish program installation serendipitysdc Linux - Software 1 07-18-2004 11:16 PM
Lan Config file / internet config file Raven_X_Neo Linux - Networking 1 10-30-2002 02:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration