Skip to content

Commit

Permalink
Add .report.Config.modules
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeru committed Aug 6, 2024
1 parent e874fd5 commit 31ceb8d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion message_ix_models/report/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from copy import deepcopy
from functools import partial
from importlib import import_module
from itertools import chain
from operator import itemgetter
from pathlib import Path
from typing import Callable, List, Optional, Tuple, Union
Expand Down Expand Up @@ -369,7 +370,7 @@ def prepare_reporter(
rep.configure(model=deepcopy(context.model))

# Apply callbacks for other modules which define additional reporting computations
for callback in CALLBACKS:
for callback in chain(CALLBACKS, context.report.iter_callbacks()):
callback(rep, context)

key = context.report.key
Expand Down
11 changes: 10 additions & 1 deletion message_ix_models/report/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging
from dataclasses import InitVar, dataclass, field
from importlib import import_module
from pathlib import Path
from typing import TYPE_CHECKING, Dict, Optional, Union
from typing import TYPE_CHECKING, Callable, Dict, Generator, List, Optional, Union

from message_ix_models.util import local_data_path, package_data_path
from message_ix_models.util.config import ConfigHelper
Expand Down Expand Up @@ -35,6 +36,9 @@ class Config(ConfigHelper):
#: Key for the Quantity or computation to report.
key: Optional["KeyLike"] = None

#: Modules with reporting callbacks.
modules: List[str] = field(default_factory=list)

#: Directory for output.
output_dir: Optional[Path] = field(
default_factory=lambda: local_data_path("report")
Expand All @@ -52,6 +56,11 @@ def __post_init__(self, from_file, _legacy) -> None:
self.use_file(from_file)
self.legacy.update(use=_legacy)

def iter_callbacks(self) -> Generator[Callable, None, None]:
"""Yield the :py:`callback()` function for each of :attr:`.modules`."""
for mod in map(import_module, self.modules):
yield getattr(mod, "callback")

def set_output_dir(self, arg: Optional[Path]) -> None:
"""Set :attr:`output_dir`, the output directory.
Expand Down

0 comments on commit 31ceb8d

Please sign in to comment.