summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-06 14:01:49 +0100
committerhut <hut@lavabit.com>2009-12-06 14:01:49 +0100
commit65cb1a32d4d5f83f48169846afec201ab3b6f9c3 (patch)
treeccd022db0eedc2377e0318085174b96a20d614b9
parent465bff736d234e57efb2e9232df8882f3fd3a5cb (diff)
downloadranger-65cb1a32d4d5f83f48169846afec201ab3b6f9c3.tar.gz
"SettingsAware", random cleanups
-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: