-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathhyperapi-cli.py
More file actions
executable file
·36 lines (30 loc) · 1.45 KB
/
hyperapi-cli.py
File metadata and controls
executable file
·36 lines (30 loc) · 1.45 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
#!/usr/bin/env python3
import readline
from argparse import ArgumentParser
from tableauhyperapi import HyperProcess, Connection, Telemetry, CreateMode, HyperException
def main():
parser = ArgumentParser("HyperAPI interactive cli.")
parser.add_argument("database", type=str, nargs='?',
help="A Hyper file to attach on startup")
args = parser.parse_args()
create_mode = CreateMode.CREATE_IF_NOT_EXISTS if args.database else CreateMode.NONE
with HyperProcess(Telemetry.SEND_USAGE_DATA_TO_TABLEAU) as hyper_process:
try:
with Connection(hyper_process.endpoint, args.database, create_mode) as connection:
while True:
try:
sql = input("> ")
except (EOFError, KeyboardInterrupt):
return
try:
with connection.execute_query(sql) as result:
print("\t".join(str(column.name)
for column in result.schema.columns))
for row in result:
print("\t".join(str(column) for column in row))
except HyperException as exception:
print(f"Error executing SQL: {exception}")
except HyperException as exception:
print(f"Unable to connect to the database: {exception}")
if __name__ == "__main__":
main()