Configuration recommendations

Chris Liechti cliechti at gmx.net
Sat Jun 8 00:29:35 CEST 2002


"Stuart D. Gathman" <stuart at bmsi.com> wrote in news:adra81$qf9$1 at nntp2-
cm.news.eni.net:

> I have an application - of the background daemon variety - written in
> python.  It has some configuration data like the following:
> 
> scan_rfc822 = 1
> scan_html = 1           # can be CPU intensive
> block_chinese = 0
> hide_path = ()          # domains that block visible private nodes
> #hide_path = ( 'jcpenney.com' )
> block_forward = {}      # people that hate forwards
> #block_forward = { 'mycorp.com': ('egghead','busybee') }
> log_headers = 0
> blind_wiretap = 1
> wiretap_users = {}
> wiretap_dest = None
> #wiretap_users = { 'bigcorp.com': ('disloyal', 'bigmouth') }
> #wiretap_dest = '<spy at bigcorp.com>'
> check_user = {}
> #check_user = { 'mycorp.com': ('joe','mary','bill') }
> reject_virus_from = ()
> #reject_virus_from = ( 'mycorp.com' )
> 
> What is standard practice for incorporating such data?  I would like it
> to remain executable python code for maximum flexibility (plus I don't
> have to deal with parsing or XML). Should the application exec the config
> script?  Can this be done so that the module namespace is updated?
> Or should the config script be the entry point, setting up the data, then
> calling the main application?

have a look at the ConfigParser module. it uses ini-file style and it's 
easy to load the defaults from a file and update with the user preferences 
save settings etc. it also has sections so that you can group the options. 
dictionaries and list have to be inserted and read back as strings. you can 
use eval (i don't recomend this as it could have nasty sideeffects 
depending on your users...) or you can make some simple code to parse the 
data.
e.g. for a list:
inifile:
reject_virus_from = comp1, comp2

code:
s = config.get('section','reject_virus_from')
lst = [q.strip() for q in s.split(',')]

(or separate by whitespace instead of colon: lst = s.split())

or for a dict:
d = dict([(k.strip(),v.strip())
          for k,v in q.split(':')
          for q in s.split(',')
         ])

(add some appropriate exception handling to detect errors)
note that you don't need to write quotes around the strings, which might be 
user-frendlier to non programmers that use your program.

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list