summary refs log tree commit diff stats
path: root/ranger/shared/settings.py
blob: 36728b48add06ac454c6fea06fb440b32fea1621 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Copyright (C) 2009, 2010  Roman Zimbelmann <romanz@lavabit.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from ranger.ext.openstruct import OpenStruct

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'),
}

# -- globalize the settings --
class SettingsAware(object):
	settings = OpenStruct()

	@staticmethod
	def _setup():
		from inspect import isclass, ismodule
		from ranger.gui.colorscheme import ColorScheme

		# overwrite single default options with custom options
		from ranger.defaults import options
		try:
			import options as custom_options
			for setting in ALLOWED_SETTINGS:
				if hasattr(custom_options, setting):
					setattr(options, setting, getattr(custom_options, setting))
				elif not hasattr(options, setting):
					raise Exception("This option was not defined: " + setting)
		except ImportError:
			pass

		assert check_option_types(options)

		try:
			import apps
		except ImportError:
			from ranger.defaults import apps

		try:
			import keys
		except ImportError:
			from ranger.defaults import keys


		# If a module is specified as the colorscheme, replace it with one
		# valid colorscheme inside that module.

		if isclass(options.colorscheme) and \
				issubclass(options.colorscheme, ColorScheme):
			options.colorscheme = options.colorscheme()

		elif ismodule(options.colorscheme):
			for var_name in dir(options.colorscheme):
				var = getattr(options.colorscheme, var_name)
				if var != ColorScheme and isclass(var) \
						and issubclass(var, ColorScheme):
					options.colorscheme = var()
					break
			else:
				raise Exception("The module contains no valid colorscheme!")

		else:
			raise Exception("Cannot locate colorscheme!")

		for setting in ALLOWED_SETTINGS:
			SettingsAware.settings[setting] = getattr(options, setting)

		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
