Overall optimations for application id naming

This commit is contained in:
2025-07-17 17:41:52 +02:00
parent 562603a8cd
commit 409e659143
9 changed files with 102 additions and 57 deletions

View File

@@ -1,9 +1,15 @@
def get_docker_paths(path_docker_compose_instances: str, application_id: str) -> dict:
import sys, os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from module_utils.entity_name_utils import get_entity_name
def get_docker_paths(application_id: str, path_docker_compose_instances: str) -> dict:
"""
Build the docker_compose dict based on
path_docker_compose_instances and application_id.
Uses get_entity_name to extract the entity name from application_id.
"""
base = f"{path_docker_compose_instances}{application_id}/"
entity = get_entity_name(application_id)
base = f"{path_docker_compose_instances}{entity}/"
return {
'directories': {
@@ -23,5 +29,5 @@ def get_docker_paths(path_docker_compose_instances: str, application_id: str) ->
class FilterModule(object):
def filters(self):
return {
'get_docker_paths': get_docker_paths,
'get_docker_paths': get_docker_paths,
}

View File

@@ -1,52 +1,6 @@
import os
import yaml
def _load_categories_tree(categories_file):
with open(categories_file, 'r', encoding='utf-8') as f:
categories = yaml.safe_load(f)['roles']
return categories
def _flatten_categories(tree, prefix=''):
"""Flattens nested category tree to all possible category paths."""
result = []
for k, v in tree.items():
current = f"{prefix}-{k}" if prefix else k
result.append(current)
if isinstance(v, dict):
for sk, sv in v.items():
if isinstance(sv, dict):
result.extend(_flatten_categories({sk: sv}, current))
return result
def get_entity_name(role_name):
"""
Automatically get the entity name from a role name by removing the
longest matching category path from categories.yml.
"""
possible_locations = [
os.path.join(os.getcwd(), 'roles', 'categories.yml'),
os.path.join(os.path.dirname(__file__), '..', 'roles', 'categories.yml'),
'roles/categories.yml',
]
categories_file = None
for loc in possible_locations:
if os.path.exists(loc):
categories_file = loc
break
if not categories_file:
return role_name
categories_tree = _load_categories_tree(categories_file)
all_category_paths = _flatten_categories(categories_tree)
role_name_lc = role_name.lower()
all_category_paths = [cat.lower() for cat in all_category_paths]
for cat in sorted(all_category_paths, key=len, reverse=True):
if role_name_lc.startswith(cat + "-"):
return role_name[len(cat) + 1:]
if role_name_lc == cat:
return ""
return role_name
import sys, os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from module_utils.entity_name_utils import get_entity_name
class FilterModule(object):
def filters(self):