Skip to content

Commit

Permalink
style: apply ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
siemdejong committed Oct 1, 2024
1 parent edb2ad0 commit f772500
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 51 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: Ruff
on: [ push, pull_request ]
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ namespaces = false
[tool.setuptools.package-data]
shithappens = ["opensans/fonts/ttf/*.ttf", "images/*.png", "locales/*/LC_MESSAGES/*.mo"]

[tool.ruff]
extend-exclude = ["opensans"]

[tool.semantic_release]
version_variables = ["pyproject.toml:version"] # version location
build_command = "pip install build && python -m build"
Expand Down
16 changes: 7 additions & 9 deletions src/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
from tkinter import filedialog
from pathlib import Path

from shithappens.utils import install_lang
from shithappens.create_cards import main


def select_folder():
root = tk.Tk()
root.withdraw()
folder_path = filedialog.askdirectory(parent=root)
root.destroy()
return Path(folder_path)
root = tk.Tk()
root.withdraw()
folder_path = filedialog.askdirectory(parent=root)
root.destroy()
return Path(folder_path)


uploaded_file = st.file_uploader("Please provide your excel input")

if uploaded_file is not None:

df = pd.read_excel(uploaded_file)
expansion_name = st.text_input("Expansion name")
input_dir = st.session_state.get("input_dir", None)
Expand All @@ -36,7 +35,6 @@ def select_folder():
format = st.selectbox("Output format", ["pdf", "png"])
workers = st.number_input("Number of workers", value=4)
chunks = st.number_input("Number of chunks for the workers to process", value=30)


if st.button("Create cards"):
main(
Expand All @@ -49,4 +47,4 @@ def select_folder():
chunks=chunks,
lang=lang,
)
st.write("Cards written to ", output_dir)
st.write("Cards written to ", output_dir)
79 changes: 69 additions & 10 deletions src/shithappens/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,85 @@
from shithappens.create_cards import main_cli as create_cli
from shithappens.sort_situations import main_cli as sort_cli


@click.group(help="Create custom Shit Happens expansion playing cards.")
def cli():
pass


@cli.command(help="Create cards.")
@click.argument("input_dir")
@click.option("-n", "--name", "name", help="Expansion name. If no name is specified, infers name from input_dir.")
@click.option(
"-n",
"--name",
"name",
help="Expansion name. If no name is specified, infers name from input_dir.",
)
@click.option("-m", "--merge", "merge", is_flag=True, help="Merge output.")
@click.option("-s", "--side", "side", type=click.Choice(["front", "back", "both"], case_sensitive=False), default="both", help="Side(s) to generate.")
@click.option("-l", "--language", "lang", type=click.Choice(["en", "nl"], case_sensitive=False), default="en", help="Language.")
@click.option("-f", "--format", "format", type=click.Choice(["pdf", "png"], case_sensitive=False), default="pdf", help="Output format.")
@click.option("-w", "--workers", "workers", type=int, default=4, help="Number of workers.")
@click.option("-c", "--chunks", "chunks", type=int, default=30, help="Number of chunks for the workers to process")
@click.option(
"-s",
"--side",
"side",
type=click.Choice(["front", "back", "both"], case_sensitive=False),
default="both",
help="Side(s) to generate.",
)
@click.option(
"-l",
"--language",
"lang",
type=click.Choice(["en", "nl"], case_sensitive=False),
default="en",
help="Language.",
)
@click.option(
"-f",
"--format",
"format",
type=click.Choice(["pdf", "png"], case_sensitive=False),
default="pdf",
help="Output format.",
)
@click.option(
"-w", "--workers", "workers", type=int, default=4, help="Number of workers."
)
@click.option(
"-c",
"--chunks",
"chunks",
type=int,
default=30,
help="Number of chunks for the workers to process",
)
def create(**kwargs):
create_cli(**kwargs)


