All pastes #809087 Raw Edit

Anonymous

public python v1 · immutable
·published 2007-12-08 06:32 UTC
rendered paste body
#!/usr/bin/env python# # Copyright (C) 2007 Terry < snip, not in public ^_^ >## This software is provided 'as-is', without any express or implied# warranty.  In no event will the authors be held liable for any damages# arising from the use of this software.## Permission is granted to anyone to use this software for any purpose,# including commercial applications, and to alter it and redistribute it# freely, subject to the following restrictions:## 1. The origin of this software must not be misrepresented; you must not#    claim that you wrote the original software. If you use this software#    in a product, an acknowledgment in the product documentation would be#    appreciated but is not required.# 2. Altered source versions must be plainly marked as such, and must not be#    misrepresented as being the original software.# 3. This notice may not be removed or altered from any source distribution.## < snip again >##############################################################################""" -- pdpp.py -- A simple Python-written Document Pre-Processor. Purpose:     Allow a simple syntax for defining constants, macros, variables, and     other associated items.      And combine this with a simple style of text and formating instructions      To create a easy way of generating an HTML document."""import sys, osimport reimport optparseEXIT_SUCCESS    =   0EXIT_FAILURE    =   1class ConstantNotDefinedError(Exception):    passclass PreProcessor:    """Simple text preprocessor        #define FOO bar  -> causes PrePrcessor#defines[] to have a dictionary    appended as { 'FOO' : bar }. The add_define() and get_define() methods     insert a key,value and fetch a value by key.    #macro ....    """    defines = []    macros = []        def __init__(self):        pass        def add_define(self, const, value):        "Register a constant as translating to value with the preprocessor"        p = { "%s" % const : value }        self.defines.append(p)    def get_define(self, const):        """Access a stored constant and return it's definition (value)        ThrowsConstantNotDefinedError if constant is not defined"""        for hash in self.defines:            for key in hash:                if key == "%s" % const:                    return hash[key]                #raise ConstantNotDefinedError, "%s not defined!" % key    def add_macro(self):        pass    def get_macro(self, macro):        pass    def parse(self, file):        lines = file.readlines()        for ln in lines:            if re.match(r'^#.*', ln):                cmd=ln.split(' ')                if cmd[0].lower() == '#define':                    val=' '.join(cmd[2:])                    self.add_define( cmd[1], val)        return Truedef usage():    sys.stderr.write("write me")    sys.exit(EXIT_FAILURE)def main(argv):    try:        fp = file('./test.txt')        pp=PreProcessor()        pp.parse(fp)        print pp.defines,"\n\n\n"        print pp.get_define('OS')        print pp.get_define('VERSION')        print pp.get_define('VIM')        print pp.get_define('undefined')    except IOError:        usage()if __name__ == "__main__":    main(sys.argv)