Skip to content

Commit

Permalink
fix!: add newline at the end of formatted notebooks (#36)
Browse files Browse the repository at this point in the history
Fixes #37 and closes #38.

BREAKING CHANGE: All notebooks will now have a newline character `\n` at the end of the file.
  • Loading branch information
weery authored Feb 10, 2023
1 parent 5cd9989 commit 1ba5094
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
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

0 comments on commit 1ba5094

Please sign in to comment.