2020-02-29 18:26:32 +01:00
|
|
|
import collections
|
2020-02-23 09:10:10 +01:00
|
|
|
import importlib
|
|
|
|
import pkgutil
|
|
|
|
|
|
|
|
import click
|
|
|
|
|
2020-02-23 09:23:42 +01:00
|
|
|
from ofx_processor import processors
|
2020-02-23 09:10:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
def discover_processors(cli: click.Group):
|
|
|
|
"""
|
2020-02-23 09:23:42 +01:00
|
|
|
Discover processors.
|
2020-02-23 09:10:10 +01:00
|
|
|
|
2020-02-23 09:23:42 +01:00
|
|
|
To be discovered, processors must:
|
|
|
|
* Be in the `processors` package.
|
|
|
|
* Declare a <BankName>Processor class
|
|
|
|
* Declare a static main function in this class,
|
|
|
|
which must be a click command
|
2020-02-23 09:10:10 +01:00
|
|
|
|
|
|
|
:param cli: The main CLI to add discovered processors to.
|
|
|
|
"""
|
2020-02-23 09:23:42 +01:00
|
|
|
prefix = processors.__name__ + "."
|
|
|
|
for module in pkgutil.iter_modules(processors.__path__, prefix):
|
|
|
|
module = importlib.import_module(module.name)
|
|
|
|
for item in dir(module):
|
2020-03-31 18:32:54 +02:00
|
|
|
if (
|
|
|
|
item.endswith("Processor")
|
|
|
|
and item != "Processor"
|
|
|
|
and "Base" not in item
|
|
|
|
):
|
2020-02-23 09:23:42 +01:00
|
|
|
cls = getattr(module, item)
|
|
|
|
cli.add_command(cls.main)
|
2020-02-29 18:26:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
class OrderedGroup(click.Group):
|
|
|
|
def __init__(self, name=None, commands=None, **attrs):
|
|
|
|
super(OrderedGroup, self).__init__(name, commands, **attrs)
|
|
|
|
#: the registered subcommands by their exported names.
|
|
|
|
self.commands = commands or collections.OrderedDict()
|
|
|
|
|
|
|
|
def list_commands(self, ctx):
|
|
|
|
return self.commands
|