示例#1
0
def dispatch_load(line, env={}):
    """Load code! gotta be able to read in datafiles...

    This function takes responsability for loading models and when
    they are implemented aliases and armies from long term storage in
    serialized forms to Cortex's runtime for analysis.

    As this is a refactored block out of cortex/repl, the invocation
    is simply (line, env={ .. }). As with the other dispatch_*
    functions this function returns an (env, code) pair, where code is
    true if this was the correct function and it ran, else false.

    Valid commands are:
        'load models <filename>'

    """
    if (line[0] == "load"):
        if (len(line) < 2):
            help(dispatch_load)
            return (env, False)

        # what are we loading?
        elif (line[1] == "models"):
            file = helpers.unixpath(line[2])
            if (os.path.exists(file)):
                new_models = {}
                try:
                    new_models = {
                        m['name']: wm.Model(m)
                        for m in json.load(open(file))
                    }
                except ValueError as e:
                    print("Error parsing data file '%s':\n  %s" % (file, e))
                    return (env, True)

                _tmp = dict()
                _tmp.update(env['models'])
                _tmp.update(new_models)

                env['models'] = _tmp
                return (env, True)
            else:
                print("Error: no such file %s" % line[2])

        elif (line[1] == "aliases"):
            # FIXME
            print("Alias loading isn't supported yet!")

        elif (line[1] == "army"):
            # FIXME
            print("Army loading isn't supported yet!")

        else:
            pass
        return (env, True)
    else:
        return (env, False)
示例#2
0
def dispatch_load(line, env={}):
    """Load code! gotta be able to read in datafiles...

    This function takes responsability for loading models and when
    they are implemented aliases and armies from long term storage in
    serialized forms to Cortex's runtime for analysis.

    As this is a refactored block out of cortex/repl, the invocation
    is simply (line, env={ .. }). As with the other dispatch_*
    functions this function returns an (env, code) pair, where code is
    true if this was the correct function and it ran, else false.

    Valid commands are:
        'load models <filename>'

    """
    if(line[0] == "load"):
        if(len(line) < 2):
            help(dispatch_load)
            return (env, False)

        # what are we loading?
        elif(line[1] == "models"):
            file = helpers.unixpath(line[2])
            if(os.path.exists(file)):
                new_models = {}
                try:
                    new_models = {m['name']:wm.Model(m)
                                  for m in json.load(open(file))}
                except ValueError as e:
                    print("Error parsing data file '%s':\n  %s" % (file, e))
                    return (env, True)

                _tmp = dict()
                _tmp.update(env['models'])
                _tmp.update(new_models)

                env['models'] = _tmp
                return (env, True)
            else:
                print("Error: no such file %s" % line[2])

        elif(line[1] == "aliases"):
            # FIXME
            print("Alias loading isn't supported yet!")

        elif(line[1] == "army"):
            # FIXME
            print("Army loading isn't supported yet!")

        else:
            pass
        return (env, True)
    else:
        return (env, False)
示例#3
0
            continue

        # FINALLY
        #    this is where code that is intended to run after a
        #    successfull command goes. so if I add a history logger
        #    (which would be cool and kinda usefull) then this is
        #    where that write to file would go.

        # END FINALLY
        continue

    return env


if __name__ == '__main__':
    # Cortex
    #   a Warmachine strategery toolkit
    #
    #   Loads and interprets the file ~/.cortexrc as though all
    #   commands were being typed at the repl before dropping into
    #   read/eval/print mode for user IO.
    #
    _env = {'models': {}, 'model aliases': {}}

    file = helpers.unixpath("~/.cortexrc")

    if os.path.exists(file):
        print("loading config file...")
        _env = repl(open(file), env=_env)
    repl(sys.stdin, env=_env)
示例#4
0
        # FINALLY
        #    this is where code that is intended to run after a
        #    successfull command goes. so if I add a history logger
        #    (which would be cool and kinda usefull) then this is
        #    where that write to file would go.


        # END FINALLY
        continue

    return env


if __name__ == '__main__':
    # Cortex
    #   a Warmachine strategery toolkit
    #
    #   Loads and interprets the file ~/.cortexrc as though all
    #   commands were being typed at the repl before dropping into
    #   read/eval/print mode for user IO.
    #
    _env = {'models':{}, 'model aliases':{}}

    file=helpers.unixpath("~/.cortexrc")

    if os.path.exists(file):
        print("loading config file...")
        _env = repl(open(file), env=_env)
    repl(sys.stdin, env=_env)