简体   繁体   中英

Manipulate configuration files with ConfigParser lib on python

I have a conf file with several configurations, i need to parse the information automatically in order to create a interface for easy use.

i give an example of one of my sections in configuration:

[OutOfHoursAlert]
comment_max_hour = Max Hour
max_hour = 20
comment_min_hour = Min Hour
min_hour = 8
comment_freq = Frequency of Plugin Executation
frequency = 10

the goals are read all sections (this is ok, I just do it easily)

then the hard part i don't know how to do it, is to show on my terminal interface the comment, and the the option to change the option below of a comment.

like this:

Menu to configure OutOfHoursAlert
  Max Hour (read from comment) : (then show the field to edit, in this case max_hour)
  Min Hour (read from comment) : (then show the field to edit, in this case min_hour)
  FRequency of PLugin Executation (read from comment) : (then show the field to edit, in this case frequency)

to read all plugins i have a write this simple code: import ConfigParser

import plugnplay
from plugins.PluginAlert import PluginAlerter

plugnplay.plugin_dirs = ['./plugins']
plugnplay.load_plugins()
plugins = PluginAlerter.implementors()

templist = []

for x in plugins:
    temp = str(x).split('.')
    templist.append(temp[1])

config = ConfigParser.ConfigParser()
config.readfp(open('./conf.ini'))

for x in templist:
    print "\nThe Configurations for plugin "+x+" are:\n"
    print dict(config.items(x))

whit this i create a dictionary with all sections i want, but i like do it like i told before, is any way to do that? any tip? thanks

You can use new_value = input(comment+": ") to get a new value from the user.

If you want to build an advanced console program that allows to actually edit variables, move the cursor etc., you might want to take a look at the curses module of the python standard library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM