Skip to content

Commit 84d6c4b

Browse files
authored
Merge pull request #20 from SkwalExe/add-ruff
Add ruff for linting and formatting
2 parents 9a9713d + 8f5d1d2 commit 84d6c4b

File tree

10 files changed

+191
-98
lines changed

10 files changed

+191
-98
lines changed

.flake8

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

pdm.lock

Lines changed: 28 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,13 @@ octologo = "octologo.__main__:main"
3030
[tool.pdm]
3131
distribution = true
3232

33+
[tool.pdm.dev-dependencies]
34+
dev = [
35+
"ruff>=0.4.7",
36+
]
37+
38+
[tool.pdm.scripts]
39+
format = "ruff format"
40+
format-check = "ruff format --check"
41+
lint = "ruff check --fix --show-fixes"
42+
lint-check = "ruff check"

ruff.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Default formatting rules are pretty good
2+
line-length = 120
3+
[lint]
4+
# Read more here https://beta.ruff.rs/docs/rules/
5+
# By default, Ruff enables Flake8's E and F rules
6+
# Pyflakes - F, pycodestyle - E, W
7+
# flake8-builtins - A
8+
# flake8-annotations - ANN
9+
# flake8-simplify - SIM
10+
# Pylint - PLC, PLE, PLW
11+
# isort - I
12+
select = ['E', 'F', 'W', 'A', 'PLC', 'PLE', 'PLW', 'I', 'SIM', 'ANN']
13+
ignore = [
14+
'ANN101'
15+
]

