Fix yaml support

This commit is contained in:
evilchili 2022-08-06 17:33:54 -07:00
parent f80d087ac4
commit b43e972d1b
3 changed files with 15 additions and 7 deletions

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = 'dnd-rolltable' name = 'dnd-rolltable'
version = '1.1' version = '1.1.1'
license = 'The Unlicense' license = 'The Unlicense'
authors = ['Greg Boyington <evilchili@gmail.com>'] authors = ['Greg Boyington <evilchili@gmail.com>']
description = 'Generate roll tables using weighted random distributions' description = 'Generate roll tables using weighted random distributions'

View File

@ -22,7 +22,10 @@ def create(
help='The size of the die for which to create a table'), help='The size of the die for which to create a table'),
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.'),
yaml: bool = typer.Option(
False,
help='Render output as yaml.')
): ):
""" """
CLI for creating roll tables. CLI for creating roll tables.
@ -30,6 +33,10 @@ def create(
rt = tables.RollTable([Path(s).read_text() for s in sources], frequency=frequency, die=die) rt = tables.RollTable([Path(s).read_text() for s in sources], frequency=frequency, die=die)
if yaml:
print(rt.as_yaml)
return
rows = rt.rows if collapsed else rt.expanded_rows rows = rt.rows if collapsed else rt.expanded_rows
table = Table(*rows[0]) table = Table(*rows[0])
for row in rows[1:]: for row in rows[1:]:

View File

@ -113,7 +113,10 @@ class RollTable:
freqs = random.choices(options, weights=weights, k=self.die) freqs = random.choices(options, weights=weights, k=self.die)
values = [] values = []
for option in freqs: for option in freqs:
choice = random.choice(ds.data[option]) if ds.data[option] else '' if not ds.data[option]:
values.append([option])
continue
choice = random.choice(ds.data[option])
if hasattr(choice, 'keys'): if hasattr(choice, 'keys'):
c = [option] c = [option]
for (k, v) in choice.items(): for (k, v) in choice.items():
@ -169,11 +172,9 @@ class RollTable:
@property @property
def as_yaml(self) -> dict: def as_yaml(self) -> dict:
struct = [{'headers': self.rows[0]}] struct = {'headers': self.rows[0]}
for row in self.rows[1:]: for row in self.rows[1:]:
struct.append({ struct[row[0]] = row[1:]
row[0]: row[1:]
})
return yaml.dump(struct) return yaml.dump(struct)
def _config(self): def _config(self):