summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-11-28 17:29:57 +0100
committerhut <hut@lavabit.com>2009-11-28 17:29:57 +0100
commit3d566884280b1db24cb9fa13adde3985f0e7c5a2 (patch)
tree4e84f707b965d1a7bbebf84858a15df6d8c3ef92
parent419e4aa53fc99c29dce66cd46b7f894efd2d57a8 (diff)
downloadranger-3d566884280b1db24cb9fa13adde3985f0e7c5a2.tar.gz
restructuration
-rwxr-xr-xranger.py66
-rw-r--r--ranger/api.py (renamed from ranger/debug.py)2
-rw-r--r--ranger/cli.py32
-rw-r--r--ranger/command.py14
-rw-r--r--ranger/conf/__init__.py0
-rw-r--r--ranger/conf/keys.py54
-rw-r--r--ranger/conf/options.py (renamed from ranger/options.py)0
-rw-r--r--ranger/directory.py7
-rw-r--r--ranger/environment.py13
-rw-r--r--ranger/file.py6
-rw-r--r--ranger/fm.py7
-rw-r--r--ranger/fsobject.py33
-rw-r--r--ranger/fstype.py5
-rw-r--r--ranger/gui/__init__.py0
-rw-r--r--ranger/gui/color.py (renamed from ranger/color.py)0
-rw-r--r--ranger/gui/defaultui.py (renamed from ranger/defaultui.py)16
-rw-r--r--ranger/gui/ui.py (renamed from ranger/ui.py)4
-rw-r--r--ranger/gui/wdisplay.py (renamed from ranger/wdisplay.py)8
-rw-r--r--ranger/gui/widget.py (renamed from ranger/widget.py)2
-rw-r--r--ranger/gui/wtitlebar.py (renamed from ranger/wtitlebar.py)3
-rw-r--r--ranger/keys.py69
21 files changed, 133 insertions, 208 deletions
diff --git a/ranger.py b/ranger.py
index a7a1a864..9e8dbd70 100755
--- a/ranger.py
+++ b/ranger.py
@@ -4,6 +4,9 @@
 # An embedded shell script. Assuming this file is /usr/bin/ranger,
 # this hack allows you to use the cd-after-exit feature by typing:
 # source ranger ranger
+# Now when you quit ranger, it should change the directory of the
+# parent shell to where you have last been in ranger.
+# Works with at least bash and zsh.
 """":
 if [ $1 ]; then
 	cd "`$1 --cd-after-exit $@ 3>&1 1>&2 2>&3 3>&-`"
@@ -12,11 +15,14 @@ else
 fi
 return 1
 """
+from ranger.fm import FM
+from ranger.environment import Environment
+from ranger.command import CommandList
+from ranger.conf import keys, options
+from ranger.gui.defaultui import DefaultUI as UI
 
-import sys, os
+import sys, os, locale
 
-# Change the directory of the parent shell after exiting Ranger.
-# Read the comments in wrapper.sh for more info.
 try:
 	assert sys.argv[1] == '--cd-after-exit'
 	cd_after_exit = True
@@ -25,39 +31,33 @@ try:
 except:
 	cd_after_exit = False
 
-from ranger import debug, fm, options, environment, command, keys
-from ranger.defaultui import DefaultUI as UI
-
 # TODO: Parse arguments
 
 # TODO: load config
 
-def main():
-	import locale, os
-	os.stat_float_times(True)
-	locale.setlocale(locale.LC_ALL, 'en_US.utf8')
-
-	try:
-		path = os.path.abspath('.')
-		opt = options.dummy()
-		env = environment.Environment(opt)
-		commandlist = command.CommandList()
-		keys.initialize_commands(commandlist)
-
-		my_ui = UI(env, commandlist)
-		my_fm = fm.FM(env)
-		my_fm.feed(path, my_ui)
-		my_fm.run()
-
-	except BaseException as original_error:
-		try: my_ui.exit()
-		except: pass
-	
-		raise original_error
+os.stat_float_times(True)
+locale.setlocale(locale.LC_ALL, 'en_US.utf8')
 
