def list(): """list all installed plugins""" plugins = get_user_plugins() plugin_names = [p.id() for p in plugins] output_result = f'User plugins (from {config.user_plugin_dir}): \n' output_result += '\n'.join(plugin_names) logger.log(output_result)
def init(): """Init user plugins directory.""" logger.log(f'Initialize plugin directory: {config.user_plugin_dir}') config.user_plugin_dir.mkdir(parents=True, exist_ok=True) for example in config.yo_plugin_example_dir.glob('*'): if example.is_dir() and not example.stem.startswith('_'): _init_example(example.stem) _reload_plugins()
def list_cli(): """list all cli modules""" internal_plugins = get_internal_cli() external_plugins = get_external_cli() output_result = 'External cli: \n' output_result += '\n'.join(external_plugins) output_result += '\n\nInternal cli:\n' output_result += '\n'.join(internal_plugins) logger.log(output_result)
def load_external_cli(): try: sys.path.insert(0, str(config.user_folder)) cli_modules = get_external_cli() _dynamic_load(f'commands', cli_modules) except Exception as e: logger.log(detail_error(e)) logger.log( 'Failed to load external plugins cli, please try `yo plugin reload`' )
def _reload_plugins(): _clear_external_cli(print_log=False) plugins = get_user_plugins() cmd_groups = [] for plugin in plugins: load_info = f'Loading plugin: {plugin.id()} => ' assert isinstance(plugin, Plugin) plugin.validate() if plugin.error: logger.log(f'{load_info}{plugin.error}') else: logger.log(f'{load_info}OK.') logger.vlog(f'Plugin detail: {plugin.__dict__}') cmd_groups = _merge_command_groups(cmd_groups, plugin.command_group_obj) logger.vlog(f'All command groups: {cmd_groups}') for cmd_group in cmd_groups: info = f'Register command group: {cmd_group.name} => ' internal_cli = get_internal_cli() try: assert cmd_group.name not in internal_cli, f'Conflicted with internal commands: {internal_cli}' cmd_group.generate_cli() info += 'OK.' except Exception as e: info += detail_error(e) logger.log(info)
def _clear_external_cli(print_log=True): for _cli in config.yo_cli_external.glob('*.py'): if not _cli.stem.startswith('_'): if print_log: logger.log(f'Clear plugin command: {_cli.stem}') _cli.unlink()
def _init_example(example_name): example_from = config.yo_plugin_example_dir / example_name example_to = config.user_plugin_dir / example_name logger.log(f'Init plugin example: {example_name} => {example_to}') copy_and_overwrite(str(example_from), str(example_to))