src/octologo/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Simple program that generates a logo for your open source projects"""
22

3-
__version__ = "3.0.0"
3+
__version__ = "3.0.0-dev"

src/octologo/__main__.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import os
2+
from collections.abc import Generator
23
from time import time
3-
from octologo.utils import BASE_DIR, style_names, styles, logger
4-
from octologo import __version__
5-
from octologo.wizard import TextQuestion, SelectQuestion, Wizard, inq_ask
4+
5+
from click_extra import ExtraContext, Parameter, extra_command, option
6+
from PIL.Image import Image
67
from textual.app import App
7-
from textual.widgets import Header, Footer, Label, LoadingIndicator
8-
from textual.validation import Length
9-
from PIL import Image
108
from textual.events import Key
11-
from click_extra import extra_command, option, ExtraContext, Parameter
9+
from textual.validation import Length
10+
from textual.widgets import Footer, Header, Label, LoadingIndicator
11+
12+
from octologo import __version__
13+
from octologo.utils import BASE_DIR, logger, style_names, styles
14+
from octologo.wizard import SelectQuestion, TextQuestion, Wizard, inq_ask
1215

1316
# from textual import log
1417

@@ -23,7 +26,7 @@
2326
]
2427

2528

26-
def get_output_filaname(project_name):
29+
def get_output_filaname(project_name: str) -> str:
2730
return f"octologo_{project_name}_{int(time())}.png"
2831

2932

@@ -38,16 +41,16 @@ class OctoLogoApp(App):
3841
TITLE = "Octo Logo Wizard"
3942
finished: bool = False
4043
save_to: str | None = None
41-
result: Image.Image | None = None
44+
result: Image | None = None
4245
loading_wid: LoadingIndicator = LoadingIndicator(classes="hidden")
4346

44-
async def on_key(self, event: Key):
47+
async def on_key(self, event: Key) -> None:
4548
if event.key == "enter" and self.finished:
4649
await self.action_quit()
4750
elif event.key == "v" and self.finished:
4851
self.result.show()
4952

50-
def on_wizard_finished(self, message: Wizard.Finished):
53+
def on_wizard_finished(self, message: Wizard.Finished) -> None:
5154
# Get the wizard answers and the wizard's id
5255
self.answers.update(message.answers)
5356
finished_wizard_id = message.wizard_id
@@ -70,7 +73,7 @@ def on_wizard_finished(self, message: Wizard.Finished):
7073
self.set_timer(2, self.final_message)
7174

7275
# Final message
73-
def final_message(self):
76+
def final_message(self) -> None:
7477
self.loading_wid.add_class("hidden")
7578
self.mount(
7679
Label(
@@ -82,7 +85,7 @@ def final_message(self):
8285
self.result.save(self.save_to)
8386
self.finished = True
8487

85-
def compose(self):
88+
def compose(self) -> Generator:
8689
self.app.title = f"Octo Logo v{__version__}"
8790

8891
yield Header(show_clock=True)
@@ -95,7 +98,7 @@ def compose(self):
9598
yield self.loading_wid
9699

97100

98-
def disable_ansi(ctx: ExtraContext, param: Parameter, val: bool):
101+
def disable_ansi(ctx: ExtraContext, param: Parameter, val: bool) -> bool:
99102
ctx.color = not val
100103

101104
# We must return the value for the main function no_ansi parameter not to be None
@@ -106,7 +109,7 @@ def disable_ansi(ctx: ExtraContext, param: Parameter, val: bool):
106109
@option(
107110
"-t", "--no-tui", is_flag=True, help="Dont use the Textual Terminal User Interface"
108111
)
109-
def main(no_tui: bool):
112+
def main(no_tui: bool) -> None:
110113
use_tui = not no_tui
111114

112115
if use_tui:
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from PIL.Image import Image
2+
13
from . import underline_core
24

35
display_name = "First letters underlined"
46
active = True
57
questions = underline_core.questions
68

79

8-
def get_image(answers):
10+
def get_image(answers: dict) -> Image:
911
return underline_core.get_image(answers)

src/octologo/styles/underline_core.py

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,63 @@
1-
from octologo.wizard import TextQuestion, SelectQuestion
2-
from textual.validation import Number
3-
from octologo.utils import font_list, color_scheme_names, FONTS_DIR, color_schemes, os, get_font_height, get_text_size
4-
from PIL import Image, ImageDraw, ImageFont, ImageColor
51
import sys
62

3+
from octologo.utils import (
4+
FONTS_DIR,
5+
color_scheme_names,
6+
color_schemes,
7+
font_list,
8+
get_font_height,
9+
get_text_size,
10+
os,
11+
)
12+
from octologo.wizard import SelectQuestion, TextQuestion
13+
from PIL import Image, ImageColor, ImageDraw, ImageFont
14+
from PIL.Image import Image as ImageClass
15+
from PIL.ImageFont import FreeTypeFont
16+
from textual.validation import Number
17+
718
sys.path.append("..")
819

920

1021
questions = [
11-
SelectQuestion("font", "Select a font", [(font, font) for font in font_list], "Iosevka-Nerd-Font-Complete.ttf"),
22+
SelectQuestion(
23+
"font",
24+
"Select a font",
25+
[(font, font) for font in font_list],
26+
"Iosevka-Nerd-Font-Complete.ttf",
27+
),
1228
SelectQuestion("color", "Select a color scheme", color_scheme_names, "adi1090x"),
13-
TextQuestion("underline_count", "Lettrs to undrline", [Number(minimum=0)], "1", "1"),
29+
TextQuestion(
30+
"underline_count", "Lettrs to undrline", [Number(minimum=0)], "1", "1"
31+
),
1432
TextQuestion("padding_x", "Padding x (px)", [Number()], "200", "200"),
1533
TextQuestion("padding_y", "Padding y (px)", [Number()], "20", "20"),
1634
TextQuestion("gap", "Gap between text and bar (px)", [Number()], "20", "20"),
1735
TextQuestion("bar_size", "Bar weight (px)", [Number()], "20", "20"),
18-
TextQuestion("additionnal_bar_width", "Additionnal bar width (px)", [Number()], "20", "20"),
36+
TextQuestion(
37+
"additionnal_bar_width", "Additionnal bar width (px)", [Number()], "20", "20"
38+
),
1939
]
2040

2141
active = False
2242

2343

24-
def get_image(answers):
44+
def get_image(answers: dict) -> ImageClass:
2545
# Load the selected font
2646
font_size = 500
27-
font = ImageFont.truetype(os.path.join(FONTS_DIR, answers["font"]), font_size)
47+
font: FreeTypeFont = ImageFont.truetype(os.path.join(FONTS_DIR, answers["font"]), font_size)
2848

2949
# Set the colors
30-
background = ImageColor.getrgb(color_schemes[answers['color']]["background"])
31-
text = ImageColor.getrgb(color_schemes[answers['color']]["text"])
32-
accent = ImageColor.getrgb(color_schemes[answers['color']]["accent"])
50+
background = ImageColor.getrgb(color_schemes[answers["color"]]["background"])
51+
text = ImageColor.getrgb(color_schemes[answers["color"]]["text"])
52+
accent = ImageColor.getrgb(color_schemes[answers["color"]]["accent"])
3353

3454
# Get the width and height of the texts
35-
text_width, text_height = get_text_size(answers['name'], font)
55+
text_width, text_height = get_text_size(answers["name"], font)
3656
font_height = get_font_height(font)
3757

3858
# Get the correct image width and height
39-
image_width = 2 * int(answers['padding_x']) + text_width
40-
image_height = 2 * int(answers['padding_y']) + font_height
59+
image_width = 2 * int(answers["padding_x"]) + text_width
60+
image_height = 2 * int(answers["padding_y"]) + font_height
4161

4262
# Create the image
4363
image = Image.new("RGB", (image_width, image_height), background)
@@ -46,39 +66,42 @@ def get_image(answers):
4666
# Get the text anchor type and position on the image (where the text will be drawn)
4767
# LM = Left/Middle
4868
anchor_type = "lm"
49-
anchor_x = int(answers['padding_x'])
50-
anchor_y = image_height / 2 - (int(answers['gap']) + int(answers['bar_size'])) / 2
69+
anchor_x = int(answers["padding_x"])
70+
anchor_y = image_height / 2 - (int(answers["gap"]) + int(answers["bar_size"])) / 2
5171

5272
anchor_pos = (anchor_x, anchor_y)
5373

54-
if int(answers['underline_count']) > 0:
74+
if int(answers["underline_count"]) > 0:
5575
# Get the bbox of the first n letter to underline
5676
first_letters_bbox = draw.textbbox(
5777
anchor_pos,
58-
answers['name'][:int(answers['underline_count'])],
78+
answers["name"][: int(answers["underline_count"])],
5979
font=font,
60-
anchor=anchor_type)
80+
anchor=anchor_type,
81+
)
6182

6283
# Get the underline position
63-
underline_start_x = first_letters_bbox[0] - int(answers['additionnal_bar_width'])
64-
underline_start_y = first_letters_bbox[3] + int(answers['gap'])
84+
underline_start_x = first_letters_bbox[0] - int(
85+
answers["additionnal_bar_width"]
86+
)
87+
underline_start_y = first_letters_bbox[3] + int(answers["gap"])
6588

66-
underline_end_x = int(answers['additionnal_bar_width']) + first_letters_bbox[2]
89+
underline_end_x = int(answers["additionnal_bar_width"]) + first_letters_bbox[2]
6790

68-
underline_end_y = underline_start_y + int(answers['bar_size'])
91+
underline_end_y = underline_start_y + int(answers["bar_size"])
6992

7093
underline_start = (underline_start_x, underline_start_y)
7194
underline_end = (underline_end_x, underline_end_y)
7295

7396
underline_pos = [underline_start, underline_end]
7497

7598
# Underline the first letter
76-
draw.rectangle(underline_pos, fill=accent, width=answers['bar_size'])
99+
draw.rectangle(underline_pos, fill=accent, width=answers["bar_size"])
77100

78101
# Draw the text
79102
draw.text(
80103
anchor_pos,
81-
answers['name'],
104+
answers["name"],
82105
font=font,
83106
fill=text,
84107
anchor=anchor_type,
@@ -87,7 +110,7 @@ def get_image(answers):
87110
# Redraw the first letter
88111
draw.text(
89112
anchor_pos,
90-
answers['name'][0],
113+
answers["name"][0],
91114
font=font,
92115
fill=accent,
93116
anchor=anchor_type,

0 commit comments

Comments
 (0)