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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# 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/>.
import os
import types
from inspect import isclass, ismodule
import ranger
from ranger.ext.openstruct import OpenStruct
from ranger.gui.colorscheme import ColorScheme
ALLOWED_SETTINGS = {
'show_hidden': bool,
'show_cursor': bool,
'autosave_bookmarks': bool,
'collapse_preview': bool,
'sort': str,
'reverse': bool,
'directories_first': bool,
'update_title': bool,
'shorten_title': int, # Note: False is an instance of int
'max_filesize_for_preview': (int, type(None)),
'max_history_size': (int, type(None)),
'scroll_offset': int,
'preview_files': bool,
'flushinput': bool,
'colorscheme': str,
'hidden_filter': lambda x: isinstance(x, str) or hasattr(x, 'match'),
}
# -- globalize the settings --
class SettingsAware(object):
settings = OpenStruct()
@staticmethod
def _setup():
settings = OpenStruct()
from ranger.defaults import options
for setting in ALLOWED_SETTINGS:
try:
settings[setting] = getattr(options, setting)
except AttributeError:
raise Exception("The option `{0}' was not defined" \
" in the defaults!".format(setting))
import sys
if not ranger.arg.clean:
# overwrite single default options with custom options
try:
import options as my_options
except ImportError:
pass
else:
for setting in ALLOWED_SETTINGS:
try:
settings[setting] = getattr(my_options, setting)
except AttributeError:
pass
assert check_option_types(settings)
# 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.
scheme_name = settings.colorscheme
def exists(colorscheme):
return os.path.exists(colorscheme + '.py')
def is_scheme(x):
return isclass(x) and issubclass(x, ColorScheme)
# 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 scheme_module.__dict__.items():
if var != ColorScheme and is_scheme(var):
settings.colorscheme = var()
break
else:
raise Exception("The module contains no " \
"valid colorscheme!")
try:
import apps
except ImportError:
from ranger.defaults import apps
settings.apps = apps
try:
import keys
except ImportError:
from ranger.defaults import keys
settings.keys = keys
SettingsAware.settings = settings
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!"\
" Got " + str(type(optvalue)) + ", expected " + str(typ) + "!"
return True
|