view rhodecode/lib/vcs/utils/imports.py @ 3774:60335b702a00 beta

invalidation: don't create CacheInvalidation records on startup Creating the records early gave an advantage before lightweight was introduced. With lightweight it is no longer necessary. The records will be created on demand anyway and there is no reason to create and maintain them before they are used.
author Mads Kiilerich <madski@unity3d.com>
date Wed, 03 Apr 2013 15:56:12 +0200
parents 324ac367a4da
children 7e5f8c12a3fc
line wrap: on
line source

from rhodecode.lib.vcs.exceptions import VCSError


def import_class(class_path):
    """
    Returns class from the given path.

    For example, in order to get class located at
    ``vcs.backends.hg.MercurialRepository``:

        try:
            hgrepo = import_class('vcs.backends.hg.MercurialRepository')
        except VCSError:
            # hadle error
    """
    splitted = class_path.split('.')
    mod_path = '.'.join(splitted[:-1])
    class_name = splitted[-1]
    try:
        class_mod = __import__(mod_path, {}, {}, [class_name])
    except ImportError, err:
        msg = "There was problem while trying to import backend class. "\
            "Original error was:\n%s" % err
        raise VCSError(msg)
    cls = getattr(class_mod, class_name)

    return cls