zoukankan      html  css  js  c++  java
  • 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, " "
                    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, " "
                            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(" ")
                            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 from con_content
            for item in con_content:
    #               print "item is ", item, " "
                    new_item = item.rstrip(' ').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, " "
                            con_content_new.append(new_item)
    #               print "con_content_new is ", con_content_new, " "
    #       print "after remove , 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()



  • 相关阅读:
    QFramework 使用指南 2020(二):下载与版本介绍
    QFramework 使用指南 2020 (一): 概述
    Unity 游戏框架搭建 2018 (二) 单例的模板与最佳实践
    Unity 游戏框架搭建 2018 (一) 架构、框架与 QFramework 简介
    Unity 游戏框架搭建 2017 (二十三) 重构小工具 Platform
    Unity 游戏框架搭建 2017 (二十二) 简易引用计数器
    Unity 游戏框架搭建 2017 (二十一) 使用对象池时的一些细节
    你确定你会写 Dockerfile 吗?
    小白学 Python 爬虫(8):网页基础
    老司机大型车祸现场
  • 原文地址:https://www.cnblogs.com/amandaysu/p/3651813.html
Copyright © 2011-2022 走看看