Skip to content

Commit

Permalink
Make sure each plugin is activated only once
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Feb 7, 2022
1 parent 3b50bae commit d461b55
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/ini2toml/base_translator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
from functools import reduce
from types import MappingProxyType
from typing import Dict, Generic, List, Mapping, Sequence, TypeVar, cast
Expand Down Expand Up @@ -84,7 +85,7 @@ def __init__(
profile_augmentations: Sequence[types.ProfileAugmentation] = (),
ini_parser_opts: Mapping = EMPTY,
):
self.plugins = list(plugins)
self.plugins = _deduplicate_plugins(plugins)
self.ini_parser_opts = ini_parser_opts
self.profiles = {p.name: p for p in profiles}
self.augmentations: Dict[str, types.ProfileAugmentation] = {
Expand Down Expand Up @@ -153,3 +154,14 @@ def translate(
irepr = reduce(apply, profile.intermediate_processors, irepr)
toml = self.dumps(irepr)
return reduce(apply, profile.post_processors, toml)


def _deduplicate_plugins(plugins: Sequence[types.Plugin]) -> List[types.Plugin]:
deduplicated = {_plugin_name(p): p for p in plugins}
return list(deduplicated.values())


def _plugin_name(plugin: types.Plugin) -> str:
mod = inspect.getmodule(plugin)
name = getattr(plugin, "__qualname__", getattr(plugin, "__name__", "**plugin**"))
return f"{mod}:{name}"
9 changes: 9 additions & 0 deletions tests/test_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,12 @@ def test_reuse_object():
deduplicated = {getattr(p, "__name__", ""): p for p in processors}
# Make sure there is no duplication in the processors
assert len(processors) == len(deduplicated)


def test_deduplicate_plugins():
plugins = [
profile_independent_tasks.activate,
profile_independent_tasks.activate,
]
translator = Translator(plugins=plugins)
assert len(translator.plugins) == 1

0 comments on commit d461b55

Please sign in to comment.