from parallels.common.actions.base.common_action \
	import CommonAction
from parallels.common.actions.utils.logging_properties \
	import LoggingProperties

class LegacyAction(CommonAction):
	"""Action based on Migrator object functions

	This wrapper is necessary for existing migrator functions that we can't
	move out of migrator as separate actions right now. With that class we can
	use the functions as actions and build them into workflow.
	"""
	def __init__(self, description, function, logging_properties=None):
		"""Class constructor

		Arguments:
		- description - action description as string
		- function - function which performs action; it accepts global_context
		  as the only argument
		"""
		self.description = description
		self.function = function
		if logging_properties is not None:
			self._logging_properties = logging_properties
		else:
			self._logging_properties = LoggingProperties()

	def get_description(self):
		"""Get short description of action as string"""
		return self.description

	def get_failure_message(self, global_context):
		"""Get message for situation when action failed

		Arguments:
		- global_context - registry with different objects that reused among
		  different actions
		"""
		return 'Failed to perform action: %s' % self.get_description()

	def get_logging_properties(self):
		return self._logging_properties

	def run(self, global_context):
		"""Run action

		Arguments:
		- global_context - registry with different objects that reused among
		  different actions
		"""
		self.function(global_context)
