def workon_project(self, project): config_path = get_config_path(project) output('配置文件位置:{}'.format(config_path)) active_path = get_active_path() with open(active_path, 'w') as fp: fp.write(project) output('[green]已激活 {} 的项目配置'.format(project))
def add_project(self, args): project = args.project.lower() config_path = get_config_path(project, check_exists=False) if exists(config_path): output('[red]配置 {} 已存在'.format(project)) sys.exit(1) self._new_project(config_path)
def show_project(self, args): project = args.project if not project: project = get_active_project() project = project.lower() config_path = get_config_path(project) title = 'BGE 开放平台 Python SDK 配置文件' output_file(config_path, title=title, subtitle=config_path)
def remove_project(self, args): project = args.project.lower() activate_project = get_active_project() if activate_project == project: output( '[red]无法删除正在使用的配置,请先使用 bge workon 切换至其他项目[/red]' ) sys.exit(1) config_path = get_config_path(project) if not confirm(prompt='确认删除配置项目 {}?'.format(project)): output('已取消删除') sys.exit(0) try: os.unlink(config_path) except (IOError, OSError): pass output('[green]成功删除配置项目[/green] {}'.format(project))
def _write_token_config(self, project, token_result): access_token = token_result['access_token'] token_type = token_result['token_type'] expires_in = str(token_result['expires_in']) scope = token_result['scope'] config_path = get_config_path(project) config_parser = get_config_parser(config_path) section_name = constants.DEFAULT_TOKEN_SECTION if section_name not in config_parser.sections(): config_parser.add_section(section_name) config_parser.set(section_name, 'access_token', access_token) config_parser.set(section_name, 'token_type', token_type) config_parser.set(section_name, 'expires_in', expires_in) config_parser.set(section_name, 'scope', scope) with open(config_path, 'w') as config_file: config_parser.write(config_file) output('[green]令牌已保存至:[/green]{}'.format(config_path))
def handler(self, args): project = get_active_project() config_path = get_config_path(project) config_parser = get_config_parser(config_path) oauth2_section = constants.DEFAULT_OAUTH2_SECTION client_id = config_get(config_parser.get, oauth2_section, 'client_id') client_secret = config_get(config_parser.get, oauth2_section, 'client_secret') endpoint = config_get(config_parser.get, oauth2_section, 'endpoint') redirect_uri = args.redirect_uri oauth2 = OAuth2(client_id, client_secret, endpoint=endpoint, timeout=60.) authorization_url = oauth2.get_authorization_url(redirect_uri, state=args.state, scopes=args.scopes) output('授权页面地址为:{}'.format(authorization_url)) output('浏览器启动中...') webbrowser.open(authorization_url, new=1, autoraise=False) output('请在浏览器登录并完成授权,复制跳转后页面链接的 code 参数并关闭浏览器。\n') while True: code = Prompt.ask('请输入浏览器回调地址携带的参数 [cyan]code[/cyan]') code = code.strip() if code: break output('[red]参数 code 不能为空[/red]') try: token_result = oauth2.exchange_authorization_code( code, redirect_uri=redirect_uri) except APIError as e: output('[red]令牌获取出错:[/red]') output_json(e.result) sys.exit(1) self._write_token_config(project, token_result) output('[green]令牌内容如下[/green]') content = [] for key in [ 'access_token', 'token_type', 'expires_in', 'scope', 'refresh_token' ]: content.append('{} = {}'.format(key, token_result[key])) output_syntax('\n'.join(content), lexer='INI')
def handler(self, args): project = get_active_project() config_path = get_config_path(project) config_parser = get_config_parser(config_path) oauth2_section = constants.DEFAULT_OAUTH2_SECTION client_id = config_get(config_parser.get, oauth2_section, 'client_id') client_secret = config_get( config_parser.get, oauth2_section, 'client_secret') endpoint = config_get(config_parser.get, oauth2_section, 'endpoint') oauth2 = OAuth2( client_id, client_secret, endpoint=endpoint, timeout=60.) try: token_result = oauth2.get_credentials_token() except APIError as e: output('[red]令牌获取出错:[/red]') output_json(e.result) sys.exit(1) self._write_token_config(project, token_result) console.rule('[green]令牌内容如下[/green]') content = [] for key in ['access_token', 'token_type', 'expires_in', 'scope']: content.append('{} = {}'.format(key, token_result[key])) output_syntax('\n'.join(content), lexer='INI')
def handler(self, args): project = get_active_project() output('当前正在配置 {}:\n'.format(project)) config_path = get_config_path(project, check_exists=False) self._new_project(config_path)