Skip to content

Commit

Permalink
Added support for topic substitutions.
Browse files Browse the repository at this point in the history
This allows domain classes to be moved and renamed.
  • Loading branch information
johnbywater committed Jun 27, 2019
1 parent e4836eb commit 35288f6
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion eventsourcing/utils/topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def get_topic(domain_class):
return domain_class.__module__ + '#' + getattr(domain_class, '__qualname__', domain_class.__name__)


substitutions = {}


def resolve_topic(topic):
"""Return class described by given topic.
Expand All @@ -27,11 +30,20 @@ def resolve_topic(topic):
Raises:
TopicResolutionError: If there is no such class.
"""
# Substitute one topic for another, if so defined.
# - this allows classes to be moved and renamed
topic = substitutions.get(topic, topic)

# Partition topic into module and class names.
module_name, _, class_name = topic.partition('#')

# Import the module.
try:
module_name, _, class_name = topic.partition('#')
module = importlib.import_module(module_name)
except ImportError as e:
raise TopicResolutionError("{}: {}".format(topic, e))

# Identify the class.
try:
cls = resolve_attr(module, class_name)
except AttributeError as e:
Expand Down

0 comments on commit 35288f6

Please sign in to comment.