def repository_factory(vcs_type, **kw):
<EOL>if isinstance(vcs_type, string_types):<EOL><INDENT>vcs_type = vcs_type.lower()<EOL>for cls in load_backends():<EOL><INDENT>if vcs_type in cls.ALIASES:<EOL><INDENT>vcs_type = cls<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if not (isinstance(vcs_type, type) and issubclass(vcs_type, Repository)):<EOL><INDENT>raise UnknownRepositoryTypeError("<STR_LIT>" % vcs_type)<EOL><DEDENT>cache_key = tuple('<STR_LIT>' % (k, v) for k, v in sorted(kw.items()))<EOL>logger.debug("<STR_LIT>", cache_key)<EOL>if cache_key in loaded_repositories:<EOL><INDENT>logger.debug("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>logger.debug("<STR_LIT>")<EOL>loaded_repositories[cache_key] = vcs_type(**kw)<EOL><DEDENT>return loaded_repositories[cache_key]<EOL>
Instantiate a :class:`Repository` object based on the given type and arguments.

:param vcs_type: One of the strings 'bazaar', 'bzr', 'git', 'hg' or
                 'mercurial' or a subclass of :class:`Repository`.
:param kw: The keyword arguments to :func:`Repository.__init__()`.
:returns: A :class:`Repository` object.
:raises: :exc:`~vcs_repo_mgr.exceptions.UnknownRepositoryTypeError` when
         the given type is unknown.