2022-10-09 12:54:11 +00:00
|
|
|
import json
|
2023-04-10 02:57:44 +00:00
|
|
|
import os
|
2022-09-09 11:17:25 +00:00
|
|
|
from functools import partial
|
|
|
|
|
2023-04-20 03:13:28 +00:00
|
|
|
from common.utils.yml import yaml_load_with_i18n
|
2023-04-10 02:57:44 +00:00
|
|
|
|
2022-09-09 11:17:25 +00:00
|
|
|
|
|
|
|
def check_platform_method(manifest, manifest_path):
|
|
|
|
required_keys = ['category', 'method', 'name', 'id', 'type']
|
|
|
|
less_key = set(required_keys) - set(manifest.keys())
|
|
|
|
if less_key:
|
|
|
|
raise ValueError("Manifest missing keys: {}, {}".format(less_key, manifest_path))
|
|
|
|
if not isinstance(manifest['type'], list):
|
|
|
|
raise ValueError("Manifest type must be a list: {}".format(manifest_path))
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def check_platform_methods(methods):
|
|
|
|
ids = [m['id'] for m in methods]
|
|
|
|
for i, _id in enumerate(ids):
|
2023-01-16 11:02:09 +00:00
|
|
|
if _id in ids[i + 1:]:
|
2022-09-09 11:17:25 +00:00
|
|
|
raise ValueError("Duplicate id: {}".format(_id))
|
|
|
|
|
|
|
|
|
2023-04-13 11:02:04 +00:00
|
|
|
def generate_serializer(data):
|
|
|
|
from common.serializers import create_serializer_class
|
|
|
|
params = data.pop('params', None)
|
|
|
|
if not params:
|
|
|
|
return None
|
|
|
|
serializer_name = data['id'].title().replace('_', '') + 'Serializer'
|
|
|
|
return create_serializer_class(serializer_name, params)
|
|
|
|
|
|
|
|
|
2023-11-22 10:22:51 +00:00
|
|
|
def get_platform_automation_methods(path, lang=None):
|
2022-09-09 11:17:25 +00:00
|
|
|
methods = []
|
2023-01-16 11:02:09 +00:00
|
|
|
for root, dirs, files in os.walk(path, topdown=False):
|
2022-09-09 11:17:25 +00:00
|
|
|
for name in files:
|
|
|
|
path = os.path.join(root, name)
|
|
|
|
if not path.endswith('manifest.yml'):
|
|
|
|
continue
|
|
|
|
|
2023-05-27 03:53:00 +00:00
|
|
|
with open(path, 'r', encoding='utf8') as f:
|
2023-12-05 09:14:35 +00:00
|
|
|
manifest = yaml_load_with_i18n(f, lang=lang)
|
2022-09-09 11:17:25 +00:00
|
|
|
check_platform_method(manifest, path)
|
|
|
|
manifest['dir'] = os.path.dirname(path)
|
2023-04-13 11:02:04 +00:00
|
|
|
manifest['params_serializer'] = generate_serializer(manifest)
|
2022-09-09 11:17:25 +00:00
|
|
|
methods.append(manifest)
|
|
|
|
|
|
|
|
check_platform_methods(methods)
|
|
|
|
return methods
|
|
|
|
|
|
|
|
|
|
|
|
def filter_key(manifest, attr, value):
|
|
|
|
manifest_value = manifest.get(attr, '')
|
|
|
|
if isinstance(manifest_value, str):
|
|
|
|
manifest_value = [manifest_value]
|
|
|
|
return value in manifest_value or 'all' in manifest_value
|
|
|
|
|
|
|
|
|
2023-04-10 02:57:44 +00:00
|
|
|
def filter_platform_methods(category, tp_name, method=None, methods=None):
|
2023-01-16 11:02:09 +00:00
|
|
|
methods = platform_automation_methods if methods is None else methods
|
2022-09-09 11:17:25 +00:00
|
|
|
if category:
|
|
|
|
methods = filter(partial(filter_key, attr='category', value=category), methods)
|
2023-04-10 02:57:44 +00:00
|
|
|
if tp_name:
|
|
|
|
methods = filter(partial(filter_key, attr='type', value=tp_name), methods)
|
2022-09-09 11:17:25 +00:00
|
|
|
if method:
|
|
|
|
methods = filter(lambda x: x['method'] == method, methods)
|
|
|
|
return methods
|
|
|
|
|
|
|
|
|
2024-02-19 09:32:32 +00:00
|
|
|
def sorted_methods(methods):
|
|
|
|
return sorted(methods, key=lambda x: x.get('priority', 10))
|
|
|
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
platform_automation_methods = get_platform_automation_methods(BASE_DIR)
|
2022-09-09 11:17:25 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-10-09 12:54:11 +00:00
|
|
|
print(json.dumps(platform_automation_methods, indent=4))
|