April 2024 - This site, and Kamaelia are being updated. There is significant work needed, and PRs are welcome.

Cookbook Example

How can I...?

Example 6: How to build a network controllable graph viewer. Components used:TopologyViewerServer

#!/usr/bin/python

from Kamaelia.Visualisation.PhysicsGraph.TopologyViewerServer import TopologyViewerServer

def parseArgs(argv, extraShortArgs="", extraLongArgs=[]):
    import getopt

    shortargs = "fh" + extraShortArgs
    longargs  = list("help","fullscreen","resolution=","port=") + extraLongArgs

    optlist, remargs = getopt.getopt(argv, shortargs, longargs)

    dictArgs = {}
    for o,a in optlist:
        if o in ("-h","--help"):
            dictArgs['help'] = "Arguments:\n" + \
                               "   -h, --help\n" + \
                               "      This help message\n\n" + \
                               "   -f, --fullscreen\n" + \
                               "      Full screen mode\n\n" + \
                               "   --resolution=WxH\n" + \
                               "      Set window size to W by H pixels\n\n" + \
                               "   --port=N\n" + \
                               "      Listen on port N (default is 1500)\n\n"

        elif o in ("-f","--fullscreen"):
            dictArgs['fullscreen'] = True

        elif o in ("--resolution"):
            match = re.match(r"^(\d+)[x,-](\d+)$", a)
            x=int(match.group(1))
            y=int(match.group(2))
            dictArgs['screensize'] = (x,y)

        elif o in ("--port"):
            dictArgs['serverPort'] = int(a)

    return dictArgs, optlist, remargs

if __name__=="__main__":
    import sys
    dictArgs, remargs, junk = parseArgs(sys.argv[1:])

    if "help" in dictArgs:
        print dictArgs["help"]
    else:
        TopologyViewerServer(**dictArgs).run()

Source: Examples/example6/TopologyVisualiser.py