diff --git a/aptos/__main__.py b/aptos/__main__.py index 1e33ebe..5c84b1e 100644 --- a/aptos/__main__.py +++ b/aptos/__main__.py @@ -3,7 +3,7 @@ import sys import colorama -from termcolor import colored +from .formatter import Formatter from .parser import SchemaParser from .primitive import Object from .visitor import ValidationVisitor @@ -18,8 +18,8 @@ def validate(arguments): try: component.accept(ValidationVisitor(instance)) except AssertionError as e: - sys.exit(colored('error', 'red') + ' {!r}'.format(e.args[0])) - print(colored('success', 'green') + ' instance {!r} is valid against the schema {!r}'.format(instance, arguments.schema)) # noqa: E501 + sys.exit(Formatter.format_unsuccessful_action( ' {!r}'.format(e.args[0]))) + print(Formatter.format_successful_action(Formatter.valid_instance_meesage(instance, arguments.schema))) # noqa: E501 def convert(arguments): @@ -27,7 +27,7 @@ def convert(arguments): schema = json.load(fp) component = SchemaParser.parse(schema) if not isinstance(component, Object): - sys.exit(colored('error', 'red') + ' cannot convert schema {!r} into {!r} format, schema must be of type "object"'.format(arguments.schema, arguments.format)) # noqa: E501 + sys.exit(Formatter.format_unsuccessful_action(Formatter.cannot_convert_schema_message(arguments.schema, arguments.format))) # noqa: E501 Visitor = { 'avro': AvroSchemaVisitor, }[arguments.format] diff --git a/aptos/formatter.py b/aptos/formatter.py new file mode 100644 index 0000000..5a209e4 --- /dev/null +++ b/aptos/formatter.py @@ -0,0 +1,21 @@ +from termcolor import colored + + +class Formatter: + + @staticmethod + def format_successful_action(for_message): + return colored('success', 'green') + ' {!r}'.format(for_message) + + @staticmethod + def format_unsuccessful_action(for_message): + return colored('error', 'red') + ' {!r}'.format(for_message) + + @staticmethod + def valid_instance_meesage(instance, schema): + return ' instance {!r} is valid against the schema {!r}'.format(instance, schema) + + @staticmethod + def cannot_convert_schema_message(schema, format): + return ' cannot convert schema {!r} into {!r} format, schema must be of type "object"'.format(schema, + format)