python 配置文件处理

为什么80%的码农都做不了架构师?>>>   hot3.png

1.  配置文件如下:

ver=0.0.1
file=test.xml



#email address 注释
author="[email protected]"
port="12345"
user="tsh185"
ip="10.0.0.1"
myaddr=$ip:$port





CVS_RSH=ssh
COLORTERM=1
LANG=en_US.UTF-8

2. 程序如下: 

#!/usr/bin/env python
# encoding: utf8

import sys, os
reload(sys)
sys.setdefaultencoding('utf-8')
import re
import string

class PkgConfig(object):
    def __init__(self, configXml):
        self.config = {}
        self.baseInfo = {}
        self.misc = []
        self.xml = open(configXml)
        self.parse()
        self.parseBaseInfo()
        self.printAll()

    def parse(self):
        reStartTag = re.compile(r'^<(\w+)>$')
        reEndTag = re.compile(r'^$')
        endTag = startTag = buffer = None
        for line in self.xml:
            lineStripped = line.strip()

            match = reStartTag.match(lineStripped)
            if match:
                startTag = match.group(1)
                buffer = []
                continue

            match = reEndTag.match(lineStripped)
            if match:
                endTag = match.group(1)
                if (not startTag) or (startTag != endTag):
                    return False

                self.config[startTag] = "".join(buffer)
                endTag = startTag = buffer = None
                continue;
            if buffer is not None:
                buffer.append(line)
            else:
                if (not lineStripped) or lineStripped.startswith("
                    

你可能感兴趣的:(python 配置文件处理)