latest version of apis event and cahce ablitites added

This commit is contained in:
2025-02-10 11:41:38 +03:00
parent e832ec7603
commit 26f601f01a
396 changed files with 34981 additions and 2 deletions

View File

@@ -0,0 +1,46 @@
from Events.Engine import CategoryCluster
class CategoryBulk:
def __init__(self, category_cluster: CategoryCluster = None, name: str = ""):
self.category_cluster = category_cluster
self.name = name
class CategoryClusterController:
imports_dict: list[CategoryBulk] = []
@property
def imports(self):
return self.imports_dict
@classmethod
def import_all_category_clusters(cls, category_clusters):
"""
Imports all category clusters from the given list
{ "category_cluster_name": "category_cluster_module" }
"""
if not hasattr(category_clusters, "__all__"):
raise ValueError(
f"Given module {str(category_clusters)} does not have __all__ attribute"
)
for iter_module in [str(item) for item in category_clusters.__all__]:
# CategoryCluster which represent api routers for each category
cls.imports_dict.append(
CategoryBulk(
category_cluster=getattr(category_clusters, iter_module, None),
name=iter_module,
)
)
@classmethod
def as_dict(cls):
to_dict = {}
for cluster in cls.imports_dict:
to_dict[cluster.name] = cluster.category_cluster
return to_dict
cluster_controller = CategoryClusterController()

View File

@@ -0,0 +1,108 @@
from ApiLayers.AllConfigs.Redis.configs import (
RedisCategoryKeys,
RedisCategoryPageInfoKeysAction,
)
class PrepareRedisItems:
MENU_FIRST_LAYER_KEY: str = RedisCategoryKeys.MENU_FIRST_LAYER
MENU_FIRST_LAYER_VALUE: set[str] = set()
CLUSTER_INDEX_KEY: str = RedisCategoryKeys.CLUSTER_INDEX
CLUSTER_INDEX_VALUE: dict = {}
CLUSTER_FUNCTION_CODES_KEY: str = RedisCategoryKeys.CLUSTER_FUNCTION_CODES
CLUSTER_FUNCTION_CODES_VALUE: dict = {}
METHOD_FUNCTION_CODES_KEY: str = RedisCategoryKeys.METHOD_FUNCTION_CODES
METHOD_FUNCTION_CODES_VALUE: dict = {}
ENDPOINT2CLASS_KEY: str = RedisCategoryKeys.ENDPOINT2CLASS
ENDPOINT2CLASS_VALUE: dict = {}
PAGE_INFO_KEY: str = RedisCategoryPageInfoKeysAction.page_index
PAGE_INFO_VALUE: dict = {}
@property
def as_dict(self):
return {
self.MENU_FIRST_LAYER_KEY: list(self.MENU_FIRST_LAYER_VALUE),
self.CLUSTER_INDEX_KEY: self.CLUSTER_INDEX_VALUE,
self.CLUSTER_FUNCTION_CODES_KEY: self.CLUSTER_FUNCTION_CODES_VALUE,
self.METHOD_FUNCTION_CODES_KEY: self.METHOD_FUNCTION_CODES_VALUE,
self.ENDPOINT2CLASS_KEY: self.ENDPOINT2CLASS_VALUE,
self.PAGE_INFO_KEY: self.PAGE_INFO_VALUE,
}
class DecoratorModule:
@staticmethod
def get_all_decorators(func):
"""
Get all decorators of a function, excluding the original function itself.
Returns a list of decorator functions in the order they were applied.
"""
decorators = []
current_func = func
original_qualname = getattr(func, "__qualname__", "")
while hasattr(current_func, "__wrapped__"):
if hasattr(current_func, "__closure__") and current_func.__closure__:
for cell in current_func.__closure__:
decorator = cell.cell_contents
# Only add if it's a callable and not the original function
if (
callable(decorator)
and getattr(decorator, "__qualname__", "") != original_qualname
):
decorators.append(decorator)
current_func = current_func.__wrapped__
return list(
dict.fromkeys(decorators)
) # Remove duplicates while preserving order
@staticmethod
def get_actual_decorators(method_endpoint):
original_qualname = getattr(
method_endpoint.endpoint_callable, "__qualname__", ""
)
actual_decorators = [
d
for d in method_endpoint.DECORATORS_LIST or []
if callable(d) and getattr(d, "__qualname__", "") != original_qualname
]
return actual_decorators
@classmethod
def apply_decorators(cls, method_endpoint):
# Get the original function and its qualname
function_callable = method_endpoint.endpoint_callable
# Filter out the original function and apply decorators
actual_decorators = cls.get_actual_decorators(method_endpoint)
# Apply decorators in reverse order (to match @ syntax behavior)
for decorator in reversed(actual_decorators):
try:
function_callable = decorator(function_callable)
except Exception as e:
print(
f"Warning: Failed to apply decorator {decorator.__qualname__}: {str(e)}"
)
method_endpoint.endpoint_callable = function_callable
# Get the final list of applied decorators (for debugging)
applied_decorators = cls.get_all_decorators(method_endpoint.endpoint_callable)
applied_decorators_qualname = [
getattr(d, "__qualname__", str(d)) for d in applied_decorators
]
if applied_decorators:
print(
f"Applied decorators for {method_endpoint.name}:",
applied_decorators_qualname,
)
return applied_decorators_qualname
@classmethod
def list_qualname(cls, method_endpoint_list):
return [
getattr(method_endpoint, "__qualname__", "")
for method_endpoint in method_endpoint_list
]

