|
16 | 16 | from wherobots.db.region import Region |
17 | 17 | from wherobots.db.runtime import Runtime |
18 | 18 | from wherobots.db.session_type import SessionType |
| 19 | +from wherobots.db.models import Store, StoreResult |
| 20 | + |
19 | 21 |
|
20 | 22 | if __name__ == "__main__": |
21 | 23 | parser = argparse.ArgumentParser() |
|
24 | 26 | parser.add_argument("--region", help="Region to connect to (ie. aws-us-west-2)") |
25 | 27 | parser.add_argument("--runtime", help="Runtime type (ie. tiny)") |
26 | 28 | parser.add_argument("--version", help="Runtime version (ie. latest)") |
| 29 | + parser.add_argument("--force-new", action="store_true") |
| 30 | + parser.add_argument("--store", action="store_true") |
27 | 31 | parser.add_argument( |
28 | 32 | "--session-type", |
29 | 33 | help="Type of session to create", |
|
64 | 68 | api_key = None |
65 | 69 | token = None |
66 | 70 | headers = None |
| 71 | + store = None |
67 | 72 |
|
68 | 73 | if args.api_key_file: |
69 | 74 | with open(args.api_key_file) as f: |
|
75 | 80 | token = f.read().strip() |
76 | 81 | headers = {"Authorization": f"Bearer {token}"} |
77 | 82 |
|
| 83 | + if args.store: |
| 84 | + store = Store.for_download() |
| 85 | + logging.info("Will requests for results to be stored in cloud storage.") |
| 86 | + |
78 | 87 | if args.ws_url: |
79 | 88 | conn_func = functools.partial(connect_direct, uri=args.ws_url, headers=headers) |
80 | 89 | else: |
|
88 | 97 | runtime=Runtime(args.runtime) if args.runtime else Runtime.MICRO, |
89 | 98 | region=Region(args.region) if args.region else Region.AWS_US_WEST_2, |
90 | 99 | version=args.version, |
| 100 | + force_new=args.force_new, |
91 | 101 | session_type=SessionType(args.session_type), |
92 | 102 | ) |
93 | 103 |
|
94 | | - def render(results: pandas.DataFrame) -> None: |
95 | | - table = Table() |
| 104 | + def render_df(df: pandas.DataFrame) -> Table: |
| 105 | + table = Table(show_header=True) |
96 | 106 | table.add_column("#") |
97 | | - for column in results.columns: |
| 107 | + for column in df.columns: |
98 | 108 | table.add_column(column, max_width=args.wide, no_wrap=True) |
99 | | - for row in results.itertuples(name=None): |
| 109 | + for row in df.itertuples(name=None): |
100 | 110 | r = [str(x) for x in row] |
101 | 111 | table.add_row(*r) |
102 | | - Console().print(table) |
| 112 | + return table |
| 113 | + |
| 114 | + def render_stored(sr: StoreResult) -> Table: |
| 115 | + table = Table(show_header=True) |
| 116 | + table.add_column("URI") |
| 117 | + table.add_column("Size", justify="right") |
| 118 | + table.add_row(sr.result_uri, str(sr.size)) |
| 119 | + return table |
| 120 | + |
| 121 | + def render(results: pandas.DataFrame | StoreResult) -> None: |
| 122 | + if isinstance(results, StoreResult): |
| 123 | + Console().print(render_stored(results)) |
| 124 | + else: |
| 125 | + Console().print(render_df(results)) |
103 | 126 |
|
104 | | - def execute(conn: Connection, sql: str) -> pandas.DataFrame: |
| 127 | + def execute(conn: Connection, sql: str) -> pandas.DataFrame | StoreResult: |
105 | 128 | with conn.cursor() as cursor: |
106 | | - cursor.execute(sql) |
107 | | - return cursor.fetchall() |
| 129 | + cursor.execute(sql, store=store) |
| 130 | + if args.store: |
| 131 | + return cursor.get_store_result() |
| 132 | + else: |
| 133 | + return cursor.fetchall() |
108 | 134 |
|
109 | 135 | try: |
110 | 136 | with conn_func() as conn: |
|
0 commit comments