Apply defaults to all of the configs. Much fewer .get()s

This commit is contained in:
Anthony Sottile
2014-03-31 23:22:13 -07:00
parent b23ad5d6a3
commit ac67af21ec
13 changed files with 73 additions and 48 deletions

View File

@@ -4,6 +4,8 @@ import jsonschema.exceptions
import os.path
import yaml
from pre_commit.jsonschema_extensions import apply_defaults
def get_validator(
json_schema,
@@ -39,6 +41,8 @@ def get_validator(
'File {0} is not a valid file'.format(filename), e,
)
obj = apply_defaults(obj, json_schema)
additional_validation_strategy(obj)
return obj

View File

@@ -28,10 +28,10 @@ CONFIG_JSON_SCHEMA = {
'properties': {
'id': {'type': 'string'},
'files': {'type': 'string'},
'exclude': {'type': 'string'},
'exclude': {'type': 'string', 'default': '^$'},
'args': {
'type': 'array',
'minItems': 1,
'default': [],
'items': {'type': 'string'},
},
},
@@ -59,7 +59,7 @@ def validate_config_extra(config):
for repo in config:
for hook in repo['hooks']:
try_regex(repo, hook['id'], hook['files'], 'files')
try_regex(repo, hook['id'], hook.get('exclude', ''), 'exclude')
try_regex(repo, hook['id'], hook['exclude'], 'exclude')
load_config = get_validator(

View File

@@ -20,10 +20,10 @@ MANIFEST_JSON_SCHEMA = {
'properties': {
'id': {'type': 'string'},
'name': {'type': 'string'},
'description': {'type': 'string'},
'description': {'type': 'string', 'default': ''},
'entry': {'type': 'string'},
'language': {'type': 'string'},
'expected_return_value': {'type': 'number'},
'expected_return_value': {'type': 'number', 'default': 0},
},
'required': ['id', 'name', 'entry', 'language'],
},
@@ -32,11 +32,9 @@ MANIFEST_JSON_SCHEMA = {
def additional_manifest_check(obj):
for hook_config in obj:
language = hook_config.get('language')
language = hook_config['language']
if language is not None and not any(
language.startswith(lang) for lang in all_languages
):
if not any(language.startswith(lang) for lang in all_languages):
raise InvalidManifestError(
'Expected language {0} for {1} to start with one of {2!r}'.format(
hook_config['id'],

View File

@@ -2,7 +2,7 @@
def run_hook(env, hook, file_args):
return env.run(
' '.join(['xargs', hook['entry']] + hook.get('args', [])),
' '.join(['xargs', hook['entry']] + hook['args']),
stdin='\n'.join(list(file_args) + ['']),
retcode=None,
)

View File

@@ -6,7 +6,7 @@ def install_environment(repo_cmd_runner):
def run_hook(repo_cmd_runner, hook, file_args):
return repo_cmd_runner.run(
['xargs', '{{prefix}}{0}'.format(hook['entry'])] + hook.get('args', []),
['xargs', '{{prefix}}{0}'.format(hook['entry'])] + hook['args'],
# TODO: this is duplicated in pre_commit/languages/helpers.py
stdin='\n'.join(list(file_args) + ['']),
retcode=None,

View File

@@ -6,7 +6,7 @@ def install_environment(repo_cmd_runner):
def run_hook(repo_cmd_runner, hook, file_args):
return repo_cmd_runner.run(
['xargs', hook['entry']] + hook.get('args', []),
['xargs', hook['entry']] + hook['args'],
# TODO: this is duplicated in pre_commit/languages/helpers.py
stdin='\n'.join(list(file_args) + ['']),
retcode=None,

View File

@@ -27,9 +27,7 @@ class Repository(object):
@cached_property
def languages(self):
return set(filter(None, (
hook.get('language') for hook in self.hooks.values()
)))
return set(hook['language'] for hook in self.hooks.values())
@cached_property
def hooks(self):

View File

@@ -38,7 +38,7 @@ def _run_single_hook(runner, repository, hook_id, all_files=False):
get_filenames(hook['files']),
)
if retcode != repository.hooks[hook_id].get('expected_return_value', 0):
if retcode != repository.hooks[hook_id]['expected_return_value']:
output = '\n'.join([stdout, stderr]).strip()
retcode = 1
color = RED