-
Notifications
You must be signed in to change notification settings - Fork 23
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
10 changed files
with
149 additions
and
31 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
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,39 @@ | ||
import argparse | ||
import logging | ||
import os | ||
import sys | ||
|
||
from rele import config, discover, subscription | ||
|
||
from rele.worker import create_and_run | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def main(): | ||
# modify path so we can import modules and packages | ||
sys.path.insert(0, os.getcwd()) | ||
|
||
parser = argparse.ArgumentParser( | ||
prog="Relé", description="Harness the power of Relé from the command line" | ||
) | ||
|
||
subparsers = parser.add_subparsers(help="Select a command", dest="command") | ||
|
||
run_parser = subparsers.add_parser( | ||
"run", | ||
help="Run a Relé worker with auto-discovery of subs modules in the " | ||
"current path. Auto-discovery will include all subs " | ||
"and settings modules. If no settings module is discovered, " | ||
"defaults will be used.", | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
if args.command == "run": | ||
settings, module_paths = discover.sub_modules() | ||
configuration = config.setup(settings.RELE if settings else None) | ||
subs = config.load_subscriptions_from_paths( | ||
module_paths, configuration.sub_prefix, configuration.filter_by | ||
) | ||
create_and_run(subs, configuration) |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import logging | ||
import pkgutil | ||
from importlib.util import find_spec as importlib_find | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def module_has_submodule(package, module_name): | ||
""" | ||
See if 'module' is in 'package'. | ||
Taken from https://github.com/django/django/blob/master/django/utils/module_loading.py#L63 | ||
""" | ||
package = __import__(package) | ||
package_name = package.__name__ | ||
package_path = package.__path__ | ||
full_module_name = package_name + "." + module_name | ||
|
||
try: | ||
return importlib_find(full_module_name, package_path) is not None | ||
except (ModuleNotFoundError, AttributeError): | ||
# When module_name is an invalid dotted path, Python raises | ||
# ModuleNotFoundError. | ||
return False | ||
|
||
|
||
def sub_modules(): | ||
""" | ||
In the current PYTHONPATH, we can traverse all modules and determine if they | ||
have a settings.py or directory with a subs.py module. If either one of | ||
those exists, we import it, and return the settings module, and | ||
paths to the subs file. | ||
If a settings module is not found, we return None. | ||
:return: (settings module, List[string: subs module paths]) | ||
""" | ||
settings = None | ||
module_paths = [] | ||
for f, package, is_package in pkgutil.iter_modules(path=["."]): | ||
if package == "settings": | ||
settings = __import__(package) | ||
if is_package and module_has_submodule(package, "subs"): | ||
module = package + ".subs" | ||
module_paths.append(module) | ||
logger.debug(" * Discovered subs module: %r" % module) | ||
return settings, module_paths |
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
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