-	finally:
-		if cd_after_exit:
-			try: sys.__stderr__.write(env.pwd.path)
-			except: pass
+try:
+	path = os.path.abspath('.')
+	opt = options.dummy()
+	env = Environment(opt)
+	commandlist = CommandList()
+	keys.initialize_commands(commandlist)
+
+	my_ui = UI(env, commandlist)
+	my_fm = FM(env)
+	my_fm.feed(path, my_ui)
+	my_fm.run()
+
+except BaseException as original_error:
+	try: my_ui.exit()
+	except: pass
+
+	raise original_error
+
+finally:
+	if cd_after_exit:
+		try: sys.__stderr__.write(env.pwd.path)
+		except: pass
 
-if __name__ == "__main__": main()
diff --git a/ranger/debug.py b/ranger/api.py
index 12d5d654..b820e085 100644
--- a/ranger/debug.py
+++ b/ranger/api.py
@@ -1,4 +1,3 @@
-# a module to faciliate debuggin
 
 LOGFILE = '/tmp/errorlog'
 
@@ -9,4 +8,3 @@ def log(txt):
 	f.write("\n")
 	f.close()
 
-
diff --git a/ranger/cli.py b/ranger/cli.py
deleted file mode 100644
index 7f8fd77f..00000000
--- a/ranger/cli.py
+++ /dev/null
@@ -1,32 +0,0 @@
-import curses
-import _thread
-
-class CLIError(): pass
-
-class CLI():
-	def __init__(self):
-		self.lock = _thread.allocalte_lock()
-		self.running = False
-
-	def start(self):
-		with self.lock:
-			stdscr = curses.initscr()
-			self.running = True
-
-	def exit(self):
-		self.stop_unless_running()
-		with self.lock:
-			self.running = False
-			curses.nocbreak()
-			stdscr.keypad(1)
-			curses.endwin()
-
-	def stop_unless_running(self):
-		if not self.running:
-			raise CLIError("This function needs the cli to be runnig!")
-
-	def print(self, text, x=0, y=0, attr=None):
-		with self.lock:
-
-	
-
diff --git a/ranger/command.py b/ranger/command.py
index f8c9f146..5e6c74d1 100644
--- a/ranger/command.py
+++ b/ranger/command.py
@@ -1,12 +1,9 @@
-class CommandDummy():
-	pass
-
 class CommandList():
 	def __init__(self):
 		self.commandlist = []
 		self.paths = {}
 		self.dummies_in_paths = False
-		self.dummy_object = CommandDummy
+		self.dummy_object = None
 
 	# 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
@@ -69,15 +66,6 @@ class Command():
 		self.keys = keys
 		self.commandlist = None
 
-#	def rebind(keys):
-#		self.keys = keys
-#		self.commandlist.rebuild_paths()
-
 	def execute(self, fm):
 		self.fnc(fm)
 
