| Conditions | 11 |
| Total Lines | 70 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like tabpy.tabpy_server.handlers.arrow_client.main() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # Licensed to the Apache Software Foundation (ASF) under one |
||
| 125 | def main(): |
||
| 126 | parser = argparse.ArgumentParser() |
||
| 127 | subcommands = parser.add_subparsers() |
||
| 128 | |||
| 129 | cmd_list = subcommands.add_parser('list') |
||
| 130 | cmd_list.set_defaults(action='list') |
||
| 131 | _add_common_arguments(cmd_list) |
||
| 132 | cmd_list.add_argument('-l', '--list', action='store_true', |
||
| 133 | help="Print more details.") |
||
| 134 | |||
| 135 | cmd_do = subcommands.add_parser('do') |
||
| 136 | cmd_do.set_defaults(action='do') |
||
| 137 | _add_common_arguments(cmd_do) |
||
| 138 | cmd_do.add_argument('action_type', type=str, |
||
| 139 | help="The action type to run.") |
||
| 140 | |||
| 141 | cmd_put = subcommands.add_parser('put') |
||
| 142 | cmd_put.set_defaults(action='put') |
||
| 143 | _add_common_arguments(cmd_put) |
||
| 144 | cmd_put.add_argument('file', type=str, |
||
| 145 | help="CSV file to upload.") |
||
| 146 | |||
| 147 | cmd_get = subcommands.add_parser('get') |
||
| 148 | cmd_get.set_defaults(action='get') |
||
| 149 | _add_common_arguments(cmd_get) |
||
| 150 | cmd_get_descriptor = cmd_get.add_mutually_exclusive_group(required=True) |
||
| 151 | cmd_get_descriptor.add_argument('-p', '--path', type=str, action='append', |
||
| 152 | help="The path for the descriptor.") |
||
| 153 | cmd_get_descriptor.add_argument('-c', '--command', type=str, |
||
| 154 | help="The command for the descriptor.") |
||
| 155 | |||
| 156 | args = parser.parse_args() |
||
| 157 | if not hasattr(args, 'action'): |
||
| 158 | parser.print_help() |
||
| 159 | sys.exit(1) |
||
| 160 | |||
| 161 | commands = { |
||
| 162 | 'list': list_flights, |
||
| 163 | 'do': do_action, |
||
| 164 | 'get': get_flight_by_path, |
||
| 165 | 'put': push_data, |
||
| 166 | } |
||
| 167 | host, port = args.host.split(':') |
||
| 168 | port = int(port) |
||
| 169 | scheme = "grpc+tcp" |
||
| 170 | connection_args = {} |
||
| 171 | if args.tls: |
||
| 172 | scheme = "grpc+tls" |
||
| 173 | if args.tls_roots: |
||
| 174 | with open(args.tls_roots, "rb") as root_certs: |
||
| 175 | connection_args["tls_root_certs"] = root_certs.read() |
||
| 176 | if args.mtls: |
||
| 177 | with open(args.mtls[0], "rb") as cert_file: |
||
| 178 | tls_cert_chain = cert_file.read() |
||
| 179 | with open(args.mtls[1], "rb") as key_file: |
||
| 180 | tls_private_key = key_file.read() |
||
| 181 | connection_args["cert_chain"] = tls_cert_chain |
||
| 182 | connection_args["private_key"] = tls_private_key |
||
| 183 | client = pyarrow.flight.FlightClient(f"{scheme}://{host}:{port}", |
||
| 184 | **connection_args) |
||
| 185 | while True: |
||
| 186 | try: |
||
| 187 | action = pyarrow.flight.Action("healthcheck", b"") |
||
| 188 | options = pyarrow.flight.FlightCallOptions(timeout=1) |
||
| 189 | list(client.do_action(action, options=options)) |
||
| 190 | break |
||
| 191 | except pyarrow.ArrowIOError as e: |
||
| 192 | if "Deadline" in str(e): |
||
| 193 | print("Server is not ready, waiting...") |
||
| 194 | commands[args.action](args, client, connection_args) |
||
| 195 | |||
| 200 |