Skip to content

Python code format #1954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 41 additions & 22 deletions client/python/cli/command/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class Command(ABC):
"""

@staticmethod
def from_options(options: argparse.Namespace) -> 'Command':

def from_options(options: argparse.Namespace) -> "Command":
def options_get(key, f=lambda x: x):
return f(getattr(options, key)) if hasattr(options, key) else None

Expand All @@ -44,8 +43,9 @@ def options_get(key, f=lambda x: x):
command = None
if options.command == Commands.CATALOGS:
from cli.command.catalogs import CatalogsCommand

command = CatalogsCommand(
options_get(f'{Commands.CATALOGS}_subcommand'),
options_get(f"{Commands.CATALOGS}_subcommand"),
catalog_type=options_get(Arguments.TYPE),
default_base_location=options_get(Arguments.DEFAULT_BASE_LOCATION),
storage_type=options_get(Arguments.STORAGE_TYPE),
Expand All @@ -61,81 +61,100 @@ def options_get(key, f=lambda x: x):
catalog_name=options_get(Arguments.CATALOG),
properties={} if properties is None else properties,
set_properties={} if set_properties is None else set_properties,
remove_properties=[] if remove_properties is None else remove_properties
remove_properties=[]
if remove_properties is None
else remove_properties,
)
elif options.command == Commands.PRINCIPALS:
from cli.command.principals import PrincipalsCommand

command = PrincipalsCommand(
options_get(f'{Commands.PRINCIPALS}_subcommand'),
options_get(f"{Commands.PRINCIPALS}_subcommand"),
type=options_get(Arguments.TYPE),
principal_name=options_get(Arguments.PRINCIPAL),
client_id=options_get(Arguments.CLIENT_ID),
principal_role=options_get(Arguments.PRINCIPAL_ROLE),
properties={} if properties is None else properties,
set_properties={} if set_properties is None else set_properties,
remove_properties=[] if remove_properties is None else remove_properties
remove_properties=[]
if remove_properties is None
else remove_properties,
)
elif options.command == Commands.PRINCIPAL_ROLES:
from cli.command.principal_roles import PrincipalRolesCommand

command = PrincipalRolesCommand(
options_get(f'{Commands.PRINCIPAL_ROLES}_subcommand'),
options_get(f"{Commands.PRINCIPAL_ROLES}_subcommand"),
principal_role_name=options_get(Arguments.PRINCIPAL_ROLE),
principal_name=options_get(Arguments.PRINCIPAL),
catalog_name=options_get(Arguments.CATALOG),
catalog_role_name=options_get(Arguments.CATALOG_ROLE),
properties={} if properties is None else properties,
set_properties={} if set_properties is None else set_properties,
remove_properties=[] if remove_properties is None else remove_properties
remove_properties=[]
if remove_properties is None
else remove_properties,
)
elif options.command == Commands.CATALOG_ROLES:
from cli.command.catalog_roles import CatalogRolesCommand

command = CatalogRolesCommand(
options_get(f'{Commands.CATALOG_ROLES}_subcommand'),
options_get(f"{Commands.CATALOG_ROLES}_subcommand"),
catalog_name=options_get(Arguments.CATALOG),
catalog_role_name=options_get(Arguments.CATALOG_ROLE),
principal_role_name=options_get(Arguments.PRINCIPAL_ROLE),
properties={} if properties is None else properties,
set_properties={} if set_properties is None else set_properties,
remove_properties=[] if remove_properties is None else remove_properties
remove_properties=[]
if remove_properties is None
else remove_properties,
)
elif options.command == Commands.PRIVILEGES:
from cli.command.privileges import PrivilegesCommand
subcommand = options_get(f'{Commands.PRIVILEGES}_subcommand')

subcommand = options_get(f"{Commands.PRIVILEGES}_subcommand")
command = PrivilegesCommand(
subcommand,
action=options_get(f'{subcommand}_subcommand'),
action=options_get(f"{subcommand}_subcommand"),
catalog_name=options_get(Arguments.CATALOG),
catalog_role_name=options_get(Arguments.CATALOG_ROLE),
namespace=options_get(Arguments.NAMESPACE, lambda s: s.split('.') if s else None),
namespace=options_get(
Arguments.NAMESPACE, lambda s: s.split(".") if s else None
),
view=options_get(Arguments.VIEW),
table=options_get(Arguments.TABLE),
privilege=options_get(Arguments.PRIVILEGE),
cascade=options_get(Arguments.CASCADE)
cascade=options_get(Arguments.CASCADE),
)
elif options.command == Commands.NAMESPACES:
from cli.command.namespaces import NamespacesCommand
subcommand = options_get(f'{Commands.NAMESPACES}_subcommand')

subcommand = options_get(f"{Commands.NAMESPACES}_subcommand")
command = NamespacesCommand(
subcommand,
catalog=options_get(Arguments.CATALOG),
namespace=options_get(Arguments.NAMESPACE, lambda s: s.split('.')),
parent=options_get(Arguments.PARENT, lambda s: s.split('.') if s else None),
namespace=options_get(Arguments.NAMESPACE, lambda s: s.split(".")),
parent=options_get(
Arguments.PARENT, lambda s: s.split(".") if s else None
),
location=options_get(Arguments.LOCATION),
properties=properties
properties=properties,
)
elif options.command == Commands.PROFILES:
from cli.command.profiles import ProfilesCommand
subcommand = options_get(f'{Commands.PROFILES}_subcommand')

subcommand = options_get(f"{Commands.PROFILES}_subcommand")
command = ProfilesCommand(
subcommand,
profile_name=options_get(Arguments.PROFILE)
subcommand, profile_name=options_get(Arguments.PROFILE)
)

if command is not None:
command.validate()
return command
else:
raise Exception("Please specify a command or run ./polaris --help to view the available commands")
raise Exception(
"Please specify a command or run ./polaris --help to view the available commands"
)

def execute(self, api: PolarisDefaultApi) -> None:
"""
Expand Down
52 changes: 35 additions & 17 deletions client/python/cli/command/catalog_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@
from cli.command import Command
from cli.constants import Subcommands, Arguments
from cli.options.option_tree import Argument
from polaris.management import PolarisDefaultApi, CreateCatalogRoleRequest, CatalogRole, UpdateCatalogRoleRequest, \
GrantCatalogRoleRequest
from polaris.management import (
PolarisDefaultApi,
CreateCatalogRoleRequest,
CatalogRole,
UpdateCatalogRoleRequest,
GrantCatalogRoleRequest,
)


