Skip to content

Commit

Permalink
feat: Allow user to specify a format for stdout (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yogesh Tewari committed Jun 29, 2021
1 parent 3c21ee5 commit 77ba13d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
7 changes: 4 additions & 3 deletions data_validation/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,12 @@ def convert_config_to_yaml(args, config_managers):
return yaml_config


def run_validation(config_manager, verbose=False):
def run_validation(config_manager, result_format="table", verbose=False):
"""Run a single validation.
Args:
config_manager (ConfigManager): Validation config manager instance.
result_format: pretty printing results format
verbose (bool): Validation setting to log queries run.
"""
validator = DataValidation(
Expand All @@ -285,7 +286,7 @@ def run_validation(config_manager, verbose=False):
result_handler=None,
verbose=verbose,
)
validator.execute()
validator.execute(result_format)


def run_validations(args, config_managers):
Expand All @@ -296,7 +297,7 @@ def run_validations(args, config_managers):
"""
# TODO(issue/31): Add parallel execution logic
for config_manager in config_managers:
run_validation(config_manager, verbose=args.verbose)
run_validation(config_manager, result_format=args.format, verbose=args.verbose)


def store_yaml_config_file(args, config_managers):
Expand Down
6 changes: 6 additions & 0 deletions data_validation/cli_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ def _configure_run_parser(subparsers):
"-filters",
help="Filters in the format source_filter:target_filter",
)
run_parser.add_argument(
"--format",
"-format",
default="table",
help="Set the format for printing command output",
)


def _configure_connection_parser(subparsers):
Expand Down
4 changes: 2 additions & 2 deletions data_validation/data_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(

# TODO(dhercher) we planned on shifting this to use an Execution Handler.
# Leaving to to swast on the design of how this should look.
def execute(self):
def execute(self, result_format="table"):
""" Execute Queries and Store Results """
if self.config_manager.validation_type == consts.ROW_VALIDATION:
grouped_fields = self.validation_builder.pop_grouped_fields()
Expand All @@ -102,7 +102,7 @@ def execute(self):
)

# Call Result Handler to Manage Results
return self.result_handler.execute(self.config, result_df)
return self.result_handler.execute(self.config, result_df, result_format)

def query_too_large(self, rows_df, grouped_fields):
""" Return bool to dictate if another level of recursion
Expand Down
24 changes: 22 additions & 2 deletions data_validation/result_handlers/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,28 @@
"""


class TextResultHandler(object):
def execute(self, config, result_df):
def print_formatted_(result_df, result_format):
"""
Utility for printing formatted results
:param result_df
:param result_format
"""
if result_format == "text":
print(result_df.to_string(index=False))
elif result_format == "csv":
print(result_df.to_csv(index=False))
elif result_format == "json":
print(result_df.to_json(orient="index"))
elif result_format == "table":
print(result_df.to_markdown(tablefmt="fancy_grid"))
else:
error_msg = f"format [{result_format}] not supported, printed in default(table grid) mode"
print(result_df.to_markdown(tablefmt="fancy_grid"))
raise Exception(error_msg)


class TextResultHandler(object):
def execute(self, config, result_df, result_format="table"):
print_formatted_(result_df, result_format)

return result_df
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"google-cloud-spanner==3.1.0",
"setuptools>=34.0.0",
"jellyfish==0.8.2",
"tabulate==0.8.9",
]

extras_require = {
Expand Down

0 comments on commit 77ba13d

Please sign in to comment.