From 7d564ac1e13477d964db708305f58ce9b828f8ed Mon Sep 17 00:00:00 2001 From: Mark Wolfman Date: Fri, 11 Jun 2021 09:07:48 -0500 Subject: [PATCH] Allow importing of homebrew material stored in a separate module. --- docs/character_files.rst | 5 ++++- dungeonsheets/__init__.py | 2 ++ dungeonsheets/homebrew.py | 28 ++++++++++++++++++++++++++++ dungeonsheets/mechanics.py | 2 ++ examples/homebrew.py | 7 +++++-- examples/homebrew_campaign.py | 13 +++++++++++++ 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 dungeonsheets/homebrew.py create mode 100644 examples/homebrew_campaign.py diff --git a/docs/character_files.rst b/docs/character_files.rst index 97b63f8..fcab000 100644 --- a/docs/character_files.rst +++ b/docs/character_files.rst @@ -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] diff --git a/dungeonsheets/__init__.py b/dungeonsheets/__init__.py index d4f0cc9..dcb0b0f 100644 --- a/dungeonsheets/__init__.py +++ b/dungeonsheets/__init__.py @@ -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 diff --git a/dungeonsheets/homebrew.py b/dungeonsheets/homebrew.py new file mode 100644 index 0000000..1378955 --- /dev/null +++ b/dungeonsheets/homebrew.py @@ -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 diff --git a/dungeonsheets/mechanics.py b/dungeonsheets/mechanics.py index 686457f..19eadd9 100644 --- a/dungeonsheets/mechanics.py +++ b/dungeonsheets/mechanics.py @@ -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 diff --git a/examples/homebrew.py b/examples/homebrew.py index 80cb6d0..61c08ca 100644 --- a/examples/homebrew.py +++ b/examples/homebrew.py @@ -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" diff --git a/examples/homebrew_campaign.py b/examples/homebrew_campaign.py new file mode 100644 index 0000000..7dbc004 --- /dev/null +++ b/examples/homebrew_campaign.py @@ -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"