-if __name__ == '__main__':
-	cl = CommandList()
-	cl.initialize_commands()
-
-	print(cl.paths)
diff --git a/ranger/conf/__init__.py b/ranger/conf/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/ranger/conf/__init__.py
diff --git a/ranger/conf/keys.py b/ranger/conf/keys.py
new file mode 100644
index 00000000..7c08ae4f
--- /dev/null
+++ b/ranger/conf/keys.py
@@ -0,0 +1,54 @@
+def initialize_commands(cl):
+	from ranger.fm import FM
+	from curses.ascii import ctrl
+	import curses
+
+	# syntax for binding keys: cl.bind(fnc, *keys)
+	# fnc is a function which is called with the FM instance,
+	# keys are one or more key-combinations which are either:
+	# * a string
+	# * an integer which represents an ascii value
+	# * a tuple of integers
+
+	def move(relative = 0, absolute = None):
+		return lambda fm: fm.move_pointer(
+				relative = relative, absolute = absolute)
+
+	def move_pages(n):
+		return lambda fm: fm.move_pointer_by_pages(n)
+
+	def toggle_option(string):
+		return lambda fm: fm.toggle_boolean_option(string)
+
+	def cd(path):
+		return lambda fm: fm.enter_dir(path)
+
+	cl.bind(FM.move_left,           'h', curses.KEY_BACKSPACE, 127)
+	cl.bind(FM.move_right,          'l', curses.KEY_ENTER, ctrl('j'))
+	cl.bind(move( relative = 1 ),   'j')
+	cl.bind(move_pages( 0.5 ),      'J')
+	cl.bind(move( relative = -1 ),  'k')
+	cl.bind(move_pages( -0.5 ),     'K')
+	cl.bind(move( absolute = 0 ),   'gg')
+	cl.bind(move( absolute = -1 ),  'G')
+
+	cl.bind(toggle_option('show_hidden'), 'th')
+
+	# key combinations which change the current directory
+	cl.bind(cd("~"),          'gh')
+	cl.bind(cd("/etc"),       'ge')
+	cl.bind(cd("/usr"),       'gu')
+	cl.bind(cd("/"),          'gr')
+	cl.bind(cd("/media"),     'gm')
+	cl.bind(cd("/mnt"),       'gn')
+	cl.bind(cd("~/.trash"),   'gt')
+	cl.bind(cd("/srv"),       'gs')
+
+	# system functions
+	cl.bind(FM.exit,      ctrl('D'), 'q', 'ZZ')
+	cl.bind(FM.reset,     ctrl('R'))
+	cl.bind(FM.redraw,    ctrl('L'))
+	cl.bind(FM.resize,    curses.KEY_RESIZE)
+
+	cl.rebuild_paths()
+
diff --git a/ranger/options.py b/ranger/conf/options.py
index 92e38c8d..92e38c8d 100644
--- a/ranger/options.py
+++ b/ranger/conf/options.py
diff --git a/ranger/directory.py b/ranger/directory.py
index 2cad7ca8..f9175778 100644
--- a/ranger/directory.py
+++ b/ranger/directory.py
@@ -1,7 +1,8 @@
 import ranger.fsobject
-from ranger import file, debug
+from ranger.file import File
+#from ranger.api import log
 
-from ranger.fsobject import FSObject as SuperClass
+from ranger.fsobject import FileSystemObject as SuperClass
 
 def sort_by_basename(path):
 	return path.basename
@@ -61,7 +62,7 @@ class Directory(SuperClass):
 				if isdir(name):
 					f = Directory(name)
 				else:
-					f = file.File(name)
+					f = File(name)
 				f.load()
 				files.append(f)
 
diff --git a/ranger/environment.py b/ranger/environment.py
index 4b53d25a..e3d9bb9f 100644
--- a/ranger/environment.py
+++ b/ranger/environment.py
@@ -1,4 +1,4 @@
-import os
+from os.path import abspath, normpath, join, expanduser
 from ranger.directory import Directory, NoDirectoryGiven
 
 class Environment():
@@ -36,8 +36,7 @@ class Environment():
 				return self.cf
 	
 	def get_directory(self, path):
-		import os
-		path = os.path.abspath(path)
+		path = abspath(path)
 		try:
 			return self.directories[path]
 		except KeyError:
@@ -46,8 +45,6 @@ class Environment():
 
 	def assign_correct_cursor_positions(self):
 		# Assign correct cursor positions for subdirectories
-		from ranger.debug import log
-
 		last_path = None
 		for path in reversed(self.pathway):
 			if last_path is None:
@@ -58,7 +55,6 @@ class Environment():
 			last_path = path
 
 	def enter_dir(self, path):
