#!/usr/bin/env python

# Prompt Sum. This shows off the basic usage of the command_format decorator and
# associated validators to parse a "sum" command with automatic prompting for
# missing arguments.
#
# Usage: ./promptsum.py, followed by typing any number of "sum x y" commands.
#
# http://blog.codezen.org/2010/09/03/using-decorators-for-flexible-prompts

import logging

logging.basicConfig(
        filemode = "w",
        format = "%(message)s",
        datefmt = "%H:%M:%S",
        level = logging.DEBUG
)

log = logging.getLogger()

def command_format(*types):
    def cf(fn):
        def cfdec(self, **kwargs):

            rem = kwargs["args"]
            realkwargs = {}

            for kw, validator in types:
                validator = getattr(self, validator)
                valid, result, rem = validator(rem.lstrip())
                if not valid:
                    log.debug("Couldn't properly parse %s" % kwargs["args"])
                    return

                realkwargs[kw] = result

            return fn(self, **realkwargs)
        return cfdec
    return cf

class MyCommandHandler():
    def uint(self, args):
        if not args:
            args = raw_input("uint: ")

        terms = args.split()
        if not terms:
            return (False, None, None)

        try:
            ret = int(terms[0])
        except:
            log.error("Couldn't parse %s as integer!" % terms[0])
            return (False, None, None)

        if ret < 0:
            log.error("Argument must be > 0")
            return (False, None, None)
        
        return (True, ret, " ".join(terms[1:]))

    @command_format(("term1","uint"),("term2","uint"))
    def sum(self, **kwargs):
        print kwargs["term1"] + kwargs["term2"]

if __name__ == "__main__":
    m = MyCommandHandler()
    while(True):
        command = raw_input()

        # I know, I know, this could be a better, introspective
        # command dispatcher. - Jack

        if command == "sum" or command.startswith("sum "):
            m.sum(args=command[4:])
