Skip to content

Commit

Permalink
Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasily Negrebetskiy committed Aug 28, 2024
1 parent 64dc200 commit b8ba334
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 42 deletions.
6 changes: 3 additions & 3 deletions muckraker/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async def get_issue(issue_id: str):
body=issue_dict["body"],
fonts=issue_dict["fonts"],
output=pdf_path,
image_dir=dir_path
image_dir=dir_path,
)
with open(pdf_path, "rb") as fd:
buf = BytesIO(fd.read())
Expand All @@ -118,5 +118,5 @@ async def get_issue(issue_id: str):
# Delete cached data
await cache.delete_issue(issue_id)

headers = {'Content-Disposition': 'attachment; filename="out.pdf"'}
return Response(pdf_bytes, headers=headers, media_type='application/pdf')
headers = {"Content-Disposition": 'attachment; filename="out.pdf"'}
return Response(pdf_bytes, headers=headers, media_type="application/pdf")
22 changes: 7 additions & 15 deletions muckraker/md_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class FilterExtension(Extension):
""" Ignore some tags """
"""Ignore some tags"""

def extendMarkdown(self, md: Markdown) -> None:
md.inlinePatterns.deregister("link")
Expand All @@ -26,17 +26,13 @@ def extendMarkdown(self, md: Markdown) -> None:


class ImagePathProcessor(ImageInlineProcessor):
""" Return an `img` element from the given match. """
"""Return an `img` element from the given match."""

def __init__(self, pattern: str, md: Markdown, image_dir: str = ""):
def __init__(self, pattern: str, md: Markdown, image_dir: str = "") -> None:
super().__init__(pattern, md)
self.image_dir = image_dir

def handleMatch(
self,
m: re.Match[str],
data: str
) -> tuple[etree.Element | None, int | None, int | None]:
def handleMatch(self, m: re.Match[str], data: str) -> tuple[etree.Element | None, int | None, int | None]:
el, start, ind = super().handleMatch(m, data)
src_path = Path(el.get("src"))
src_path = Path(self.image_dir) / src_path
Expand All @@ -45,9 +41,9 @@ def handleMatch(


class ImagePathExtension(Extension):
""" Modify image paths so that Weasyprint could handle them """
"""Modify image paths so that Weasyprint could handle them"""

def __init__(self, **kwargs):
def __init__(self, **kwargs) -> None:
self.config = {"image_dir": ["", "Images root directory"]}
super().__init__(**kwargs)

Expand All @@ -56,9 +52,5 @@ def extendMarkdown(self, md: Markdown) -> None:
md.inlinePatterns.deregister("image_reference")
md.inlinePatterns.deregister("short_image_ref")

processor = ImagePathProcessor(
IMAGE_LINK_RE,
md,
self.getConfig("image_dir")
)
processor = ImagePathProcessor(IMAGE_LINK_RE, md, self.getConfig("image_dir"))
md.inlinePatterns.register(processor, "image_path", 140)
2 changes: 1 addition & 1 deletion muckraker/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Issue(BaseModel):
body: str = Field(max_length=MAX_BODY_LEN)
fonts: Optional[IssueFonts] = IssueFonts()

@model_validator(mode='before')
@model_validator(mode="before")
@classmethod
def validate_to_json(cls, value):
if isinstance(value, str):
Expand Down
27 changes: 11 additions & 16 deletions muckraker/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,17 @@ def font_size_css(selector: str, size_pt: int | None):
return f"{selector} {{ font-size: {size_pt}pt !important; }}\n"


def render_issue(
page: dict,
header: dict,
body: str,
fonts: dict,
output: str,
image_dir: str = ""
) -> None:
def render_issue(page: dict, header: dict, body: str, fonts: dict, output: str, image_dir: str = "") -> None:
# Sanitize Markdown and convert it to HTML
body = nh3.clean(body, tags=TAGS)
md = Markdown(extensions=[
"tables",
"sane_lists",
FilterExtension(),
ImagePathExtension(image_dir=image_dir)
])
md = Markdown(
extensions=[
"tables",
"sane_lists",
FilterExtension(),
ImagePathExtension(image_dir=image_dir),
]
)
body = md.convert(body)

# Sanitize all str header fields
Expand All @@ -55,7 +50,7 @@ def render_issue(
page=page,
header=header,
body=body,
static="file://" + str(STATIC.resolve())
static="file://" + str(STATIC.resolve()),
)

# Configure fonts
Expand All @@ -74,7 +69,7 @@ def render_issue(
HTML(string=html).write_pdf(
output,
stylesheets=[css, fonts_css],
font_config=font_config
font_config=font_config,
)


Expand Down
2 changes: 1 addition & 1 deletion muckraker/sqlcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, path: str) -> None:
""")

async def put_issue(self, issue_id: str, issue_dict: dict) -> None:
data = json.dumps(issue_dict).encode('utf-8')
data = json.dumps(issue_dict).encode("utf-8")
async with aiosqlite.connect(self.path) as db:
await db.execute("INSERT INTO issues (issue_id, data) VALUES (?, ?)", (issue_id, data))
await db.commit()
Expand Down
9 changes: 3 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@
"nisi ut aliquip ex ea commodo consequat."
)
__ISSUE_DICT = {
"page": {
"size": "demitab",
"bg": None
},
"page": {"size": "demitab", "bg": None},
"header": {
"title": "Muckraker",
"subtitle": "A vintage gazette generator for creative projects",
"no": "№ 22",
"date": "April 1, 2024",
"cost": "Price 1 c.p.",
"title_font": 10
"title_font": 10,
},
"body": LOREM
"body": LOREM,
}


Expand Down

0 comments on commit b8ba334

Please sign in to comment.