pbf.cli
1import json 2import click 3import os 4import pathlib 5import art 6import pbf 7 8art_txt = art.text2art("PBF CLI", font='larry3d') 9_template_repo = "https://github.com/PigBotFrameworkPlugins/" 10_home_path = pathlib.Path().home() 11_plugins_path = os.path.join(_home_path, ".pbf", "plugins") 12_start_template_version = "0.1.0" 13_start_template = """ 14# Step 1: import config and modify it 15from pbf import config 16 17config.logs_level = "DEBUG" 18# Modify more configurations here 19 20# Step 2: import setup and setup 21from pbf import setup 22 23setup.setup(__name__) 24 25# Step 3: import driver and start it 26if __name__ == "__main__": 27 from pbf.driver import Fastapi 28 29 Fastapi.start() 30""" 31 32 33def printPBF(): 34 # 打印`PigBotFramework`字符画 35 print(click.style(art_txt, fg='green')) 36 37 38def installPlugin(plugin_id: str, plugin_name: str, plugins_path: str): 39 try: 40 from git import Repo 41 Repo.clone_from(f"{_template_repo}{plugin_id}", os.path.join(plugins_path, plugin_name)) 42 except Exception as e: 43 click.secho(f'Error: {e}', fg='red') 44 return 45 46@click.group( 47 help=f'{art_txt}\n' 48) 49@click.version_option( 50 version=pbf.version, 51 prog_name='PBF CLI' 52) 53def cli(): 54 pass 55 56 57@cli.command( 58 help='Initialize the project in the current directory' 59) 60def init(): 61 click.secho('Initializing') 62 if '.pbflock' in os.listdir(): 63 click.secho('.pbflock exists, exiting', fg="red") 64 return 65 with open('start.py', 'w', encoding="utf-8") as f: 66 f.write(_start_template) 67 click.secho('start.py created', fg='green') 68 with open(".pbflock", 'w', encoding="utf-8") as f: 69 f.write(json.dumps({ 70 "version": _start_template_version 71 })) 72 click.secho('.pbflock created', fg='green') 73 click.secho("Now you can run 'pbf start' to start the server", fg='green') 74 75 76@cli.command( 77 help='Start the server' 78) 79def start(): 80 click.secho('Starting') 81 if '.pbflock' not in os.listdir(): 82 click.secho('.pbflock not found, please run `pbf init` first', fg='red') 83 return 84 if 'start.py' not in os.listdir(): 85 click.secho('start.py not found, please run `pbf init` first', fg='red') 86 os.remove('.pbflock') 87 return 88 # 后台执行start.py 89 # 检测运行系统 90 if os.name == 'nt': 91 # Windows 使用cmd指令将进程挂起到后台 92 os.system('start python start.py') 93 else: 94 # Linux 使用nohup 95 os.system('nohup python start.py &') 96 97 click.secho('Server started', fg='green') 98 99 100@cli.command( 101 help='Create a new plugin' 102) 103@click.argument('plugin_name') 104@click.option('--plugins_path', '-p', help='The path to store plugins', default=_plugins_path) 105def create_plugin(plugin_name: str, plugins_path: str): 106 click.secho('Clone template ...') 107 installPlugin('template', plugin_name, plugins_path) 108 click.secho('Done.', fg='green') 109 110@cli.command( 111 help='Install a plugin from GitHub/PigBotFrameworkPlugins/' 112) 113@click.argument('plugin_id') 114@click.option('--plugins_path', '-p', help='The path to store plugins', default=_plugins_path) 115def install_plugin(plugin_id: str, plugins_path: str): 116 click.secho('Installing plugin ...') 117 installPlugin(plugin_id, plugin_id, plugins_path) 118 click.secho('Done.', fg='green') 119 120 121@cli.command( 122 help='List all plugins' 123) 124@click.option('--plugins_path', '-p', help='The path to store plugins', default=_plugins_path) 125def list_plugin(plugins_path: str): 126 plugins = os.listdir(plugins_path) 127 for plugin in plugins: 128 click.secho(plugin) 129 130 131@cli.command( 132 help='Remove a plugin' 133) 134@click.argument('plugin_name') 135@click.option('--plugins_path', '-p', help='The path to store plugins', default=_plugins_path) 136def remove_plugin(plugin_name: str, plugins_path: str): 137 try: 138 os.system(f'rm -rf {os.path.join(plugins_path, plugin_name)}') 139 except Exception as e: 140 click.secho(f'Error: {e}', fg='red') 141 return 142 click.secho('Done.', fg='green') 143 144 145if __name__ == '__main__': 146 cli()
art_txt =
" ____ ____ ____ ____ __ ______ \n/\\ _`\\ /\\ _`\\ /\\ _`\\ /\\ _`\\ /\\ \\ /\\__ _\\ \n\\ \\ \\L\\ \\\\ \\ \\L\\ \\ \\ \\ \\L\\_\\ \\ \\ \\/\\_\\ \\ \\ \\ \\/_/\\ \\/ \n \\ \\ ,__/ \\ \\ _ <' \\ \\ _\\/ \\ \\ \\/_/_ \\ \\ \\ __ \\ \\ \\ \n \\ \\ \\/ \\ \\ \\L\\ \\ \\ \\ \\/ \\ \\ \\L\\ \\ \\ \\ \\L\\ \\ \\_\\ \\__ \n \\ \\_\\ \\ \\____/ \\ \\_\\ \\ \\____/ \\ \\____/ /\\_____\\\n \\/_/ \\/___/ \\/_/ \\/___/ \\/___/ \\/_____/\n \n \n"
def
printPBF():
def
installPlugin(plugin_id: str, plugin_name: str, plugins_path: str):