Skip to content

Commit

Permalink
Make custom modes/roles imports more robust
Browse files Browse the repository at this point in the history
The roles/ and gamemodes/ directories always exist, so imports of them
will always succeed regardless of whether their respective __init__.py
files exist (if they don't, they're imported as a namespace package).

As such, we don't need a try/except wrapper here, allowing for any
errors in custom roles/modes to bubble up properly.

We also unconditionally import src.roles and src.gamemodes, since these
packages don't actually import the builtin roles/modes, only shared
stuff useful across all roles/modes (including custom ones).
  • Loading branch information
skizzerz committed Jan 18, 2024
1 parent 9f3267b commit 6d59a63
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
3 changes: 0 additions & 3 deletions roles/__init__.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import os.path
import glob
import importlib

# get built-in roles
import src.roles

path = os.path.dirname(os.path.abspath(__file__))
search = os.path.join(path, "*.py")

Expand Down
25 changes: 7 additions & 18 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,16 @@
from src import votes
from src import trans
from src import gamecmds, wolfgame
from src import roles, gamemodes

# Import the user-defined game modes
# These are not required, so failing to import it doesn't matter
# The file then imports our game modes
# Fall back to importing our game modes if theirs fail
# Do the same with roles

try:
import roles as custom_roles # type: ignore
if not custom_roles.CUSTOM_ROLES_DEFINED:
raise AttributeError()
except (ModuleNotFoundError, AttributeError):
from src import roles
# Import the user-defined roles, as well as builtins if custom roles don't exist or they want them
import roles as custom_roles # type: ignore
if not getattr(custom_roles, "CUSTOM_ROLES_DEFINED", False):
roles.import_builtin_roles()

try:
import gamemodes as custom_gamemodes # type: ignore
if not custom_gamemodes.CUSTOM_MODES_DEFINED:
raise AttributeError()
except (ModuleNotFoundError, AttributeError):
from src import gamemodes
# Import the user-defined modes, as well as builtins if custom modes don't exist or they want them
import gamemodes as custom_gamemodes # type: ignore
if not getattr(custom_gamemodes, "CUSTOM_MODES_DEFINED", False):
gamemodes.import_builtin_modes()

# Import user-defined hooks
Expand Down

0 comments on commit 6d59a63

Please sign in to comment.