Skip to content

Commit

Permalink
feat: add more codes for standalone testing
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn committed Oct 3, 2024
1 parent 7aff0cd commit 3c262a3
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
typedef struct ten_py_decorator_register_addon_t {
PyObject_HEAD
ten_string_t addon_name;
ten_string_t base_dir;
} ten_py_decorator_register_addon_t;

TEN_RUNTIME_PRIVATE_API bool
Expand Down
33 changes: 17 additions & 16 deletions core/src/ten_runtime/addon/addon.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ static void ten_addon_load_metadata(ten_addon_host_t *self, ten_env_t *ten_env,
}
}

static const char *ten_addon_type_to_string(TEN_ADDON_TYPE type) {
switch (type) {
case TEN_ADDON_TYPE_EXTENSION:
return TEN_STR_EXTENSION;
case TEN_ADDON_TYPE_EXTENSION_GROUP:
return TEN_STR_EXTENSION_GROUP;
case TEN_ADDON_TYPE_PROTOCOL:
return TEN_STR_PROTOCOL;
default:
TEN_ASSERT(0, "Should not happen.");
return NULL;
}
}

/**
* @brief The registration flow of an addon is as follows.
*
Expand All @@ -181,8 +195,6 @@ void ten_addon_register(ten_addon_store_t *addon_store,
TEN_ASSERT(!addon_host->ten_env, "Invalid argument.");
TEN_ASSERT(name, "Should not happen.");

TEN_LOGD("Register addon: %s", name);

addon_host->addon = addon;
addon_host->store = addon_store;
addon_host->ten_env = ten_env_create();
Expand All @@ -199,6 +211,9 @@ void ten_addon_register(ten_addon_store_t *addon_store,
ten_addon_find_and_set_base_dir(addon_host, base_dir);
}

TEN_LOGD("Register addon: %s as %s", name,
ten_addon_type_to_string(addon_host->type));

ten_addon_load_metadata(addon_host, addon_host->ten_env,
addon_host->addon->on_init);
}
Expand Down Expand Up @@ -571,20 +586,6 @@ ten_string_t *ten_addon_find_base_dir_from_app(const char *addon_type,
return NULL;
}

static const char *ten_addon_type_to_string(TEN_ADDON_TYPE type) {
switch (type) {
case TEN_ADDON_TYPE_EXTENSION:
return TEN_STR_EXTENSION;
case TEN_ADDON_TYPE_EXTENSION_GROUP:
return TEN_STR_EXTENSION_GROUP;
case TEN_ADDON_TYPE_PROTOCOL:
return TEN_STR_PROTOCOL;
default:
TEN_ASSERT(0, "Should not happen.");
return NULL;
}
}

void ten_addon_find_and_set_base_dir(ten_addon_host_t *self,
const char *start_path) {
TEN_ASSERT(start_path && self && ten_addon_host_check_integrity(self),
Expand Down
4 changes: 0 additions & 4 deletions core/src/ten_runtime/addon/extension/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,12 @@ ten_addon_host_t *ten_addon_register_extension(const char *name,

ten_addon_register(ten_extension_get_store(), addon_host, name, base_dir,
addon);
TEN_LOGI("Registered addon '%s' as extension",
ten_string_get_raw_str(&addon_host->name));

return addon_host;
}

ten_addon_t *ten_addon_unregister_extension(const char *name) {
TEN_ASSERT(name, "Should not happen.");

TEN_LOGV("Unregistered addon of extension '%s'.", name);

return ten_addon_unregister(ten_extension_get_store(), name);
}
2 changes: 0 additions & 2 deletions core/src/ten_runtime/addon/extension_group/extension_group.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ ten_addon_host_t *ten_addon_register_extension_group(const char *name,

ten_addon_register(ten_extension_group_get_store(), addon_host, name,
base_dir, addon);
TEN_LOGI("Registered addon '%s' as extension group",
ten_string_get_raw_str(&addon_host->name));

return addon_host;
}
Expand Down
2 changes: 0 additions & 2 deletions core/src/ten_runtime/addon/protocol/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ void ten_addon_register_protocol(const char *name, const char *base_dir,

ten_addon_register(ten_protocol_get_store(), addon_host, name, base_dir,
addon);
TEN_LOGI("Registered addon '%s' as protocol.",
ten_string_get_raw_str(&addon_host->name));
}

static bool ten_addon_protocol_match_protocol(ten_addon_host_t *self,
Expand Down
36 changes: 32 additions & 4 deletions core/src/ten_runtime/binding/python/interface/ten/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,43 @@
# Licensed under the Apache License, Version 2.0, with certain conditions.
# Refer to the "LICENSE" file in the root directory for more information.
#
import os
import sys
from libten_runtime_python import (
_register_addon_as_extension,
_register_addon_as_extension_group,
)


def register_addon_as_extension(name: str):
return _register_addon_as_extension(name)
def register_addon_as_extension(name: str, base_dir: str | None = None):
if base_dir is None:
try:
# Attempt to get the caller's file path using sys._getframe()
caller_frame = sys._getframe(1)
base_dir = os.path.dirname(caller_frame.f_code.co_filename)
except (AttributeError, ValueError):
# Fallback in case sys._getframe() is not available or fails.
base_dir = None

# If base_dir is not None, convert it to its directory name.
if base_dir is not None:
base_dir = os.path.dirname(base_dir)

def register_addon_as_extension_group(name: str):
return _register_addon_as_extension_group(name)
return _register_addon_as_extension(name, base_dir)


def register_addon_as_extension_group(name: str, base_dir: str | None = None):
if base_dir is None:
try:
# Attempt to get the caller's file path using sys._getframe()
caller_frame = sys._getframe(1)
base_dir = os.path.dirname(caller_frame.f_code.co_filename)
except (AttributeError, ValueError):
# Fallback in case sys._getframe() is not available or fails.
base_dir = None

# If base_dir is not None, convert it to its directory name.
if base_dir is not None:
base_dir = os.path.dirname(base_dir)

return _register_addon_as_extension_group(name, base_dir)
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,5 @@ class _Addon:
self, ten_env: _TenEnv, name: str, context
) -> None: ...

def _register_addon_as_extension(name: str): ...
def _register_addon_as_extension_group(name: str): ...
def _register_addon_as_extension(name: str, base_dir: str | None): ...
def _register_addon_as_extension_group(name: str, base_dir: str | None): ...
13 changes: 11 additions & 2 deletions core/src/ten_runtime/binding/python/native/addon/decorator.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static PyObject *ten_py_decorator_register_addon_create(
}

ten_string_init(&py_decorator->addon_name);
ten_string_init(&py_decorator->base_dir);

return (PyObject *)py_decorator;
}
Expand All @@ -36,12 +37,18 @@ static int ten_py_decorator_register_addon_init(PyObject *self, PyObject *args,
(ten_py_decorator_register_addon_t *)self;

const char *name = NULL;
if (!PyArg_ParseTuple(args, "s", &name)) {
const char *base_dir = NULL;

if (!PyArg_ParseTuple(args, "s|z", &name, &base_dir)) {
return -1;
}

ten_string_set_formatted(&py_decorator->addon_name, "%s", name);

if (base_dir) {
ten_string_set_formatted(&py_decorator->base_dir, "%s", base_dir);
}

return 0;
}

Expand All @@ -52,6 +59,7 @@ static void ten_py_decorator_register_addon_destroy(PyObject *self) {
TEN_ASSERT(py_decorator, "Invalid argument.");

ten_string_deinit(&py_decorator->addon_name);
ten_string_deinit(&py_decorator->base_dir);

Py_TYPE(self)->tp_free(self);
}
Expand Down Expand Up @@ -91,7 +99,8 @@ static PyObject *ten_py_decorator_register_addon_call(
// TEN world.
ten_py_addon_t *py_addon = (ten_py_addon_t *)py_addon_object;
ten_addon_host_t *c_addon_host = ten_addon_register(
ten_string_get_raw_str(&self->addon_name), NULL, &py_addon->c_addon);
ten_string_get_raw_str(&self->addon_name),
ten_string_get_raw_str(&self->base_dir), &py_addon->c_addon);
TEN_ASSERT(c_addon_host, "Should not happen.");

py_addon->c_addon_host = c_addon_host;
Expand Down
6 changes: 6 additions & 0 deletions packages/core_extensions/py_init_extension_cpp/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ class py_init_addon_t : public ten::addon_t {
// <app_root>/ten_packages/system/ten_runtime_python/lib
// <app_root>/ten_packages/system/ten_runtime_python/interface
// <app_root>
//
// The reason for adding `<app_root>` to `sys.path` is that when using
// `PyImport_Import` to load Python packages under `ten_packages/`, the module
// name used will be in the form of `ten_packages.extensions.xxx`. Therefore,
// `<app_root>` must be in `sys.path` to ensure that `ten_packages` can be
// located.
void complete_sys_path() {
ten_list_t paths;
ten_list_init(&paths);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ from .extension import DefaultExtension
from .log import logger


@register_addon_as_extension("default_extension_python")
@register_addon_as_extension("default_extension_python", __file__)
class DefaultExtensionAddon(Addon):

def on_create_instance(self, ten_env: TenEnv, name: str, context) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .log import logger


@register_addon_as_extension("default_extension_python")
@register_addon_as_extension("default_extension_python", __file__)
class DefaultExtensionAddon(Addon):

def on_create_instance(self, ten_env: TenEnv, name: str, context) -> None:
Expand Down

0 comments on commit 3c262a3

Please sign in to comment.