def add_plugins_args(f): """Adds installed plugin options.""" schemas = [] if isinstance(f, click.Command): for p in plugins.all(): schemas.append(get_plugin_properties(p.schema)) f.params.extend(params_factory(schemas)) else: if not hasattr(f, "__click_params__"): f.__click_params__ = [] for p in plugins.all(): schemas.append(get_plugin_properties(p.schema)) f.__click_params__.extend(params_factory(schemas)) return f
def decorator(group): if not isinstance(group, click.Group): raise TypeError("Plugins can only be attached to an instance of click.Group()") for p in plugins.all(plugin_type=plugin_type) or (): # create a new subgroup for each plugin name = p.slug.split("-")[0] plugin_group = click.Group(name) try: for command in p.commands: command_func = getattr(p, command) props = get_plugin_properties(p.schema) params = params_factory([props]) command_obj = click.Command( command, params=params, callback=command_func, help=command_func.__doc__ ) plugin_group.add_command(command_obj) except Exception: # Catch this so a busted plugin doesn't take down the CLI. # Handled by registering a dummy command that does nothing # other than explain the error. plugin_group.add_command(BrokenCommand(p.slug, plugin_type)) group.add_command(plugin_group) return group
def install_plugin_events(api): """Adds plugin endpoints to the event router.""" for plugin in plugins.all(): if plugin.events: api.include_router(plugin.events, prefix="/events", tags=["events"])
def sync_tags(db_session=None): """Syncs tags from external sources.""" for p in plugins.all(plugin_type="tag"): log.debug(f"Getting tags via: {p.slug}") for t in p.get(): log.debug(f"Adding Tag. Tag: {t}") tag_service.get_or_create(db_session=db_session, tag_in=TagCreate(**t))
def testapp(): # we only want to use test plugins so unregister everybody else from dispatch.plugins.base import unregister, plugins for p in plugins.all(): unregister(p) yield app
def sync_tags(db_session=None): """Syncs tags from external sources.""" for p in plugins.all(plugin_type="tag"): log.debug(f"Getting tags via: {p.slug}") for app in p.get(): log.debug(f"Adding Tag. Tag: {app}") tag_in = TagCreate(**app) tag = tag_service.get_by_name(db_session=db_session, name=tag_in.name) if tag: tag_service.update(db_session=db_session, tag=tag, tag_in=tag_in) else: tag_service.create(db_session=db_session, tag_in=tag_in)
def sync_terms(db_session=None): """Syncs terms from external sources.""" for p in plugins.all(plugin_type="term"): log.debug(f"Getting terms via: {p.slug}") for t in p.get(): log.debug(f"Adding Term. Term: {t}") term_in = TermCreate(**t) term = term_service.get_by_text(db_session=db_session, text=term_in.text) if term: term_service.update(db_session=db_session, term=term, term_in=term_in) else: term_service.create(db_session=db_session, term_in=term_in)
def sync_applications(db_session=None): """Syncs applications from external sources.""" for p in plugins.all(plugin_type="application"): log.debug(f"Getting applications via: {p.slug}") for app in p.get(): log.debug(f"Adding Application. Application: {app}") application_in = ApplicationCreate(**app) application = application_service.get_by_name( db_session=db_session, name=application_in.name) if application: application_service.update(db_session=db_session, app=application, app_in=application_in) else: application_service.create(db_session=db_session, app_in=application_in)
def install_plugins(force): """Installs all plugins, or only one.""" from dispatch.database.core import SessionLocal from dispatch.plugin import service as plugin_service from dispatch.plugin.models import Plugin from dispatch.common.utils.cli import install_plugins from dispatch.plugins.base import plugins install_plugins() db_session = SessionLocal() for p in plugins.all(): record = plugin_service.get_by_slug(db_session=db_session, slug=p.slug) if not record: click.secho( f"Installing plugin... Slug: {p.slug} Version: {p.version}", fg="blue") record = Plugin( title=p.title, slug=p.slug, type=p.type, version=p.version, author=p.author, author_url=p.author_url, multiple=p.multiple, description=p.description, ) db_session.add(record) if force: click.secho( f"Updating plugin... Slug: {p.slug} Version: {p.version}", fg="blue") # we only update values that should change record.title = p.title record.version = p.version record.author = p.author record.author_url = p.author_url record.description = p.description record.type = p.type db_session.add(record) db_session.commit()