diff options
-rw-r--r-- | ranger/api/options.py | 14 | ||||
-rw-r--r-- | ranger/colorschemes/__init__.py | 28 | ||||
-rw-r--r-- | ranger/defaults/options.py | 2 | ||||
-rw-r--r-- | ranger/shared/settings.py | 53 |
4 files changed, 54 insertions, 43 deletions
diff --git a/ranger/api/options.py b/ranger/api/options.py index 10778379..7ead8c90 100644 --- a/ranger/api/options.py +++ b/ranger/api/options.py @@ -15,4 +15,16 @@ import re from re import compile as regexp -from ranger import colorschemes +from ranger import colorschemes as allschemes + +class AttrToString(object): + """ + Purely for compatibility to 1.0.3. + """ + def __getattr__(self, attr): + print("NOTE: your configuration is out of date.") + print("instead of this: colorscheme = colorschemes." + attr) + print("please use a string: colorscheme = \"" + attr + "\"") + return attr + +colorschemes = AttrToString() diff --git a/ranger/colorschemes/__init__.py b/ranger/colorschemes/__init__.py index 8b364c03..8b7a21a9 100644 --- a/ranger/colorschemes/__init__.py +++ b/ranger/colorschemes/__init__.py @@ -13,28 +13,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -"""Colorschemes are required to be located here, -or in the CONFDIR/colorschemes/ directory""" -import sys -import ranger -from ranger.ext.get_all_modules import get_all_modules -from os.path import expanduser, dirname, exists, join - -__all__ = get_all_modules(dirname(__file__)) - -if not ranger.arg.clean: - if exists(ranger.relpath_conf('colorschemes')): - initpy = ranger.relpath_conf('colorschemes', '__init__.py') - if not exists(initpy): - open(initpy, 'w').write("""# Automatically generated: -from ranger.ext.get_all_modules import get_all_modules -from os.path import dirname - -__all__ = get_all_modules(dirname(__file__)) -""") - -from ranger.colorschemes import * -try: - from colorschemes import * -except ImportError: - pass +""" +Colorschemes are required to be located here or in CONFDIR/colorschemes/ +""" diff --git a/ranger/defaults/options.py b/ranger/defaults/options.py index 9d9461b5..df755b51 100644 --- a/ranger/defaults/options.py +++ b/ranger/defaults/options.py @@ -41,7 +41,7 @@ show_hidden = False # Which colorscheme to use? These colorschemes are available by default: # default, default88, texas, jungle, snow # Snow is monochrome, texas and default88 use 88 colors. -colorscheme = colorschemes.default +colorscheme = 'default' # Preview files on the rightmost column? # And collapse the last column if there is nothing to preview? diff --git a/ranger/shared/settings.py b/ranger/shared/settings.py index 2d947beb..bdb0bd8d 100644 --- a/ranger/shared/settings.py +++ b/ranger/shared/settings.py @@ -13,6 +13,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +import os import types from inspect import isclass, ismodule import ranger @@ -33,7 +34,7 @@ ALLOWED_SETTINGS = { 'scroll_offset': int, 'preview_files': bool, 'flushinput': bool, - 'colorscheme': (ColorScheme, types.ModuleType), + 'colorscheme': str, 'hidden_filter': lambda x: isinstance(x, str) or hasattr(x, 'match'), } @@ -69,32 +70,52 @@ class SettingsAware(object): assert check_option_types(settings) - # If a module is specified as the colorscheme, replace it with one - # valid colorscheme inside that module. + # Find the colorscheme. First look for it at ~/.ranger/colorschemes, + # then at RANGERDIR/colorschemes. If the file contains a class + # named Scheme, it is used. Otherwise, an arbitrary other class + # is picked. - all_content = settings.colorscheme.__dict__.items() + scheme_name = settings.colorscheme - if isclass(settings.colorscheme) and \ - issubclass(settings.colorscheme, ColorScheme): - settings.colorscheme = settings.colorscheme() + def exists(colorscheme): + return os.path.exists(colorscheme + '.py') - elif ismodule(settings.colorscheme): - def is_scheme(x): - return isclass(x) and issubclass(x, ColorScheme) + def is_scheme(x): + return isclass(x) and issubclass(x, ColorScheme) - if hasattr(settings.colorscheme, 'Scheme') \ - and is_scheme(settings.colorscheme.Scheme): - settings.colorscheme = settings.colorscheme.Scheme() + # create ~/.ranger/colorschemes/__init__.py if it doesn't exist + if os.path.exists(ranger.relpath_conf('colorschemes')): + initpy = ranger.relpath_conf('colorschemes', '__init__.py') + if not os.path.exists(initpy): + open(initpy, 'a').close() + + if exists(ranger.relpath_conf('colorschemes', scheme_name)): + scheme_supermodule = 'colorschemes' + elif exists(ranger.relpath('colorschemes', scheme_name)): + scheme_supermodule = 'ranger.colorschemes' + else: + scheme_supermodule = None # found no matching file. + + if scheme_supermodule is None: + print("ERROR: colorscheme not found, fall back to builtin scheme") + if ranger.arg.debug: + raise Exception("Cannot locate colorscheme!") + settings.colorscheme = ColorScheme() + else: + scheme_module = getattr(__import__(scheme_supermodule, + globals(), locals(), [scheme_name], 0), scheme_name) + assert ismodule(scheme_module) + if hasattr(scheme_module, 'Scheme') \ + and is_scheme(scheme_module.Scheme): + settings.colorscheme = scheme_module.Scheme() else: - for name, var in settings.colorscheme.__dict__.items(): + for name, var in scheme_module.__dict__.items(): if var != ColorScheme and is_scheme(var): settings.colorscheme = var() break else: raise Exception("The module contains no " \ "valid colorscheme!") - else: - raise Exception("Cannot locate colorscheme!") try: import apps |