From b9be18c781c31c93d1f025992e595cd345005880 Mon Sep 17 00:00:00 2001 From: YunoHost Bot Date: Sun, 8 Jan 2023 15:52:48 +0100 Subject: [PATCH] [CI] Format code with Black (#1562) --- doc/generate_api_doc.py | 348 ++++++++++++++++++---------------------- src/app.py | 23 ++- 2 files changed, 175 insertions(+), 196 deletions(-) diff --git a/doc/generate_api_doc.py b/doc/generate_api_doc.py index 939dd90bd..fc44ffbcd 100644 --- a/doc/generate_api_doc.py +++ b/doc/generate_api_doc.py @@ -24,289 +24,261 @@ import yaml import json import requests + def main(): - """ - """ - with open('../share/actionsmap.yml') as f: + """ """ + with open("../share/actionsmap.yml") as f: action_map = yaml.safe_load(f) try: - with open('/etc/yunohost/current_host', 'r') as f: + with open("/etc/yunohost/current_host", "r") as f: domain = f.readline().rstrip() except IOError: - domain = requests.get('http://ip.yunohost.org').text - with open('../debian/changelog') as f: + domain = requests.get("http://ip.yunohost.org").text + with open("../debian/changelog") as f: top_changelog = f.readline() - api_version = top_changelog[top_changelog.find("(")+1:top_changelog.find(")")] + api_version = top_changelog[top_changelog.find("(") + 1 : top_changelog.find(")")] csrf = { - 'name': 'X-Requested-With', - 'in': 'header', - 'required': True, - 'schema': { - 'type': 'string', - 'default': 'Swagger API' - } - + "name": "X-Requested-With", + "in": "header", + "required": True, + "schema": {"type": "string", "default": "Swagger API"}, } resource_list = { - 'openapi': '3.0.3', - 'info': { - 'title': 'YunoHost API', - 'description': 'This is the YunoHost API used on all YunoHost instances. This API is essentially used by YunoHost Webadmin.', - 'version': api_version, - + "openapi": "3.0.3", + "info": { + "title": "YunoHost API", + "description": "This is the YunoHost API used on all YunoHost instances. This API is essentially used by YunoHost Webadmin.", + "version": api_version, }, - 'servers': [ + "servers": [ { - 'url': "https://{domain}/yunohost/api", - 'variables': { - 'domain': { - 'default': 'demo.yunohost.org', - 'description': 'Your yunohost domain' + "url": "https://{domain}/yunohost/api", + "variables": { + "domain": { + "default": "demo.yunohost.org", + "description": "Your yunohost domain", } - } + }, } ], - 'tags': [ - { - 'name': 'public', - 'description': 'Public route' - } - ], - 'paths': { - '/login': { - 'post': { - 'tags': ['public'], - 'summary': 'Logs in and returns the authentication cookie', - 'parameters': [csrf], - 'requestBody': { - 'required': True, - 'content': { - 'multipart/form-data': { - 'schema': { - 'type': 'object', - 'properties': { - 'credentials': { - 'type': 'string', - 'format': 'password' + "tags": [{"name": "public", "description": "Public route"}], + "paths": { + "/login": { + "post": { + "tags": ["public"], + "summary": "Logs in and returns the authentication cookie", + "parameters": [csrf], + "requestBody": { + "required": True, + "content": { + "multipart/form-data": { + "schema": { + "type": "object", + "properties": { + "credentials": { + "type": "string", + "format": "password", } }, - 'required': [ - 'credentials' - ] + "required": ["credentials"], } } + }, + }, + "security": [], + "responses": { + "200": { + "description": "Successfully login", + "headers": {"Set-Cookie": {"schema": {"type": "string"}}}, } }, - 'security': [], - 'responses': { - '200': { - 'description': 'Successfully login', - 'headers': { - 'Set-Cookie': { - 'schema': { - 'type': 'string' - } - } - } - } - } } }, - '/installed': { - 'get': { - 'tags': ['public'], - 'summary': 'Test if the API is working', - 'parameters': [], - 'security': [], - 'responses': { - '200': { - 'description': 'Successfully working', + "/installed": { + "get": { + "tags": ["public"], + "summary": "Test if the API is working", + "parameters": [], + "security": [], + "responses": { + "200": { + "description": "Successfully working", } - } + }, } - } + }, }, } - def convert_categories(categories, parent_category=""): for category, category_params in categories.items(): if parent_category: category = f"{parent_category} {category}" - if 'subcategory_help' in category_params: - category_params['category_help'] = category_params['subcategory_help'] + if "subcategory_help" in category_params: + category_params["category_help"] = category_params["subcategory_help"] - if 'category_help' not in category_params: - category_params['category_help'] = '' - resource_list['tags'].append({ - 'name': category, - 'description': category_params['category_help'] - }) + if "category_help" not in category_params: + category_params["category_help"] = "" + resource_list["tags"].append( + {"name": category, "description": category_params["category_help"]} + ) - - for action, action_params in category_params['actions'].items(): - if 'action_help' not in action_params: - action_params['action_help'] = '' - if 'api' not in action_params: + for action, action_params in category_params["actions"].items(): + if "action_help" not in action_params: + action_params["action_help"] = "" + if "api" not in action_params: continue - if not isinstance(action_params['api'], list): - action_params['api'] = [action_params['api']] + if not isinstance(action_params["api"], list): + action_params["api"] = [action_params["api"]] - for i, api in enumerate(action_params['api']): + for i, api in enumerate(action_params["api"]): print(api) - method, path = api.split(' ') + method, path = api.split(" ") method = method.lower() - key_param = '' - if '{' in path: - key_param = path[path.find("{")+1:path.find("}")] - resource_list['paths'].setdefault(path, {}) + key_param = "" + if "{" in path: + key_param = path[path.find("{") + 1 : path.find("}")] + resource_list["paths"].setdefault(path, {}) - notes = '' + notes = "" operationId = f"{category}_{action}" if i > 0: operationId += f"_{i}" operation = { - 'tags': [category], - 'operationId': operationId, - 'summary': action_params['action_help'], - 'description': notes, - 'responses': { - '200': { - 'description': 'successful operation' - } - } + "tags": [category], + "operationId": operationId, + "summary": action_params["action_help"], + "description": notes, + "responses": {"200": {"description": "successful operation"}}, } - if action_params.get('deprecated'): - operation['deprecated'] = True + if action_params.get("deprecated"): + operation["deprecated"] = True - operation['parameters'] = [] - if method == 'post': - operation['parameters'] = [csrf] + operation["parameters"] = [] + if method == "post": + operation["parameters"] = [csrf] - if 'arguments' in action_params: - if method in ['put', 'post', 'patch']: - operation['requestBody'] = { - 'required': True, - 'content': { - 'multipart/form-data': { - 'schema': { - 'type': 'object', - 'properties': { - }, - 'required': [] + if "arguments" in action_params: + if method in ["put", "post", "patch"]: + operation["requestBody"] = { + "required": True, + "content": { + "multipart/form-data": { + "schema": { + "type": "object", + "properties": {}, + "required": [], } } - } + }, } - for arg_name, arg_params in action_params['arguments'].items(): - if 'help' not in arg_params: - arg_params['help'] = '' - param_type = 'query' + for arg_name, arg_params in action_params["arguments"].items(): + if "help" not in arg_params: + arg_params["help"] = "" + param_type = "query" allow_multiple = False required = True allowable_values = None - name = str(arg_name).replace('-', '_') - if name[0] == '_': + name = str(arg_name).replace("-", "_") + if name[0] == "_": required = False - if 'full' in arg_params: - name = arg_params['full'][2:] + if "full" in arg_params: + name = arg_params["full"][2:] else: name = name[2:] - name = name.replace('-', '_') + name = name.replace("-", "_") - if 'choices' in arg_params: - allowable_values = arg_params['choices'] - _type = 'string' - if 'type' in arg_params: - types = { - 'open': 'file', - 'int': 'int' - } - _type = types[arg_params['type']] - if 'action' in arg_params and arg_params['action'] == 'store_true': - _type = 'boolean' + if "choices" in arg_params: + allowable_values = arg_params["choices"] + _type = "string" + if "type" in arg_params: + types = {"open": "file", "int": "int"} + _type = types[arg_params["type"]] + if ( + "action" in arg_params + and arg_params["action"] == "store_true" + ): + _type = "boolean" - if 'nargs' in arg_params: - if arg_params['nargs'] == '*': + if "nargs" in arg_params: + if arg_params["nargs"] == "*": allow_multiple = True required = False - _type = 'array' - if arg_params['nargs'] == '+': + _type = "array" + if arg_params["nargs"] == "+": allow_multiple = True required = True - _type = 'array' - if arg_params['nargs'] == '?': + _type = "array" + if arg_params["nargs"] == "?": allow_multiple = False required = False else: allow_multiple = False - if name == key_param: - param_type = 'path' + param_type = "path" required = True allow_multiple = False - if method in ['put', 'post', 'patch']: - schema = operation['requestBody']['content']['multipart/form-data']['schema'] - schema['properties'][name] = { - 'type': _type, - 'description': arg_params['help'] + if method in ["put", "post", "patch"]: + schema = operation["requestBody"]["content"][ + "multipart/form-data" + ]["schema"] + schema["properties"][name] = { + "type": _type, + "description": arg_params["help"], } if required: - schema['required'].append(name) - prop_schema = schema['properties'][name] + schema["required"].append(name) + prop_schema = schema["properties"][name] else: parameters = { - 'name': name, - 'in': param_type, - 'description': arg_params['help'], - 'required': required, - 'schema': { - 'type': _type, + "name": name, + "in": param_type, + "description": arg_params["help"], + "required": required, + "schema": { + "type": _type, }, - 'explode': allow_multiple + "explode": allow_multiple, } - prop_schema = parameters['schema'] - operation['parameters'].append(parameters) + prop_schema = parameters["schema"] + operation["parameters"].append(parameters) if allowable_values is not None: - prop_schema['enum'] = allowable_values - if 'default' in arg_params: - prop_schema['default'] = arg_params['default'] - if arg_params.get('metavar') == 'PASSWORD': - prop_schema['format'] = 'password' - if arg_params.get('metavar') == 'MAIL': - prop_schema['format'] = 'mail' + prop_schema["enum"] = allowable_values + if "default" in arg_params: + prop_schema["default"] = arg_params["default"] + if arg_params.get("metavar") == "PASSWORD": + prop_schema["format"] = "password" + if arg_params.get("metavar") == "MAIL": + prop_schema["format"] = "mail" # Those lines seems to slow swagger ui too much - #if 'pattern' in arg_params.get('extra', {}): + # if 'pattern' in arg_params.get('extra', {}): # prop_schema['pattern'] = arg_params['extra']['pattern'][0] - - - resource_list['paths'][path][method.lower()] = operation + resource_list["paths"][path][method.lower()] = operation # Includes subcategories - if 'subcategories' in category_params: - convert_categories(category_params['subcategories'], category) + if "subcategories" in category_params: + convert_categories(category_params["subcategories"], category) - del action_map['_global'] + del action_map["_global"] convert_categories(action_map) openapi_json = json.dumps(resource_list) # Save the OpenAPI json - with open(os.getcwd() + '/openapi.json', 'w') as f: + with open(os.getcwd() + "/openapi.json", "w") as f: f.write(openapi_json) openapi_js = f"var openapiJSON = {openapi_json}" - with open(os.getcwd() + '/openapi.js', 'w') as f: + with open(os.getcwd() + "/openapi.js", "w") as f: f.write(openapi_js) - -if __name__ == '__main__': +if __name__ == "__main__": sys.exit(main()) diff --git a/src/app.py b/src/app.py index b1ae7410b..dfaa36a1e 100644 --- a/src/app.py +++ b/src/app.py @@ -2300,7 +2300,9 @@ def _extract_app(src: str) -> Tuple[Dict, str]: url = app_info["git"]["url"] branch = app_info["git"]["branch"] revision = str(app_info["git"]["revision"]) - return _extract_app_from_gitrepo(url, branch=branch, revision=revision, app_info=app_info) + return _extract_app_from_gitrepo( + url, branch=branch, revision=revision, app_info=app_info + ) # App is a git repo url elif _is_app_repo_url(src): url = src.strip().strip("/") @@ -2372,18 +2374,21 @@ def _extract_app_from_gitrepo( url: str, branch: Optional[str] = None, revision: str = "HEAD", app_info: Dict = {} ) -> Tuple[Dict, str]: - logger.debug("Checking default branch") try: - git_remote_show = check_output(["git", "remote", "show", url], env={"GIT_TERMINAL_PROMPT": "0", "LC_ALL": "C"}, shell=False) + git_remote_show = check_output( + ["git", "remote", "show", url], + env={"GIT_TERMINAL_PROMPT": "0", "LC_ALL": "C"}, + shell=False, + ) except Exception: raise YunohostError("app_sources_fetch_failed") if not branch: default_branch = None try: - for line in git_remote_show.split('\n'): + for line in git_remote_show.split("\n"): if "HEAD branch:" in line: default_branch = line.split()[-1] except Exception: @@ -2391,11 +2396,13 @@ def _extract_app_from_gitrepo( if not default_branch: logger.warning("Failed to parse default branch, trying 'main'") - branch = 'main' + branch = "main" else: - if default_branch in ['testing', 'dev']: - logger.warning(f"Trying 'master' branch instead of default '{default_branch}'") - branch = 'master' + if default_branch in ["testing", "dev"]: + logger.warning( + f"Trying 'master' branch instead of default '{default_branch}'" + ) + branch = "master" else: branch = default_branch