From 24cd97b78d90b3b7311381d1c4bab7160d14604f Mon Sep 17 00:00:00 2001 From: hut Date: Thu, 25 Feb 2010 16:44:22 +0100 Subject: settings: check the type of options --- ranger/shared/settings.py | 46 +++++++++++++++++++++++++++++++++++----------- 1 file 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 -- cgit 1.4.1-2-gfad0