#!python

import argh
import json

#!!! This is a hack to allow testing of this CLT without
#!!! re-installing great_expectations every time there's an edit
import sys
import os
import os.path
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(dir_path+'/..')

import great_expectations as ge

def initialize():
	raise NotImplementedError()

@argh.arg('data_set')
@argh.arg('expectations_config_file')
@argh.arg('--result_format', '-o', default="SUMMARY")
@argh.arg('--catch_exceptions', '-e', default=True)
@argh.arg('--only_return_failures', '-f', default=False)
@argh.arg('--custom_dataset_module', '-m', default=None)
@argh.arg('--custom_dataset_class', '-c', default=None)
def validate(data_set, expectations_config_file, **kwargs):

	expectations_config = json.load(open(expectations_config_file))

	if kwargs["custom_dataset_module"]:
		sys.path.insert(0, os.path.dirname(kwargs["custom_dataset_module"]))
		module_name = os.path.basename(kwargs["custom_dataset_module"]).split('.')[0]
		custom_module = __import__(module_name)
		dataset_class = getattr(custom_module, kwargs["custom_dataset_class"])

	else:
		dataset_class = ge.dataset.PandasDataset

	df = ge.read_csv(data_set, expectations_config=expectations_config, dataset_class=dataset_class)

	result = df.validate(
		result_format=kwargs["result_format"],
		catch_exceptions=kwargs["catch_exceptions"],
		only_return_failures=kwargs["only_return_failures"],
	)

	print(json.dumps(result, indent=2))

argh.dispatch_commands([
	initialize,
	validate,
])
