summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ranger/__init__.py8
-rw-r--r--ranger/command.py9
-rw-r--r--ranger/conf/__init__.py49
-rw-r--r--ranger/defaults/options.py17
-rw-r--r--ranger/directory.py22
-rw-r--r--ranger/environment.py10
-rw-r--r--ranger/fm.py6
-rw-r--r--ranger/gui/colorscheme.py9
-rw-r--r--ranger/gui/ui.py4
-rw-r--r--ranger/gui/wconsole.py7
-rw-r--r--ranger/gui/wdisplay.py10
-rw-r--r--ranger/gui/widget.py3
-rw-r--r--ranger/helper.py17
-rw-r--r--ranger/main.py27
14 files changed, 115 insertions, 83 deletions
diff --git a/ranger/__init__.py b/ranger/__init__.py
index e69de29b..d2716895 100644
--- a/ranger/__init__.py
+++ b/ranger/__init__.py
@@ -0,0 +1,8 @@
+import os
+import sys
+
+confdir = os.path.expanduser('~/.ranger')
+rangerdir = os.path.dirname(__file__)
+
+sys.path.append(confdir)
+
diff --git a/ranger/command.py b/ranger/command.py
index 389f0df5..a252fea2 100644
--- a/ranger/command.py
+++ b/ranger/command.py
@@ -1,4 +1,4 @@
-class CommandList():
+class CommandList(object):
 	def __init__(self):
 		self.commandlist = []
 		self.paths = {}
@@ -7,7 +7,7 @@ class CommandList():
 
 	# We need to know when to clear the keybuffer (when a wrong key is pressed)
 	# and when to wait for the rest of the key combination. For "gg" we
-	# will assign "g" to a dummy which tells the program not to do the latter.
+	# will assign "g" to a dummy which tells the program to do the latter.
 	def rebuild_paths(self):
 		""" fill the path dictionary with dummie objects """
 		if self.dummies_in_paths:
@@ -70,12 +70,9 @@ class CommandList():
 		for key in keys:
 			self.paths[key] = cmd
 	
-class Command():
+class Command(object):
 	def __init__(self, fnc, keys):
 		self.execute = fnc
 		self.keys = keys
 		self.commandlist = None
 
-#	def execute(self, fm):
-#		self.fnc(fm)
-
diff --git a/ranger/conf/__init__.py b/ranger/conf/__init__.py
index 4c02ae0a..626fe98e 100644
--- a/ranger/conf/__init__.py
+++ b/ranger/conf/__init__.py
@@ -1,7 +1,14 @@
+from inspect import isclass, ismodule
+from ranger.helper import OpenStruct
+from ranger.gui.colorscheme import ColorScheme
 
-import sys, os
-sys.path[0:0] = [os.path.expanduser('~/.ranger')]
+ALLOWED_SETTINGS = """
+show_hidden scroll_offset directories_first
+preview_files max_history_size colorscheme
+""".split()
 
+# -- import the options --
+# either use the custom file or the default file
 try:
 	import keys
 except ImportError:
@@ -12,7 +19,41 @@ try:
 except ImportError:
 	from ranger.defaults import apps
 
+# overwrite single default options with custom options
+from ranger.defaults import options
 try:
-	import options
+	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("Following option was not defined: " + setting)
 except ImportError:
-	from ranger.defaults import options
+	pass
+
+# 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):
+	pass # everything ok
+
+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 given colorscheme module contains no valid colorscheme!")
+
+else:
+	raise Exception("Cannot locate colorscheme!")
+
+
+# -- globalize the settings --
+class SettingsAware(object):
+	settings = OpenStruct()
+
+for setting in ALLOWED_SETTINGS:
+	SettingsAware.settings[setting] = getattr(options, setting)
+
diff --git a/ranger/defaults/options.py b/ranger/defaults/options.py
index 874cf9fb..8d44089f 100644
--- a/ranger/defaults/options.py
+++ b/ranger/defaults/options.py
@@ -2,17 +2,10 @@ from ranger import colorschemes
 
 colorscheme = colorschemes.snow
 
-def get():
-	""" to be implemented. read the options from a file. """
-	pass
+show_hidden = False
 
