All pastes #17185 Raw Edit

Mine

public python v1 · immutable
#17185 ·published 2005-07-07 20:05 UTC
rendered paste body
#/usr/bin/env pythonimport reimport sysimport shlexfrom StringIO import StringIOblock_re = re.compile("(@begin_block_(?P<begin>\w+)|@end_block_(?P<end>\w+))(\((?P<args>.*?)\))?")class PushableFile(file):    def __init__(self, *args):        file.__init__(self, *args)        self.__pushed_lines = []    def push(self, line):        self.__pushed_lines.append(line)    def next(self):        if self.__pushed_lines:            return self.__pushed_lines.pop(0)        else:            return file.next(self)def parse_args(argv):    option_keys = []    option_values = {}    option_args = []    for arg in argv:        if arg.startswith("--"):            arg_name = arg[2:]            option_keys.append(arg_name)            option_values[arg_name] = []        elif option_keys:            last_key = option_keys[-1]            option_values[last_key].append(arg)        else:            option_args.append(arg)    for arg_name, arg_values in option_values.items():        if len(arg_values) == 1:            option_values[arg_name] = arg_values[0]    return option_values, option_args                class Block(StringIO):    def __init__(self, name, args=[]):        StringIO.__init__(self)        self.name = name        self.args = args        block_stack = [sys.stdout,]block_list = []pushable_f = PushableFile("index.mamba")count = 1import generatordef process(pushable_f):    global count    while True:        try:            line = pushable_f.next()        except StopIteration:            return        match = block_re.search(line)        if match:            groups = match.groupdict()            begin_name,end_name = groups["begin"],groups["end"]            args = groups["args"]            begin_pos,end_pos = match.span()            before_s,after_s = line[:begin_pos],line[end_pos:]            if begin_name:                block = Block(begin_name, args)                block_stack[-1].write(before_s)                pushable_f.push(after_s)                block_stack.append(block)                block_list.append(block)                process(pushable_f)            else:                last_block_name = block_stack[-1].name                if last_block_name != end_name:                    raise "Got %s expecting %s" % (end_name,last_block_name)                else:                    block_stack[-1].write(before_s)                    pushable_f.push(after_s)                    block = block_stack.pop()                    process_generator(block)		    count += 1        else:            block_stack[-1].write(line)def process_generator(block):    try:        generator_f = getattr(generator, "generator_%s" % block.name)        argv = shlex.split(block.args)        (options, args) = parse_args(argv)        for value in generator_f(*args, **options):            block_stack[-1].write(block.getvalue() % value)    except AttributeError:        raisedef main():    process(pushable_f)if __name__ == "__main__":    main()