diff options
-rw-r--r-- | TODO | 2 | ||||
-rwxr-xr-x | ranger.py | 2 | ||||
-rw-r--r-- | ranger/__init__.py | 74 | ||||
-rw-r--r-- | ranger/fm.py | 25 | ||||
-rw-r--r-- | ranger/main.py | 84 |
5 files changed, 94 insertions, 93 deletions
diff --git a/TODO b/TODO index 9c0ec5f5..bd078d54 100644 --- a/TODO +++ b/TODO @@ -10,5 +10,5 @@ Console General (X) #5 09/12/06 move code from fm into objects - ( ) #6 09/12/06 move main to fm + (X) #6 09/12/06 move main to __init__ ( ) #7 09/12/06 cooler titlebar diff --git a/ranger.py b/ranger.py index 7ab00a26..807e28a6 100755 --- a/ranger.py +++ b/ranger.py @@ -21,7 +21,7 @@ return 1 __doc__ = """Ranger - file browser for the unix terminal""" try: - from ranger.main import main + from ranger import main except ImportError as errormessage: if str(errormessage).endswith("main"): diff --git a/ranger/__init__.py b/ranger/__init__.py index b26a76cb..553e57f5 100644 --- a/ranger/__init__.py +++ b/ranger/__init__.py @@ -18,3 +18,77 @@ CONFDIR = os.path.expanduser('~/.ranger') RANGERDIR = os.path.dirname(__file__) sys.path.append(CONFDIR) + +USAGE = '''%s [options] [path/filename]''' + +def main(): + """initialize objects and run the filemanager""" + try: + import curses + except ImportError as errormessage: + print(errormessage) + print('ranger requires the python curses module. Aborting.') + sys.exit(1) + + from locale import setlocale, LC_ALL + 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.fsobject.file import File + + setlocale(LC_ALL, 'en_US.utf8') + os.stat_float_times(True) + + # Parse options + parser = OptionParser( usage = USAGE, + version = 'ranger ' + __version__ ) + + # Instead of using this directly, use the embedded + # shell script by running ranger with: + # source /path/to/ranger /path/to/ranger + parser.add_option( '--cd-after-exit', + action = 'store_true', + dest = 'cd_after_exit', + help = SUPPRESS_HELP ) + + args, rest = parser.parse_args() + + if args.cd_after_exit: + sys.stderr = sys.__stdout__ + if rest[0] == sys.argv[0]: + del rest[0] + + # Initialize objects + target = ' '.join(rest) + if target: + if not os.access(target, os.F_OK): + print("File or directory doesn't exist: %s" % target) + sys.exit(1) + elif os.path.isfile(target): + thefile = File(target) + FM().execute_file(thefile) + sys.exit(0) + else: + path = target + else: + path = '.' + + Environment(path) + + try: + my_ui = UI() + my_fm = FM(ui = my_ui) + + # Run the file manager + my_fm.initialize() + my_ui.initialize() + my_fm.loop() + finally: + # Finish, clean up + if 'my_ui' in vars(): + my_ui.destroy() + if args.cd_after_exit: + try: sys.__stderr__.write(my_fm.env.pwd.path) + except: pass diff --git a/ranger/fm.py b/ranger/fm.py index 5abb2a8e..43319836 100644 --- a/ranger/fm.py +++ b/ranger/fm.py @@ -2,16 +2,24 @@ from ranger.actions import Actions from ranger.container import Bookmarks from ranger import __version__ -USAGE = '''%s [options] [path/filename]''' CTRL_C = 3 +TICKS_BEFORE_COLLECTING_GARBAGE = 100 class FM(Actions): def __init__(self, ui = None, bookmarks = None): + """Initialize FM.""" Actions.__init__(self) self.ui = ui + self.bookmarks = bookmarks self.apps = self.settings.apps.CustomApplications() - if bookmarks is None: + from ranger.shared import FileManagerAware + FileManagerAware.fm = self + + def initialize(self): + """If ui/bookmarks are None, they will be initialized here.""" + + if self.bookmarks is None: self.bookmarks = Bookmarks() self.bookmarks.load() @@ -19,15 +27,18 @@ class FM(Actions): self.bookmarks = bookmarks self.bookmarks.enter_dir_function = self.enter_dir - from ranger.shared import FileManagerAware - FileManagerAware.fm = self - - def loop(self): if self.ui is None: from ranger.gui.defaultui import DefaultUI self.ui = DefaultUI() self.ui.initialize() + def loop(self): + """The main loop consists of: +1. reloading bookmarks if outdated +2. drawing and finalizing ui +3. reading and handling user input +4. after X loops: collecting unused directory objects""" + self.env.enter_dir(self.env.path) gc_tick = 0 @@ -42,7 +53,7 @@ class FM(Actions): self.ui.handle_key(key) gc_tick += 1 - if gc_tick > 10: + if gc_tick > TICKS_BEFORE_COLLECTING_GARBAGE: gc_tick = 0 self.env.garbage_collect() diff --git a/ranger/main.py b/ranger/main.py deleted file mode 100644 index d2a931fd..00000000 --- a/ranger/main.py +++ /dev/null @@ -1,84 +0,0 @@ -import sys -import os -from locale import setlocale, LC_ALL -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.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: - print(errormessage) - print('ranger requires the python curses module. Aborting.') - sys.exit(1) - - setlocale(LC_ALL, 'en_US.utf8') - os.stat_float_times(True) - - # Parse options - parser = OptionParser( - usage = USAGE, - version = 'ranger ' + VERSION ) - - # Instead of using this directly, use the embedded - # shell script by running ranger with: - # source /path/to/ranger /path/to/ranger - parser.add_option( '--cd-after-exit', - action = 'store_true', - dest = 'cd_after_exit', - help = SUPPRESS_HELP ) - - args, rest = parser.parse_args() - - if args.cd_after_exit: - sys.stderr = sys.__stdout__ - if rest[0] == sys.argv[0]: - del rest[0] - - # Initialize objects - target = ' '.join(rest) - if target: - if not os.access(target, os.F_OK): - print("File or directory doesn't exist: %s" % target) - sys.exit(1) - elif os.path.isfile(target): - thefile = File(target) - FM().execute_file(thefile) - sys.exit(0) - else: - path = target - - else: - path = '.' - - Environment(path) - - try: - my_ui = UI() - my_fm = FM(ui = my_ui) - - # Run the file manager - my_ui.initialize() - my_fm.loop() - -# except: -# from ranger import log -# log(str(sys.exc_info())) - - finally: - # Finish, clean up - if 'my_ui' in vars(): - my_ui.destroy() - - if args.cd_after_exit: - try: sys.__stderr__.write(my_fm.env.pwd.path) - except: pass |