-		from os.path import normpath, join, expanduser
 		# get the absolute path
 		path = normpath(join(self.path, expanduser(path)))
 
@@ -80,7 +76,7 @@ class Environment():
 			pathway = []
 			currentpath = '/'
 			for dir in path.split('/'):
-				currentpath = os.path.join(currentpath, dir)
+				currentpath = join(currentpath, dir)
 				pathway.append(self.get_directory(currentpath))
 			self.pathway = tuple(pathway)
 
@@ -90,7 +86,6 @@ class Environment():
 		self.pwd.directories_first = self.opt['directories_first']
 		self.pwd.sort_if_outdated()
 		self.cf = self.pwd.pointed_file
-		from ranger.debug import log
-		log(self.cf)
 
 		return True
+
diff --git a/ranger/file.py b/ranger/file.py
index 94c62f9a..e95067b1 100644
--- a/ranger/file.py
+++ b/ranger/file.py
@@ -1,6 +1,4 @@
-import ranger.fsobject
-class File(ranger.fsobject.FSObject):
+from ranger.fsobject import FileSystemObject as SuperClass
+class File(SuperClass):
 	pass
-#	def __init__(self, path):
-#		fsobject.FSObject.__init__(self, path)
 
diff --git a/ranger/fm.py b/ranger/fm.py
index 8c6c7f07..c30fb92c 100644
--- a/ranger/fm.py
+++ b/ranger/fm.py
@@ -43,12 +43,6 @@ class FM():
 		except AttributeError:
 			pass
 
-#	def execute_file(self, path):
-#		import os
-#		self.ui.exit()
-#		os.system("mplayer '" + path + "'")
-#		self.ui.initialize()
-
 	def execute_file(self, path):
 		from subprocess import Popen
 		Popen(('mplayer', '-fs', path), stdout = null, stderr = null)
@@ -71,3 +65,4 @@ class FM():
 	def toggle_boolean_option(self, string):
 		if isinstance(self.env.opt[string], bool):
 			self.env.opt[string] ^= True
+
diff --git a/ranger/fsobject.py b/ranger/fsobject.py
index 91c73bfc..e482f5fa 100644
--- a/ranger/fsobject.py
+++ b/ranger/fsobject.py
@@ -1,14 +1,21 @@
-import ranger.fstype
+class NotLoadedYet(Exception):
+	pass
 
-class FrozenException(Exception): pass
-class NotLoadedYet(Exception): pass
+T_FILE = 'file'
+T_DIRECTORY = 'directory'
+T_UNKNOWN = 'unknown'
+T_NONEXISTANT = 'nonexistant'
+
+BAD_INFO = None
+
+class FileSystemObject(object):
 
-class FSObject(object):
-	BAD_INFO = None
 	def __init__(self, path):
-		if type(self) == FSObject:
-			raise TypeError("FSObject is an abstract class and cannot be initialized.")
+		if type(self) == FileSystemObject:
+			raise TypeError("FileSystemObject is an abstract class and cannot be initialized.")
+
 		from os.path import basename
+
 		self.path = path
 		self.basename = basename(path)
 		self.exists = False
@@ -23,7 +30,7 @@ class FSObject(object):
 		self.stat = None
 		self.infostring = None
 		self.permissions = None
-		self.type = ranger.fstype.Unknown
+		self.type = T_UNKNOWN
 	
 	def __str__(self):
 		return str(self.path)
@@ -41,27 +48,27 @@ class FSObject(object):
 			self.accessible = True
 
 			if os.path.isdir(self.path):
-				self.type = ranger.fstype.Directory
+				self.type = T_DIRECTORY
 				try:
 					self.size = len(os.listdir(self.path))
 					self.infostring = ' %d' % self.size
 					self.runnable = True
 				except OSError:
-					self.infostring = FSObject.BAD_INFO
+					self.infostring = BAD_INFO
 					self.runnable = False
 					self.accessible = False
 			elif os.path.isfile(self.path):
