diff options
-rw-r--r-- | ranger/core/fm.py | 46 | ||||
-rw-r--r-- | ranger/core/helper.py | 5 | ||||
-rw-r--r-- | ranger/core/main.py | 6 |
3 files changed, 30 insertions, 27 deletions
diff --git a/ranger/core/fm.py b/ranger/core/fm.py index f51a7834..314127ca 100644 --- a/ranger/core/fm.py +++ b/ranger/core/fm.py @@ -122,35 +122,35 @@ class FM(Actions, SignalDispatcher): self.input_blocked = False return self.input_blocked - def _copy_config_files(self): + def copy_config_files(self, which): if ranger.arg.clean: + sys.stderr.write("refusing to copy config files in clean mode\n") return import shutil - files = {'data/apps.py': 'apps.py', - 'data/rc.conf': 'rc.conf', - 'data/commands.py': 'commands.py', - 'data/options.py': 'options.py', - 'data/scope.sh': 'scope.sh'} - copied_any = False - for fname, target in files.items(): - if not os.path.exists(self.confpath(target)): - if not copied_any: - copied_any = True - try: - os.makedirs(self.confpath()) - except: - pass - sys.stderr.write("creating: %s\n" % self.confpath(target)) + def copy(_from, to): + if os.path.exists(self.confpath(to)): + sys.stderr.write("already exists: %s\n" % self.confpath(to)) + else: + sys.stderr.write("creating: %s\n" % self.confpath(to)) try: - shutil.copy(self.relpath(fname), self.confpath(target)) + shutil.copy(self.relpath(_from), self.confpath(to)) except Exception as e: sys.stderr.write(" ERROR: %s\n" % str(e)) - if target == 'scope.sh': - os.chmod(self.confpath('scope.sh'), os.stat( - self.confpath('scope.sh')).st_mode | stat.S_IXUSR) - if copied_any: - sys.stderr.write("Use --dont-copy-config to disable " - "automatic copying of example config files.\n") + if which == 'apps' or which == 'all': + copy('defaults/apps.py', 'apps.py') + if which == 'commands' or which == 'all': + copy('defaults/commands.py', 'commands.py') + if which == 'rc' or which == 'all': + copy('defaults/rc.conf', 'rc.conf') + if which == 'options' or which == 'all': + copy('defaults/options.py', 'options.py') + if which == 'scope' or which == 'all': + copy('data/scope.sh', 'scope.sh') + os.chmod(self.confpath('scope.sh'), + os.stat(self.confpath('scope.sh')).st_mode | stat.S_IXUSR) + if which not in \ + ('all', 'apps', 'scope', 'commands', 'rc', 'options'): + sys.stderr.write("Unknown config file `%s'\n" % which) def confpath(self, *paths): """returns the path relative to rangers configuration directory""" diff --git a/ranger/core/helper.py b/ranger/core/helper.py index e95c0752..a4c56712 100644 --- a/ranger/core/helper.py +++ b/ranger/core/helper.py @@ -38,8 +38,9 @@ def parse_arguments(): help="activate debug mode") parser.add_option('-c', '--clean', action='store_true', help="don't touch/require any config files. ") - parser.add_option('--dont-copy-config', action='store_true', - help="dont copy the sample configs if they're not found") + parser.add_option('--copy-config', type='string', metavar='which', + help="copy the default configs to the local config directory. " + "Possible values: all, apps, commands, keys, options, scope") parser.add_option('--fail-unless-cd', action='store_true', help="experimental: return the exit code 1 if ranger is" \ "used to run a file (with `ranger filename`)") diff --git a/ranger/core/main.py b/ranger/core/main.py index 5fe93d61..abb1d469 100644 --- a/ranger/core/main.py +++ b/ranger/core/main.py @@ -42,6 +42,10 @@ def main(): os.environ['SHELL'] = 'bash' ranger.arg = arg = parse_arguments() + if arg.copy_config is not None: + fm = FM() + fm.copy_config_files(arg.copy_config) + return 1 if arg.fail_unless_cd else 0 SettingsAware._setup(clean=arg.clean) @@ -68,8 +72,6 @@ def main(): # Initialize objects from ranger.core.environment import Environment fm = FM() - if not arg.dont_copy_config and not arg.clean: - fm._copy_config_files() FileManagerAware.fm = fm EnvironmentAware.env = Environment(target) fm.tabs = dict((n+1, os.path.abspath(path)) for n, path \ |