diff options
author | nfnty <git@nfnty.se> | 2017-04-12 00:49:49 +0200 |
---|---|---|
committer | nfnty <git@nfnty.se> | 2017-04-12 00:49:49 +0200 |
commit | ac27f17599eb06076494fdf8b82bdb195270c8bb (patch) | |
tree | 2a0cddefe61b01623ea7e661d30588b54e7428f3 | |
parent | fffd0c056ebf0378308cd47f7f690d20663e3c95 (diff) | |
download | ranger-ac27f17599eb06076494fdf8b82bdb195270c8bb.tar.gz |
core.fm: confpath, datapath: Be strict about clean mode
Fixes #848
-rw-r--r-- | ranger/core/fm.py | 19 | ||||
-rw-r--r-- | ranger/core/main.py | 58 |
2 files changed, 42 insertions, 35 deletions
diff --git a/ranger/core/fm.py b/ranger/core/fm.py index f8163a73..e53d36e1 100644 --- a/ranger/core/fm.py +++ b/ranger/core/fm.py @@ -104,10 +104,10 @@ class FM(Actions, # pylint: disable=too-many-instance-attributes self.settings.signal_bind('setopt.preview_images_method', set_image_displayer, priority=settings.SIGNAL_PRIORITY_AFTER_SYNC) - if not ranger.args.clean and self.tags is None: - self.tags = Tags(self.datapath('tagged')) - elif ranger.args.clean: + if ranger.args.clean: self.tags = TagsDummy("") + elif self.tags is None: + self.tags = Tags(self.datapath('tagged')) if self.bookmarks is None: if ranger.args.clean: @@ -296,15 +296,18 @@ class FM(Actions, # pylint: disable=too-many-instance-attributes else: sys.stderr.write("Unknown config file `%s'\n" % which) - @staticmethod - def confpath(*paths): + def confpath(self, *paths): """returns path to ranger's configuration directory""" - assert not ranger.args.clean, "Accessed configuration directory in clean mode" + if ranger.args.clean: + self.notify("Accessed configuration directory in clean mode", bad=True) + return None return os.path.join(ranger.args.confdir, *paths) def datapath(self, *paths): """returns path to ranger's data directory""" - assert not ranger.args.clean, "Accessed data directory in clean mode" + if ranger.args.clean: + self.notify("Accessed data directory in clean mode", bad=True) + return None path_compat = self.confpath(*paths) # COMPAT if os.path.exists(path_compat): return path_compat @@ -404,7 +407,7 @@ class FM(Actions, # pylint: disable=too-many-instance-attributes self.bookmarks.save() # Save tabs - if self.settings.save_tabs_on_exit and len(self.tabs) > 1: + if not ranger.args.clean and self.settings.save_tabs_on_exit and len(self.tabs) > 1: with open(self.datapath('tabs'), 'a') as fobj: # Don't save active tab since launching ranger changes the active tab fobj.write('\0'.join(v.path for t, v in self.tabs.items() diff --git a/ranger/core/main.py b/ranger/core/main.py index 9c066e0b..38513970 100644 --- a/ranger/core/main.py +++ b/ranger/core/main.py @@ -67,21 +67,24 @@ def main( fm.copy_config_files(args.copy_config) return 0 if args.list_tagged_files: + if args.clean: + print("Can't access tag data in clean mode", file=sys.stderr) + return 1 fm = FM() try: if sys.version_info[0] >= 3: fobj = open(fm.datapath('tagged'), 'r', errors='replace') else: fobj = open(fm.datapath('tagged'), 'r') - except OSError: - pass - else: - for line in fobj.readlines(): - if len(line) > 2 and line[1] == ':': - if line[0] in args.list_tagged_files: - sys.stdout.write(line[2:]) - elif line and '*' in args.list_tagged_files: - sys.stdout.write(line) + except OSError as ex: + print('Unable to open `tagged` data file: {0}'.format(ex), file=sys.stderr) + return 1 + for line in fobj.readlines(): + if len(line) > 2 and line[1] == ':': + if line[0] in args.list_tagged_files: + sys.stdout.write(line[2:]) + elif line and '*' in args.list_tagged_files: + sys.stdout.write(line) return 0 SettingsAware.settings_set(Settings()) @@ -104,13 +107,13 @@ def main( if not os.access(path_abs, os.F_OK): paths_inaccessible += [path] if paths_inaccessible: - print("Inaccessible paths: %s" % paths) + print('Inaccessible paths: {0}'.format(paths), file=sys.stderr) return 1 profile = None exit_msg = '' exit_code = 0 - try: + try: # pylint: disable=too-many-nested-blocks # Initialize objects fm = FM(paths=paths) FileManagerAware.fm_set(fm) @@ -144,26 +147,27 @@ def main( if fm.settings.preview_images and fm.settings.use_preview_script: if not os.path.exists(args.cachedir): os.makedirs(args.cachedir) - # Create data directory + if not args.clean: + # Create data directory if not os.path.exists(args.datadir): os.makedirs(args.datadir) - # Restore saved tabs - tabs_datapath = fm.datapath('tabs') - if fm.settings.save_tabs_on_exit and os.path.exists(tabs_datapath) and not args.paths: - try: - with open(tabs_datapath, 'r') as fobj: - tabs_saved = fobj.read().partition('\0\0') - fm.start_paths += tabs_saved[0].split('\0') - if tabs_saved[-1]: - with open(tabs_datapath, 'w') as fobj: - fobj.write(tabs_saved[-1]) - else: - os.remove(tabs_datapath) - except OSError as ex: - LOG.error('Unable to restore saved tabs') - LOG.exception(ex) + # Restore saved tabs + tabs_datapath = fm.datapath('tabs') + if fm.settings.save_tabs_on_exit and os.path.exists(tabs_datapath) and not args.paths: + try: + with open(tabs_datapath, 'r') as fobj: + tabs_saved = fobj.read().partition('\0\0') + fm.start_paths += tabs_saved[0].split('\0') + if tabs_saved[-1]: + with open(tabs_datapath, 'w') as fobj: + fobj.write(tabs_saved[-1]) + else: + os.remove(tabs_datapath) + except OSError as ex: + LOG.error('Unable to restore saved tabs') + LOG.exception(ex) # Run the file manager fm.initialize() |