-				self.type = ranger.fstype.File
+				self.type = T_FILE
 				self.size = self.stat.st_size
 				self.infostring = ' %d' % self.stat.st_size
 			else:
-				self.type = ranger.fstype.Unknown
+				self.type = T_UNKNOWN
 				self.infostring = None
 
 		else:
 			self.islink = False
 			self.infostring = None
-			self.type = ranger.fstype.Nonexistent
+			self.type = T_NONEXISTANT
 			self.exists = False
 			self.runnable = False
 			self.accessible = False
diff --git a/ranger/fstype.py b/ranger/fstype.py
deleted file mode 100644
index 4bd0988d..00000000
--- a/ranger/fstype.py
+++ /dev/null
@@ -1,5 +0,0 @@
-class File: pass
-class Directory: pass
-class Nonexistent: pass
-class Unknown: pass
-
diff --git a/ranger/gui/__init__.py b/ranger/gui/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/ranger/gui/__init__.py
diff --git a/ranger/color.py b/ranger/gui/color.py
index 5701c0ee..5701c0ee 100644
--- a/ranger/color.py
+++ b/ranger/gui/color.py
diff --git a/ranger/defaultui.py b/ranger/gui/defaultui.py
index 36eee49d..4d4f6d44 100644
--- a/ranger/defaultui.py
+++ b/ranger/gui/defaultui.py
@@ -1,8 +1,10 @@
-import ranger.ui
-from ranger.wdisplay import WDisplay
-from ranger.wtitlebar import WTitleBar
+from ranger.gui.ui import UI as SuperClass
+from ranger.gui.wdisplay import WDisplay
+from ranger.gui.wtitlebar import WTitleBar
 
-class DefaultUI(ranger.ui.UI):
+RATIO = ( 0.15, 0.15, 0.4, 0.3 )
+
+class DefaultUI(SuperClass):
 	def setup(self):
 		self.titlebar = WTitleBar(self.win)
 		self.add_widget(self.titlebar)
@@ -17,16 +19,14 @@ class DefaultUI(ranger.ui.UI):
 		for disp in self.displays:
 			self.add_widget(disp)
 	
-	RATIO = ( 0.15, 0.15, 0.4, 0.3 )
-	
 	def resize(self):
-		ranger.ui.UI.resize(self)
+		SuperClass.resize(self)
 		y, x = self.win.getmaxyx()
 
 		leftborder = 0
 
 		i = 0
-		for ratio in DefaultUI.RATIO:
+		for ratio in RATIO:
 			wid = int(ratio * x)
 			try:
 				self.displays[i].setdim(1, leftborder, y-1, wid - 1)
diff --git a/ranger/ui.py b/ranger/gui/ui.py
index 6f98364b..2abf8939 100644
--- a/ranger/ui.py
+++ b/ranger/gui/ui.py
@@ -1,5 +1,5 @@
 import curses
-from ranger.debug import log
+from ranger.api import log
 class UI():
 	def __init__(self, env, commandlist):
 		self.env = env
@@ -40,7 +40,7 @@ class UI():
 
 	def press(self, key, fm):
 		self.env.key_append(key)
-#		log(self.env.keybuffer)
+		log(self.env.keybuffer)
 
 		try:
 			cmd = self.commandlist.paths[self.env.keybuffer]
diff --git a/ranger/wdisplay.py b/ranger/gui/wdisplay.py
index e5229c7d..95014d51 100644
--- a/ranger/wdisplay.py
+++ b/ranger/gui/wdisplay.py
@@ -1,8 +1,6 @@
-import ranger.widget
-from ranger.debug import log
-from ranger.color import color_pairs
+from ranger.gui.color import color_pairs
+from ranger.gui.widget import Widget as SuperClass
 import curses
-from ranger.widget import Widget as SuperClass
 #from ranger.color import color
 
 class WDisplay(SuperClass):
