import inspect
import hashlib


def _CanonicalStepNameKeyGenerator(func, args, kwargs, namespace=None):
  """Generates a key to a cached canonical step name.

  Using the step_name as key, assuming it's practically not possible for 2 steps
  with different canonical_step_names have exactly the same step_name.

  Args:
    func (function): An arbitrary function.
    args (list): Positional arguments passed to ``func``.
    kwargs (dict): Keyword arguments passed to ``func``.
    namespace (str): A prefix to the key for the cache.

  Returns:
    A string to represent a call to the given function with the given arguments.
  """
  params = inspect.getcallargs(func, *args, **kwargs)
  step_name = params.get('step_name')
  assert step_name, 'No step name provided when requesting step_metadata.'
  encoded_params = hashlib.md5(step_name).hexdigest()
  return '%s-%s' % (namespace, encoded_params)