简体   繁体   English

在python上使用ConfigParser lib处理配置文件

[英]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. 我有一个带有几种配置的conf文件,我需要自动解析信息才能创建易于使用的界面。

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 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. 您可以使用new_value = input(comment+": ")从用户那里获取新值。

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. 如果要构建一个高级控制台程序,该程序可以实际编辑变量,移动光标等,则可能需要查看python标准库的curses模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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