-def dummy():
-	""" provide a way of getting options until get() is implemented """
-	return {
-		'show_hidden': False,
-		'scroll_offset': 2,
-		'directories_first': True,
-		'preview_files' : False,
-		'max_history_size': 20
-	}
+scroll_offset = 2
+directories_first = True
+preview_files = False
+max_history_size = 20
 
diff --git a/ranger/directory.py b/ranger/directory.py
index d8aa3b71..113cf792 100644
--- a/ranger/directory.py
+++ b/ranger/directory.py
@@ -4,6 +4,7 @@ from ranger.file import File
 
 from ranger.fsobject import BAD_INFO
 from ranger.fsobject import FileSystemObject as SuperClass
+from ranger.conf import SettingsAware
 
 def sort_by_basename(path):
 	return path.basename
@@ -14,7 +15,7 @@ def sort_by_directory(path):
 class NoDirectoryGiven(Exception):
 	pass
 
-class Directory(SuperClass):
+class Directory(SuperClass, SettingsAware):
 	def __init__(self, path):
 		from os.path import isdir
 
@@ -33,12 +34,9 @@ class Directory(SuperClass):
 		self.pointed_file = None
 		self.scroll_begin = 0
 
-		self.show_hidden = False
-		self.directories_first = True
-
 		# to find out if something has changed:
-		self.old_show_hidden = self.show_hidden
-		self.old_directories_first = None #self.directories_first
+		self.old_show_hidden = self.settings.show_hidden
+		self.old_directories_first = self.settings.directories_first
 	
 	def load_content(self):
 		from os.path import join, isdir, basename
@@ -50,7 +48,7 @@ class Directory(SuperClass):
 		if self.exists and self.runnable:
 			filenames = []
 			for fname in listdir(self.path):
-				if not self.show_hidden and fname[0] == '.':
+				if not self.settings.show_hidden and fname[0] == '.':
 					continue
 				if isinstance(self.filter, str) and self.filter in fname:
 					continue
@@ -88,7 +86,7 @@ class Directory(SuperClass):
 		old_pointed_file = self.pointed_file
 		self.files.sort(key = sort_by_basename)
 
-		if self.directories_first:
+		if self.settings.directories_first:
 			self.files.sort(key = sort_by_directory)
 
 		if self.pointed_index is not None:
@@ -96,10 +94,10 @@ class Directory(SuperClass):
 		else:
 			self.correct_pointer()
 
-		self.old_directories_first = self.directories_first
+		self.old_directories_first = self.settings.directories_first
 	
 	def sort_if_outdated(self):
-		if self.old_directories_first != self.directories_first:
+		if self.old_directories_first != self.settings.directories_first:
 			self.sort()
 
 	# Notice: fm.env.cf should always point to the current file. If you
@@ -185,8 +183,8 @@ class Directory(SuperClass):
 	def load_content_if_outdated(self):
 		if self.load_content_once(): return True
 
-		if self.old_show_hidden != self.show_hidden:
-			self.old_show_hidden = self.show_hidden
+		if self.old_show_hidden != self.settings.show_hidden:
+			self.old_show_hidden = self.settings.show_hidden
 			self.load_content()
 			return True
 
diff --git a/ranger/environment.py b/ranger/environment.py
index d8a535ab..a5e54414 100644
--- a/ranger/environment.py
+++ b/ranger/environment.py
@@ -1,13 +1,13 @@
 from os.path import abspath, normpath, join, expanduser
 from ranger.directory import Directory, NoDirectoryGiven
+from ranger.conf import SettingsAware
 
-class Environment():
+class Environment(SettingsAware):
 	# A collection of data which is relevant for more than
 	# one class.
-	def __init__(self, path, opt):
+	def __init__(self, path):
 		from ranger.history import History
 		self.path = abspath(expanduser(path))
-		self.opt = opt
 		self.pathway = ()
 		self.last_search = None
 		self.directories = {}
@@ -16,7 +16,7 @@ class Environment():
 		self.keybuffer = ()
 		self.copy = None
 		self.termsize = (24, 80)
-		self.history = History(opt['max_history_size'])
+		self.history = History(self.settings.max_history_size)
 
 	def key_append(self, key):
 		self.keybuffer += (key, )