an class="p">>Classes</strong></big></font></td></tr> <tr><td bgcolor="#ee77aa"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td> <td width="100%"><dl> <dt><font face="helvetica, arial"><a href="builtins.html#Exception">builtins.Exception</a>(<a href="builtins.html#BaseException">builtins.BaseException</a>) </font></dt><dd> <dl> <dt><font face="helvetica, arial"><a href="ranger.fsobject.html#NotLoadedYet">NotLoadedYet</a> </font></dt></dl> </dd> </dl> <p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#ffc8d8"> <td colspan=3 valign=bottom>&nbsp;<br> <font color="#000000" face="helvetica, arial"><a name="NotLoadedYet">class <strong>NotLoadedYet</strong></a>(<a href="builtins.html#Exception">builtins.Exception</a>)</font></td></tr> <tr><td bgcolor="#ffc8d8"><tt>&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td> <td width="100%"><dl><dt>Method resolution order:</dt> <dd><a href="ranger.fsobject.html#NotLoadedYet">NotLoadedYet</a></dd> <dd><a href="builtins.html#Exception">builtins.Exception</a></dd> <dd><a href="builtins.html#BaseException">builtins.BaseException</a></dd> <dd><a href="builtins.html#object">builtins.object</a></dd> </dl> <hr> Data descriptors defined here:<br> <dl><dt><strong>__weakref__</strong></dt> <dd><tt>list&nbsp;of&nbsp;weak&nbsp;references&nbsp;to&nbsp;the&nbsp;object&nbsp;(if&nbsp;defined)</tt></dd> </dl> <hr> Methods inherited from <a href="builtins.html#Exception">builtins.Exception</a>:<br> <dl><dt><a name="NotLoadedYet-__init__"><strong>__init__</strong></a>(...)</dt><dd><tt>x.<a href="#NotLoadedYet-__init__">__init__</a>(...)&nbsp;initializes&nbsp;x;&nbsp;see&nbsp;x.__class__.__doc__&nbsp;for&nbsp;signature</tt></dd></dl> <hr> Data and other attributes inherited from <a href="builtins.html#Exception">builtins.Exception</a>:<br> <dl><dt><strong>__new__</strong> = &lt;built-in method __new__ of type object at 0x7f6a9223e120&gt;<dd><tt>T.<a href="#NotLoadedYet-__new__">__new__</a>(S,&nbsp;...)&nbsp;-&gt;&nbsp;a&nbsp;new&nbsp;object&nbsp;with&nbsp;type&nbsp;S,&nbsp;a&nbsp;subtype&nbsp;of&nbsp;T</tt></dl> <hr> Methods inherited from <a href="builtins.html#BaseException">builtins.BaseException</a>:<br> <dl><dt><a name="NotLoadedYet-__delattr__"><strong>__delattr__</strong></a>(...)</dt><dd><tt>x.<a href="#NotLoadedYet-__delattr__">__delattr__</a>('name')&nbsp;&lt;==&gt;&nbsp;del&nbsp;x.name</tt></dd></dl> <dl><dt><a name="NotLoadedYet-__getattribute__"><strong>__getattribute__</strong></a>(...)</dt><dd><tt>x.<a href="#NotLoadedYet-__getattribute__">__getattribute__</a>('name')&nbsp;&lt;==&gt;&nbsp;x.name</tt></dd></dl> <dl><dt><a name="NotLoadedYet-__reduce__"><strong>__reduce__</strong></a>(...)</dt></dl> <dl><dt><a name="NotLoadedYet-__repr__"><strong>__repr__</strong></a>(...)</dt><dd><tt>x.<a href="#NotLoadedYet-__repr__">__repr__</a>()&nbsp;&lt;==&gt;&nbsp;repr(x)</tt></dd></dl> <dl><dt><a name="NotLoadedYet-__setattr__"><strong>__setattr__</strong></a>(...)</dt><dd><tt>x.<a href="#NotLoadedYet-__setattr__">__setattr__</a>('name',&nbsp;value)&nbsp;&lt;==&gt;&nbsp;x.name&nbsp;=&nbsp;value</tt></dd></dl> <dl><dt><a name="NotLoadedYet-__setstate__"><strong>__setstate__</strong></a>(...)</dt></dl> <dl><dt><a name="NotLoadedYet-__str__"><strong>__str__</strong></a>(...)</dt><dd><tt>x.<a href="#NotLoadedYet-__str__">__str__</a>()&nbsp;&lt;==&gt;&nbsp;str(x)</tt></dd></dl> <dl><dt><a name="NotLoadedYet-with_traceback"><strong>with_traceback</strong></a>(...)</dt><dd><tt><a href="builtins.html#Exception">Exception</a>.<a href="#NotLoadedYet-with_traceback">with_traceback</a>(tb)&nbsp;--<br> set&nbsp;self.<strong>__traceback__</strong>&nbsp;to&nbsp;tb&nbsp;and&nbsp;return&nbsp;self.</tt></dd></dl> <hr> Data descriptors inherited from <a href="builtins.html#BaseException">builtins.BaseException</a>:<br> <dl><dt><strong>__cause__</strong></dt> <dd><tt>exception&nbsp;cause</tt></dd> </dl> <dl><dt><strong>__context__</strong></dt> <dd><tt>exception&nbsp;context</tt></dd> </dl> <dl><dt><strong>__dict__</strong></dt> </dl> <dl><dt><strong>__traceback__</strong></dt> </dl> <dl><dt><strong>args</strong></dt> </dl> </td></tr></table></td></tr></table><p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#55aa55"> <td colspan=3 valign=bottom>&nbsp;<br> <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> <tr><td bgcolor="#55aa55"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td> <td width="100%"><strong>BAD_INFO</strong> = None<br> <strong>T_DIRECTORY</strong> = 'directory'<br> <strong>T_FILE</strong> = 'file'<br> <strong>T_NONEXISTANT</strong> = 'nonexistant'<br> <strong>T_UNKNOWN</strong> = 'unknown'</td></tr></table> </body></html>