Skip to content

Commit

Permalink
Format code with black
Browse files Browse the repository at this point in the history
  • Loading branch information
russellkan committed Sep 29, 2023
1 parent ff2a983 commit 0259005
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 26 deletions.
14 changes: 11 additions & 3 deletions n2y/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ def __init__(self, client, notion_data, page=None, get_children=True):

def get_children(self):
if self.has_children:
self.children = self.client.get_child_blocks(self.notion_id, self.page, True)
self.children = self.client.get_child_blocks(
self.notion_id, self.page, True
)
else:
self.children = []

Expand Down Expand Up @@ -316,7 +318,9 @@ def get_children(self):
)
for subsection in subsections:
notion_data = self.generate_item_block(subsection)
children.append(TableOfContentsItemBlock(self.client, notion_data, self.page))
children.append(
TableOfContentsItemBlock(self.client, notion_data, self.page)
)
if children:
self.has_children = True
self.children = children
Expand Down Expand Up @@ -666,7 +670,11 @@ def to_pandoc(self):
pandoc = self.filter_linebreaks(pandoc)
cells.append(
Cell(
("", [], []), AlignDefault(), RowSpan(1), ColSpan(1), [Plain(pandoc)]
("", [], []),
AlignDefault(),
RowSpan(1),
ColSpan(1),
[Plain(pandoc)],
)
)
return Row(("", [], []), cells)
Expand Down
12 changes: 10 additions & 2 deletions n2y/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ def merge_config(config_items, builtin_defaults, defaults):
master_defaults_copy = copy.deepcopy(builtin_defaults)
defaults_copy = copy.deepcopy(defaults)
config_item_copy = copy.deepcopy(config_item)
merged_config_item = {**master_defaults_copy, **defaults_copy, **config_item_copy}
merged_config_item = {
**master_defaults_copy,
**defaults_copy,
**config_item_copy,
}
merged_config_items.append(merged_config_item)
return merged_config_items

Expand Down Expand Up @@ -122,7 +126,11 @@ def _validate_config_item(config_item):
if "node_type" not in config_item:
logger.error("Export config item missing the 'node_type' key")
return False
if config_item["node_type"] not in ["page", "database_as_yaml", "database_as_files"]:
if config_item["node_type"] not in [
"page",
"database_as_yaml",
"database_as_files",
]:
logger.error(
"Invalid node_type in export config item: %s", config_item["node_type"]
)
Expand Down
4 changes: 3 additions & 1 deletion n2y/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ class HTTPResponseError(N2YError):

def __init__(self, response, message=None) -> None:
if message is None:
message = f"Request to Notion API failed with status: {response.status_code}"
message = (
f"Request to Notion API failed with status: {response.status_code}"
)
super().__init__(message)
self.status = response.status_code
self.headers = response.headers
Expand Down
4 changes: 3 additions & 1 deletion n2y/logger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging

FORMATTER = logging.Formatter("%(asctime)s - %(levelname)s - n2y.%(module)s: %(message)s")
FORMATTER = logging.Formatter(
"%(asctime)s - %(levelname)s - n2y.%(module)s: %(message)s"
)
HANDLER = logging.StreamHandler()
HANDLER.setFormatter(FORMATTER)
logging.basicConfig(level=logging.INFO, handlers=[HANDLER])
Expand Down
8 changes: 6 additions & 2 deletions n2y/notion.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ def get_default_classes(self):
notion_classes = {}
for notion_object, object_types in DEFAULT_NOTION_CLASSES.items():
if type(object_types) is dict:
notion_classes[notion_object] = {k: [v] for k, v in object_types.items()}
notion_classes[notion_object] = {
k: [v] for k, v in object_types.items()
}
else:
notion_classes[notion_object] = [object_types]
return notion_classes
Expand All @@ -118,7 +120,9 @@ def load_plugin(self, notion_classes):
else:
raise PluginError(f'Invalid notion object "{notion_object}"')

