3 # Interactive shell for testing PLCAPI
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2005 The Trustees of Princeton University
8 # $Id: plcsh,v 1.3 2006/01/09 19:57:24 mlhuang Exp $
16 from PLC.API import PLCAPI
17 from PLC.Method import Method
20 config = "/etc/planetlab/plc_config"
23 print "Usage: %s [OPTION]..." % sys.argv[0]
25 print " -f, --config=FILE PLC configuration file (default: %s)" % config
26 print " -h, --help This message"
31 (opts, argv) = getopt.getopt(sys.argv[1:], "f:h", ["config=", "help"])
32 except getopt.GetoptError, err:
33 print "Error: " + err.msg
36 for (opt, optval) in opts:
37 if opt == "-f" or opt == "--config":
39 elif opt == "-h" or opt == "--help":
46 Dummy class to support tab completion of API methods with dots in
47 their names (e.g., system.listMethods).
51 # Define all methods in the global namespace to support tab completion
52 for method in api.methods:
53 paths = method.split(".")
56 if first not in globals():
57 globals()[first] = Dummy()
58 obj = globals()[first]
60 if not hasattr(obj, path):
62 setattr(obj, path, api.callable(method))
64 setattr(obj, path, Dummy())
65 obj = getattr(obj, path)
67 globals()[method] = api.callable(method)
72 Override builtin help() function to support calling help(method).
75 if isinstance(thing, Method):
76 return pydoc.pager(thing.help())
80 # If a file is specified
85 # Otherwise, create an interactive shell environment
87 print "PlanetLab Central Direct API Access"
89 print 'Type "system.listMethods()" or "help(method)" for more information.'
91 # Readline and tab completion support
96 # Load command history
97 history_path = os.path.join(os.environ["HOME"], ".plcapi_history")
99 file(history_path, 'a').close()
100 readline.read_history_file(history_path)
101 atexit.register(readline.write_history_file, history_path)
105 # Enable tab completion
106 readline.parse_and_bind("tab: complete")
118 line = raw_input(prompt + sep)
120 except KeyboardInterrupt:
125 # Build up multi-line command
128 # Blank line or first line does not end in :
129 if line == "" or (command == line and line[-1] != ':'):
132 command += os.linesep
138 elif command in ["q", "quit", "exit"]:
143 # Try evaluating as an expression and printing the result
144 result = eval(command)
145 if result is not None:
148 # Fall back to executing as a statement
150 except Exception, err:
151 traceback.print_exc()