Example #1
0
def _colorscheme_name_to_class(signal):
    # Find the colorscheme.  First look in ~/.config/ranger/colorschemes,
    # then at RANGERDIR/colorschemes.  If the file contains a class
    # named Scheme, it is used.  Otherwise, an arbitrary other class
    # is picked.
    if isinstance(signal.value, ColorScheme): return

    if not signal.value:
        signal.value = 'default'

    scheme_name = signal.value
    usecustom = not ranger.arg.clean

    def exists(colorscheme):
        return os.path.exists(colorscheme + '.py')

    def is_scheme(x):
        try:
            return issubclass(x, ColorScheme)
        except:
            return False

    # create ~/.config/ranger/colorschemes/__init__.py if it doesn't exist
    if usecustom:
        if os.path.exists(signal.fm.confpath('colorschemes')):
            initpy = signal.fm.confpath('colorschemes', '__init__.py')
            if not os.path.exists(initpy):
                open(initpy, 'a').close()

    if usecustom and \
            exists(signal.fm.confpath('colorschemes', scheme_name)):
        scheme_supermodule = 'colorschemes'
    elif exists(signal.fm.relpath('colorschemes', scheme_name)):
        scheme_supermodule = 'ranger.colorschemes'
        usecustom = False
    else:
        scheme_supermodule = None  # found no matching file.

    if scheme_supermodule is None:
        if signal.previous and isinstance(signal.previous, ColorScheme):
            signal.value = signal.previous
        else:
            signal.value = ColorScheme()
        raise Exception("Cannot locate colorscheme `%s'" % scheme_name)
    else:
        if usecustom: allow_access_to_confdir(ranger.arg.confdir, True)
        scheme_module = getattr(
            __import__(scheme_supermodule, globals(), locals(), [scheme_name],
                       0), scheme_name)
        if usecustom: allow_access_to_confdir(ranger.arg.confdir, False)
        if hasattr(scheme_module, 'Scheme') \
                and is_scheme(scheme_module.Scheme):
            signal.value = scheme_module.Scheme()
        else:
            for var in scheme_module.__dict__.values():
                if var != ColorScheme and is_scheme(var):
                    signal.value = var()
                    break
            else:
                raise Exception("The module contains no valid colorscheme!")
Example #2
0
def _colorscheme_name_to_class(signal):
    # Find the colorscheme.  First look in ~/.config/ranger/colorschemes,
    # then at RANGERDIR/colorschemes.  If the file contains a class
    # named Scheme, it is used.  Otherwise, an arbitrary other class
    # is picked.
    if isinstance(signal.value, ColorScheme): return

    if not signal.value:
        signal.value = 'default'

    scheme_name = signal.value
    usecustom = not ranger.arg.clean

    def exists(colorscheme):
        return os.path.exists(colorscheme + '.py')

    def is_scheme(x):
        try:
            return issubclass(x, ColorScheme)
        except:
            return False

    # create ~/.config/ranger/colorschemes/__init__.py if it doesn't exist
    if usecustom:
        if os.path.exists(signal.fm.confpath('colorschemes')):
            initpy = signal.fm.confpath('colorschemes', '__init__.py')
            if not os.path.exists(initpy):
                open(initpy, 'a').close()

    if usecustom and \
            exists(signal.fm.confpath('colorschemes', scheme_name)):
        scheme_supermodule = 'colorschemes'
    elif exists(signal.fm.relpath('colorschemes', scheme_name)):
        scheme_supermodule = 'ranger.colorschemes'
        usecustom = False
    else:
        scheme_supermodule = None  # found no matching file.

    if scheme_supermodule is None:
        if signal.previous and isinstance(signal.previous, ColorScheme):
            signal.value = signal.previous
        else:
            signal.value = ColorScheme()
        raise Exception("Cannot locate colorscheme `%s'" % scheme_name)
    else:
        if usecustom: allow_access_to_confdir(ranger.arg.confdir, True)
        scheme_module = getattr(__import__(scheme_supermodule,
                globals(), locals(), [scheme_name], 0), scheme_name)
        if usecustom: allow_access_to_confdir(ranger.arg.confdir, False)
        if hasattr(scheme_module, 'Scheme') \
                and is_scheme(scheme_module.Scheme):
            signal.value = scheme_module.Scheme()
        else:
            for var in scheme_module.__dict__.values():
                if var != ColorScheme and is_scheme(var):
                    signal.value = var()
                    break
            else:
                raise Exception("The module contains no valid colorscheme!")