about summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-02-25 16:44:22 +0100
committerhut <hut@lavabit.com>2010-02-25 20:37:11 +0100
commit24cd97b78d90b3b7311381d1c4bab7160d14604f (patch)
treefb26b1f2f704d38c38a3c57c22e87f48aedc48c9 /ranger
parent7a268c8be8896d3ec71952a6fd261012e39c1c1a (diff)
downloadranger-24cd97b78d90b3b7311381d1c4bab7160d14604f.tar.gz
settings: check the type of options
Diffstat (limited to 'ranger')
-rw-r--r--ranger/shared/settings.py46
1 files changed, 35 insertions, 11 deletions
diff --git a/ranger/shared/settings.py b/ranger/shared/settings.py
index 152dfbd1..580c258e 100644
--- a/ranger/shared/settings.py
+++ b/ranger/shared/settings.py
@@ -12,19 +12,27 @@
 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
-ALLOWED_SETTINGS = """
-show_hidden scroll_offset show_cursor
-directories_first sort reverse
-preview_files max_history_size colorscheme
-collapse_preview update_title
-hidden_filter flushinput
-autosave_bookmarks
-max_filesize_for_preview
-""".split()
+from ranger.ext.openstruct import OpenStruct
 
-# -- globalize the settings --
+ALLOWED_SETTINGS = {
+	'show_hidden': bool,
+	'show_cursor': bool,
+	'autosave_bookmarks': bool,
+	'collapse_preview': bool,
+	'sort': str,
+	'reverse': bool,
+	'directories_first': bool,
+	'update_title': bool,
+	'max_filesize_for_preview': (int, type(None)),
+	'max_history_size': (int, type(None)),
+	'scroll_offset': int,
+	'preview_files': bool,
+	'flushinput': bool,
+	'colorscheme': object,
+	'hidden_filter': lambda x: isinstance(x, str) or hasattr(x, 'match'),
+}
 
-from ranger.ext.openstruct import OpenStruct
+# -- globalize the settings --
 class SettingsAware(object):
 	settings = OpenStruct()
 
@@ -45,6 +53,8 @@ class SettingsAware(object):
 		except ImportError:
 			pass
 
+		assert check_option_types(options)
+
 		try:
 			import apps
 		except ImportError:
@@ -81,3 +91,17 @@ class SettingsAware(object):
 
 		SettingsAware.settings.keys = keys
 		SettingsAware.settings.apps = apps
+
+
+def check_option_types(opt):
+	import inspect
+	for name, typ in ALLOWED_SETTINGS.items():
+		optvalue = getattr(opt, name)
+		if inspect.isfunction(typ):
+			assert typ(optvalue), \
+				"The option `" + name + "' has an incorrect type!"
+		else:
+			assert isinstance(optvalue, typ), \
+				"The option `" + name + "' has an incorrect type!"\
+				" Expected " + str(typ) + "!"
+	return True