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 newline at the end of formatted notebooks #36

Merged
merged 2 commits into from
Feb 10, 2023
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
2 changes: 1 addition & 1 deletion blackbricks/blackbricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def format_str(content: str, config: FormatConfig = FormatConfig()) -> str:

output_ws_normalized += line + "\n"

return output_ws_normalized.strip()
return output_ws_normalized.strip() + "\n"


def _format_sql_cell(
Expand Down
1 change: 0 additions & 1 deletion blackbricks/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def process_files(
n_notebooks = 0

for file_ in files:

try:
content = file_.content
except UnicodeDecodeError:
Expand Down
26 changes: 23 additions & 3 deletions blackbricks/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,32 @@ class RemoteNotebook(File):

@property
def content(self) -> str:
return self.api_client.read_notebook(self.path)
"""
Get the content of the notebook as a string.

Note: Databricks will not include a trailing newlines from a notebook.
That is, even if you checkout a notebook from a repo where there is a trailing
newline, Databricks won't "show" an empty line in the UI. And similarly, this
API doesn't include it. This contrasts with the prefered style of terminating
the files with a newline, as enforced by black and blackbricks. To avoid
perpetually reporting diffs due to newlines, we add a trailing newline to the
content, _assuming_ that the
we assume it to be present and add it back if it is missing. Failing to do this
will cause blackbricks to perpetually report that it wants to add a trailing
newline to remote notebooks.
"""
return self.api_client.read_notebook(self.path) + "\n"

@content.setter
def content(self, new_content: str, /) -> None:
"""
Set the content of the notebook to the given string.

Note: We do _not_ need to do the inverse handling of trailing newlines as in
the getter. The new content here is presumably the output of blackbricks, and
we should let Databricks import that from the same source as it would find by
checking out a repo notebook that has been formatted with blackbricks.
"""
self.api_client.write_notebook(self.path, new_content)


Expand All @@ -71,12 +93,10 @@ def resolve_filepaths(paths: List[str]) -> List[str]:
raise typer.Exit(1)

if os.path.isdir(path):

# Recursively add all the files/dirs in path to the paths to be consumed.
paths.extend([os.path.join(path, f) for f in os.listdir(path)])

else:

file_paths.append(path)

return file_paths
Expand Down
2 changes: 1 addition & 1 deletion test_notebooks/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ def test_func(input_param):
# MAGIC %sql
# MAGIC SELECT id
# MAGIC FROM test_table
# MAGIC LIMIT 1
# MAGIC LIMIT 1