adding encounters, fixing tests and cli

This commit is contained in:
evilchili 2024-02-17 15:24:13 -08:00
parent 20f1608cbe
commit 6984bfc5e8
4 changed files with 90 additions and 25 deletions

View File

@ -18,11 +18,31 @@ random_sets = { git = "https://github.com/evilchili/random-sets", branch="main"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
pytest = "^7.4.3" pytest = "^7.4.3"
black = "^23.3.0"
isort = "^5.12.0"
pyproject-autoflake = "^1.0.2"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
[tool.black]
line-length = 120
target-version = ['py310']
[tool.isort]
multi_line_output = 3
line_length = 120
include_trailing_comma = true
[tool.autoflake]
check = false # return error code if changes are needed
in-place = true # make changes to files instead of printing diffs
recursive = true # drill down directories recursively
remove-all-unused-imports = true # remove all unused imports (not just those from the standard library)
ignore-init-module-imports = true # exclude __init__.py when removing unused imports
remove-duplicate-keys = true # remove all duplicate keys in objects
remove-unused-variables = true # remove unused variables
[tool.poetry.scripts] [tool.poetry.scripts]
roll-table = "rolltable.cli:app" roll-table = "rolltable.cli:app"
[build-system]
requires = ['poetry-core~=1.0']
build-backend = 'poetry.core.masonry.api'

View File

@ -1,3 +1,4 @@
from collections import defaultdict
from rolltable.types import RollTable from rolltable.types import RollTable
from rolltable import tables from rolltable import tables
import typer import typer
@ -8,6 +9,10 @@ from typing import List
app = typer.Typer() app = typer.Typer()
app_state = defaultdict(
str,
options=defaultdict(str),
)
class OUTPUT_FORMATS(Enum): class OUTPUT_FORMATS(Enum):
@ -16,49 +21,74 @@ class OUTPUT_FORMATS(Enum):
markdown = 'markdown' markdown = 'markdown'
@app.command("custom") @app.callback()
def custom( def main(
sources: List[Path] = typer.Argument(
...,
help="Path to one or more yaml-formatted source file."),
frequency: str = typer.Option( frequency: str = typer.Option(
'default', 'default',
help='use the specified frequency from the source file'), help='use the specified frequency from the source file'
),
die: int = typer.Option( die: int = typer.Option(
20, 20,
help='The size of the die for which to create a table'), help='The size of the die for which to create a table'
),
hide_rolls: bool = typer.Option( hide_rolls: bool = typer.Option(
False, False,
help='If True, do not show the Roll column.', help='If True, do not show the Roll column.',
), ),
collapsed: bool = typer.Option( collapsed: bool = typer.Option(
True, True,
help='If True, collapse multiple die values with the same option.'), help='If True, collapse multiple die values with the same option.'
),
width: int = typer.Option( width: int = typer.Option(
120, 120,
help='Width of the table.'), help='Width of the table.'
),
output: OUTPUT_FORMATS = typer.Option( output: OUTPUT_FORMATS = typer.Option(
'text', 'text',
help='The output format to use.', help='The output format to use.',
) )
):
app_state['options'] = {
'frequency': frequency,
'die': die,
'hide_rolls': hide_rolls,
}
app_state['collapsed'] = collapsed
app_state['width'] = width
app_state['output'] = output
@app.command("custom")
def custom(
sources: List[Path] = typer.Argument(
...,
help="Path to one or more yaml-formatted source file."),
): ):
""" """
Create roll tables from custom sources. Create roll tables from custom sources.
""" """
rt = RollTable([Path(s).read_text() for s in sources], frequency=frequency, die=die, hide_rolls=hide_rolls) rt = RollTable([Path(s).read_text() for s in sources], **app_state['options'])
print_table(rt)
if output == OUTPUT_FORMATS.yaml:
print(rt.as_yaml()) def print_table(table):
elif output == OUTPUT_FORMATS.markdown: if app_state['output'] == OUTPUT_FORMATS.yaml:
print(rt.as_markdown()) print(table.as_yaml())
elif app_state['output'] == OUTPUT_FORMATS.markdown:
print(table.as_markdown())
else: else:
print(rt.as_table(width=width, expanded=not collapsed)) print(table.as_table(
width=app_state['width'],
expanded=not app_state['collapsed']
))
def make_callback(roll_table_instance): def make_callback(roll_table_instance):
def inner(): def inner():
print(roll_table_instance.as_table()) roll_table_instance.frequency = app_state['options']['frequency']
roll_table_instance.die = app_state['options']['die']
print_table(roll_table_instance)
return inner return inner

View File

@ -14,7 +14,8 @@ index = dict(
psychadelic_effects=from_sources(['psychadelic_effects.yaml']), psychadelic_effects=from_sources(['psychadelic_effects.yaml']),
trinkets=from_sources(['trinkets.yaml']), trinkets=from_sources(['trinkets.yaml']),
wild_magic=from_sources(['wild_magic.yaml']), wild_magic=from_sources(['wild_magic.yaml']),
spells=from_sources(['spells.yaml']) spells=from_sources(['spells.yaml']),
encounters=from_sources(['encounters.yaml']),
) )

View File

@ -4,11 +4,25 @@ from rolltable import tables
@pytest.mark.parametrize('table, expected', [ @pytest.mark.parametrize('table, expected', [
(tables.wild_magic, ['d1000 ', 'A third eye', 'Advantage on perception checks']), (tables.wild_magic, ['A third eye', 'Advantage on perception checks']),
(tables.trinkets, ['d1000 ', 'ivory mimic']), (tables.trinkets, ['ivory mimic']),
(tables.psychadelic_effects, ['d1000', 'Cosmic', 'mind expands', 'it will become so']), (tables.psychadelic_effects, ['Cosmic', 'mind expands', 'it will become so']),
(tables.encounters, ['None', 'Easy', 'Difficult', 'Dangerous', 'Deadly'])
]) ])
def test_flat(table, expected): def test_flat(table, expected):
table.die = 1000 table.die = 1000
assert 'd1000' in str(table)
for txt in expected: for txt in expected:
assert txt in str(table) assert txt in str(table)
def test_encounter_frequencies():
table = tables.encounters
def test_markdown():
tables.trinkets.die = 1
md = tables.trinkets.as_markdown()
assert '| Roll | Trinket ' in md
assert '| ---- |' in md
assert 'd1' in md