Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python): Support @template.function("name") definitions #7183

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions packages/cubejs-backend-native/python/cube/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ class AttrRef:
config: Configuration
attribute: str

def __init__(self, config: Configuration, attribue: str):
def __init__(self, config: Configuration, attribute: str):
self.config = config
self.attribute = attribue
self.attribute = attribute

def __call__(self, func):
if not callable(func):
Expand All @@ -163,21 +163,65 @@ class TemplateException(Exception):
pass

class TemplateContext:
def function(self, func):
functions: dict[str, Callable]

def __init__(self):
self.functions = {}

def add_function(self, name, func):
if not callable(func):
raise TemplateException("function registration must be used with functions, actual: '%s'" % type(func).__name__)

return context_func(func)

def filter(self, func):
self.functions[name] = func

def add_filter(self, name, func):
if not callable(func):
raise TemplateException("function registration must be used with functions, actual: '%s'" % type(func).__name__)

raise TemplateException("filter registration is not supported")

def function(self, func):
if isinstance(func, str):
return TemplateFunctionRef(self, func)

self.add_function(func.__name__, func)
return func

def filter(self, func):
if isinstance(func, str):
return TemplateFilterRef(self, func)

self.add_filter(func.__name__, func)
return func

def variable(self, func):
raise TemplateException("variable registration is not supported")

class TemplateFunctionRef:
context: TemplateContext
attribute: str

def __init__(self, context: TemplateContext, attribute: str):
self.context = context
self.attribute = attribute

def __call__(self, func):
self.context.add_function(self.attribute, func)
return func


class TemplateFilterRef:
context: TemplateContext
attribute: str

def __init__(self, context: TemplateContext, attribute: str):
self.context = context
self.attribute = attribute

def __call__(self, func):
self.context.add_filter(self.attribute, func)
return func

def context_func(func):
func.cube_context_func = True
return func
Expand Down
16 changes: 15 additions & 1 deletion packages/cubejs-backend-native/src/python/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,21 @@ fn python_load_model(mut cx: FunctionContext) -> JsResult<JsPromise> {
let model_module = PyModule::from_code(py, &model_content, &model_file_name, "")?;
let mut collected_functions = CLReprObject::new();

if model_module.hasattr("__execution_context_locals")? {
if model_module.hasattr("template")? {
let functions = model_module
.getattr("template")?
.getattr("functions")?
.downcast::<PyDict>()?;

for (local_key, local_value) in functions.iter() {
if local_value.is_instance_of::<PyFunction>() {
let fun: Py<PyFunction> = local_value.downcast::<PyFunction>()?.into();
collected_functions
.insert(local_key.to_string(), CLRepr::PyExternalFunction(fun));
}
}
// TODO remove all other ways of defining functions
} else if model_module.hasattr("__execution_context_locals")? {
let execution_context_locals = model_module
.getattr("__execution_context_locals")?
.downcast::<PyDict>()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
def arg_sum_integers(a, b):
return a + b

@template.function
def arg_bool(a):
@template.function("arg_bool")
def ab(a):
return a + 0

@template.function
Expand Down