mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 17:14:43 +04:00
DRY up validate_*
This commit is contained in:
parent
9ce6cc6c05
commit
9a1799b9a8
3 changed files with 34 additions and 44 deletions
|
|
@ -1,10 +1,14 @@
|
||||||
|
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import argparse
|
||||||
import jsonschema
|
import jsonschema
|
||||||
import jsonschema.exceptions
|
import jsonschema.exceptions
|
||||||
import os.path
|
import os.path
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from pre_commit.jsonschema_extensions import apply_defaults
|
from pre_commit.jsonschema_extensions import apply_defaults
|
||||||
|
from pre_commit.util import entry
|
||||||
|
|
||||||
|
|
||||||
def get_validator(
|
def get_validator(
|
||||||
|
|
@ -48,3 +52,25 @@ def get_validator(
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
return validate
|
return validate
|
||||||
|
|
||||||
|
|
||||||
|
def get_run_function(filenames_help, validate_strategy, exception_cls):
|
||||||
|
@entry
|
||||||
|
def run(argv):
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('filenames', nargs='*', help=filenames_help)
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
|
retval = 0
|
||||||
|
for filename in args.filenames:
|
||||||
|
try:
|
||||||
|
validate_strategy(filename)
|
||||||
|
except exception_cls as e:
|
||||||
|
print(e.args[0])
|
||||||
|
# If there was an inner exception, print the stringified
|
||||||
|
# version of that.
|
||||||
|
if len(e.args) > 1:
|
||||||
|
print(str(e.args[1]))
|
||||||
|
retval = 1
|
||||||
|
return retval
|
||||||
|
return run
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,9 @@
|
||||||
|
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from pre_commit.clientlib.validate_base import get_run_function
|
||||||
from pre_commit.clientlib.validate_base import get_validator
|
from pre_commit.clientlib.validate_base import get_validator
|
||||||
from pre_commit.util import entry
|
|
||||||
|
|
||||||
|
|
||||||
class InvalidConfigError(ValueError): pass
|
class InvalidConfigError(ValueError): pass
|
||||||
|
|
@ -69,24 +66,7 @@ load_config = get_validator(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@entry
|
run = get_run_function('Config filenames.', load_config, InvalidConfigError)
|
||||||
def run(argv):
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument('filenames', nargs='*', help='Config filenames.')
|
|
||||||
args = parser.parse_args(argv)
|
|
||||||
|
|
||||||
retval = 0
|
|
||||||
for filename in args.filenames:
|
|
||||||
try:
|
|
||||||
load_config(filename)
|
|
||||||
except InvalidConfigError as e:
|
|
||||||
print(e.args[0])
|
|
||||||
# If we have more than one exception argument print the stringified
|
|
||||||
# version
|
|
||||||
if len(e.args) > 1:
|
|
||||||
print(str(e.args[1]))
|
|
||||||
retval = 1
|
|
||||||
return retval
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,9 @@
|
||||||
|
|
||||||
from __future__ import print_function
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from pre_commit.clientlib.validate_base import get_run_function
|
||||||
from pre_commit.clientlib.validate_base import get_validator
|
from pre_commit.clientlib.validate_base import get_validator
|
||||||
from pre_commit.languages.all import all_languages
|
from pre_commit.languages.all import all_languages
|
||||||
from pre_commit.util import entry
|
|
||||||
|
|
||||||
|
|
||||||
class InvalidManifestError(ValueError): pass
|
class InvalidManifestError(ValueError): pass
|
||||||
|
|
@ -51,24 +48,11 @@ load_manifest = get_validator(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@entry
|
run = get_run_function(
|
||||||
def run(argv):
|
'Manifest filenames.',
|
||||||
parser = argparse.ArgumentParser()
|
load_manifest,
|
||||||
parser.add_argument('filenames', nargs='*', help='Manifest filenames.')
|
InvalidManifestError,
|
||||||
args = parser.parse_args(argv)
|
)
|
||||||
|
|
||||||
retval = 0
|
|
||||||
for filename in args.filenames:
|
|
||||||
try:
|
|
||||||
load_manifest(filename)
|
|
||||||
except InvalidManifestError as e:
|
|
||||||
print(e.args[0])
|
|
||||||
# If we have more than one exception argument print the stringified
|
|
||||||
# version
|
|
||||||
if len(e.args) > 1:
|
|
||||||
print(str(e.args[1]))
|
|
||||||
retval = 1
|
|
||||||
return retval
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue