Source

cloudster / api / shelf / console / shell.py

Full commit
#reference: http://www.doughellmann.com/PyMOTW/cmd/

import cmd

from functools import wraps
from api.shelf.configuration.accounts import AccountsManager
from api.shelf.registry.clients import StorageClientsRegistry

__author__ = 'guillermo'

#http://alex.kavanagh.name/2011/05/exploring-python-decorators/
#http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/
def single_client_command(f):
	@wraps(f)
	def wrapped(*args,**kwds):
#
#			for arg in args:
#				print "another arg:", dir(arg)

		if args[0]._CloudsterShell__client_class is None:
			print 'Non client has been selected'
			return

		return f(*args,**kwds)

	return wrapped

def global_command(requires_session=False):
	pass


class CloudsterShell(cmd.Cmd):

	#doc_header = 'doc_header'
	#misc_header = 'misc_header'
	#undoc_header = 'undoc_header'

	__default_prompt = 'cloudster'
	__prompt_delimiter = '>'
	__registry = StorageClientsRegistry()
	__client_class = None


	prompt = __default_prompt + __prompt_delimiter
	intro = "\nCloudster administration console\n"

	CMD_REGISTRY_OPTIONS = ['list', 'use', 'dismiss']
	CMD_ACCOUNT_OPTIONS = ['connect', 'create', 'list', 'delete']

	def __init__(self):
		cmd.Cmd.__init__(self)


	def do_EOF(self, line):

		print '\nBye!'
		return True

	@single_client_command
	def do_account(self, operation):

		account_manager = AccountsManager(self.__client_class)

		operation_parts = operation.rsplit(" ")

		if operation_parts[0] == 'create':
			account_fields_descriptor = account_manager.get_configuration_descriptor()

			#1- Asks for all values
			field_values = {}

			for field in account_fields_descriptor:
				value=raw_input(">> " + field.label() + ": ")

				field_values[field.name()]=value

			#2- Creates new account data
			account_data = account_manager.create(field_values)

			#3- If account needs authorization
			account_data = account_manager.authorize(account_data)

			#4- Save data into a file
			account_manager.save(account_data)

			print 'Account created successfully'


	@single_client_command
	def do_disconnect(self, operation):
		print 'Connecting....'

	@global_command
	def do_start(self, operation):
		print 'Starting cloudster.....'

	@global_command
	def do_shutdown(self, operation):
		print 'Shuting down cloudster.....'

	def do_client(self, operation):

		operation_parts = operation.rsplit(" ")

		if operation_parts[0] == 'list':

			for storage_client in self.__registry.get_available():
				print storage_client.registry_metadata()

		elif operation_parts[0] == 'use':

			if len(operation_parts) > 1:

				client_id = operation_parts[1]

				try:
					self.__client_class = self.__registry.get_by_id(client_id)
					self.__update_prompt()
				except KeyError:
					print "Client id '%s' couldn't be found" % client_id
			else:
				print 'Client id is missing'

		elif operation_parts[0] == 'dismiss':

			#TODO Close session
			self.__client_class = None
			self.__update_prompt()

	def complete_client(self, text, line, begidx, endidx):
		if not text:
			completions = self.CMD_REGISTRY_OPTIONS[:]
		else:
			completions = [ f
							for f in self.CMD_REGISTRY_OPTIONS
							if f.startswith(text)
			]
		return completions

	def complete_account(self, text, line, begidx, endidx):
		if not text:
			completions = self.CMD_ACCOUNT_OPTIONS[:]
		else:
			completions = [ f
							for f in self.CMD_ACCOUNT_OPTIONS
							if f.startswith(text)
			]
		return completions

	def __update_prompt(self):

		prompt_string = None

		if self.__client_class is not None:
			prompt_string = self.__default_prompt + '@' + self.__client_class.registry_metadata().unique_id() + self.__prompt_delimiter
		else:
			prompt_string = self.__default_prompt + self.__prompt_delimiter

		self.prompt = prompt_string

def main():

	shell = CloudsterShell()
	shell.cmdloop()


if __name__ == "__main__":
	main()