-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidator_cli.py
More file actions
executable file
·101 lines (73 loc) · 2.65 KB
/
validator_cli.py
File metadata and controls
executable file
·101 lines (73 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import sys
sys.path.append("lib/")
import json
import argparse
from lib.validate_styles import validate_styles
def validate_styles_local_cli():
"""
HOW TO RUN STYLE VALIDATOR AD HOC WITH LOCAL FILES (INSTEAD OF USING AWS LAMBDA DEPLOYMENT):
Run as Command Line Interface
$ python validator_cli.py -s ./tests/example_style_guide.json -w ./tests/example_workbook.twb
Run in PyCharm: PyCharm Run Config Parameters
Script Path: ~/tableau-style-validator/validator_cli.py
Parameters: -s"./tests/example_style_guide.json" -w"./tests/example_workbook.twb"
"""
#
# Get input from command line arguments
#
input_files = get_cli_input()
# Style Guide
sg_json = ingest_style_guide(input_files)
sg_json.pop("_README")
# Tableau Workbook
wb_file = ingest_tableau_workbook(input_files)
# Run Tableau Style Validator from command line inputs
validate_styles(sg_json, wb_file)
def get_cli_input():
"""
Accept input JSON and TWB files from the command line.
Usage:
$ python validator_cli.py --style-guide './tests/example_style_guide.json' \
--tableau-workbook './tests/example_workbook.twb'
"""
parser = argparse.ArgumentParser()
# Style Guide
parser.add_argument(
"-s",
"--style-guide",
required=True,
help="""
JSON Style Guide (.json) filepath containing style standards to test against Tableau file.
""",
type=str,
)
# Tableau Workbook
parser.add_argument(
"-w",
"--tableau-workbook",
required=True,
help="Tableau Workbook (.twb) file to test for style guide compliance.",
type=str,
)
arguments = parser.parse_args()
return arguments
def ingest_style_guide(args):
"""Ingest JSON style guide file (~/foo.json) from command line arguments."""
# Test Style Guide input for valid JSON
with open(args.style_guide) as style_guide_infile:
try:
style_guide_json = json.load(style_guide_infile)
except json.JSONDecodeError:
print(
"Invalid JSON format. \n"
"Check for double quotes and matching brackets."
)
return style_guide_json
def ingest_tableau_workbook(args):
"""Ingest Tableau Workbook file (~/foo.twb) from command line arguments."""
# Pass Tableau Workbook to parser as open file
with open(args.tableau_workbook) as tableau_workbook_infile:
tableau_workbook_file = tableau_workbook_infile.read()
return tableau_workbook_file
if __name__ == "__main__":
validate_styles_local_cli()