Allow importing of homebrew material stored in a separate module.

This commit is contained in:
Mark Wolfman
2021-06-11 09:07:48 -05:00
parent b432e67a35
commit 7d564ac1e1
6 changed files with 54 additions and 3 deletions
+4 -1
View File
@@ -310,7 +310,10 @@ files:
.. code:: python
import my_homebrew
from dungeonsheets import import_homebrew
my_homebrew = import_homebrew("my_campaign.py")
weapons = ["shortsword", my_homebrew.DullSword]
+2
View File
@@ -7,10 +7,12 @@ __all__ = (
"race",
"background",
"spells",
"import_homebrew"
)
from dungeonsheets import background, features, race, spells, weapons, mechanics
from dungeonsheets.character import Character
from dungeonsheets.homebrew import import_homebrew
import os
+28
View File
@@ -0,0 +1,28 @@
"""Tools useful for defining homebrew content."""
from pathlib import Path
import importlib.util
from typing import Union
def import_homebrew(filepath: Union[str, Path]):
"""Import a module file containing homebrew content.
This is intended to be used in a character/GM sheet to load in
homebrew content defined in an external file.
Parameters
==========
filepath
The location of the python file containing the homebrew content.
Returns
=======
mod
The imported module of homebrew content.
"""
spec = importlib.util.spec_from_file_location("module.name", filepath)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
+2
View File
@@ -7,3 +7,5 @@ from dungeonsheets.infusions import Infusion
from dungeonsheets.weapons import Weapon
from dungeonsheets.armor import Armor, Shield
from dungeonsheets.magic_items import MagicItem
from dungeonsheets.monsters import Monster
from dungeonsheets.stats import Ability
+5 -2
View File
@@ -6,7 +6,10 @@ Modify this file as you level up and then re-generate the character
sheet by running ``makesheets`` from the command line.
"""
from dungeonsheets import mechanics
from dungeonsheets import mechanics, import_homebrew
# Load the module containing the homebrew content.
campaign = import_homebrew("homebrew_campaign.py")
dungeonsheets_version = "0.9.4"
@@ -93,7 +96,7 @@ class LegoShield(mechanics.Shield):
base_armor_class = 114
weapons = (DullSword, "rusty_shiv") # Example: ('shortsword', 'longsword')
weapons = (DullSword, "rusty_shiv", campaign.BrightSword) # Example: ('shortsword', 'longsword')
magic_items = (RobeOfBreadSummoning, "staff_of_the_arbor_abode")
armor = PlasticArmor # Eg "leather armor"
shield = LegoShield # Eg "shield"
+13
View File
@@ -0,0 +1,13 @@
"""This file defines some homebrew mechanics that can be imported into
character sheets using ``dungeonsheets.import_homebrew``. See
``homebrew.py`` for an example of how these homebrew mechanics can be
used.
"""
from dungeonsheets import mechanics
class BrightSword(mechanics.Weapon):
"""Give you enemies the old razzle-dazzle."""
name = "Brightsword"