about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-03-17 18:30:26 +0100
committerhut <hut@lavabit.com>2010-03-17 18:30:26 +0100
commita3528772e7970cdd894b9f22400e9bee6a53ff29 (patch)
tree9dd95b6a907487e9c4fd828d6c5f06fb593dccf0
parent165bde5607a718fc276142a3bc0fa0b75873634c (diff)
downloadranger-a3528772e7970cdd894b9f22400e9bee6a53ff29.tar.gz
colorschemes: more efficient and simple color importing.
-rw-r--r--ranger/api/options.py14
-rw-r--r--ranger/colorschemes/__init__.py28
-rw-r--r--ranger/defaults/options.py2
-rw-r--r--ranger/shared/settings.py53
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