def _override_notion_classes(self, notion_object, object_types, default_object_types):
def _override_notion_classes(
self, notion_object, object_types, default_object_types
):
# E.g., there are many types of notion blocks but only one type of notion page.
notion_object_has_types = isinstance(default_object_types, dict)

Expand Down
4 changes: 3 additions & 1 deletion n2y/notion_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ def mock_property_value(property_value_type, content):


def mock_rich_text_property_value(text_blocks_descriptors):
return mock_property_value("rich_text", mock_rich_text_array(text_blocks_descriptors))
return mock_property_value(
"rich_text", mock_rich_text_array(text_blocks_descriptors)
)


def mock_formula_property_value(formula_type, content):
Expand Down
4 changes: 3 additions & 1 deletion n2y/plugins/footnotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def _append_footnote_to_ast(self, pandoc_ast, token, ref):
pandoc_ast.append(Str(suffix))

def _is_footnote(self):
return any(self._footnote_from_token(t) is not None for t in super().to_pandoc())
return any(
self._footnote_from_token(t) is not None for t in super().to_pandoc()
)

def _footnote_from_token(self, token):
if not isinstance(token, Str):
Expand Down
14 changes: 11 additions & 3 deletions n2y/plugins/jinjarenderpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ def join_to(foreign_keys, table, primary_key="notion_id"):
def list_matches(string, text):
return list(
re.finditer(
"(?<![a-zA-Z])" + re.escape(_canonicalize(string)) + "(?:s|es)?(?![a-zA-Z])",
"(?<![a-zA-Z])"
+ re.escape(_canonicalize(string))
+ "(?:s|es)?(?![a-zA-Z])",
_canonicalize(text),
re.IGNORECASE,
)
Expand Down Expand Up @@ -267,7 +269,9 @@ def to_pandoc(self, ignore_toc=False):
first_pass_output = self.jinja_environment.globals["first_pass_output"]
if first_pass_output.second_pass_is_requested:
first_pass_output_text = pandoc_ast_to_markdown(ast)
first_pass_output.set_lines(first_pass_output_text.splitlines(keepends=True))
first_pass_output.set_lines(
first_pass_output_text.splitlines(keepends=True)
)
ast = super().to_pandoc(ignore_toc=True)
jinja2.clear_caches()
return ast if ignore_toc else self.generate_toc(ast)
Expand Down Expand Up @@ -449,7 +453,11 @@ def render_content(notion_id, level_adjustment=0):
def to_pandoc(self):
if self.databases is None:
self._get_yaml_from_mentions()
if self.render_count < 1 or self.render_count < 2 and self.uses_first_pass_output:
if (
self.render_count < 1
or self.render_count < 2
and self.uses_first_pass_output
):
self._render_text()
if self.error:
children_ast = self._error_ast()
Expand Down
4 changes: 3 additions & 1 deletion tests/test_append_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def test_append_and_delete_blocks():
has_children=True,
)
del notion_block["url"]
creation_response = client.append_child_notion_blocks(page.notion_id, [notion_block])
creation_response = client.append_child_notion_blocks(
page.notion_id, [notion_block]
)
new_block = creation_response[0]
assert creation_response
deletion_response = client.delete_notion_block(new_block)
Expand Down
11 changes: 8 additions & 3 deletions tests/test_jinjarenderpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ def test_fuzzy_find_in():
catch_all_string = "a bc def"
assert fuzzy_find_in(dict_list, "a", "data") == [{"id": "1", "data": "a"}]
assert fuzzy_find_in(dict_list, catch_all_string, "data") == reversed_dict_list
assert fuzzy_find_in(dict_list, catch_all_string, "data", False) == reversed_dict_list
assert (
fuzzy_find_in(dict_list, catch_all_string, "data", False) == reversed_dict_list
)
assert fuzzy_find_in(dict_list, catch_all_string, "data", False, False) == dict_list
assert fuzzy_find_in(dict_list, catch_all_string, "data", True, False) == dict_list

