code:根据提供的参数提取信息 do.py mod test.cnf

能够根据mod输出mod中的配置信息。如:do.py mod3 test.cnf, output is :

key="name1"
file="test"

---------------------------

file: test.cnf
[mod1]
JAVA_ARGS="-server -Xms512m -Xmx512m -XX:NewSize=64m -XX:+UseConcMarkSweepGC"

JAVA_OPTSS="${JAVA_ARGS} -Dsun.rmi.dgc.server.gcInterval=3600000"

[mod2]
key="name"
JAVA_ARGS="${JAVA_ARGS} -Dsun.rmi.dgc.client.gcInterval=30000"

[mod3]
key="name1"
file="test"
-------------------------------------

file:do.py

#!/usr/bin/env python

from sys import argv
import re

mod = argv[1]
conf = argv[2]


#pre-check
def pre_check():
        if len(argv) == 3:
                print "OK , will get the info"
        else:
                print "NOK, you should provide two parameter"
                sys.exit()


#format the list to dico
def format_content(con_content):
        key = ""
        content = ""
        content_dico = {}
        item_dico = {}
#       print "the con_content to be processed is", con_content
        for item in con_content:
#               print "item is", item, "\n"
                if re.match("\[", item) and  key == "" :
                        key = item.rstrip("]").lstrip("[")

                elif re.match("\[", item) and key != "":

                        item_dico = {key:content}
#                       print "item_dico is ", item_dico, "\n"
                        content_dico.update(item_dico)
                        item_dico.clear()
                        key = item.rstrip("]").lstrip("[")
                        content = ""
                elif item == con_content[len(con_content) - 1]:
#                       print "item is ", item ," in the last one"
#                       print "the last content is ", con_content[len(con_content) - 1]
                        item_dico = {key:content}
                        content_dico.update(item_dico)
                        item_dico = {key:content}
                        content_dico.update(item_dico)
                else:
                        content = content.rstrip("\n")
                        content = content + "," + item
        print "after format, the dico is", content_dico
        return content_dico

#match the mod
#def check_mod(mod,con_content):


#match the conf and get the info
def get_matchedInfo(mode, new_dict):
        if new_dict.has_key(mode):
                print "mathced is ", new_dict[mode]
                return new_dict[mode]
        else:
                print "no matched mode"
                exit()





def main():
        pre_check()
        con_file = open(conf,'r')
        con_content = con_file.readlines()
#       print "before format, the con_content is", con_content
        con_content_new = []
        #remove the \n from con_content
        for item in con_content:
#               print "item is ", item, "\n"
                new_item = item.rstrip('\n').strip()
#               print "new_item is", new_item
#               con_content.remove(item)
                if new_item == '':
                        pass
                else:

                        con_content_new.append(new_item)
#               print "con_content_new is ", con_content_new, "\n"
                        con_content_new.append(new_item)
#               print "con_content_new is ", con_content_new, "\n"
#       print "after remove \n , the new con_content is ", con_content_new
        new_dict = format_content(con_content_new)
        info = get_matchedInfo(mod, new_dict)
        print "info is ", info
if __name__ == "__main__":
        main()



posted @ 2014-04-08 13:39  amandaysu  阅读(140)  评论(0编辑  收藏  举报