Fokko commented on code in PR #5417: URL: https://github.com/apache/iceberg/pull/5417#discussion_r946664640
########## python/pyiceberg/cli/console.py: ########## @@ -0,0 +1,404 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# pylint: disable=broad-except,redefined-builtin,redefined-outer-name +import os +from typing import ( + Dict, + Literal, + Optional, + Tuple, + Type, +) + +import click +from click import Context + +from pyiceberg.catalog import Catalog +from pyiceberg.catalog.hive import HiveCatalog +from pyiceberg.catalog.rest import RestCatalog +from pyiceberg.cli.output import ConsoleOutput, JsonOutput, Output +from pyiceberg.exceptions import NoSuchNamespaceError, NoSuchPropertyException, NoSuchTableError + +SUPPORTED_CATALOGS: Dict[str, Type[Catalog]] = {"thrift": HiveCatalog, "http": RestCatalog} + + [email protected]() [email protected]("--catalog", default=None) [email protected]("--output", type=click.Choice(["text", "json"]), default="text") [email protected]("--uri") [email protected]("--credential") [email protected]_context +def run(ctx: Context, catalog: Optional[str], output: str, uri: Optional[str], credential: Optional[str]): + uri_env_var = "PYICEBERG_URI" + credential_env_var = "PYICEBERG_CREDENTIAL" + + if not uri: + uri = os.environ.get(uri_env_var) + if not credential: + credential = os.environ.get(credential_env_var) + + ctx.ensure_object(dict) + if output == "text": + ctx.obj["output"] = ConsoleOutput() + else: + ctx.obj["output"] = JsonOutput() + + if not uri: + ctx.obj["output"].exception( + ValueError(f"Missing uri. Please provide using --uri or using environment variable {uri_env_var}") + ) + ctx.exit(1) + + assert uri # for mypy + + for scheme, catalog_type in SUPPORTED_CATALOGS.items(): + if uri.startswith(scheme): + ctx.obj["catalog"] = catalog_type(catalog, uri=uri, credential=credential) # type: ignore + break + + if not isinstance(ctx.obj["catalog"], Catalog): + + ctx.obj["output"].exception( + ValueError("Could not determine catalog type from uri. REST (http/https) and Hive (thrift) is supported") + ) + ctx.exit(1) + + +def _catalog_and_output(ctx: Context) -> Tuple[Catalog, Output]: + """ + Small helper to set the types + """ + return ctx.obj["catalog"], ctx.obj["output"] + + [email protected]() [email protected]_context [email protected]("parent", required=False) +def list(ctx: Context, parent: Optional[str]): # pylint: disable=redefined-builtin + """Lists tables or namespaces""" + catalog, output = _catalog_and_output(ctx) + + # still wip, will become more beautiful + # https://github.com/apache/iceberg/pull/5467/ + # has been merged + try: + if parent: + identifiers = catalog.list_tables(parent) + else: + identifiers = catalog.list_namespaces() + output.identifiers(identifiers) + except Exception as exc: + output.exception(exc) + ctx.exit(1) + + [email protected]() [email protected]("--entity", type=click.Choice(["any", "namespace", "table"]), default="any") [email protected]("identifier") [email protected]_context +def describe(ctx: Context, entity: Literal["name", "namespace", "table"], identifier: str): + """Describes a namespace xor table""" + catalog, output = _catalog_and_output(ctx) + identifier_tuple = Catalog.identifier_to_tuple(identifier) + + if entity in {"namespace", "any"} and len(identifier_tuple) > 0: + try: + namespace_properties = catalog.load_namespace_properties(identifier_tuple) + output.describe_properties(namespace_properties) + ctx.exit(0) + except NoSuchNamespaceError as exc: + if entity != "any": + output.exception(exc) + ctx.exit(1) + + if entity in {"table", "any"} and len(identifier_tuple) > 1: + try: + catalog_table = catalog.load_table(identifier) + output.describe_table(catalog_table) + ctx.exit(0) + except NoSuchTableError as exc: + output.exception(exc) + ctx.exit(1) Review Comment: It is a bit more complicated because we both output plain text and json. If we just throw an exception, the exit code will also be non-zero, but we want to format the exception based on the output. I've created a PR that will introduce a decorator that will catch the exception: https://github.com/apache/iceberg/pull/5546 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