@@ -69,8 +67,6 @@ class WDisplay(SuperClass):
 			self.win.addnstr(self.y, self.x, "not accessible", self.wid)
 			return
 
-#		log(color_pairs)
-
 		self.set_scroll_begin()
 
 		selected_i = self.target.pointed_index
diff --git a/ranger/widget.py b/ranger/gui/widget.py
index e07db7a8..cb5fdc1f 100644
--- a/ranger/widget.py
+++ b/ranger/gui/widget.py
@@ -1,5 +1,5 @@
 import curses
-from ranger.color import color_pairs
+from ranger.gui.color import color_pairs
 
 class OutOfBoundsException(Exception): pass
 
diff --git a/ranger/wtitlebar.py b/ranger/gui/wtitlebar.py
index 231914e9..2ac8ae65 100644
--- a/ranger/wtitlebar.py
+++ b/ranger/gui/wtitlebar.py
@@ -1,6 +1,5 @@
 import curses
-import ranger.widget
-from ranger.widget import Widget as SuperClass
+from ranger.gui.widget import Widget as SuperClass
 
 class WTitleBar(SuperClass):
 	def feed_env(self, env):
diff --git a/ranger/keys.py b/ranger/keys.py
deleted file mode 100644
index c9b817e3..00000000
--- a/ranger/keys.py
+++ /dev/null
@@ -1,69 +0,0 @@
-def initialize_commands(command_list):
-	from ranger.fm import FM
-	from curses.ascii import ctrl
-	import curses
-
-	cl = command_list
-	
-	# syntax for binding keys: cl.bind(fnc, *keys)
-	# fnc is a function which is called with the FM instance,
-	# keys are one or more key-combinations which are either:
-	# * a string
-	# * an integer which represents an ascii value
-	# * a tuple of integers
-
-	def move(relative = 0, absolute = None):
-		return lambda fm: fm.move_pointer(
-				relative = relative, absolute = absolute)
-
-	def move_pages(n):
-		return lambda fm: fm.move_pointer_by_pages(n)
-
-	def toggle_option(string):
-		return lambda fm: fm.toggle_boolean_option(string)
-
-	def cd(path):
-		return lambda fm: fm.enter_dir(path)
-
-	cl.bind(FM.move_left, 'h', 195, 'back')
-	cl.bind(FM.move_right, 'l', 'forward')
-	cl.bind(move( relative = 1 ), 'j')
-	cl.bind(move_pages( 0.5 ), 'J')
-	cl.bind(move( relative = -1 ), 'k')
-	cl.bind(move_pages( -0.5 ), 'K')
-	cl.bind(move( absolute = 0 ), 'gg')
-	cl.bind(move( absolute = -1 ), 'G')
-
-	cl.bind(toggle_option('show_hidden'), 'th')
-
-
-	gX = {
-			'h': '~',
-			'e': '/etc',
-			'u': '/usr',
-			'r': '/',
-			'm': '/media',
-			'n': '/mnt',
-			't': '~/.trash',
-			's': '/srv',
-			}
-
-	for x, path in gX.items():
-		cl.bind( cd(path), 'g' + x )
-
-#	cl.bind(cd("~"), 'gh')
-#	cl.bind(cd("/etc"), 'ge')
-#	cl.bind(cd("/usr"), 'gu')
-#	cl.bind(cd("/"), 'gr')
-#	cl.bind(cd("/media"), 'gm')
-#	cl.bind(cd("/mnt"), 'gn')
-#	cl.bind(cd("~/.trash"), 'gt')
-#	cl.bind(cd("/srv"), 'gs')
-
-	cl.bind(FM.exit, 'q', ctrl('D'), 'ZZ')
-	cl.bind(FM.reset, ctrl('R'))
-	cl.bind(FM.redraw, ctrl('L'))
-	cl.bind(FM.resize, curses.KEY_RESIZE)
-
-	cl.rebuild_paths()
-