Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Commit

Permalink
feat(tools): allows listeners to use plugin parser and connector modules
Browse files Browse the repository at this point in the history
Adds the connectors_path and parsers_path variables to get_listeners_from_dict() and get_mappings_from_dict() to properly create listeners that use mappings which reference custom connector and parser modules.
  • Loading branch information
jaredhendrickson13 committed Aug 13, 2022
1 parent 2d63e16 commit 5540be2
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions mail2beyond/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,21 @@ def get_parser_modules(path: (str, None) = None):
return available_parsers


def get_mappings_from_dict(config: dict):
def get_mappings_from_dict(config: dict, connectors_path: (str, None) = None, parsers_path: (str, None) = None):
"""
Converts a dictionary representations of mappings to Mapping objects. Since Mapping objects are dependent on
a Connector object, the dictionary must also include representations of Connector objects to use.
Args:
config (dict): A dictionary representation of different mappings to create.
connectors_path (str, None): A path to a directory that contains plugin connector modules. Only .py files within
this directory will be included. Each .py file must include a class named `Connector` that extends the
`mail2beyond.framework.BaseConnector` class. If `None` is specified, only the built-in connector modules
will be available.
parsers_path (str, None): A path to a directory that contains plugin parser modules. Only .py files within this
directory will be included. Each .py file must include a class named `Parser` that extends the
`mail2beyond.framework.BaseParser` class. If `None` is specified, only the built-in parser modules
will be available.
Raises:
mail2beyond.framework.Error: When a validation error occurs.
Expand All @@ -221,8 +229,8 @@ def get_mappings_from_dict(config: dict):
list: A list of Mapping objects that can be used.
"""
# Variables
config_connectors = get_connectors_from_dict(config)
available_parsers = dict(inspect.getmembers(parsers, inspect.ismodule))
config_connectors = get_connectors_from_dict(config, path=connectors_path)
available_parsers = get_parser_modules(path=parsers_path)
valid_mappings = []

# Require mappings config to be defined
Expand Down Expand Up @@ -273,7 +281,7 @@ def get_mappings_from_dict(config: dict):
return valid_mappings


def get_listeners_from_dict(config: dict, log_level: int = logging.NOTSET):
def get_listeners_from_dict(config: dict, log_level: int = logging.NOTSET, **kwargs):
"""
Converts a dictionary representations of listeners to Listener objects. Since Listener objects are dependent on
a Mapping objects, and Mapping objects are dependent on Connector objects, the dictionary must also include
Expand All @@ -283,6 +291,14 @@ def get_listeners_from_dict(config: dict, log_level: int = logging.NOTSET):
config (dict): A dictionary representation of different mappings to create.
log_level (int): Sets the logging level the Listeners' Logger will start logging at. See
https://docs.python.org/3/library/logging.html#logging-levels
**connectors_path (str, None): A path to a directory that contains plugin connector modules. Only .py files
within this directory will be included. Each .py file must include a class named `Connector` that extends
the`mail2beyond.framework.BaseConnector` class. If `None` is specified, only the built-in connector modules
will be available.
**parsers_path (str, None): A path to a directory that contains plugin parser modules. Only .py files within
this directory will be included. Each .py file must include a class named `Parser` that extends the
`mail2beyond.framework.BaseParser` class. If `None` is specified, only the built-in parser modules
will be available.
Raises:
mail2beyond.framework.Error: When a validation error occurs.
Expand All @@ -308,8 +324,12 @@ def get_listeners_from_dict(config: dict, log_level: int = logging.NOTSET):
tls_minimum_version_default = "tls1_2"

# Variables
mappings = get_mappings_from_dict(config)
valid_listeners = []
mappings = get_mappings_from_dict(
config,
connectors_path=kwargs.get("connectors_path"),
parsers_path=kwargs.get("parsers_path")
)

# Require listeners config to be defined.
if "listeners" not in config.keys():
Expand Down

0 comments on commit 5540be2

Please sign in to comment.