Skip to content
This repository has been archived by the owner on Jul 7, 2022. It is now read-only.

Refactor the formatting #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions aptos/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,16 +18,16 @@ 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):
with open(arguments.schema) as fp:
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]
Expand Down
21 changes: 21 additions & 0 deletions aptos/formatter.py
Original file line number Diff line number Diff line change
@@ -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)