From b81c9802ae68854bbfd171ee725ac730fc5150f7 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 15 Sep 2016 08:17:18 -0700 Subject: [PATCH] Remove py26 format literals Resolves #403 --- pre_commit/clientlib/validate_base.py | 8 ++++---- pre_commit/clientlib/validate_config.py | 4 ++-- pre_commit/clientlib/validate_manifest.py | 6 +++--- pre_commit/color.py | 2 +- pre_commit/commands/autoupdate.py | 6 +++--- pre_commit/commands/clean.py | 2 +- pre_commit/commands/install_uninstall.py | 8 ++++---- pre_commit/commands/run.py | 10 +++++----- pre_commit/languages/helpers.py | 2 +- pre_commit/languages/node.py | 2 +- pre_commit/languages/python.py | 4 ++-- pre_commit/languages/ruby.py | 8 ++++---- pre_commit/logging_handler.py | 4 ++-- pre_commit/main.py | 6 +++--- pre_commit/make_archives.py | 2 +- pre_commit/output.py | 2 +- pre_commit/parse_shebang.py | 2 +- pre_commit/repository.py | 8 ++++---- pre_commit/staged_files_only.py | 6 +++--- pre_commit/store.py | 2 +- pre_commit/util.py | 6 +++--- .../resources/python3_hooks_repo/python3_hook/main.py | 2 +- tests/clientlib/validate_config_test.py | 2 +- tests/color_test.py | 2 +- tests/commands/run_test.py | 2 +- tests/languages/python_test.py | 2 +- tests/parse_shebang_test.py | 2 +- tests/repository_test.py | 4 ++-- 28 files changed, 58 insertions(+), 58 deletions(-) diff --git a/pre_commit/clientlib/validate_base.py b/pre_commit/clientlib/validate_base.py index 3d08a6c3..ce0c932e 100644 --- a/pre_commit/clientlib/validate_base.py +++ b/pre_commit/clientlib/validate_base.py @@ -38,7 +38,7 @@ def get_validator( """ def validate(filename, load_strategy=yaml.load): if not os.path.exists(filename): - raise exception_type('File {0} does not exist'.format(filename)) + raise exception_type('File {} does not exist'.format(filename)) file_contents = open(filename, 'r').read() @@ -46,14 +46,14 @@ def get_validator( obj = load_strategy(file_contents) except Exception as e: raise exception_type( - 'Invalid yaml: {0}\n{1}'.format(os.path.relpath(filename), e), + 'Invalid yaml: {}\n{}'.format(os.path.relpath(filename), e), ) try: jsonschema.validate(obj, json_schema) except jsonschema.exceptions.ValidationError as e: raise exception_type( - 'Invalid content: {0}\n{1}'.format( + 'Invalid content: {}\n{}'.format( os.path.relpath(filename), e ), ) @@ -75,7 +75,7 @@ def get_run_function(filenames_help, validate_strategy, exception_cls): parser.add_argument( '-V', '--version', action='version', - version='%(prog)s {0}'.format( + version='%(prog)s {}'.format( pkg_resources.get_distribution('pre-commit').version ) ) diff --git a/pre_commit/clientlib/validate_config.py b/pre_commit/clientlib/validate_config.py index 8991850e..3624e62b 100644 --- a/pre_commit/clientlib/validate_config.py +++ b/pre_commit/clientlib/validate_config.py @@ -57,7 +57,7 @@ CONFIG_JSON_SCHEMA = { def try_regex(repo, hook, value, field_name): if not is_regex_valid(value): raise InvalidConfigError( - 'Invalid {0} regex at {1}, {2}: {3}'.format( + 'Invalid {} regex at {}, {}: {}'.format( field_name, repo, hook, value, ) ) @@ -72,7 +72,7 @@ def validate_config_extra(config): ) elif 'sha' not in repo: raise InvalidConfigError( - 'Missing "sha" field for repository {0}'.format(repo['repo']) + 'Missing "sha" field for repository {}'.format(repo['repo']) ) for hook in repo['hooks']: try_regex(repo, hook['id'], hook.get('files', ''), 'files') diff --git a/pre_commit/clientlib/validate_manifest.py b/pre_commit/clientlib/validate_manifest.py index d11ce2b8..10234556 100644 --- a/pre_commit/clientlib/validate_manifest.py +++ b/pre_commit/clientlib/validate_manifest.py @@ -55,7 +55,7 @@ MANIFEST_JSON_SCHEMA = { def validate_languages(hook_config): if hook_config['language'] not in all_languages: raise InvalidManifestError( - 'Expected language {0} for {1} to be one of {2!r}'.format( + 'Expected language {} for {} to be one of {!r}'.format( hook_config['id'], hook_config['language'], all_languages, @@ -66,14 +66,14 @@ def validate_languages(hook_config): def validate_files(hook_config): if not is_regex_valid(hook_config['files']): raise InvalidManifestError( - 'Invalid files regex at {0}: {1}'.format( + 'Invalid files regex at {}: {}'.format( hook_config['id'], hook_config['files'], ) ) if not is_regex_valid(hook_config.get('exclude', '')): raise InvalidManifestError( - 'Invalid exclude regex at {0}: {1}'.format( + 'Invalid exclude regex at {}: {}'.format( hook_config['id'], hook_config['exclude'], ) ) diff --git a/pre_commit/color.py b/pre_commit/color.py index 6fd6d96d..686d85bf 100644 --- a/pre_commit/color.py +++ b/pre_commit/color.py @@ -24,7 +24,7 @@ def format_color(text, color, use_color_setting): if not use_color_setting: return text else: - return u'{0}{1}{2}'.format(color, text, NORMAL) + return '{}{}{}'.format(color, text, NORMAL) COLOR_CHOICES = ('auto', 'always', 'never') diff --git a/pre_commit/commands/autoupdate.py b/pre_commit/commands/autoupdate.py index b72ab289..53055a8d 100644 --- a/pre_commit/commands/autoupdate.py +++ b/pre_commit/commands/autoupdate.py @@ -61,7 +61,7 @@ def _update_repository(repo_config, runner): if hooks_missing: raise RepositoryCannotBeUpdatedError( 'Cannot update because the tip of master is missing these hooks:\n' - '{0}'.format(', '.join(sorted(hooks_missing))) + '{}'.format(', '.join(sorted(hooks_missing))) ) return new_config @@ -86,7 +86,7 @@ def autoupdate(runner): if is_local_hooks(repo_config): output_configs.append(repo_config) continue - sys.stdout.write('Updating {0}...'.format(repo_config['repo'])) + sys.stdout.write('Updating {}...'.format(repo_config['repo'])) sys.stdout.flush() try: new_repo_config = _update_repository(repo_config, runner) @@ -99,7 +99,7 @@ def autoupdate(runner): if new_repo_config['sha'] != repo_config['sha']: changed = True print( - 'updating {0} -> {1}.'.format( + 'updating {} -> {}.'.format( repo_config['sha'], new_repo_config['sha'], ) ) diff --git a/pre_commit/commands/clean.py b/pre_commit/commands/clean.py index e0d307fb..5e5c6548 100644 --- a/pre_commit/commands/clean.py +++ b/pre_commit/commands/clean.py @@ -9,5 +9,5 @@ from pre_commit.util import rmtree def clean(runner): if os.path.exists(runner.store.directory): rmtree(runner.store.directory) - print('Cleaned {0}.'.format(runner.store.directory)) + print('Cleaned {}.'.format(runner.store.directory)) return 0 diff --git a/pre_commit/commands/install_uninstall.py b/pre_commit/commands/install_uninstall.py index a60f7273..926f22d8 100644 --- a/pre_commit/commands/install_uninstall.py +++ b/pre_commit/commands/install_uninstall.py @@ -62,7 +62,7 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'): os.remove(legacy_path) elif os.path.exists(legacy_path): print( - 'Running in migration mode with existing hooks at {0}\n' + 'Running in migration mode with existing hooks at {}\n' 'Use -f to use only pre-commit.'.format( legacy_path, ) @@ -83,7 +83,7 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'): pre_commit_file_obj.write(contents) make_executable(hook_path) - print('pre-commit installed at {0}'.format(hook_path)) + print('pre-commit installed at {}'.format(hook_path)) # If they requested we install all of the hooks, do so. if hooks: @@ -110,10 +110,10 @@ def uninstall(runner, hook_type='pre-commit'): return 0 os.remove(hook_path) - print('{0} uninstalled'.format(hook_type)) + print('{} uninstalled'.format(hook_type)) if os.path.exists(legacy_path): os.rename(legacy_path, hook_path) - print('Restored previous hooks to {0}'.format(hook_path)) + print('Restored previous hooks to {}'.format(hook_path)) return 0 diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py index 424e2958..30245216 100644 --- a/pre_commit/commands/run.py +++ b/pre_commit/commands/run.py @@ -24,8 +24,8 @@ def _get_skips(environ): def _hook_msg_start(hook, verbose): - return '{0}{1}'.format( - '[{0}] '.format(hook['id']) if verbose else '', + return '{}{}'.format( + '[{}] '.format(hook['id']) if verbose else '', hook['name'], ) @@ -51,7 +51,7 @@ def _print_user_skipped(hook, write, args): def get_changed_files(new, old): return cmd_output( - 'git', 'diff', '--name-only', '{0}...{1}'.format(old, new), + 'git', 'diff', '--name-only', '{}...{}'.format(old, new), )[1].splitlines() @@ -107,7 +107,7 @@ def _run_single_hook(hook, repo, args, write, skips=frozenset()): write(color.format_color(pass_fail, print_color, args.color) + '\n') if (stdout or stderr or file_modifications) and (retcode or args.verbose): - write('hookid: {0}\n'.format(hook['id'])) + write('hookid: {}\n'.format(hook['id'])) write('\n') # Print a message if failing due to file modifications @@ -200,7 +200,7 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ): if hook['id'] == args.hook ] if not repo_hooks: - write('No hook with id `{0}`\n'.format(args.hook)) + write('No hook with id `{}`\n'.format(args.hook)) return 1 # Filter hooks for stages diff --git a/pre_commit/languages/helpers.py b/pre_commit/languages/helpers.py index 322c55f1..f0c4240a 100644 --- a/pre_commit/languages/helpers.py +++ b/pre_commit/languages/helpers.py @@ -11,4 +11,4 @@ def environment_dir(ENVIRONMENT_DIR, language_version): if ENVIRONMENT_DIR is None: return None else: - return '{0}-{1}'.format(ENVIRONMENT_DIR, language_version) + return '{}-{}'.format(ENVIRONMENT_DIR, language_version) diff --git a/pre_commit/languages/node.py b/pre_commit/languages/node.py index 2a23fe10..cf104bd8 100644 --- a/pre_commit/languages/node.py +++ b/pre_commit/languages/node.py @@ -47,7 +47,7 @@ def install_environment( with clean_path_on_failure(env_dir): cmd = [ sys.executable, '-m', 'nodeenv', '--prebuilt', - '{{prefix}}{0}'.format(directory), + '{{prefix}}{}'.format(directory), ] if version != 'default': diff --git a/pre_commit/languages/python.py b/pre_commit/languages/python.py index 5c8a60bf..b763c26b 100644 --- a/pre_commit/languages/python.py +++ b/pre_commit/languages/python.py @@ -49,7 +49,7 @@ def norm_version(version): # If it is in the form pythonx.x search in the default # place on windows if version.startswith('python'): - return r'C:\{0}\python.exe'.format(version.replace('.', '')) + return r'C:\{}\python.exe'.format(version.replace('.', '')) # Otherwise assume it is a path return os.path.expanduser(version) @@ -67,7 +67,7 @@ def install_environment( with clean_path_on_failure(repo_cmd_runner.path(directory)): venv_cmd = [ sys.executable, '-m', 'virtualenv', - '{{prefix}}{0}'.format(directory) + '{{prefix}}{}'.format(directory) ] if version != 'default': venv_cmd.extend(['-p', norm_version(version)]) diff --git a/pre_commit/languages/ruby.py b/pre_commit/languages/ruby.py index 3b8ddd35..d79b6da5 100644 --- a/pre_commit/languages/ruby.py +++ b/pre_commit/languages/ruby.py @@ -70,20 +70,20 @@ def _install_rbenv(repo_cmd_runner, version='default'): # We also modify the PS1 variable for manual debugging sake. activate_file.write( '#!/usr/bin/env bash\n' - "export RBENV_ROOT='{0}'\n" + "export RBENV_ROOT='{directory}'\n" 'export PATH="$RBENV_ROOT/bin:$PATH"\n' 'eval "$(rbenv init -)"\n' 'export PS1="(rbenv)$PS1"\n' # This lets us install gems in an isolated and repeatable # directory - "export GEM_HOME='{0}/gems'\n" + "export GEM_HOME='{directory}/gems'\n" 'export PATH="$GEM_HOME/bin:$PATH"\n' - '\n'.format(repo_cmd_runner.path(directory)) + '\n'.format(directory=repo_cmd_runner.path(directory)) ) # If we aren't using the system ruby, add a version here if version != 'default': - activate_file.write('export RBENV_VERSION="{0}"\n'.format(version)) + activate_file.write('export RBENV_VERSION="{}"\n'.format(version)) def _install_ruby(runner, version): diff --git a/pre_commit/logging_handler.py b/pre_commit/logging_handler.py index 5e3a1ae0..c1cdf851 100644 --- a/pre_commit/logging_handler.py +++ b/pre_commit/logging_handler.py @@ -22,9 +22,9 @@ class LoggingHandler(logging.Handler): def emit(self, record): self.__write( - u'{0}{1}\n'.format( + '{}{}\n'.format( color.format_color( - '[{0}]'.format(record.levelname), + '[{}]'.format(record.levelname), LOG_LEVEL_COLORS[record.levelname], self.use_color, ) + ' ', diff --git a/pre_commit/main.py b/pre_commit/main.py index 128968fe..1afbef0b 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -34,7 +34,7 @@ def main(argv=None): parser.add_argument( '-V', '--version', action='version', - version='%(prog)s {0}'.format( + version='%(prog)s {}'.format( pkg_resources.get_distribution('pre-commit').version ) ) @@ -157,11 +157,11 @@ def main(argv=None): return run(runner, args) else: raise NotImplementedError( - 'Command {0} not implemented.'.format(args.command) + 'Command {} not implemented.'.format(args.command) ) raise AssertionError( - 'Command {0} failed to exit with a returncode'.format(args.command) + 'Command {} failed to exit with a returncode'.format(args.command) ) diff --git a/pre_commit/make_archives.py b/pre_commit/make_archives.py index 35bb303c..67582114 100644 --- a/pre_commit/make_archives.py +++ b/pre_commit/make_archives.py @@ -61,7 +61,7 @@ def make_archive(name, repo, ref, destdir): def main(): for archive_name, repo, ref in REPOS: - print('Making {0}.tar.gz for {1}@{2}'.format(archive_name, repo, ref)) + print('Making {}.tar.gz for {}@{}'.format(archive_name, repo, ref)) make_archive(archive_name, repo, ref, RESOURCES_DIR) diff --git a/pre_commit/output.py b/pre_commit/output.py index 4d829bf3..43c4d5e9 100644 --- a/pre_commit/output.py +++ b/pre_commit/output.py @@ -60,7 +60,7 @@ def get_hook_message( if end_len: return start + '.' * (cols - len(start) - end_len - 1) else: - return '{0}{1}{2}{3}\n'.format( + return '{}{}{}{}\n'.format( start, '.' * (cols - len(start) - len(postfix) - len(end_msg) - 1), postfix, diff --git a/pre_commit/parse_shebang.py b/pre_commit/parse_shebang.py index 13a1a722..a243a826 100644 --- a/pre_commit/parse_shebang.py +++ b/pre_commit/parse_shebang.py @@ -75,7 +75,7 @@ def normexe(orig_exe): exe = find_executable(orig_exe) if exe is None: raise ExecutableNotFoundError( - 'Executable `{0}` not found'.format(orig_exe), + 'Executable `{}` not found'.format(orig_exe), ) return exe else: diff --git a/pre_commit/repository.py b/pre_commit/repository.py index 2fd2d826..f48f431f 100644 --- a/pre_commit/repository.py +++ b/pre_commit/repository.py @@ -76,7 +76,7 @@ class Repository(object): for hook in self.repo_config['hooks']: if hook['id'] not in self.manifest.hooks: logger.error( - '`{0}` is not present in repository {1}. ' + '`{}` is not present in repository {}. ' 'Typo? Perhaps it is introduced in a newer version? ' 'Often you can fix this by removing the hook, running ' '`pre-commit autoupdate`, ' @@ -90,8 +90,8 @@ class Repository(object): ) if hook_version > _pre_commit_version: logger.error( - 'The hook `{0}` requires pre-commit version {1} but ' - 'version {2} is installed. ' + 'The hook `{}` requires pre-commit version {} but ' + 'version {} is installed. ' 'Perhaps run `pip install --upgrade pre-commit`.'.format( hook['id'], hook_version, _pre_commit_version, ) @@ -165,7 +165,7 @@ class Repository(object): for language_name, language_version in self.languages ): logger.info( - 'Installing environment for {0}.'.format(self.repo_url) + 'Installing environment for {}.'.format(self.repo_url) ) logger.info('Once installed this environment will be reused.') logger.info('This may take a few minutes...') diff --git a/pre_commit/staged_files_only.py b/pre_commit/staged_files_only.py index 7719d28b..a63662bf 100644 --- a/pre_commit/staged_files_only.py +++ b/pre_commit/staged_files_only.py @@ -29,10 +29,10 @@ def staged_files_only(cmd_runner): encoding=None, ) if retcode and diff_stdout_binary.strip(): - patch_filename = cmd_runner.path('patch{0}'.format(int(time.time()))) + patch_filename = cmd_runner.path('patch{}'.format(int(time.time()))) logger.warning('Unstaged files detected.') logger.info( - 'Stashing unstaged files to {0}.'.format(patch_filename), + 'Stashing unstaged files to {}.'.format(patch_filename), ) # Save the current unstaged changes as a patch with io.open(patch_filename, 'wb') as patch_file: @@ -56,7 +56,7 @@ def staged_files_only(cmd_runner): # Roll back the changes made by hooks. cmd_runner.run(['git', 'checkout', '--', '.']) cmd_runner.run(('git', 'apply', patch_filename), encoding=None) - logger.info('Restored changes from {0}.'.format(patch_filename)) + logger.info('Restored changes from {}.'.format(patch_filename)) else: # There weren't any staged files so we don't need to do anything # special diff --git a/pre_commit/store.py b/pre_commit/store.py index 608472bf..9b2320e6 100644 --- a/pre_commit/store.py +++ b/pre_commit/store.py @@ -111,7 +111,7 @@ class Store(object): if result: return result[0] - logger.info('Initializing environment for {0}.'.format(url)) + logger.info('Initializing environment for {}.'.format(url)) dir = tempfile.mkdtemp(prefix='repo', dir=self.directory) with clean_path_on_failure(dir): diff --git a/pre_commit/util.py b/pre_commit/util.py index 7be7582b..a1435ae4 100644 --- a/pre_commit/util.py +++ b/pre_commit/util.py @@ -131,9 +131,9 @@ class CalledProcessError(RuntimeError): return b''.join(( five.to_bytes( - 'Command: {0!r}\n' - 'Return code: {1}\n' - 'Expected return code: {2}\n'.format( + 'Command: {!r}\n' + 'Return code: {}\n' + 'Expected return code: {}\n'.format( self.cmd, self.returncode, self.expected_returncode ) ), diff --git a/testing/resources/python3_hooks_repo/python3_hook/main.py b/testing/resources/python3_hooks_repo/python3_hook/main.py index ceeca0c4..117c7969 100644 --- a/testing/resources/python3_hooks_repo/python3_hook/main.py +++ b/testing/resources/python3_hooks_repo/python3_hook/main.py @@ -4,7 +4,7 @@ import sys def func(): - print('{0}.{1}'.format(*sys.version_info[:2])) + print('{}.{}'.format(*sys.version_info[:2])) print(repr(sys.argv[1:])) print('Hello World') return 0 diff --git a/tests/clientlib/validate_config_test.py b/tests/clientlib/validate_config_test.py index 5631adbc..d3e0a737 100644 --- a/tests/clientlib/validate_config_test.py +++ b/tests/clientlib/validate_config_test.py @@ -186,7 +186,7 @@ def test_does_not_contain_defaults(): if isinstance(schema, dict): if 'default' in schema: raise AssertionError( - 'Unexpected default in schema at {0}'.format( + 'Unexpected default in schema at {}'.format( ' => '.join(route), ) ) diff --git a/tests/color_test.py b/tests/color_test.py index 500a9bbc..4fb7676a 100644 --- a/tests/color_test.py +++ b/tests/color_test.py @@ -12,7 +12,7 @@ from pre_commit.color import use_color @pytest.mark.parametrize(('in_text', 'in_color', 'in_use_color', 'expected'), ( - ('foo', GREEN, True, '{0}foo\033[0m'.format(GREEN)), + ('foo', GREEN, True, '{}foo\033[0m'.format(GREEN)), ('foo', GREEN, False, 'foo'), )) def test_format_color(in_text, in_color, in_use_color, expected): diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py index ae8b523f..2c80abe3 100644 --- a/tests/commands/run_test.py +++ b/tests/commands/run_test.py @@ -417,7 +417,7 @@ def test_lots_of_files(mock_out_store_directory, tempdir_factory): # Write a crap ton of files for i in range(400): - filename = '{0}{1}'.format('a' * 100, i) + filename = '{}{}'.format('a' * 100, i) open(filename, 'w').close() cmd_output('bash', '-c', 'git add .') diff --git a/tests/languages/python_test.py b/tests/languages/python_test.py index 8715b690..78211cb9 100644 --- a/tests/languages/python_test.py +++ b/tests/languages/python_test.py @@ -10,7 +10,7 @@ def test_norm_version_expanduser(): home = os.path.expanduser('~') if os.name == 'nt': # pragma: no cover (nt) path = r'~\python343' - expected_path = r'{0}\python343'.format(home) + expected_path = r'{}\python343'.format(home) else: # pragma: no cover (non-nt) path = '~/.pyenv/versions/3.4.3/bin/python' expected_path = home + '/.pyenv/versions/3.4.3/bin/python' diff --git a/tests/parse_shebang_test.py b/tests/parse_shebang_test.py index 95a0fcef..46ca2db8 100644 --- a/tests/parse_shebang_test.py +++ b/tests/parse_shebang_test.py @@ -69,7 +69,7 @@ def write_executable(shebang, filename='run'): os.mkdir('bin') path = os.path.join('bin', filename) with io.open(path, 'w') as f: - f.write('#!{0}'.format(shebang)) + f.write('#!{}'.format(shebang)) make_executable(path) return path diff --git a/tests/repository_test.py b/tests/repository_test.py index 9e58b090..0ac2d476 100644 --- a/tests/repository_test.py +++ b/tests/repository_test.py @@ -293,7 +293,7 @@ def _norm_pwd(path): # Under windows bash's temp and windows temp is different. # This normalizes to the bash /tmp return cmd_output( - 'bash', '-c', "cd '{0}' && pwd".format(path), + 'bash', '-c', "cd '{}' && pwd".format(path), encoding=None, )[1].strip() @@ -554,7 +554,7 @@ def test_hook_id_not_present(tempdir_factory, store, fake_log_handler): with pytest.raises(SystemExit): repo.install() assert fake_log_handler.handle.call_args[0][0].msg == ( - '`i-dont-exist` is not present in repository {0}. ' + '`i-dont-exist` is not present in repository {}. ' 'Typo? Perhaps it is introduced in a newer version? ' 'Often you can fix this by removing the hook, ' 'running `pre-commit autoupdate`, '