clan_lib: Move load_in_all_api_functions to clan_lib
This commit is contained in:
@@ -110,17 +110,39 @@ class Webview:
|
||||
def thread_task(stop_event: threading.Event) -> None:
|
||||
ctx: AsyncContext = get_async_ctx()
|
||||
ctx.should_cancel = lambda: stop_event.is_set()
|
||||
# If the API call has set log_group in metadata,
|
||||
# create the log file under that group.
|
||||
log_group = header.get("logging", {}).get("group", None)
|
||||
if log_group is not None:
|
||||
log.warning(
|
||||
f"Using log group {log_group} for {method_name} with op_key {op_key}"
|
||||
)
|
||||
|
||||
log_file = log_manager.create_log_file(
|
||||
wrap_method, op_key=op_key, group=log_group
|
||||
).get_file_path()
|
||||
try:
|
||||
# If the API call has set log_group in metadata,
|
||||
# create the log file under that group.
|
||||
log_group: list[str] = header.get("logging", {}).get("group", None)
|
||||
if log_group is not None:
|
||||
if not isinstance(log_group, list):
|
||||
msg = f"Expected log_group to be a list, got {type(log_group)}"
|
||||
raise TypeError(msg) # noqa: TRY301
|
||||
log.warning(
|
||||
f"Using log group {log_group} for {method_name} with op_key {op_key}"
|
||||
)
|
||||
|
||||
log_file = log_manager.create_log_file(
|
||||
wrap_method, op_key=op_key, group_path=log_group
|
||||
).get_file_path()
|
||||
except Exception as e:
|
||||
log.exception(f"Error while handling request header of {method_name}")
|
||||
result = ErrorDataClass(
|
||||
op_key=op_key,
|
||||
status="error",
|
||||
errors=[
|
||||
ApiError(
|
||||
message="An internal error occured",
|
||||
description=str(e),
|
||||
location=["header_middleware", method_name],
|
||||
)
|
||||
],
|
||||
)
|
||||
serialized = json.dumps(
|
||||
dataclass_to_dict(result), indent=4, ensure_ascii=False
|
||||
)
|
||||
self.return_(op_key, FuncStatus.SUCCESS, serialized)
|
||||
|
||||
with log_file.open("ab") as log_f:
|
||||
# Redirect all cmd.run logs to this file.
|
||||
|
||||
@@ -1,39 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import importlib
|
||||
import json
|
||||
import pkgutil
|
||||
from types import ModuleType
|
||||
|
||||
|
||||
def import_all_modules_from_package(pkg: ModuleType) -> None:
|
||||
for _loader, module_name, _is_pkg in pkgutil.walk_packages(
|
||||
pkg.__path__, prefix=f"{pkg.__name__}."
|
||||
):
|
||||
base_name = module_name.split(".")[-1]
|
||||
|
||||
# Skip test modules
|
||||
if (
|
||||
base_name.startswith("test_")
|
||||
or base_name.endswith("_test")
|
||||
or base_name == "conftest"
|
||||
):
|
||||
continue
|
||||
|
||||
importlib.import_module(module_name)
|
||||
|
||||
|
||||
def load_in_all_api_functions() -> None:
|
||||
"""
|
||||
For the global API object, to have all functions available.
|
||||
We have to make sure python loads every wrapped function at least once.
|
||||
This is done by importing all modules from the clan_lib and clan_cli packages.
|
||||
"""
|
||||
import clan_cli
|
||||
import clan_lib
|
||||
|
||||
import_all_modules_from_package(clan_lib)
|
||||
import_all_modules_from_package(clan_cli)
|
||||
from clan_lib.api import load_in_all_api_functions
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import importlib
|
||||
import logging
|
||||
import pkgutil
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from functools import wraps
|
||||
from inspect import Parameter, Signature, signature
|
||||
from types import ModuleType
|
||||
from typing import (
|
||||
Annotated,
|
||||
Any,
|
||||
@@ -289,4 +292,35 @@ API.register(open_file)
|
||||
return None
|
||||
|
||||
|
||||
def import_all_modules_from_package(pkg: ModuleType) -> None:
|
||||
for _loader, module_name, _is_pkg in pkgutil.walk_packages(
|
||||
pkg.__path__, prefix=f"{pkg.__name__}."
|
||||
):
|
||||
base_name = module_name.split(".")[-1]
|
||||
|
||||
# Skip test modules
|
||||
if (
|
||||
base_name.startswith("test_")
|
||||
or base_name.endswith("_test")
|
||||
or base_name == "conftest"
|
||||
):
|
||||
continue
|
||||
|
||||
importlib.import_module(module_name)
|
||||
|
||||
|
||||
def load_in_all_api_functions() -> None:
|
||||
"""
|
||||
For the global API object, to have all functions available.
|
||||
We have to make sure python loads every wrapped function at least once.
|
||||
This is done by importing all modules from the clan_lib and clan_cli packages.
|
||||
"""
|
||||
import clan_cli
|
||||
|
||||
import clan_lib
|
||||
|
||||
import_all_modules_from_package(clan_lib)
|
||||
import_all_modules_from_package(clan_cli)
|
||||
|
||||
|
||||
API = MethodRegistry()
|
||||
|
||||
Reference in New Issue
Block a user