added metadata property
This commit is contained in:
parent
75a5b884c7
commit
65c5b5199e
|
@ -1,6 +1,6 @@
|
||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = 'dnd-rolltable'
|
name = 'dnd-rolltable'
|
||||||
version = '1.1.8'
|
version = '1.1.9'
|
||||||
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'
|
||||||
|
|
|
@ -35,6 +35,7 @@ class DataSource:
|
||||||
self.headers = []
|
self.headers = []
|
||||||
self.frequencies = None
|
self.frequencies = None
|
||||||
self.data = None
|
self.data = None
|
||||||
|
self.metadata = None
|
||||||
self.load_source()
|
self.load_source()
|
||||||
|
|
||||||
def load_source(self) -> None:
|
def load_source(self) -> None:
|
||||||
|
@ -45,19 +46,19 @@ class DataSource:
|
||||||
return
|
return
|
||||||
|
|
||||||
self.data = yaml.safe_load(self.source)
|
self.data = yaml.safe_load(self.source)
|
||||||
metadata = self.data.pop('metadata', {})
|
self.metadata = self.data.pop('metadata', {})
|
||||||
|
|
||||||
num_keys = len(self.data.keys())
|
num_keys = len(self.data.keys())
|
||||||
default_freq = num_keys / 100
|
default_freq = num_keys / 100
|
||||||
|
|
||||||
if 'headers' in metadata:
|
if 'headers' in self.metadata:
|
||||||
self.headers = metadata['headers']
|
self.headers = self.metadata['headers']
|
||||||
|
|
||||||
frequencies = {
|
frequencies = {
|
||||||
'default': dict([(k, default_freq) for k in self.data.keys()])
|
'default': dict([(k, default_freq) for k in self.data.keys()])
|
||||||
}
|
}
|
||||||
if 'frequencies' in metadata:
|
if 'frequencies' in self.metadata:
|
||||||
frequencies.update(**metadata['frequencies'])
|
frequencies.update(**self.metadata['frequencies'])
|
||||||
self.frequencies = frequencies[self.frequency]
|
self.frequencies = frequencies[self.frequency]
|
||||||
|
|
||||||
|
|
||||||
|
@ -106,6 +107,10 @@ class RollTable:
|
||||||
struct[row[0]][self.headers[idx] if idx < len(self.headers) else '_'] = col
|
struct[row[0]][self.headers[idx] if idx < len(self.headers) else '_'] = col
|
||||||
return yaml.dump(struct, sort_keys=False)
|
return yaml.dump(struct, sort_keys=False)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def datasources(self) -> List:
|
||||||
|
return self._data
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def die(self) -> int:
|
def die(self) -> int:
|
||||||
return self._die
|
return self._die
|
||||||
|
|
Loading…
Reference in New Issue
Block a user