@dataclass
Expand All @@ -51,34 +56,44 @@ class CatalogRolesCommand(Command):

def validate(self):
if not self.catalog_name:
raise Exception(f'Missing required argument: {Argument.to_flag_name(Arguments.CATALOG)}')
raise Exception(
f"Missing required argument: {Argument.to_flag_name(Arguments.CATALOG)}"
)
if self.catalog_roles_subcommand in {Subcommands.GRANT, Subcommands.REVOKE}:
if not self.principal_role_name:
raise Exception(f'Missing required argument: {Argument.to_flag_name(Arguments.PRINCIPAL_ROLE)}')
raise Exception(
f"Missing required argument: {Argument.to_flag_name(Arguments.PRINCIPAL_ROLE)}"
)

def execute(self, api: PolarisDefaultApi) -> None:
if self.catalog_roles_subcommand == Subcommands.CREATE:
request = CreateCatalogRoleRequest(
catalog_role=CatalogRole(
name=self.catalog_role_name,
properties=self.properties
name=self.catalog_role_name, properties=self.properties
)
)
api.create_catalog_role(self.catalog_name, request)
elif self.catalog_roles_subcommand == Subcommands.DELETE:
api.delete_catalog_role(self.catalog_name, self.catalog_role_name)
elif self.catalog_roles_subcommand == Subcommands.GET:
print(api.get_catalog_role(self.catalog_name, self.catalog_role_name).to_json())
print(
api.get_catalog_role(
self.catalog_name, self.catalog_role_name
).to_json()
)
elif self.catalog_roles_subcommand == Subcommands.LIST:
if self.principal_role_name:
for catalog_role in api.list_catalog_roles_for_principal_role(
self.principal_role_name, self.catalog_name).roles:
self.principal_role_name, self.catalog_name
).roles:
print(catalog_role.to_json())
else:
for catalog_role in api.list_catalog_roles(self.catalog_name).roles:
print(catalog_role.to_json())
elif self.catalog_roles_subcommand == Subcommands.UPDATE:
catalog_role = api.get_catalog_role(self.catalog_name, self.catalog_role_name)
catalog_role = api.get_catalog_role(
self.catalog_name, self.catalog_role_name
)
new_properties = catalog_role.properties or {}

# Add or update all entries specified in set_properties
Expand All @@ -92,19 +107,22 @@ def execute(self, api: PolarisDefaultApi) -> None:

request = UpdateCatalogRoleRequest(
current_entity_version=catalog_role.entity_version,
properties=new_properties
properties=new_properties,
)
api.update_catalog_role(self.catalog_name, self.catalog_role_name, request)
elif self.catalog_roles_subcommand == Subcommands.GRANT:
request = GrantCatalogRoleRequest(
catalog_role=CatalogRole(
name=self.catalog_role_name
),
properties=self.properties
catalog_role=CatalogRole(name=self.catalog_role_name),
properties=self.properties,
)
api.assign_catalog_role_to_principal_role(
self.principal_role_name, self.catalog_name, request
)
api.assign_catalog_role_to_principal_role(self.principal_role_name, self.catalog_name, request)
elif self.catalog_roles_subcommand == Subcommands.REVOKE:
api.revoke_catalog_role_from_principal_role(
self.principal_role_name, self.catalog_name, self.catalog_role_name)
self.principal_role_name, self.catalog_name, self.catalog_role_name
)
else:
raise Exception(f'{self.catalog_roles_subcommand} is not supported in the CLI')
raise Exception(
f"{self.catalog_roles_subcommand} is not supported in the CLI"
)
Loading
Loading