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

Adding --format option to workspace export #281

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions databricks_cli/workspace/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def import_workspace_dir(self, source_path, target_path, overwrite, exclude_hidd
click.echo(('{} does not have a valid extension of {}. Skip this file and ' +
'continue.').format(cur_src, extensions))

def export_workspace_dir(self, source_path, target_path, overwrite, headers=None):
def export_workspace_dir(self, source_path, target_path, overwrite, format=WorkspaceFormat.SOURCE, headers=None):
if os.path.isfile(target_path):
click.echo('{} exists as a file. Skipping this subtree {}'
.format(target_path, source_path))
Expand All @@ -172,7 +172,7 @@ def export_workspace_dir(self, source_path, target_path, overwrite, headers=None
elif obj.is_notebook:
cur_dst = cur_dst + WorkspaceLanguage.to_extension(obj.language)
try:
self.export_workspace(cur_src, cur_dst, WorkspaceFormat.SOURCE, overwrite,
self.export_workspace(cur_src, cur_dst, format, overwrite,
headers=headers)
click.echo('{} -> {}'.format(cur_src, cur_dst))
except LocalFileExistsException:
Expand Down
14 changes: 8 additions & 6 deletions databricks_cli/workspace/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ def mkdirs_cli(api_client, workspace_path):
@profile_option
@eat_exceptions
@provide_api_client
def import_workspace_cli(api_client, source_path, target_path, language, format, overwrite): # NOQA
def import_workspace_cli(api_client, source_path, target_path, language, fmt, overwrite): # NOQA
"""
Imports a file from local to the Databricks workspace.

The format is by default SOURCE. Possible formats are SOURCE, HTML, JUPTYER, and DBC. Each
format is documented at
https://docs.databricks.com/api/latest/workspace.html#notebookexportformat.
"""
WorkspaceApi(api_client).import_workspace(source_path, target_path, language, format, overwrite) # NOQA
WorkspaceApi(api_client).import_workspace(source_path, target_path, language, fmt, overwrite) # NOQA


@click.command(context_settings=CONTEXT_SETTINGS,
Expand All @@ -107,7 +107,7 @@ def import_workspace_cli(api_client, source_path, target_path, language, format,
@profile_option
@eat_exceptions
@provide_api_client
def export_workspace_cli(api_client, source_path, target_path, format, overwrite): # NOQA
def export_workspace_cli(api_client, source_path, target_path, fmt, overwrite): # NOQA
"""
Exports a notebook from the Databricks workspace.

Expand All @@ -121,7 +121,7 @@ def export_workspace_cli(api_client, source_path, target_path, format, overwrite
raise RuntimeError('Export can only be called on a notebook.')
extension = WorkspaceLanguage.to_extension(file_info.language)
target_path = os.path.join(target_path, file_info.basename + extension)
WorkspaceApi(api_client).export_workspace(source_path, target_path, format, overwrite) # NOQA
WorkspaceApi(api_client).export_workspace(source_path, target_path, fmt, overwrite) # NOQA


@click.command(context_settings=CONTEXT_SETTINGS,
Expand All @@ -147,11 +147,12 @@ def delete_cli(api_client, workspace_path, recursive):
@click.argument('source_path')
@click.argument('target_path')
@click.option('--overwrite', '-o', is_flag=True, default=False)
@click.option('--format', '-f', default=WorkspaceFormat.SOURCE, type=FormatClickType())
@debug_option
@profile_option
@eat_exceptions
@provide_api_client
def export_dir_cli(api_client, source_path, target_path, overwrite):
def export_dir_cli(api_client, source_path, target_path, overwrite, fmt):
"""
Recursively exports a directory from the Databricks workspace.

Expand All @@ -162,7 +163,7 @@ def export_dir_cli(api_client, source_path, target_path, overwrite):
workspace_api = WorkspaceApi(api_client)
assert workspace_api.get_status(source_path).is_dir, 'The source path must be a directory. {}' \
.format(source_path)
workspace_api.export_workspace_dir(source_path, target_path, overwrite)
workspace_api.export_workspace_dir(source_path, target_path, overwrite, fmt)


@click.command(context_settings=CONTEXT_SETTINGS,
Expand Down Expand Up @@ -199,6 +200,7 @@ def workspace_group():
"""
pass


workspace_group.add_command(ls_cli, name='ls')
workspace_group.add_command(ls_cli, name='list')
workspace_group.add_command(mkdirs_cli, name='mkdirs')
Expand Down