Skip to content
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

Add option to force Unix EOL on YAML export via the CLI #316

Merged
merged 8 commits into from
Sep 26, 2024
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
79 changes: 69 additions & 10 deletions src/preset_cli/cli/superset/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re
from collections import defaultdict
from pathlib import Path
from typing import Any, Callable, List, Set, Tuple
from typing import Any, Callable, List, Set, Tuple, Union
from zipfile import ZipFile

import click
Expand All @@ -21,6 +21,11 @@
assert JINJA2_OPEN_MARKER != JINJA2_CLOSE_MARKER


def get_newline_char(force_unix_eol: bool = False) -> Union[str, None]:
"""Returns the newline character used by the open function"""
return "\n" if force_unix_eol else None


@click.command()
@click.argument("directory", type=click.Path(exists=True, resolve_path=True))
@click.option(
Expand All @@ -35,6 +40,12 @@
default=False,
help="Disable Jinja template escaping",
)
@click.option(
"--force-unix-eol",
is_flag=True,
default=False,
help="Force Unix end-of-line characters, otherwise use system default",
)
@click.option(
"--asset-type",
help="Asset type",
Expand Down Expand Up @@ -71,6 +82,7 @@ def export_assets( # pylint: disable=too-many-locals, too-many-arguments
dashboard_ids: List[str],
overwrite: bool = False,
disable_jinja_escaping: bool = False,
force_unix_eol: bool = False,
) -> None:
"""
Export DBs/datasets/charts/dashboards to a directory.
Expand Down Expand Up @@ -100,6 +112,7 @@ def export_assets( # pylint: disable=too-many-locals, too-many-arguments
overwrite,
disable_jinja_escaping,
skip_related=not ids_requested,
force_unix_eol=force_unix_eol,
)


Expand All @@ -111,6 +124,7 @@ def export_resource( # pylint: disable=too-many-arguments, too-many-locals
overwrite: bool,
disable_jinja_escaping: bool,
skip_related: bool = True,
force_unix_eol: bool = False,
) -> None:
"""
Export a given resource and unzip it in a directory.
Expand Down Expand Up @@ -149,7 +163,8 @@ def export_resource( # pylint: disable=too-many-arguments, too-many-locals

file_contents = yaml.dump(asset_content, sort_keys=False)

with open(target, "w", encoding="utf-8") as output:
newline = get_newline_char(force_unix_eol)
with open(target, "w", encoding="utf-8", newline=newline) as output:
output.write(file_contents)


Expand Down Expand Up @@ -221,8 +236,18 @@ def jinja_escaper(value: str) -> str:
type=click.Path(resolve_path=True),
default="users.yaml",
)
@click.option(
"--force-unix-eol",
is_flag=True,
default=False,
help="Force Unix end-of-line characters, otherwise use system default",
)
@click.pass_context
def export_users(ctx: click.core.Context, path: str) -> None:
def export_users(
ctx: click.core.Context,
path: str,
force_unix_eol: bool = False,
) -> None:
"""
Export users and their roles to a YAML file.
"""
Expand All @@ -234,7 +259,8 @@ def export_users(ctx: click.core.Context, path: str) -> None:
{k: v for k, v in user.items() if k != "id"} for user in client.export_users()
]

with open(path, "w", encoding="utf-8") as output:
newline = get_newline_char(force_unix_eol)
with open(path, "w", encoding="utf-8", newline=newline) as output:
yaml.dump(users, output)


Expand All @@ -244,16 +270,27 @@ def export_users(ctx: click.core.Context, path: str) -> None:
type=click.Path(resolve_path=True),
default="roles.yaml",
)
@click.option(
"--force-unix-eol",
is_flag=True,
default=False,
help="Force Unix end-of-line characters, otherwise use system default",
)
@click.pass_context
def export_roles(ctx: click.core.Context, path: str) -> None:
def export_roles(
ctx: click.core.Context,
path: str,
force_unix_eol: bool = False,
) -> None:
"""
Export roles to a YAML file.
"""
auth = ctx.obj["AUTH"]
url = URL(ctx.obj["INSTANCE"])
client = SupersetClient(url, auth)

with open(path, "w", encoding="utf-8") as output:
newline = get_newline_char(force_unix_eol)
with open(path, "w", encoding="utf-8", newline=newline) as output:
yaml.dump(list(client.export_roles()), output)


Expand All @@ -263,16 +300,27 @@ def export_roles(ctx: click.core.Context, path: str) -> None:
type=click.Path(resolve_path=True),
default="rls.yaml",
)
@click.option(
"--force-unix-eol",
is_flag=True,
default=False,
help="Force Unix end-of-line characters, otherwise use system default",
)
@click.pass_context
def export_rls(ctx: click.core.Context, path: str) -> None:
def export_rls(
ctx: click.core.Context,
path: str,
force_unix_eol: bool = False,
) -> None:
"""
Export RLS rules to a YAML file.
"""
auth = ctx.obj["AUTH"]
url = URL(ctx.obj["INSTANCE"])
client = SupersetClient(url, auth)

with open(path, "w", encoding="utf-8") as output:
newline = get_newline_char(force_unix_eol)
with open(path, "w", encoding="utf-8", newline=newline) as output:
yaml.dump(list(client.export_rls()), output, sort_keys=False)


Expand All @@ -282,8 +330,18 @@ def export_rls(ctx: click.core.Context, path: str) -> None:
type=click.Path(resolve_path=True),
default="ownership.yaml",
)
@click.option(
"--force-unix-eol",
is_flag=True,
default=False,
help="Force Unix end-of-line characters, otherwise use system default",
)
@click.pass_context
def export_ownership(ctx: click.core.Context, path: str) -> None:
def export_ownership(
ctx: click.core.Context,
path: str,
force_unix_eol: bool = False,
) -> None:
"""
Export DBs/datasets/charts/dashboards ownership to a YAML file.
"""
Expand All @@ -302,5 +360,6 @@ def export_ownership(ctx: click.core.Context, path: str) -> None:
},
)

with open(path, "w", encoding="utf-8") as output:
newline = get_newline_char(force_unix_eol)
with open(path, "w", encoding="utf-8", newline=newline) as output:
yaml.dump(dict(ownership), output)
Loading
Loading