Expand Down Expand Up @@ -215,7 +217,8 @@ def test_jinja_render_with_incorrect_db_property():
in markdown
)
assert (
'the available properties are "title", "notion_id", and "notion_url".' in markdown
'the available properties are "title", "notion_id", and "notion_url".'
in markdown
)


Expand Down Expand Up @@ -265,7 +268,9 @@ def test_jinja_render_with_filter_error():
pandoc_ast = page.to_pandoc()

markdown = pandoc_ast_to_markdown(pandoc_ast)
assert 'Recieved the message "type str doesn\'t define __round__ method"' in markdown
assert (
'Recieved the message "type str doesn\'t define __round__ method"' in markdown
)
assert 'The Jinja filter "round" raised this error' in markdown
assert 'argument(s): {\n\t"args": (\'a\',),\n\t"kwargs": {}\n}' in markdown

Expand Down
8 changes: 6 additions & 2 deletions tests/test_linkedheaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def mock_header_ast(level, suffix, notion_block):


def test_heading_1():
notion_block = mock_block("heading_1", {"rich_text": [mock_rich_text("Heading One")]})
notion_block = mock_block(
"heading_1", {"rich_text": [mock_rich_text("Heading One")]}
)
pandoc_ast, markdown = process_block(notion_block, linked_headers)
assert pandoc_ast == mock_header_ast(1, "One", notion_block)
assert markdown == f'# [Heading One]({notion_block["url"]})\n'
Expand All @@ -42,7 +44,9 @@ def test_heading_1_bolding_stripped():


def test_heading_2():
notion_block = mock_block("heading_2", {"rich_text": [mock_rich_text("Heading Two")]})
notion_block = mock_block(
"heading_2", {"rich_text": [mock_rich_text("Heading Two")]}
)
pandoc_ast, markdown = process_block(notion_block, linked_headers)
assert pandoc_ast == mock_header_ast(2, "Two", notion_block)
assert markdown == f'## [Heading Two]({notion_block["url"]})\n'
Expand Down
4 changes: 3 additions & 1 deletion tests/test_plugin_dbfootnotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
from n2y.plugins.dbfootnotes import PageMentionFootnote


def mock_page_mention_with_footnote(mentioned_page_parent, connect_parent_correctly=True):
def mock_page_mention_with_footnote(
mentioned_page_parent, connect_parent_correctly=True
):
client = Client("", plugins=["n2y.plugins.dbfootnotes"])
# The original page, with the footnote ref.
original_page = mock_page()
Expand Down
10 changes: 5 additions & 5 deletions tests/test_rich_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ def test_bold_space():


def test_italic_spaces():
notion_data = mock_rich_text_array([("An", []), (" italic ", ["italic"]), (word, [])])
notion_data = mock_rich_text_array(
[("An", []), (" italic ", ["italic"]), (word, [])]
)
pandoc_ast, markdown, plain_text = process_rich_text_array(notion_data)
assert pandoc_ast == [Str("An"), Space(), Emph([Str("italic")]), Space(), Str(word)]
assert markdown == "An *italic* word.\n"
Expand Down Expand Up @@ -188,8 +190,7 @@ def test_blended_annotated_spaces():
Str("?"),
]
assert (
markdown
== "**this** ***is*** *a*\n~~test~~[**` did`*"
markdown == "**this** ***is*** *a*\n~~test~~[**` did`*"
"*]{.underline}[` i pass`]{.underline}?\n"
)

Expand Down Expand Up @@ -236,8 +237,7 @@ def test_equation_inline():
Str("indeed"),
]
assert (
markdown
== "The Schrödinger Equation\n(${\\displaystyle "
markdown == "The Schrödinger Equation\n(${\\displaystyle "
"i\\hbar {\\frac {d}{dt}}\\vert \\Psi (t)\\"
"rangle={\\hat {H}}\\vert \\Psi (t)\\rangle}$)\nis"
" a very useful one indeed\n"
Expand Down

0 comments on commit 0259005

Please sign in to comment.