View File

@@ -0,0 +1,17 @@
from Events.AllEvents.events_file import events_list
from .category_cluster_models import cluster_controller
def get_cluster_controller_group():
for cluster in events_list:
cluster_controller.import_all_category_clusters(cluster)
return cluster_controller
"""
prepare_routing = PrepareRouting(cluster_controller_group=cluster_controller)
prepare_events = PrepareEvents(cluster_controller_group=cluster_controller)
print(set_items_2_redis)
print(prepare_routing)
"""

View File

@@ -0,0 +1,226 @@
from typing import Any
from ApiLayers.ApiServices.Cluster.create_router import (
CreateRouterFromCluster,
CreateEndpointFromCluster,
)
from ApiLayers.AllConfigs.Redis.configs import (
RedisCategoryKeys,
RedisCategoryPageInfoKeys,
)
from Events.Engine.abstract_class import CategoryCluster
from Services.Redis.Actions.actions import RedisActions
from Services.Redis.Models.cluster import RedisList
from .prepare_redis_items import DecoratorModule, PrepareRedisItems
from .category_cluster_models import CategoryClusterController
class PrepareRouting(DecoratorModule):
__routers_list: list[Any] = list()
__endpoints_list: list[Any] = list()
__safe_endpoint_list: list[Any] = list()
def __init__(self, cluster_controller_group: CategoryClusterController):
self.cluster_controller_group = cluster_controller_group
self.prepare_needs()
def __str__(self):
return f"\nPrepared Routing:\n\n{self.routers}\n\n{self.endpoints}\n\n{self.safe_endpoints}\n"
@property
def routers(self):
return self.__routers_list
@property
def endpoints(self):
return self.__endpoints_list
@property
def safe_endpoints(self):
return self.__safe_endpoint_list
def create_endpoints(self, cluster: CategoryCluster, created_router):
for method_endpoint in list(cluster.ENDPOINTS.values()):
# Filter out the original function and apply decorators
applied_decorators_qualname = self.apply_decorators(method_endpoint)
# Register the endpoint with FastAPI router
create_endpoint = CreateEndpointFromCluster(
router=created_router, method_endpoint=method_endpoint
)
created_router = create_endpoint.router
if "MiddlewareModule" in applied_decorators_qualname:
self.__safe_endpoint_list.append(method_endpoint)
self.__endpoints_list.append(method_endpoint)
def create_router(self, cluster: CategoryCluster):
### Create Router Parameters create router for each cluster
created_router = CreateRouterFromCluster(
prefix=cluster.PREFIX,
tags=cluster.TAGS,
include_in_schema=cluster.INCLUDE_IN_SCHEMA,
)
self.__routers_list.append(created_router.router)
return created_router.router
def prepare_needs(self):
# @Pages iterate(ClusterToMethod)
for cluster_control in self.cluster_controller_group.imports:
cluster = cluster_control.category_cluster
created_router = self.create_router(cluster)
self.create_endpoints(cluster, created_router)
class PrepareEvents(DecoratorModule):
def __init__(self, cluster_controller_group: CategoryClusterController):
self.cluster_controller_group = cluster_controller_group
self.valid_redis_items: PrepareRedisItems = PrepareRedisItems()
self.prepare_needs()
self.prepare_page_info()
def prepare_page_info(self):
"""
[SAVE]REDIS => PAGE_MENU_INDEX:PAGE_URL= {...PageInfo}
"""
for cluster_control in self.cluster_controller_group.imports:
cluster = cluster_control.category_cluster
if retrieve_page_info := cluster.retrieve_page_info():
self.valid_redis_items.PAGE_INFO_VALUE.update(
{
f"{self.valid_redis_items.PAGE_INFO_KEY}:{cluster.name}": retrieve_page_info
}
)
def prepare_needs(self):
# @Pages iterate(ClusterToMethod)
for cluster_control in self.cluster_controller_group.imports:
cluster = cluster_control.category_cluster
### Create Redis Parameters
# [SAVE]REDIS => MENU_FIRST_LAYER = [ClusterToMethod, ...]
if cluster.is_client:
self.valid_redis_items.MENU_FIRST_LAYER_VALUE.add(cluster.name)
# [SAVE]REDIS => CLUSTER_INDEX = {ClusterToMethod: [MethodEvent, ...], "MethodEvent": "ClusterToMethod"}
self.valid_redis_items.CLUSTER_INDEX_VALUE.update(
cluster.get_redis_cluster_index_value()
)
# [SAVE]REDIS => CLUSTER_FUNCTION_CODES = {"ClusterToMethod"} : [FUNCTION_CODE, ...]}
self.valid_redis_items.CLUSTER_FUNCTION_CODES_VALUE.update(
{
f"{self.valid_redis_items.CLUSTER_FUNCTION_CODES_KEY}:{cluster.name}": tuple(
cluster.retrieve_all_function_codes()
)
}
)
for method_endpoint in list(cluster.ENDPOINTS.values()):
# [SAVE]REDIS => ENDPOINT2CLASS = {MethodEvent: Endpoint("/.../.../..."), ...}
self.valid_redis_items.ENDPOINT2CLASS_VALUE.update(
{
f"{cluster.name}:{method_endpoint.name}": f"{cluster.PREFIX}{method_endpoint.URL}"
}
)
self.valid_redis_items.ENDPOINT2CLASS_VALUE.update(
{
f"{cluster.PREFIX}{method_endpoint.URL}": f"{cluster.name}:{method_endpoint.name}"
}
)
# [SAVE]REDIS => METHOD_FUNCTION_CODES:MethodEvent:Endpoint = [FUNCTION_CODE, ...]
self.valid_redis_items.METHOD_FUNCTION_CODES_VALUE.update(
method_endpoint.retrieve_redis_value(cluster=cluster)
)
class SetItems2Redis:
std_out: str = ""
def __init__(self, prepare_events: PrepareEvents):
self.prepare_events = prepare_events
self.set_items()
def __str__(self):
return f"\nSetItems2Redis:\n\n{self.std_out}"
def set_items(self):
dict_prep = self.prepare_events.valid_redis_items.as_dict
for (
redis_values_to_delete,
redis_key_type,
) in RedisCategoryKeys.__annotations__.items():
if isinstance(redis_key_type, str):
continue
RedisActions.delete(list_keys=[f"{redis_values_to_delete}*"])
for (
redis_values_to_delete,
redis_key_type,
) in RedisCategoryPageInfoKeys.__annotations__.items():
if isinstance(redis_key_type, str):
continue
RedisActions.delete(list_keys=[f"{redis_values_to_delete}*"])
# Save MENU_FIRST_LAYER to Redis
redis_list = RedisList(redis_key=RedisCategoryKeys.MENU_FIRST_LAYER)
RedisActions.set_json(
list_keys=redis_list.to_list(),
value=dict_prep.get(RedisCategoryKeys.MENU_FIRST_LAYER),
)
self.std_out += f"{RedisCategoryKeys.MENU_FIRST_LAYER}: {dict_prep.get(RedisCategoryKeys.MENU_FIRST_LAYER)}\n"
# Save CLUSTER_INDEX to Redis
redis_list = RedisList(redis_key=RedisCategoryKeys.CLUSTER_INDEX)
RedisActions.set_json(
list_keys=redis_list.to_list(),
value=dict_prep.get(RedisCategoryKeys.CLUSTER_INDEX),
)
self.std_out += f"\n{RedisCategoryKeys.CLUSTER_INDEX}: {dict_prep.get(RedisCategoryKeys.CLUSTER_INDEX)}\n"
# Save CLUSTER_FUNCTION_CODES to Redis by iterating over the dict
for redis_key, redis_value in dict_prep.get(
RedisCategoryKeys.CLUSTER_FUNCTION_CODES
).items():
redis_list = RedisList(redis_key=redis_key)
RedisActions.set_json(
list_keys=redis_list.to_list(), value=list(redis_value)
)
self.std_out += f"\n{RedisCategoryKeys.CLUSTER_FUNCTION_CODES}: {dict_prep.get(RedisCategoryKeys.CLUSTER_FUNCTION_CODES)}\n"
# Save METHOD_FUNCTION_CODES to Redis by iterating over the dict
for redis_key, redis_value in dict_prep.get(
RedisCategoryKeys.METHOD_FUNCTION_CODES
).items():
redis_list = RedisList(redis_key=redis_key)
RedisActions.set_json(
list_keys=redis_list.to_list(), value=list(redis_value)
)
self.std_out += f"\n{RedisCategoryKeys.METHOD_FUNCTION_CODES}: {dict_prep.get(RedisCategoryKeys.METHOD_FUNCTION_CODES)}\n"
# Save ENDPOINT2CLASS to Redis by iterating over the dict
for redis_key, redis_value in dict_prep.get(
RedisCategoryKeys.ENDPOINT2CLASS
).items():
redis_list = RedisList(
redis_key=f"{RedisCategoryKeys.ENDPOINT2CLASS}:{redis_key}\n"
)
RedisActions.set_json(list_keys=redis_list.to_list(), value=redis_value)
self.std_out += f"\n{RedisCategoryKeys.ENDPOINT2CLASS}: {dict_prep.get(RedisCategoryKeys.ENDPOINT2CLASS)}\n"
RedisActions.set_json(
list_keys=[f"{RedisCategoryKeys.REBUILD}"],
value={
f"{RedisCategoryKeys.MENU_FIRST_LAYER}": True,
f"{RedisCategoryKeys.CLUSTER_INDEX}": True,
f"{RedisCategoryKeys.CLUSTER_FUNCTION_CODES}": True,
f"{RedisCategoryKeys.METHOD_FUNCTION_CODES}": True,
f"{RedisCategoryKeys.ENDPOINT2CLASS}": True,
},
)
for redis_key, redis_value in dict_prep.get(
PrepareRedisItems.PAGE_INFO_KEY
).items():
redis_list = RedisList(redis_key=redis_key)
RedisActions.set_json(list_keys=redis_list.to_list(), value=redis_value)