@@ -93,7 +93,7 @@ class Environment():
 		self.assign_correct_cursor_positions()
 
 		# set the current file.
-		self.pwd.directories_first = self.opt['directories_first']
+		self.pwd.directories_first = self.settings.directories_first
 		self.pwd.sort_if_outdated()
 		self.cf = self.pwd.pointed_file
 
diff --git a/ranger/fm.py b/ranger/fm.py
index b481086a..3fcfb92f 100644
--- a/ranger/fm.py
+++ b/ranger/fm.py
@@ -3,7 +3,7 @@ from os import devnull
 from ranger.conf import apps
 null = open(devnull, 'a')
 
-class FM():
+class FM(object):
 	def __init__(self, environment, ui, bookmarks):
 		self.env = environment
 		self.ui = ui
@@ -126,6 +126,6 @@ class FM():
 		self.enter_dir(old_path)
 
 	def toggle_boolean_option(self, string):
-		if isinstance(self.env.opt[string], bool):
-			self.env.opt[string] ^= True
+		if isinstance(self.env.settings[string], bool):
+			self.env.settings[string] ^= True
 
diff --git a/ranger/gui/colorscheme.py b/ranger/gui/colorscheme.py
index 790112ec..3e28106b 100644
--- a/ranger/gui/colorscheme.py
+++ b/ranger/gui/colorscheme.py
@@ -5,9 +5,6 @@ CONTEXT_KEYS = [ 'reset', 'error',
 		'video', 'audio', 'image', 'media', 'document', 'container',
 		'broken', 'selected', 'empty', 'maindisplay']
 
-class ColorSchemeContext():
-	pass
-
 # colorscheme specification:
 #
 # A colorscheme must...
@@ -30,6 +27,8 @@ class ColorSchemeContext():
 # If your colorscheme-file contains more than one colorscheme, specify it with:
 # colorscheme = colorschemes.filename.classname
 
+from ranger.helper import OpenStruct
+
 class ColorScheme(object):
 	def __init__(self):
 		self.cache = {}
@@ -39,10 +38,10 @@ class ColorScheme(object):
 			return self.cache[keys]
 
 		except KeyError:
-			context = ColorSchemeContext()
+			context = OpenStruct()
 
 			for key in CONTEXT_KEYS:
-				context.__dict__[key] = (key in keys)
+				context[key] = (key in keys)
 
 			color = self.use(context)
 			self.cache[keys] = color
diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py
index 5cc519b1..b5ae78dc 100644
--- a/ranger/gui/ui.py
+++ b/ranger/gui/ui.py
@@ -1,6 +1,6 @@
 import curses
 
