Skip to content

Commit fb7eca2

Browse files
areeseAllen Reese
andauthored
Add --long option to workspace ls cli (#310)
* Add --long option to workspace ls cli Add --id option to workspace ls cli to display the object id as well * Add missing object_id argument to tests Make with_object_id default to False in to_row Fix lint over 100 characters error Co-authored-by: Allen Reese <areese999@apple.com>
1 parent 22a6f38 commit fb7eca2

4 files changed

Lines changed: 47 additions & 24 deletions

File tree

databricks_cli/workspace/api.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,30 @@
3737

3838

3939
class WorkspaceFileInfo(object):
40-
def __init__(self, path, object_type, language=None, **kwargs): # noqa
40+
def __init__(self, path, object_type, object_id, language=None, **kwargs): # noqa
4141
self.path = path
4242
self.object_type = object_type
4343
self.language = language
44+
self.object_id = object_id
4445

45-
def to_row(self, is_long_form, is_absolute):
46+
def to_row(self, is_long_form, is_absolute, with_object_id=False):
4647
path = self.path if is_absolute else self.basename
4748
if self.is_dir:
4849
stylized_path = click.style(path, 'cyan')
4950
elif self.is_library:
5051
stylized_path = click.style(path, 'green')
5152
else:
5253
stylized_path = path
54+
55+
result = [stylized_path]
56+
5357
if is_long_form:
54-
return [self.object_type, stylized_path, self.language]
55-
else:
56-
return [stylized_path]
58+
result = [self.object_type, stylized_path, self.language]
59+
60+
if with_object_id:
61+
result.append(self.object_id)
62+
63+
return result
5764

5865
@property
5966
def is_dir(self):

databricks_cli/workspace/cli.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@
3737
short_help='List objects in the Databricks Workspace. ls and list are synonyms.')
3838
@click.option('--absolute', is_flag=True, default=False,
3939
help='Displays absolute paths.')
40-
@click.option('-l', is_flag=True, default=False,
40+
@click.option('-l', '--long', 'is_long_form', is_flag=True, default=False,
4141
help='Displays full information including ObjectType, Path, Language')
42+
@click.option('-i', '--id', 'with_object_id', is_flag=True, default=False,
43+
help='Displays ObjectId')
4244
@click.argument('workspace_path', type=str, nargs=-1)
4345
@debug_option
4446
@profile_option
4547
@eat_exceptions
4648
@provide_api_client
47-
def ls_cli(api_client, l, absolute, workspace_path): # noqa
49+
def ls_cli(api_client, is_long_form, with_object_id, absolute, workspace_path): # noqa
4850
"""
4951
List objects in the Databricks Workspace.
5052
"""
@@ -53,8 +55,11 @@ def ls_cli(api_client, l, absolute, workspace_path): # noqa
5355
else:
5456
workspace_path = workspace_path[0]
5557
objects = WorkspaceApi(api_client).list_objects(workspace_path)
56-
table = tabulate([obj.to_row(is_long_form=l, is_absolute=absolute) for obj in objects],
57-
tablefmt='plain')
58+
objects_table = [
59+
obj.to_row(is_long_form=is_long_form, is_absolute=absolute, with_object_id=with_object_id)
60+
for obj in objects
61+
]
62+
table = tabulate(objects_table, tablefmt='plain')
5863
click.echo(table)
5964

6065

tests/workspace/test_api.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# Databricks CLI
23
# Copyright 2017 Databricks, Inc.
34
#
@@ -31,38 +32,44 @@
3132
from databricks_cli.workspace.types import WorkspaceLanguage
3233

3334
TEST_WORKSPACE_PATH = '/test/workspace/path'
35+
TEST_WORKSPACE_OBJECT_ID = '22'
3436
TEST_JSON_RESPONSE = {
3537
'path': TEST_WORKSPACE_PATH,
3638
'object_type': api.DIRECTORY,
37-
'created_at': 124
39+
'created_at': 124,
40+
'object_id': TEST_WORKSPACE_OBJECT_ID,
3841
}
3942
TEST_LANGUAGE = 'PYTHON'
4043
TEST_FMT = 'SOURCE'
4144

4245

4346
class TestWorkspaceFileInfo(object):
4447
def test_to_row_not_long_form_not_absolute(self):
45-
file_info = api.WorkspaceFileInfo(TEST_WORKSPACE_PATH, api.NOTEBOOK)
48+
file_info = api.WorkspaceFileInfo(TEST_WORKSPACE_PATH, api.NOTEBOOK,
49+
TEST_WORKSPACE_OBJECT_ID)
4650
row = file_info.to_row(is_long_form=False, is_absolute=False)
4751
assert len(row) == 1
4852
assert row[0] == file_info.basename
4953

5054
def test_to_row_not_long_form_absolute(self):
51-
file_info = api.WorkspaceFileInfo(TEST_WORKSPACE_PATH, api.NOTEBOOK)
55+
file_info = api.WorkspaceFileInfo(TEST_WORKSPACE_PATH, api.NOTEBOOK,
56+
TEST_WORKSPACE_OBJECT_ID)
5257
row = file_info.to_row(is_long_form=False, is_absolute=True)
5358
assert len(row) == 1
5459
assert row[0] == TEST_WORKSPACE_PATH
5560

5661
def test_to_row_long_form_absolute(self):
57-
file_info = api.WorkspaceFileInfo(TEST_WORKSPACE_PATH, api.NOTEBOOK)
62+
file_info = api.WorkspaceFileInfo(TEST_WORKSPACE_PATH, api.NOTEBOOK,
63+
TEST_WORKSPACE_OBJECT_ID)
5864
row = file_info.to_row(is_long_form=True, is_absolute=True)
5965
assert len(row) == 3
6066
assert row[0] == api.NOTEBOOK
6167
assert row[1] == TEST_WORKSPACE_PATH
6268
assert row[2] is None
6369

6470
def test_basename(self):
65-
file_info = api.WorkspaceFileInfo(TEST_WORKSPACE_PATH, api.NOTEBOOK)
71+
file_info = api.WorkspaceFileInfo(TEST_WORKSPACE_PATH, api.NOTEBOOK,
72+
TEST_WORKSPACE_OBJECT_ID)
6673
assert file_info.basename == 'path'
6774

6875
def test_from_json(self):
@@ -104,8 +111,8 @@ def test_import_workspace(self, workspace_api, tmpdir):
104111
test_file_path = os.path.join(tmpdir.strpath, 'test')
105112
with open(test_file_path, 'w') as f:
106113
f.write('test')
107-
workspace_api.import_workspace(test_file_path, TEST_WORKSPACE_PATH, TEST_LANGUAGE, TEST_FMT,
108-
is_overwrite=False)
114+
workspace_api.import_workspace(test_file_path, TEST_WORKSPACE_PATH, TEST_LANGUAGE,
115+
TEST_FMT, is_overwrite=False)
109116
import_workspace_mock = workspace_api.client.import_workspace
110117
assert import_workspace_mock.call_count == 1
111118
assert import_workspace_mock.call_args[0][0] == TEST_WORKSPACE_PATH
@@ -147,18 +154,22 @@ def test_export_workspace_dir(self, workspace_api, tmpdir):
147154
def _list_objects_mock(path, headers=None):
148155
if path == '/':
149156
return [
150-
WorkspaceFileInfo('/a', api.DIRECTORY),
151-
WorkspaceFileInfo('/f', api.DIRECTORY)
157+
WorkspaceFileInfo('/a', api.DIRECTORY, TEST_WORKSPACE_OBJECT_ID),
158+
WorkspaceFileInfo('/f', api.DIRECTORY, TEST_WORKSPACE_OBJECT_ID)
152159
]
153160
elif path == '/a':
154161
return [
155-
WorkspaceFileInfo('/a/b', api.NOTEBOOK, WorkspaceLanguage.SCALA),
156-
WorkspaceFileInfo('/a/c', api.NOTEBOOK, WorkspaceLanguage.PYTHON),
157-
WorkspaceFileInfo('/a/d', api.NOTEBOOK, WorkspaceLanguage.R),
158-
WorkspaceFileInfo('/a/e', api.NOTEBOOK, WorkspaceLanguage.SQL),
162+
WorkspaceFileInfo('/a/b', api.NOTEBOOK, TEST_WORKSPACE_OBJECT_ID,
163+
WorkspaceLanguage.SCALA),
164+
WorkspaceFileInfo('/a/c', api.NOTEBOOK, TEST_WORKSPACE_OBJECT_ID,
165+
WorkspaceLanguage.PYTHON),
166+
WorkspaceFileInfo('/a/d', api.NOTEBOOK, TEST_WORKSPACE_OBJECT_ID,
167+
WorkspaceLanguage.R),
168+
WorkspaceFileInfo('/a/e', api.NOTEBOOK, TEST_WORKSPACE_OBJECT_ID,
169+
WorkspaceLanguage.SQL),
159170
]
160171
elif path == '/f':
161-
return [WorkspaceFileInfo('/f/g', api.DIRECTORY)]
172+
return [WorkspaceFileInfo('/f/g', api.DIRECTORY, TEST_WORKSPACE_OBJECT_ID)]
162173
elif path == '/f/g':
163174
return []
164175
else:

tests/workspace/test_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def workspace_api_mock():
4444
def test_export_workspace_cli(workspace_api_mock, tmpdir):
4545
path = tmpdir.strpath
4646
workspace_api_mock.get_status.return_value = \
47-
WorkspaceFileInfo('/notebook-name', NOTEBOOK, WorkspaceLanguage.SCALA)
47+
WorkspaceFileInfo('/notebook-name', NOTEBOOK, '22', WorkspaceLanguage.SCALA)
4848
runner = CliRunner()
4949
runner.invoke(cli.export_workspace_cli, ['--format', 'SOURCE', '/notebook-name', path])
5050
assert workspace_api_mock.export_workspace.call_args[0][1] == os.path.join(

0 commit comments

Comments
 (0)