def test_parse_defaults(): in_weapon = {"damage": [1, 2, 3, 4, 5, 6, 7], "name": "Test Weapon"} weapon = Weapon.parse(in_weapon) assert weapon.damage == [1, 2, 3, 4, 5, 6, 7] assert weapon.name == "Test Weapon" assert weapon.style == "melee" assert not weapon.two_handed assert not weapon.ignore_armor assert weapon.aliases == []
def test_parse_explicit(): in_weapon = { "damage": [1, 2, 3, 4, 5, 6, 7], "name": "Test Weapon", "style": "melee", "two_handed": True, "ignore_armor": True, "aliases": ["fred"] } weapon = Weapon.parse(in_weapon) assert weapon.damage == [1, 2, 3, 4, 5, 6, 7] assert weapon.name == "Test Weapon" assert weapon.style == "melee" assert weapon.two_handed assert weapon.ignore_armor assert weapon.aliases == ["fred"]
def load(cls, path: str) -> Compendium: yaml_data = None config_uri_parsed = urlparse(path) if config_uri_parsed.scheme in ['https', 'http']: url = urlopen(path) yaml_data = url.read() else: if not os.path.exists(path): path = os.path.join(os.path.dirname(__file__), '..', '..', 'data', f"{path}.yaml") with open(file=path, mode='r', encoding='UTF-8') as file_data: yaml_data = file_data.read() infile = yaml.safe_load(yaml_data) key = infile['key'] title = infile['title'] url = infile.get('url', None) author = infile.get('author', None) inherits = infile.get('inherits', None) compendium = cls(key, title, url, author, inherits) if 'backgrounds' in infile: for in_bg in infile['backgrounds']: background = Background.parse(in_bg) compendium.add_background(background) if 'weapons' in infile: for in_weapon in infile['weapons']: weapon = Weapon.parse(in_weapon) compendium.add_weapon(weapon) if 'spells' in infile: for in_spell in infile['spells']: spell = Spell.parse(in_spell) compendium.add_spell(spell) if 'base_items' in infile: for base_item in infile['base_items']: compendium.add_base_item(base_item) return compendium