@cli.command(help="Rank situation.")
@click.argument("input_dir")
@click.option("-s", "--strategy", "strategy", type=click.Choice(["swiss", "round-robin"], case_sensitive=False), default="swiss", help="Ranking strategy.")
@click.option("-r", "--rounds", "rounds", type=int, default=9, help="The number of rounds to use with the swiss strategy.")
@click.option("-p", "--prescore", "prescore", type=int, default=10, help="The number of groups to prescore.")
@click.option(
"-s",
"--strategy",
"strategy",
type=click.Choice(["swiss", "round-robin"], case_sensitive=False),
default="swiss",
help="Ranking strategy.",
)
@click.option(
"-r",
"--rounds",
"rounds",
type=int,
default=9,
help="The number of rounds to use with the swiss strategy.",
)
@click.option(
"-p",
"--prescore",
"prescore",
type=int,
default=10,
help="The number of groups to prescore.",
)
def rank(**kwargs):
sort_cli(**kwargs)
sort_cli(**kwargs)
7 changes: 4 additions & 3 deletions src/shithappens/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from shithappens.utils import install_lang

install_lang("en")
_ = install_lang("en")


def verify_input_dir(input_dir: Path):
while True:
Expand Down Expand Up @@ -42,5 +43,5 @@ def verify_input_dir(input_dir: Path):

else:
xlsx_path = Path(xlsx_paths[0])
return xlsx_path, output_dir

return xlsx_path, output_dir
22 changes: 14 additions & 8 deletions src/shithappens/create_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from pathlib import Path
from typing import Literal, Optional

import click
import matplotlib.font_manager as fm
import matplotlib.image as mpimage
import matplotlib.pyplot as plt
Expand All @@ -16,7 +15,7 @@
from matplotlib.figure import Figure
from matplotlib.patches import Rectangle
from matplotlib.text import Annotation
from matplotlib.transforms import Bbox, Transform
from matplotlib.transforms import Bbox

try:
from tqdm import tqdm
Expand All @@ -30,6 +29,7 @@ def tqdm(iterable, *args, **kwargs):
from shithappens.card import Card
from shithappens.utils import merge_pdfs, slugify


def install_lang(locale: str):
localedir = Path(__file__).parent.resolve() / "locales"
lang = gettext.translation("shithappens", localedir=localedir, languages=[locale])
Expand Down Expand Up @@ -82,7 +82,12 @@ def text_with_wrap_autofit(
else:
alpha = 0

rect = Rectangle((bleed + (1 - width) * x, bleed + (1 - height) * y), width * x, height * y, alpha=alpha)
rect = Rectangle(
(bleed + (1 - width) * x, bleed + (1 - height) * y),
width * x,
height * y,
alpha=alpha,
)
ax.add_patch(rect)

# Get transformation to go from display to data-coordinates.
Expand All @@ -98,7 +103,9 @@ def text_with_wrap_autofit(
wrap_lines = 1
xy = (bleed + 0.5 * x, bleed + 0.95 * y)
while True:
wrapped_txt = "\n".join(textwrap.wrap(txt, width=len(txt) // wrap_lines, break_long_words=False))
wrapped_txt = "\n".join(
textwrap.wrap(txt, width=len(txt) // wrap_lines, break_long_words=False)
)

# For dramatic effect, place text after ellipsis on newline.
wrapped_txt = wrapped_txt.replace("... ", "...\n")
Expand Down Expand Up @@ -367,7 +374,6 @@ def save_card(
dpi: int = 300,
format: str = "pdf",
) -> None:

side_fn = _("front") if side == "front" else _("back")

output_dir = output_dir / side_fn
Expand Down Expand Up @@ -409,9 +415,11 @@ def create_card(
card.fig_back = plot_card_back(card, input_dir)
save_card(card, output_dir, "back", format=ext)


def _init(locale):
install_lang(locale)



def create_cards(
df: pd.DataFrame,
expansion_name: str,
Expand Down Expand Up @@ -455,7 +463,6 @@ def create_cards(


def main(**args) -> None:

install_lang(args["lang"])

try:
Expand All @@ -477,7 +484,6 @@ def main(**args) -> None:

# sort(xlsx_path)
else:

if args["name"]:
expansion_name = args["name"]
else:
Expand Down
Loading

0 comments on commit f772500

Please sign in to comment.