Some of us are CLI junkies, and would prefer their hands never have to leave the keyboard to perform tasks. With that in mind I did a quick work up of an interactive shell for the API. One of the goals I had for the bindings was to introduce as few dependencies outside the core python libraries as possible, so the interactive console logically inherits from code.InteractiveConsole, which is for the most part a modified version of HistoryConsole from this example. The example claims that the console (presumably when used with .interact()) would have tab complete and history enabled. Much to my dismay and wasted time debugging, this is not the case. The reality is the console is sufficiently sandboxed (my guess) as to not inherit what you may setup of readline by the time you need it (as far as I could tell). The solution I came up with was to force my will upon the new interactive shell before the user enters it.
There. Now everything seems kosher, vars, modules, methods and what not will tab complete (finally). Geeze, why didn’t someone just tell me that in the first place?! Ok, but almost certainly you’re going to want to modify the completion routine to return only what you see fit:
This kludgy code looks for ‘private’ like results and prevents them from being returned when you press tab, when properly motivated I’m sure I’ll prettify that such that only the Api class is kludged with. There really isn’t much more to the code that should be interesting, I make sure that a method pp is available to you that wraps print simplejson.dumps and your default object for accessing the api is called ‘linode’. On startup the shell looks for the environment variable LINODE_API_KEY if it’s not set it will ask you to input your api key.
So what you end up with (after pressing tab twice):
and you can do normal python-y things:
James Sinclair (irgeek in irc parlance) has been helping out, and in his branch you’ll find interesting additions including some potential caching and more high level operations like searching through your domains for records with specific parameters (e.g. “Type=’MX’, Priority=5) pretty neat stuff, I’m sure this kind of stuff will eventually make it upstream and into the interactive shell.
The next step for the shell script is to have a means to execute actions without firing up the interactive portion:
linode-shell --action=domainResourceSave --ResourceID=54 --Name=mail --Target=126.96.36.199 --Type=A
Ideally whatever the shell inherits from, base api or higher level, it should all just work the same and handle parameters properly. But if you do have the higher level one you could have a nice action domainResourceUpdate that could lookup by FQDN (which performs some aggregate of domainList and domainResourceList). Hell, we can probably have an rc file that you can stick custom defined functions in as well
and then … profit!
As usual the code is available from my git repo
git clone git://github.com/tjfontaine/linode-python.git
and viewable at