-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
284 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
include VERSION | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,35 @@ | ||
from __future__ import annotations | ||
|
||
from typing import List, Any | ||
|
||
import deepmerge.merger | ||
|
||
|
||
class DeepMergeException(Exception): | ||
pass | ||
"Base class for all `deepmerge` Exceptions" | ||
|
||
|
||
class StrategyNotFound(DeepMergeException): | ||
pass | ||
"Exception for when a strategy cannot be located" | ||
|
||
|
||
class InvalidMerge(DeepMergeException): | ||
def __init__(self, strategy_list_name, merge_args, merge_kwargs): | ||
super(InvalidMerge, self).__init__( | ||
"no more strategies found for {0} and arguments {1}, {2}".format( | ||
strategy_list_name, merge_args, merge_kwargs | ||
) | ||
"Exception for when unable to complete a merge operation" | ||
|
||
def __init__( | ||
self, | ||
strategy_list_name: str, | ||
config: deepmerge.merger.Merger, | ||
path: List, | ||
base: Any, | ||
nxt: Any, | ||
) -> None: | ||
super().__init__( | ||
f"Could not merge using {strategy_list_name!r} [{config=}, {path=}, {base=}, {nxt=}]" | ||
) | ||
self.strategy_list_name = strategy_list_name | ||
self.merge_args = merge_args | ||
self.merge_kwargs = merge_kwargs | ||
self.config = config | ||
self.path = path | ||
self.base = base | ||
self.nxt = nxt | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,66 @@ | ||
from .strategy.list import ListStrategies | ||
from .strategy.dict import DictStrategies | ||
from .strategy.set import SetStrategies | ||
from .strategy.type_conflict import TypeConflictStrategies | ||
from .strategy.fallback import FallbackStrategies | ||
from __future__ import annotations | ||
|
||
from typing import Dict, List, Type, Tuple, Any, Union, Sequence, Callable | ||
|
||
class Merger(object): | ||
from . import strategy as s | ||
|
||
|
||
class Merger: | ||
""" | ||
Merges objects based on provided strategies | ||
:param type_strategies, List[Tuple]: a list of (Type, Strategy) pairs | ||
that should be used against incoming types. For example: (dict, "override"). | ||
""" | ||
|
||
PROVIDED_TYPE_STRATEGIES = { | ||
list: ListStrategies, | ||
dict: DictStrategies, | ||
set: SetStrategies, | ||
PROVIDED_TYPE_STRATEGIES: Dict[Type, Type[s.StrategyList]] = { | ||
list: s.ListStrategies, | ||
dict: s.DictStrategies, | ||
set: s.SetStrategies, | ||
} | ||
|
||
def __init__(self, type_strategies, fallback_strategies, type_conflict_strategies): | ||
self._fallback_strategy = FallbackStrategies(fallback_strategies) | ||
def __init__( | ||
self, | ||
type_strategies: Sequence[ | ||
Tuple[Type, Union[s.StrategyCallable, s.StrategyListInitable]] | ||
], | ||
fallback_strategies: s.StrategyListInitable, | ||
type_conflict_strategies: s.StrategyListInitable, | ||
) -> None: | ||
self._fallback_strategy = s.FallbackStrategies(fallback_strategies) | ||
self._type_conflict_strategy = s.TypeConflictStrategies( | ||
type_conflict_strategies | ||
) | ||
|
||
expanded_type_strategies = [] | ||
self._type_strategies: List[Tuple[Type, s.StrategyCallable]] = [] | ||
for typ, strategy in type_strategies: | ||
if typ in self.PROVIDED_TYPE_STRATEGIES: | ||
strategy = self.PROVIDED_TYPE_STRATEGIES[typ](strategy) | ||
expanded_type_strategies.append((typ, strategy)) | ||
self._type_strategies = expanded_type_strategies | ||
# Customise a StrategyList instance for this type | ||
self._type_strategies.append( | ||
(typ, self.PROVIDED_TYPE_STRATEGIES[typ](strategy)) | ||
) | ||
elif callable(strategy): | ||
self._type_strategies.append((typ, strategy)) | ||
else: | ||
raise ValueError(f"Cannot handle ({typ}, {strategy})") | ||
return | ||
|
||
self._type_conflict_strategy = TypeConflictStrategies(type_conflict_strategies) | ||
|
||
def merge(self, base, nxt): | ||
def merge(self, base: Any, nxt: Any) -> Any: | ||
return self.value_strategy([], base, nxt) | ||
|
||
def type_conflict_strategy(self, *args): | ||
return self._type_conflict_strategy(self, *args) | ||
def type_conflict_strategy(self, path: List, base: Any, nxt: Any) -> Any: | ||
return self._type_conflict_strategy(self, path, base, nxt) | ||
|
||
def value_strategy(self, path, base, nxt): | ||
def value_strategy(self, path: List, base: Any, nxt: Any) -> Any: | ||
# Check for strategy based on type of base, next | ||
for typ, strategy in self._type_strategies: | ||
if isinstance(base, typ) and isinstance(nxt, typ): | ||
# We have a strategy for this type | ||
return strategy(self, path, base, nxt) | ||
if not (isinstance(base, type(nxt)) or isinstance(nxt, type(base))): | ||
return self.type_conflict_strategy(path, base, nxt) | ||
return self._fallback_strategy(self, path, base, nxt) | ||
|
||
if isinstance(base, type(nxt)) or isinstance(nxt, type(base)): | ||
# no known strategy but base, next are similar types | ||
return self._fallback_strategy(self, path, base, nxt) | ||
|
||
# No known strategy and base, next are different types. | ||
return self.type_conflict_strategy(path, base, nxt) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from .core import StrategyList, StrategyCallable, StrategyListInitable | ||
from .dict import DictStrategies | ||
from .fallback import FallbackStrategies | ||
from .list import ListStrategies | ||
from .set import SetStrategies | ||
from .type_conflict import TypeConflictStrategies |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.