summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-11 17:13:59 +0100
committerhut <hut@lavabit.com>2009-12-11 17:13:59 +0100
commit3de15ddd7fb0151e5f43f0b8e7d06bd76568e235 (patch)
treee27e7ee5120366418a06316740aa7de4eca496e8 /ranger
parent69e97c429fb0d9f25cf1f2dc7d19e42123b0bf72 (diff)
downloadranger-3de15ddd7fb0151e5f43f0b8e7d06bd76568e235.tar.gz
restructurations
Diffstat (limited to 'ranger')
-rw-r--r--ranger/__init__.py40
-rw-r--r--ranger/applications.py2
-rw-r--r--ranger/colorschemes/__init__.py10
-rwxr-xr-xranger/data/generate.py20
-rw-r--r--ranger/environment.py4
-rw-r--r--ranger/ext/__init__.py3
-rw-r--r--ranger/ext/get_all_modules.py8
-rw-r--r--ranger/ext/log.py15
-rw-r--r--ranger/ext/relpath.py5
-rw-r--r--ranger/file.py4
-rw-r--r--ranger/fm.py3
-rw-r--r--ranger/fsobject/__init__.py9
-rw-r--r--ranger/fsobject/directory.py (renamed from ranger/directory.py)39
-rw-r--r--ranger/fsobject/file.py4
-rw-r--r--ranger/fsobject/fsobject.py (renamed from ranger/fsobject.py)68
-rw-r--r--ranger/gui/colorscheme.py2
-rw-r--r--ranger/gui/wdisplay.py8
-rw-r--r--ranger/main.py3
-rw-r--r--ranger/shared/mimetype.py2
19 files changed, 149 insertions, 100 deletions
diff --git a/ranger/__init__.py b/ranger/__init__.py
index f5a1c480..b26a76cb 100644
--- a/ranger/__init__.py
+++ b/ranger/__init__.py
@@ -1,30 +1,20 @@
-import os
-import sys
-
-confdir = os.path.expanduser('~/.ranger')
-rangerdir = os.path.dirname(__file__)
+"""Ranger - file browser for the unix terminal"""
 
-sys.path.append(confdir)
+__copyright__ = 'none'
+__license__ = 'GPL'
+__version__ = '1.0.0'
+__credits__ = 'hut'
+__author__ = 'hut'
+__maintainer__ = 'hut'
+__email__ = 'hut@lavabit.com'
 
+import os
+import sys
 
-def relpath(*args):
-	return os.path.join(rangerdir, *args)
-
-LOGFILE = '/tmp/errorlog'
+# for easier access
+from ranger.ext.log import log
 
-def log(txt):
-	f = open(LOGFILE, 'a')
-	f.write("r1: ")
-	f.write(str(txt))
-	f.write("\n")
-	f.close()
+CONFDIR = os.path.expanduser('~/.ranger')
+RANGERDIR = os.path.dirname(__file__)
 
-# used to get all colorschemes in ~/.ranger/colorschemes
-# and ranger/colorschemes
-def get_all(dirname):
-	import os
-	result = []
-	for filename in os.listdir(dirname):
-		if filename.endswith('.py') and not filename.startswith('_'):
-			result.append(filename[0:filename.index('.')])
-	return result
+sys.path.append(CONFDIR)
diff --git a/ranger/applications.py b/ranger/applications.py
index 7f029fde..99a40e93 100644
--- a/ranger/applications.py
+++ b/ranger/applications.py
@@ -19,7 +19,7 @@ null = open(os.devnull, 'a')
 def run(*args, **kw):
 	from subprocess import Popen
 	from subprocess import PIPE
-	from ranger.ext import waitpid_no_intr
+	from ranger.ext.waitpid_no_intr import waitpid_no_intr
 
 	flags, fm = kw['flags'], kw['fm']
 	for flag in flags:
diff --git a/ranger/colorschemes/__init__.py b/ranger/colorschemes/__init__.py
index 2318ba27..6c710bd8 100644
--- a/ranger/colorschemes/__init__.py
+++ b/ranger/colorschemes/__init__.py
@@ -1,7 +1,7 @@
-from ranger import get_all, log
+from ranger.ext.get_all_modules import get_all_modules
 from os.path import expanduser, dirname, exists, join
 
-__all__ = get_all(dirname(__file__))
+__all__ = get_all_modules(dirname(__file__))
 
 from ranger.colorschemes import *
 
@@ -9,8 +9,10 @@ confpath = expanduser('~/.ranger')
 if exists(join(confpath, 'colorschemes')):
 	initpy = join(confpath, 'colorschemes/__init__.py')
 	if not exists(initpy):
-		open(initpy, 'w').write("""import ranger, os.path
-__all__ = ranger.get_all( os.path.dirname( __file__ ) )
+		open(initpy, 'w').write("""from ranger.ext.get_all_modules import get_all_modules
+from os.path import dirname
+
+__all__ = get_all_modules(dirname(__file__))
 """)
 
 	try:
diff --git a/ranger/data/generate.py b/ranger/data/generate.py
index 61309259..fb9e0017 100755
--- a/ranger/data/generate.py
+++ b/ranger/data/generate.py
@@ -1,16 +1,16 @@
 #!/usr/bin/python3
 # coding=utf-8
 
-protocol = 0
+if __name__ == '__main__':
+	import sys, pickle
 
-import sys, pickle
+	protocol = 0
+	table = {}
 
-table = {}
-for line in open(len(sys.argv) > 1 and sys.argv[1] or "mime.types"):
-	if len(line) > 3 and line[0] != '#' and '\t' in line:
-		name, *extensions = line.split()
-		for ext in extensions:
-			table[ext] = name
-
-pickle.dump(table, open('mime.dat', 'wb'), protocol)
+	for line in open(len(sys.argv) > 1 and sys.argv[1] or "mime.types"):
+		if len(line) > 3 and line[0] != '#' and '\t' in line:
+			name, *extensions = line.split()
+			for ext in extensions:
+				table[ext] = name
 
+	pickle.dump(table, open('mime.dat', 'wb'), protocol)
diff --git a/ranger/environment.py b/ranger/environment.py
index 8e45516b..d7097fc8 100644
--- a/ranger/environment.py
+++ b/ranger/environment.py
@@ -1,5 +1,5 @@
 from os.path import abspath, normpath, join, expanduser
-from ranger.directory import Directory, NoDirectoryGiven
+from ranger.fsobject.directory import Directory, NoDirectoryGiven
 from ranger.container import KeyBuffer, History
 from ranger.shared import SettingsAware
 
@@ -42,7 +42,7 @@ class Environment(SettingsAware):
 				return self.cf
 
 	def garbage_collect(self):
-		from ranger.fsobject import FileSystemObject
+		from ranger.fsobject.fsobject import FileSystemObject
 		for key in tuple(self.directories.keys()):
 			value = self.directories[key]
 			if isinstance(value, FileSystemObject):
diff --git a/ranger/ext/__init__.py b/ranger/ext/__init__.py
index f09324b7..e69de29b 100644
--- a/ranger/ext/__init__.py
+++ b/ranger/ext/__init__.py
@@ -1,3 +0,0 @@
-from .openstruct import OpenStruct
-from .human_readable import human_readable
-from .waitpid_no_intr import waitpid_no_intr
diff --git a/ranger/ext/get_all_modules.py b/ranger/ext/get_all_modules.py
new file mode 100644
index 00000000..2d98278b
--- /dev/null
+++ b/ranger/ext/get_all_modules.py
@@ -0,0 +1,8 @@
+def get_all_modules(dirname):
+	"""returns a list of strings containing the names of modules in a directory"""
+	import os
+	result = []
+	for filename in os.listdir(dirname):
+		if filename.endswith('.py') and not filename.startswith('_'):
+			result.append(filename[0:filename.index('.')])
+	return result
diff --git a/ranger/ext/log.py b/ranger/ext/log.py
new file mode 100644
index 00000000..9f56dd71
--- /dev/null
+++ b/ranger/ext/log.py
@@ -0,0 +1,15 @@
+LOGFILE = '/tmp/errorlog'
+
+def log(*objects, **keywords):
+	"""Writes objects to a logfile.
+Has the same arguments as print() in python3"""
+	start = 'start' in keywords and keywords['start'] or 'ranger:'
+	sep   =   'sep' in keywords and keywords['sep']   or ' '
+	_file =  'file' in keywords and keywords['file']  or open(LOGFILE, 'a')
+	end   =   'end' in keywords and keywords['end']   or '\n'
+	_file.write(sep.join((start, ) + objects) + end)
+	
+#for python3-only versions, this could be replaced with:
+#
+#def log(*objects, start='ranger:', sep=' ', end='\n'):
+#	print(start, *objects, end=end, sep=sep, file=open(LOGFILE, 'a'))
diff --git a/ranger/ext/relpath.py b/ranger/ext/relpath.py
new file mode 100644
index 00000000..46d8018d
--- /dev/null
+++ b/ranger/ext/relpath.py
@@ -0,0 +1,5 @@
+def relpath(*paths):
+	"""returns the path relative to rangers library directory"""
+	from os.path import join
+	from ranger import RANGERDIR
+	return join(RANGERDIR, *paths)
diff --git a/ranger/file.py b/ranger/file.py
deleted file mode 100644
index e95067b1..00000000
--- a/ranger/file.py
+++ /dev/null
@@ -1,4 +0,0 @@
-from ranger.fsobject import FileSystemObject as SuperClass
-class File(SuperClass):
-	pass
-
diff --git a/ranger/fm.py b/ranger/fm.py
index 2b4b32e4..cdcfb8d7 100644
--- a/ranger/fm.py
+++ b/ranger/fm.py
@@ -1,5 +1,8 @@
 from ranger.actions import Actions
 from ranger.container import Bookmarks
+from ranger import __version__
+
+USAGE = '''%s [options] [path/filename]'''
 
 class FM(Actions):
 	def __init__(self, ui = None, bookmarks = None):
diff --git a/ranger/fsobject/__init__.py b/ranger/fsobject/__init__.py
new file mode 100644
index 00000000..2d6da282
--- /dev/null
+++ b/ranger/fsobject/__init__.py
@@ -0,0 +1,9 @@
+T_FILE = 'file'
+T_DIRECTORY = 'directory'
+T_UNKNOWN = 'unknown'
+T_NONEXISTANT = 'nonexistant'
+
+BAD_INFO = None
+
+class NotLoadedYet(Exception):
+	pass
diff --git a/ranger/directory.py b/ranger/fsobject/directory.py
index 258266c0..cdcbc2ed 100644
--- a/ranger/directory.py
+++ b/ranger/fsobject/directory.py
@@ -1,21 +1,36 @@
-import ranger.fsobject
-from ranger.file import File
-#from ranger.helper import log
-
-from ranger.fsobject import BAD_INFO
-from ranger.fsobject import FileSystemObject as SuperClass
+from . import BAD_INFO
+from .file import File
+from .fsobject import FileSystemObject as SuperClass
 from ranger.shared import SettingsAware
+import ranger.fsobject
 
 def sort_by_basename(path):
+	"""returns path.basename (for sorting)"""
 	return path.basename
 
 def sort_by_directory(path):
-	return -int( isinstance( path, Directory ) )
+	"""returns 0 if path is a directory, otherwise 1 (for sorting)"""
+	return 1 - int( isinstance( path, Directory ) )
 
 class NoDirectoryGiven(Exception):
 	pass
 
 class Directory(SuperClass, SettingsAware):
+	content_loaded = False
+	scheduled = False
+	enterable = False
+
+	filenames = None
+	files = None
+	filter = None
+	pointed_index = None
+	pointed_file = None
+	scroll_begin = 0
+	scroll_offset = 0
+
+	old_show_hidden = None
+	old_directories_first = None
+
 	def __init__(self, path):
 		from os.path import isfile
 
@@ -23,16 +38,6 @@ class Directory(SuperClass, SettingsAware):
 			raise NoDirectoryGiven()
 
 		SuperClass.__init__(self, path)
-		self.content_loaded = False
-		self.scheduled = False
-		self.enterable = False
-
-		self.filenames = None
-		self.files = None
-		self.filter = None
-		self.pointed_index = None
-		self.pointed_file = None
-		self.scroll_begin = 0
 
 		# to find out if something has changed:
 		self.old_show_hidden = self.settings.show_hidden
diff --git a/ranger/fsobject/file.py b/ranger/fsobject/file.py
new file mode 100644
index 00000000..9087eab6
--- /dev/null
+++ b/ranger/fsobject/file.py
@@ -0,0 +1,4 @@
+from .fsobject import FileSystemObject as SuperClass
+class File(SuperClass):
+	pass
+
diff --git a/ranger/fsobject.py b/ranger/fsobject/fsobject.py
index 35d0d5b3..20b47813 100644
--- a/ranger/fsobject.py
+++ b/ranger/fsobject/fsobject.py
@@ -1,19 +1,37 @@
-class NotLoadedYet(Exception):
-	pass
-
-T_FILE = 'file'
-T_DIRECTORY = 'directory'
-T_UNKNOWN = 'unknown'
-T_NONEXISTANT = 'nonexistant'
-
-BAD_INFO = None
-
 CONTAINER_EXTENSIONS = 'rar zip tar gz bz bz2 tgz 7z iso cab'.split()
 DOCUMENT_EXTENSIONS = 'pdf doc ppt odt'.split()
 DOCUMENT_BASENAMES = 'README TODO LICENSE'.split()
 
+from . import T_FILE, T_DIRECTORY, T_UNKNOWN, T_NONEXISTANT, BAD_INFO
 from ranger.shared import MimeTypeAware, FileManagerAware
 class FileSystemObject(MimeTypeAware, FileManagerAware):
+	path = None
+	basename = None
+	dirname = None
+	extension = None
+	exists = False
+	accessible = False
+	marked = False
+	tagged = False
+	frozen = False
+	loaded = False
+	runnable = False
+	islink = False
+	brokenlink = False
+	stat = None
+	infostring = None
+	permissions = None
+	type = T_UNKNOWN
+
+	last_used = None
+
+	video = False
+	image = False
+	audio = False
+	media = False
+	document = False
+	container = False
+	mimetype_tuple = ()
 
 	def __init__(self, path):
 		MimeTypeAware.__init__(self)
@@ -25,39 +43,31 @@ class FileSystemObject(MimeTypeAware, FileManagerAware):
 		self.path = path
 		self.basename = basename(path)
 		self.dirname = dirname(path)
+
 		try:
 			self.extension = self.basename[self.basename.rindex('.') + 1:]
 		except ValueError:
 			self.extension = None
-		self.exists = False
-		self.accessible = False
-		self.marked = False
-		self.tagged = False
-		self.frozen = False
-		self.loaded = False
-		self.runnable = False
-		self.islink = False
-		self.brokenlink = False
-		self.stat = None
-		self.infostring = None
-		self.permissions = None
-		self.type = T_UNKNOWN
 
 		self.set_mimetype()
 		self.use()
 	
 	def __str__(self):
+		"""returns a string containing the absolute path"""
 		return str(self.path)
 
 	def use(self):
+		"""mark the filesystem-object as used at the current time"""
 		import time
 		self.last_used = time.time()
 	
 	def is_older_than(self, seconds):
+		"""returns whether this object wasn't use()d in the last n seconds"""
 		import time
 		return self.last_used + seconds < time.time()
 	
 	def set_mimetype(self):
+		"""assign attributes such as self.video according to the mimetype"""
 		try:
 			self.mimetype = self.mimetypes[self.extension]
 		except KeyError:
@@ -76,11 +86,11 @@ class FileSystemObject(MimeTypeAware, FileManagerAware):
 		if self.mimetype == '':
 			self.mimetype = None
 
-	# load() reads useful information about the file from the file system
-	# and caches it in instance attributes.
 	def load(self):
+		"""reads useful information about the filesystem-object from the filesystem
+and caches it for later use"""
 		import os
-		from ranger.ext import human_readable
+		from ranger.ext.human_readable import human_readable
 
 		self.loaded = True
 
@@ -118,15 +128,19 @@ class FileSystemObject(MimeTypeAware, FileManagerAware):
 			self.accessible = False
 
 	def load_once(self):
+		"""calls load() if it has not been called at least once yet"""
 		if not self.loaded:
 			self.load()
 			return True
 		return False
 
 	def go(self):
-		self.fm.enter_dir(self.path)
+		"""enter the directory if the filemanager is running"""
+		if self.fm:
+			self.fm.enter_dir(self.path)
 
 	def load_if_outdated(self):
+		"""calls load() if the currently cached information is outdated or nonexistant"""
 		if self.load_once(): return True
 
 		import os
diff --git a/ranger/gui/colorscheme.py b/ranger/gui/colorscheme.py
index 281396a5..38f4ac5a 100644
--- a/ranger/gui/colorscheme.py
+++ b/ranger/gui/colorscheme.py
@@ -28,7 +28,7 @@ CONTEXT_KEYS = [ 'reset', 'error',
 # If your colorscheme-file contains more than one colorscheme, specify it with:
 # colorscheme = colorschemes.filename.classname
 
-from ranger.ext import OpenStruct
+from ranger.ext.openstruct import OpenStruct
 
 class ColorScheme(object):
 	def __init__(self):
diff --git a/ranger/gui/wdisplay.py b/ranger/gui/wdisplay.py
index 5e5e80d6..c7e78b8e 100644
--- a/ranger/gui/wdisplay.py
+++ b/ranger/gui/wdisplay.py
@@ -12,7 +12,7 @@ class WDisplay(SuperClass):
 		self.target = env.at_level(self.level)
 		
 	def click(self, event, fm):
-		from ranger.fsobject import T_DIRECTORY
+		from ranger.fsobject.fsobject import T_DIRECTORY
 
 		if self.target is None:
 			pass
@@ -38,8 +38,8 @@ class WDisplay(SuperClass):
 				fm.move_right()
 
 	def draw(self):
-		from ranger.file import File
-		from ranger.directory import Directory
+		from ranger.fsobject.file import File
+		from ranger.fsobject.directory import Directory
 
 		if self.target is None:
 			pass
@@ -66,7 +66,7 @@ class WDisplay(SuperClass):
 				pass
 
 	def draw_directory(self):
-		from ranger.directory import Directory
+		from ranger.fsobject.directory import Directory
 		import curses
 		import stat
 
diff --git a/ranger/main.py b/ranger/main.py
index 1cac2873..48297a7c 100644
--- a/ranger/main.py
+++ b/ranger/main.py
@@ -6,13 +6,14 @@ from optparse import OptionParser, SUPPRESS_HELP
 from ranger.fm import FM
 from ranger.environment import Environment
 from ranger.gui.defaultui import DefaultUI as UI
-from ranger.file import File
+from ranger.fsobject.file import File
 
 VERSION = '1.0.0'
 
 USAGE = '''%s [options] [path/filename]'''
 
 def main():
+	"""initialize objects and run the filemanager"""
 	try:
 		import curses
 	except ImportError as errormessage:
diff --git a/ranger/shared/mimetype.py b/ranger/shared/mimetype.py
index ac4ff629..8cc658fb 100644
--- a/ranger/shared/mimetype.py
+++ b/ranger/shared/mimetype.py
@@ -1,4 +1,4 @@
-from ranger import relpath
+from ranger.ext.relpath import relpath
 class MimeTypeAware(object):
 	mimetypes = {}
 	__initialized = False