[CI] Format code with Black (#1562)

This commit is contained in:
YunoHost Bot 2023-01-08 15:52:48 +01:00 committed by GitHub
parent a6db52b7b4
commit b9be18c781
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 175 additions and 196 deletions

View file

@ -24,289 +24,261 @@ import yaml
import json import json
import requests import requests
def main(): def main():
""" """ """
""" with open("../share/actionsmap.yml") as f:
with open('../share/actionsmap.yml') as f:
action_map = yaml.safe_load(f) action_map = yaml.safe_load(f)
try: try:
with open('/etc/yunohost/current_host', 'r') as f: with open("/etc/yunohost/current_host", "r") as f:
domain = f.readline().rstrip() domain = f.readline().rstrip()
except IOError: except IOError:
domain = requests.get('http://ip.yunohost.org').text domain = requests.get("http://ip.yunohost.org").text
with open('../debian/changelog') as f: with open("../debian/changelog") as f:
top_changelog = f.readline() 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 = { csrf = {
'name': 'X-Requested-With', "name": "X-Requested-With",
'in': 'header', "in": "header",
'required': True, "required": True,
'schema': { "schema": {"type": "string", "default": "Swagger API"},
'type': 'string',
'default': 'Swagger API'
}
} }
resource_list = { resource_list = {
'openapi': '3.0.3', "openapi": "3.0.3",
'info': { "info": {
'title': 'YunoHost API', "title": "YunoHost API",
'description': 'This is the YunoHost API used on all YunoHost instances. This API is essentially used by YunoHost Webadmin.', "description": "This is the YunoHost API used on all YunoHost instances. This API is essentially used by YunoHost Webadmin.",
'version': api_version, "version": api_version,
}, },
'servers': [ "servers": [
{ {
'url': "https://{domain}/yunohost/api", "url": "https://{domain}/yunohost/api",
'variables': { "variables": {
'domain': { "domain": {
'default': 'demo.yunohost.org', "default": "demo.yunohost.org",
'description': 'Your yunohost domain' "description": "Your yunohost domain",
} }
} },
} }
], ],
'tags': [ "tags": [{"name": "public", "description": "Public route"}],
{ "paths": {
'name': 'public', "/login": {
'description': 'Public route' "post": {
} "tags": ["public"],
], "summary": "Logs in and returns the authentication cookie",
'paths': { "parameters": [csrf],
'/login': { "requestBody": {
'post': { "required": True,
'tags': ['public'], "content": {
'summary': 'Logs in and returns the authentication cookie', "multipart/form-data": {
'parameters': [csrf], "schema": {
'requestBody': { "type": "object",
'required': True, "properties": {
'content': { "credentials": {
'multipart/form-data': { "type": "string",
'schema': { "format": "password",
'type': 'object',
'properties': {
'credentials': {
'type': 'string',
'format': 'password'
} }
}, },
'required': [ "required": ["credentials"],
'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': { "/installed": {
'get': { "get": {
'tags': ['public'], "tags": ["public"],
'summary': 'Test if the API is working', "summary": "Test if the API is working",
'parameters': [], "parameters": [],
'security': [], "security": [],
'responses': { "responses": {
'200': { "200": {
'description': 'Successfully working', "description": "Successfully working",
} }
} },
} }
} },
}, },
} }
def convert_categories(categories, parent_category=""): def convert_categories(categories, parent_category=""):
for category, category_params in categories.items(): for category, category_params in categories.items():
if parent_category: if parent_category:
category = f"{parent_category} {category}" category = f"{parent_category} {category}"
if 'subcategory_help' in category_params: if "subcategory_help" in category_params:
category_params['category_help'] = category_params['subcategory_help'] category_params["category_help"] = category_params["subcategory_help"]
if 'category_help' not in category_params: if "category_help" not in category_params:
category_params['category_help'] = '' category_params["category_help"] = ""
resource_list['tags'].append({ resource_list["tags"].append(
'name': category, {"name": category, "description": category_params["category_help"]}
'description': category_params['category_help'] )
})
for action, action_params in category_params["actions"].items():
for action, action_params in category_params['actions'].items(): if "action_help" not in action_params:
if 'action_help' not in action_params: action_params["action_help"] = ""
action_params['action_help'] = '' if "api" not in action_params:
if 'api' not in action_params:
continue continue
if not isinstance(action_params['api'], list): if not isinstance(action_params["api"], list):
action_params['api'] = [action_params['api']] action_params["api"] = [action_params["api"]]
for i, api in enumerate(action_params['api']): for i, api in enumerate(action_params["api"]):
print(api) print(api)
method, path = api.split(' ') method, path = api.split(" ")
method = method.lower() method = method.lower()
key_param = '' key_param = ""
if '{' in path: if "{" in path:
key_param = path[path.find("{")+1:path.find("}")] key_param = path[path.find("{") + 1 : path.find("}")]
resource_list['paths'].setdefault(path, {}) resource_list["paths"].setdefault(path, {})
notes = '' notes = ""
operationId = f"{category}_{action}" operationId = f"{category}_{action}"
if i > 0: if i > 0:
operationId += f"_{i}" operationId += f"_{i}"
operation = { operation = {
'tags': [category], "tags": [category],
'operationId': operationId, "operationId": operationId,
'summary': action_params['action_help'], "summary": action_params["action_help"],
'description': notes, "description": notes,
'responses': { "responses": {"200": {"description": "successful operation"}},
'200': {
'description': 'successful operation'
}
}
} }
if action_params.get('deprecated'): if action_params.get("deprecated"):
operation['deprecated'] = True operation["deprecated"] = True
operation['parameters'] = [] operation["parameters"] = []
if method == 'post': if method == "post":
operation['parameters'] = [csrf] operation["parameters"] = [csrf]
if 'arguments' in action_params: if "arguments" in action_params:
if method in ['put', 'post', 'patch']: if method in ["put", "post", "patch"]:
operation['requestBody'] = { operation["requestBody"] = {
'required': True, "required": True,
'content': { "content": {
'multipart/form-data': { "multipart/form-data": {
'schema': { "schema": {
'type': 'object', "type": "object",
'properties': { "properties": {},
}, "required": [],
'required': []
} }
} }
} },
} }
for arg_name, arg_params in action_params['arguments'].items(): for arg_name, arg_params in action_params["arguments"].items():
if 'help' not in arg_params: if "help" not in arg_params:
arg_params['help'] = '' arg_params["help"] = ""
param_type = 'query' param_type = "query"
allow_multiple = False allow_multiple = False
required = True required = True
allowable_values = None allowable_values = None
name = str(arg_name).replace('-', '_') name = str(arg_name).replace("-", "_")
if name[0] == '_': if name[0] == "_":
required = False required = False
if 'full' in arg_params: if "full" in arg_params:
name = arg_params['full'][2:] name = arg_params["full"][2:]
else: else:
name = name[2:] name = name[2:]
name = name.replace('-', '_') name = name.replace("-", "_")
if 'choices' in arg_params: if "choices" in arg_params:
allowable_values = arg_params['choices'] allowable_values = arg_params["choices"]
_type = 'string' _type = "string"
if 'type' in arg_params: if "type" in arg_params:
types = { types = {"open": "file", "int": "int"}
'open': 'file', _type = types[arg_params["type"]]
'int': 'int' if (
} "action" in arg_params
_type = types[arg_params['type']] and arg_params["action"] == "store_true"
if 'action' in arg_params and arg_params['action'] == 'store_true': ):
_type = 'boolean' _type = "boolean"
if 'nargs' in arg_params: if "nargs" in arg_params:
if arg_params['nargs'] == '*': if arg_params["nargs"] == "*":
allow_multiple = True allow_multiple = True
required = False required = False
_type = 'array' _type = "array"
if arg_params['nargs'] == '+': if arg_params["nargs"] == "+":
allow_multiple = True allow_multiple = True
required = True required = True
_type = 'array' _type = "array"
if arg_params['nargs'] == '?': if arg_params["nargs"] == "?":
allow_multiple = False allow_multiple = False
required = False required = False
else: else:
allow_multiple = False allow_multiple = False
if name == key_param: if name == key_param:
param_type = 'path' param_type = "path"
required = True required = True
allow_multiple = False allow_multiple = False
if method in ['put', 'post', 'patch']: if method in ["put", "post", "patch"]:
schema = operation['requestBody']['content']['multipart/form-data']['schema'] schema = operation["requestBody"]["content"][
schema['properties'][name] = { "multipart/form-data"
'type': _type, ]["schema"]
'description': arg_params['help'] schema["properties"][name] = {
"type": _type,
"description": arg_params["help"],
} }
if required: if required:
schema['required'].append(name) schema["required"].append(name)
prop_schema = schema['properties'][name] prop_schema = schema["properties"][name]
else: else:
parameters = { parameters = {
'name': name, "name": name,
'in': param_type, "in": param_type,
'description': arg_params['help'], "description": arg_params["help"],
'required': required, "required": required,
'schema': { "schema": {
'type': _type, "type": _type,
}, },
'explode': allow_multiple "explode": allow_multiple,
} }
prop_schema = parameters['schema'] prop_schema = parameters["schema"]
operation['parameters'].append(parameters) operation["parameters"].append(parameters)
if allowable_values is not None: if allowable_values is not None:
prop_schema['enum'] = allowable_values prop_schema["enum"] = allowable_values
if 'default' in arg_params: if "default" in arg_params:
prop_schema['default'] = arg_params['default'] prop_schema["default"] = arg_params["default"]
if arg_params.get('metavar') == 'PASSWORD': if arg_params.get("metavar") == "PASSWORD":
prop_schema['format'] = 'password' prop_schema["format"] = "password"
if arg_params.get('metavar') == 'MAIL': if arg_params.get("metavar") == "MAIL":
prop_schema['format'] = 'mail' prop_schema["format"] = "mail"
# Those lines seems to slow swagger ui too much # 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] # prop_schema['pattern'] = arg_params['extra']['pattern'][0]
resource_list["paths"][path][method.lower()] = operation
resource_list['paths'][path][method.lower()] = operation
# Includes subcategories # Includes subcategories
if 'subcategories' in category_params: if "subcategories" in category_params:
convert_categories(category_params['subcategories'], category) convert_categories(category_params["subcategories"], category)
del action_map['_global'] del action_map["_global"]
convert_categories(action_map) convert_categories(action_map)
openapi_json = json.dumps(resource_list) openapi_json = json.dumps(resource_list)
# Save the OpenAPI json # 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) f.write(openapi_json)
openapi_js = f"var openapiJSON = {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) f.write(openapi_js)
if __name__ == "__main__":
if __name__ == '__main__':
sys.exit(main()) sys.exit(main())

View file

@ -2300,7 +2300,9 @@ def _extract_app(src: str) -> Tuple[Dict, str]:
url = app_info["git"]["url"] url = app_info["git"]["url"]
branch = app_info["git"]["branch"] branch = app_info["git"]["branch"]
revision = str(app_info["git"]["revision"]) 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 # App is a git repo url
elif _is_app_repo_url(src): elif _is_app_repo_url(src):
url = src.strip().strip("/") 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 = {} url: str, branch: Optional[str] = None, revision: str = "HEAD", app_info: Dict = {}
) -> Tuple[Dict, str]: ) -> Tuple[Dict, str]:
logger.debug("Checking default branch") logger.debug("Checking default branch")
try: 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: except Exception:
raise YunohostError("app_sources_fetch_failed") raise YunohostError("app_sources_fetch_failed")
if not branch: if not branch:
default_branch = None default_branch = None
try: try:
for line in git_remote_show.split('\n'): for line in git_remote_show.split("\n"):
if "HEAD branch:" in line: if "HEAD branch:" in line:
default_branch = line.split()[-1] default_branch = line.split()[-1]
except Exception: except Exception:
@ -2391,11 +2396,13 @@ def _extract_app_from_gitrepo(
if not default_branch: if not default_branch:
logger.warning("Failed to parse default branch, trying 'main'") logger.warning("Failed to parse default branch, trying 'main'")
branch = 'main' branch = "main"
else: else:
if default_branch in ['testing', 'dev']: if default_branch in ["testing", "dev"]:
logger.warning(f"Trying 'master' branch instead of default '{default_branch}'") logger.warning(
branch = 'master' f"Trying 'master' branch instead of default '{default_branch}'"
)
branch = "master"
else: else:
branch = default_branch branch = default_branch