Skip to content

Commit 6b43f4d

Browse files
authored
Add pre-commit hook (#6)
1 parent d6d21a7 commit 6b43f4d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+391
-370
lines changed

.flake8

Lines changed: 0 additions & 128 deletions
This file was deleted.

.pre-commit-config.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Pre-commit hooks
2+
# 1. Run ruff on all python files
3+
repos:
4+
- repo: https://github.com/astral-sh/ruff-pre-commit
5+
# Ruff version.
6+
rev: v0.9.4
7+
hooks:
8+
# Run the linter.
9+
# - id: ruff
10+
# Run the formatter.
11+
- id: ruff
12+
args: ["check", "--select", "I", "--fix"]
13+
- id: ruff-format

conftest.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
import os
22
import sys
3+
34
import pytest
45

6+
57
# This collects tests that aren't standard pytest tests
68
def pytest_collect_file(parent, file_path):
79
# Special handling for test_validation.py
810
if file_path.name == "test_validation.py":
911
return ValidationTestFile.from_parent(parent, path=file_path)
1012
return None
1113

14+
1215
class ValidationTestFile(pytest.File):
1316
def collect(self):
1417
# Create a special test item for test_validation.py
1518
yield ValidationTestItem.from_parent(self, name="test_validation")
1619

20+
1721
class ValidationTestItem(pytest.Item):
1822
def runtest(self):
1923
# Run the test_validation.py script with "schema" as the argument
2024
import subprocess
21-
print(f"\n{'-'*80}\nRunning test_validation.py with 'schema' argument...")
25+
26+
print(f"\n{'-' * 80}\nRunning test_validation.py with 'schema' argument...")
2227

2328
result = subprocess.run(
2429
[sys.executable, str(self.fspath), "schema"],
@@ -34,22 +39,24 @@ def runtest(self):
3439
if result.returncode != 0:
3540
raise ValidationTestFailure(result.stdout, result.stderr)
3641

37-
print(f"test_validation.py completed successfully.\n{'-'*80}")
42+
print(f"test_validation.py completed successfully.\n{'-' * 80}")
3843

3944
def reportinfo(self):
4045
return self.fspath, 0, f"test_validation.py schema"
4146

47+
4248
class ValidationTestFailure(Exception):
4349
def __init__(self, stdout, stderr):
4450
self.stdout = stdout
4551
self.stderr = stderr
4652
super().__init__(f"test_validation.py failed")
4753

54+
4855
@pytest.hookimpl(tryfirst=True)
4956
def pytest_exception_interact(node, call, report):
5057
if isinstance(call.excinfo.value, ValidationTestFailure):
5158
failure = call.excinfo.value
5259
print(f"test_validation.py failed!")
5360
if failure.stderr:
5461
print(f"STDERR:\n{failure.stderr}")
55-
print(f"{'-'*80}")
62+
print(f"{'-' * 80}")

jsondoc/bin/convert_jsondoc.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import sys
3+
34
import pypandoc
45

56
from jsondoc.convert.html import html_to_jsondoc
@@ -81,9 +82,9 @@ def convert_to_jsondoc(
8182

8283
input_content = None
8384
if input_file is None:
84-
assert (
85-
source_format is not None
86-
), "Source format must be specified if input file is not provided"
85+
assert source_format is not None, (
86+
"Source format must be specified if input file is not provided"
87+
)
8788

8889
# Read from stdin
8990
input_content = sys.stdin.read()
@@ -103,9 +104,9 @@ def convert_to_jsondoc(
103104

104105
if source_format == "jsondoc":
105106
assert target_format is not None, "Target format must be specified"
106-
assert (
107-
target_format == "markdown"
108-
), "Currently can only convert from JSON-DOC to Markdown"
107+
assert target_format == "markdown", (
108+
"Currently can only convert from JSON-DOC to Markdown"
109+
)
109110

110111
if input_content is None:
111112
raise Exception(

jsondoc/convert/html.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,16 +373,14 @@ def __init__(self, **options):
373373
self.options.update(options)
374374
if self.options["strip"] is not None and self.options["convert"] is not None:
375375
raise ValueError(
376-
"You may specify either tags to strip or tags to"
377-
" convert, but not both."
376+
"You may specify either tags to strip or tags to convert, but not both."
378377
)
379378

380379
def convert(self, html: str | bytes) -> Page | BlockBase | List[BlockBase]:
381380
soup = BeautifulSoup(html, "html.parser")
382381
return self.convert_soup(soup)
383382

384383
def convert_soup(self, soup: BeautifulSoup) -> Page | BlockBase | List[BlockBase]:
385-
386384
children = self.process_tag(soup, convert_as_inline=False, children_only=True)
387385
children = run_final_block_transformations(children)
388386
is_page = self._is_soup_page(soup)
@@ -483,9 +481,9 @@ def is_nested_node(el):
483481
# text = convert_fn(node, text, convert_as_inline)
484482
# current_level_object = convert_fn(node, convert_as_inline)
485483
convert_output = convert_fn(node, convert_as_inline)
486-
assert isinstance(
487-
convert_output, (ConvertOutput, NoneType)
488-
), f"Convert function {convert_fn} must return a ConvertOutput or None"
484+
assert isinstance(convert_output, (ConvertOutput, NoneType)), (
485+
f"Convert function {convert_fn} must return a ConvertOutput or None"
486+
)
489487

490488
if convert_output is not None:
491489
current_level_object = convert_output.main_object

jsondoc/convert/markdown.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import re
22
from typing import List, Union
3+
34
from pydantic import validate_call
5+
46
from jsondoc.convert.utils import get_rich_text_from_block
57
from jsondoc.models.block.base import BlockBase
68
from jsondoc.models.block.types.code import CodeBlock
@@ -19,7 +21,6 @@
1921
from jsondoc.models.shared_definitions import Annotations
2022
from jsondoc.serialize import load_jsondoc, load_page
2123

22-
2324
convert_heading_re = re.compile(r"convert_heading_(\d+)")
2425
line_beginning_re = re.compile(r"^", re.MULTILINE)
2526
whitespace_re = re.compile(r"[\t ]+")
@@ -60,7 +61,6 @@ def chomp(text):
6061

6162

6263
class JsonDocToMarkdownConverter(object):
63-
6464
class DefaultOptions:
6565
autolinks = True
6666
bullets = "*+-" # An iterable of bullet types.
@@ -90,8 +90,7 @@ def __init__(self, **options):
9090
self.options.update(options)
9191
if self.options["strip"] is not None and self.options["convert"] is not None:
9292
raise ValueError(
93-
"You may specify either tags to strip or tags to"
94-
" convert, but not both."
93+
"You may specify either tags to strip or tags to convert, but not both."
9594
)
9695

9796
def __getattr__(self, attr):
@@ -111,7 +110,6 @@ def convert_tag(block, convert_as_inline):
111110

112111
@validate_call
113112
def convert(self, obj: str | dict | BlockBase | List[BlockBase] | Page) -> str:
114-
115113
if isinstance(obj, (str, dict)):
116114
jsondoc = load_jsondoc(obj)
117115
else:
@@ -145,7 +143,6 @@ def _get_children_content(self, block: BlockBase, convert_as_inline: bool) -> st
145143

146144
@validate_call
147145
def convert_block(self, block: BlockBase, convert_as_inline: bool) -> str:
148-
149146
type_ = block.type
150147
convert_fn = getattr(self, f"convert_{type_}_block", None)
151148

jsondoc/convert/utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
2-
from datetime import datetime, timezone
32
import re
3+
from datetime import datetime, timezone
44
from typing import List, Literal, Optional, Type
55

66
from bs4 import Tag
@@ -109,7 +109,6 @@ def create_rich_text(
109109
color: str | None = None,
110110
annotations: Annotations | None = None,
111111
) -> RichTextText | RichTextEquation:
112-
113112
if text is not None and equation is not None:
114113
raise ValueError("Only one of text or equation must be provided")
115114

jsondoc/models/block/__init__.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@
1010

1111

1212
class Type(Enum):
13-
paragraph = 'paragraph'
14-
to_do = 'to_do'
15-
bulleted_list_item = 'bulleted_list_item'
16-
numbered_list_item = 'numbered_list_item'
17-
code = 'code'
18-
column = 'column'
19-
column_list = 'column_list'
20-
divider = 'divider'
21-
equation = 'equation'
22-
heading_1 = 'heading_1'
23-
heading_2 = 'heading_2'
24-
heading_3 = 'heading_3'
25-
image = 'image'
26-
quote = 'quote'
27-
equation_1 = 'equation'
28-
table = 'table'
29-
table_row = 'table_row'
30-
toggle = 'toggle'
13+
paragraph = "paragraph"
14+
to_do = "to_do"
15+
bulleted_list_item = "bulleted_list_item"
16+
numbered_list_item = "numbered_list_item"
17+
code = "code"
18+
column = "column"
19+
column_list = "column_list"
20+
divider = "divider"
21+
equation = "equation"
22+
heading_1 = "heading_1"
23+
heading_2 = "heading_2"
24+
heading_3 = "heading_3"
25+
image = "image"
26+
quote = "quote"
27+
equation_1 = "equation"
28+
table = "table"
29+
table_row = "table_row"
30+
toggle = "toggle"
3131

3232

3333
class Block(BlockBase):

0 commit comments

Comments
 (0)