-class MouseEvent():
+class MouseEvent(object):
 	import curses
 	PRESSED = [ 0,
 			curses.BUTTON1_PRESSED,
@@ -17,7 +17,7 @@ class MouseEvent():
 		except:
 			return False
 
-class UI():
+class UI(object):
 	def __init__(self, env, commandlist, colorscheme):
 		import os
 		os.environ['ESCDELAY'] = '25' # don't know a cleaner way
diff --git a/ranger/gui/wconsole.py b/ranger/gui/wconsole.py
index 9078d781..a0322c42 100644
--- a/ranger/gui/wconsole.py
+++ b/ranger/gui/wconsole.py
@@ -158,6 +158,13 @@ class WConsole(SuperClass):
 		pass
 
 def get_app_flags_mode(line, fm):
+	""" extracts the application, flags and mode from a string entered into the "openwith_quick" console. """
+	# examples:
+	# "mplayer d 1" => ("mplayer", "d", 1)
+	# "aunpack 4" => ("aunpack", "", 4)
+	# "p" => ("", "p", 0)
+	# "" => None
+
 	app = ''
 	flags = ''
 	mode = 0
diff --git a/ranger/gui/wdisplay.py b/ranger/gui/wdisplay.py
index 07111437..fd4f7dec 100644
--- a/ranger/gui/wdisplay.py
+++ b/ranger/gui/wdisplay.py
@@ -10,10 +10,6 @@ class WDisplay(SuperClass):
 
 	def feed_env(self, env):
 		self.target = env.at_level(self.level)
-		self.show_hidden = env.opt['show_hidden']
-		self.scroll_offset = env.opt['scroll_offset']
-		self.directories_first = env.opt['directories_first']
-		self.preview_files = env.opt['preview_files']
 		
 	def click(self, event, fm):
 		from ranger.fsobject import T_DIRECTORY
@@ -59,7 +55,7 @@ class WDisplay(SuperClass):
 			self.win.addnstr(self.y, self.x, "not accessible", self.wid)
 			return
 		
-		if self.preview_files:
+		if self.settings.preview_files:
 			try:
 				if self.target.size < 1024 * 20:
 					f = open(self.target.path, 'r')
@@ -74,9 +70,7 @@ class WDisplay(SuperClass):
 		import curses
 		import stat
 
-		self.target.show_hidden = self.show_hidden
 		self.target.load_content_if_outdated()
-		self.target.directories_first = self.directories_first
 		self.target.sort_if_outdated()
 
 		base_color = ['in_display']
@@ -140,7 +134,7 @@ class WDisplay(SuperClass):
 			self.color_reset()
 
 	def get_scroll_begin(self):
-		offset = self.scroll_offset
+		offset = self.settings.scroll_offset
 		dirsize = len(self.target)
 		winsize = self.hei
 		halfwinsize = winsize // 2
diff --git a/ranger/gui/widget.py b/ranger/gui/widget.py
index fa1ca585..071b88bc 100644
--- a/ranger/gui/widget.py
+++ b/ranger/gui/widget.py
@@ -8,7 +8,8 @@ def combine(keylist, keys):
 	else:
 		return tuple((keylist, ) + keys)
 
-class Widget():
+from ranger.conf import SettingsAware
+class Widget(SettingsAware):
 	def __init__(self, win, colorscheme):
 		self.win = win
 		self.focused = False
diff --git a/ranger/helper.py b/ranger/helper.py
index 0a457837..086d7b09 100644
--- a/ranger/helper.py
+++ b/ranger/helper.py
@@ -12,6 +12,23 @@ ONE_KB = 1024
 UNITS = tuple('BKMGTP')
 NINE_THOUSAND = len(UNITS) - 1
 
+class OpenStruct(object):
+	def __init__(self, __dictionary=None, **__keywords):
+		if __dictionary:
+			self.__dict__.update(__dictionary)
+		if __keywords:
+			self.__dict__.update(keywords)
+
+	def __getitem__(self, key):
+		return self.__dict__[key]
+	
+	def __setitem__(self, key, value):
+		self.__dict__[key] = value
+		return value
+
+	def __contains__(self, key):
+		return key in self.__dict__
+
 def get_all(dirname):
 	import os
 	lst = []
diff --git a/ranger/main.py b/ranger/main.py
index 960bab65..9faf069f 100644
--- a/ranger/main.py
+++ b/ranger/main.py
@@ -1,6 +1,5 @@
 import sys
 import os
-from inspect import isclass, ismodule
 from locale import setlocale, LC_ALL
 from optparse import OptionParser, SUPPRESS_HELP
 
@@ -62,35 +61,13 @@ def main():
 	else:
 		path = '.'
 
-	opt = options.dummy()
-
-	# get colorscheme
-	scheme = options.colorscheme
-	if isclass(scheme) and issubclass(scheme, ColorScheme):
-		colorscheme = scheme()
-
-	elif ismodule(scheme):
-		for var_name in dir(scheme):
-			var = getattr(scheme, var_name)
-			if var != ColorScheme and isclass(var) and\
-					issubclass(var, ColorScheme):
-				colorscheme = var()
-				break
-		else:
-			print("The given colorscheme module contains no valid colorscheme!")
-			sys.exit(1)
-
-	else:
-		print("Cannot locate colorscheme!")
-		sys.exit(1)
-
-	env = Environment(path, opt)
+	env = Environment(path)
 	commandlist = CommandList()
 	keys.initialize_commands(commandlist)
 	bookmarks = Bookmarks()
 	bookmarks.load()
 
-	my_ui = UI(env, commandlist, colorscheme)
+	my_ui = UI(env, commandlist, options.colorscheme())
 	my_fm = FM(env, my_ui, bookmarks)
 
 	try:
080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715