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

[WIP] feat: Add utils for table printing. #30

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Singletons
.env
/__init__.py
.table.png

# IDE config folders
.idea
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
bottle==0.12.19
Pillow~=9.0.1
python-dotenv==0.19.2
slackclient==2.9.3
sentry-sdk[bottle]==0.16.2
tabulate~=0.8.9
1 change: 0 additions & 1 deletion slack_bot/messenger.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def compose_message(event: GitHubEvent) -> tuple[str, str | None]:
message: str = ""
details: str | None = None

# TODO: Beautify messages.
if event.type == EventType.BRANCH_CREATED:
message = f"Branch created by {event.user}: `{event.ref}`"
elif event.type == EventType.BRANCH_DELETED:
Expand Down
Binary file added utils/font-style.ttf
Binary file not shown.
107 changes: 107 additions & 0 deletions utils/table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""
Contains the `Table` class, to convert an iterable into an image of a table.
"""

from math import ceil

from PIL import ImageFont, Image, ImageDraw, ImageChops
from tabulate import tabulate

MARGIN_PIXELS = 2
IMAGE_PATH = "../.table.png"


class Table:
"""
Wrapper for a single method (`iter_to_image`), for consistency's sake only.
"""

@staticmethod
def data_to_image(headers: list[str], rows: list[list[str]]):
"""
Converts the passed iterable into an image of a table and saves it in the `.table` file.
"""
lines = Table._data_to_table(headers, rows)
Table._table_to_image(lines)

@staticmethod
def _data_to_table(headers: list[str], rows: list[list[str]]) -> list[str]:
"""
Uses the `tabulate` package to create a table using ASCII characters.
:param headers: List of headers of the table.
:param rows: Row-wise data in the table.
:return: ASCII table as a list of lines.
"""
table_string = tabulate(
rows,
headers=headers,
tablefmt="grid",
)
return table_string.splitlines()

@staticmethod
def _table_to_image(lines: list[str]) -> None:
"""
Draws the passed ASCII table using `Pillow` and save to `IMAGE_PATH`.
:param lines: ASCII table as a list of lines.
"""
font = ImageFont.truetype(font="font-style.ttf", size=40)

image, draw, max_line_height = Table._draw_background(lines, font)
Table._draw_text(draw, lines, font, max_line_height)
Table._save(image)

@staticmethod
def _draw_background(
lines: list[str],
font: ImageFont,
) -> tuple[Image, ImageDraw, int]:
# Calculate dimensions
tallest_line = max(lines, key=lambda line: font.getsize(line)[1])
widest_line = max(lines, key=lambda line: font.getsize(line)[0])
max_line_height, max_line_width = (
Table._fpt_to_px(font.getsize(tallest_line)[1]),
Table._fpt_to_px(font.getsize(widest_line)[0]),
)
image_height = ceil(max_line_height * 0.8 * len(lines) + 2 * MARGIN_PIXELS)
image_width = ceil(max_line_width + 2 * MARGIN_PIXELS)

# Draw the background
background_color = 255
image = Image.new("L", (image_width, image_height), color=background_color)
draw = ImageDraw.Draw(image)

return image, draw, max_line_height

@staticmethod
def _draw_text(
draw: ImageDraw,
lines: list[str],
font: ImageFont,
max_line_height: int,
):
for i, line in enumerate(lines):
draw.text(
(MARGIN_PIXELS, round(MARGIN_PIXELS + i * max_line_height * 0.8)),
line,
fill=0,
font=font,
)

@staticmethod
def _fpt_to_px(points: int) -> int:
"""
Converts font points to pixels
:param points: Font points value
:return: Pixels value
"""
return round(points * 96 / 72)

@staticmethod
def _save(image: Image):
background = image.getpixel((0, 0))
border = Image.new("L", image.size, background)
diff = ImageChops.difference(image, border)
bbox = diff.getbbox()
image = image.crop(bbox) if bbox else image
image.save(IMAGE_PATH)