From 6984bfc5e830a2701afc00d0c8346097e7fc2cb9 Mon Sep 17 00:00:00 2001 From: evilchili Date: Sat, 17 Feb 2024 15:24:13 -0800 Subject: [PATCH] adding encounters, fixing tests and cli --- pyproject.toml | 30 +++++++++++++++++---- rolltable/cli.py | 62 ++++++++++++++++++++++++++++++++------------ rolltable/tables.py | 3 ++- tests/test_tables.py | 20 +++++++++++--- 4 files changed, 90 insertions(+), 25 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2148d55..ddb32ce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,11 +18,31 @@ random_sets = { git = "https://github.com/evilchili/random-sets", branch="main" [tool.poetry.dev-dependencies] 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] roll-table = "rolltable.cli:app" - - -[build-system] -requires = ['poetry-core~=1.0'] -build-backend = 'poetry.core.masonry.api' diff --git a/rolltable/cli.py b/rolltable/cli.py index 874ae1b..a0b95d0 100644 --- a/rolltable/cli.py +++ b/rolltable/cli.py @@ -1,3 +1,4 @@ +from collections import defaultdict from rolltable.types import RollTable from rolltable import tables import typer @@ -8,6 +9,10 @@ from typing import List app = typer.Typer() +app_state = defaultdict( + str, + options=defaultdict(str), +) class OUTPUT_FORMATS(Enum): @@ -16,49 +21,74 @@ class OUTPUT_FORMATS(Enum): markdown = 'markdown' -@app.command("custom") -def custom( - sources: List[Path] = typer.Argument( - ..., - help="Path to one or more yaml-formatted source file."), +@app.callback() +def main( frequency: str = typer.Option( 'default', - help='use the specified frequency from the source file'), + help='use the specified frequency from the source file' + ), die: int = typer.Option( 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( False, help='If True, do not show the Roll column.', ), collapsed: bool = typer.Option( 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( 120, - help='Width of the table.'), + help='Width of the table.' + ), output: OUTPUT_FORMATS = typer.Option( 'text', 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. """ - 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()) - elif output == OUTPUT_FORMATS.markdown: - print(rt.as_markdown()) + +def print_table(table): + if app_state['output'] == OUTPUT_FORMATS.yaml: + print(table.as_yaml()) + elif app_state['output'] == OUTPUT_FORMATS.markdown: + print(table.as_markdown()) 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 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 diff --git a/rolltable/tables.py b/rolltable/tables.py index ecace52..15047e2 100644 --- a/rolltable/tables.py +++ b/rolltable/tables.py @@ -14,7 +14,8 @@ index = dict( psychadelic_effects=from_sources(['psychadelic_effects.yaml']), trinkets=from_sources(['trinkets.yaml']), wild_magic=from_sources(['wild_magic.yaml']), - spells=from_sources(['spells.yaml']) + spells=from_sources(['spells.yaml']), + encounters=from_sources(['encounters.yaml']), ) diff --git a/tests/test_tables.py b/tests/test_tables.py index 9018ac8..616e1b8 100644 --- a/tests/test_tables.py +++ b/tests/test_tables.py @@ -4,11 +4,25 @@ from rolltable import tables @pytest.mark.parametrize('table, expected', [ - (tables.wild_magic, ['d1000 ', 'A third eye', 'Advantage on perception checks']), - (tables.trinkets, ['d1000 ', 'ivory mimic']), - (tables.psychadelic_effects, ['d1000', 'Cosmic', 'mind expands', 'it will become so']), + (tables.wild_magic, ['A third eye', 'Advantage on perception checks']), + (tables.trinkets, ['ivory mimic']), + (tables.psychadelic_effects, ['Cosmic', 'mind expands', 'it will become so']), + (tables.encounters, ['None', 'Easy', 'Difficult', 'Dangerous', 'Deadly']) ]) def test_flat(table, expected): table.die = 1000 + assert 'd1000' in str(table) for txt in expected: 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