summary refs log tree commit diff stats
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
parent7a268c8be8896d3ec71952a6fd261012e39c1c1a (diff)
downloadranger-24cd97b78d90b3b7311381d1c4bab7160d14604f.tar.gz
settings: check the type of options
-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
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417