3 # Interactive shell for testing PLCAPI
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2005 The Trustees of Princeton University
8 # $Id: Shell.py,v 1.1 2006/09/06 15:33:59 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":
44 auth = {'AuthMethod': "capability",
45 'Username': api.config.PLC_API_MAINTENANCE_USER,
46 'AuthString': api.config.PLC_API_MAINTENANCE_PASSWORD,
51 Dummy class to support tab completion of API methods with dots in
52 their names (e.g., system.listMethods).
56 # Define all methods in the global namespace to support tab completion
57 for method in api.methods:
58 paths = method.split(".")
61 if first not in globals():
62 globals()[first] = Dummy()
63 obj = globals()[first]
65 if not hasattr(obj, path):
67 setattr(obj, path, api.callable(method))
69 setattr(obj, path, Dummy())
70 obj = getattr(obj, path)
72 globals()[method] = api.callable(method)
77 Override builtin help() function to support calling help(method).
80 if isinstance(thing, Method):
81 return pydoc.pager(thing.help())
85 # If a file is specified
90 # Otherwise, create an interactive shell environment
92 print "PlanetLab Central Direct API Access"
94 print 'Type "system.listMethods()" or "help(method)" for more information.'
96 # Readline and tab completion support
101 # Load command history
102 history_path = os.path.join(os.environ["HOME"], ".plcapi_history")
104 file(history_path, 'a').close()
105 readline.read_history_file(history_path)
106 atexit.register(readline.write_history_file, history_path)
110 # Enable tab completion
111 readline.parse_and_bind("tab: complete")
123 line = raw_input(prompt + sep)
125 except KeyboardInterrupt:
130 # Build up multi-line command
133 # Blank line or first line does not end in :
134 if line == "" or (command == line and line[-1] != ':'):
137 command += os.linesep
143 elif command in ["q", "quit", "exit"]:
148 # Try evaluating as an expression and printing the result
149 result = eval(command)
150 if result is not None:
153 # Fall back to executing as a statement
155 except Exception, err:
156 traceback.print_exc()