Skip to main content

Python functions for Cube projects using cube.py config file.

Project description

Cube Utils

Cube Utils is a Python library for parsing and extracting information from Cube query payloads. It provides utilities to extract cubes, members, filters, and URL parameters from query data structures. Additionally, it includes the complete Cube package functionality for configuration management and template context handling, making it compatible with both open-source Cube deployments and Cube Cloud.

Installation

You can install Cube Utils using pip:

pip install cube-utils

If you are using Cube, just add cube-utils to your requirements.txt file. e.g.

cube-utils

Usage

Here is an example of how to use the extract_cubes and extract_members functions from the cube_utils.query_parser module:

from cube_utils.query_parser import extract_cubes, extract_members

# Example payload
payload = {
    "dimensions": ["test_a.city", "test_a.country", "test_a.state"],
    "measures": ["test_b.count"],
    "filters": [
        {"values": ["US"], "member": "test_a.country", "operator": "equals"}
    ],
    "segments": ["test_d.us_segment"],
    "timeDimensions": [
        {
            "dimension": "test_c.time",
            "dateRange": ["2021-01-01", "2021-12-31"],
            "granularity": "month",
        }
    ],
}

# Extract cubes
cubes = extract_cubes(payload)
print(cubes)  # Output: ['test_a', 'test_b', 'test_c', 'test_d']

# Extract members
members = extract_members(payload)
print(members)  # Output: ['test_a.city', 'test_a.country', 'test_a.state', 'test_b.count', 'test_a.country', 'test_d.us_segment', 'test_c.time']

# Extract members from specific query keys only
dimensions_and_measures = extract_members(payload, query_keys=["dimensions", "measures"])
print(dimensions_and_measures)  # Output: ['test_a.city', 'test_a.country', 'test_a.state', 'test_b.count']

Filter Members and Values

You can extract filter members along with their values using the extract_filters_members_with_values function:

from cube_utils.query_parser import extract_filters_members_with_values

# Example payload with complex filters
payload = {
    "filters": [
        {"values": ["US", "CA"], "member": "test_a.country", "operator": "equals"},
        {
            "or": [
                {"values": ["New York"], "member": "test_a.city", "operator": "equals"},
                {"member": "test_a.state", "operator": "set"}
            ]
        }
    ],
    "segments": ["test_b.premium_users"]
}

# Extract filter members with their values
filter_members = extract_filters_members_with_values(payload)
print(filter_members)  
# Output: [('test_a.country', ['CA', 'US']), ('test_a.city', ['New York']), ('test_a.state', None), ('test_b.premium_users', None)]

URL Parameter Extraction

You can extract query parameters from a URL using the extract_url_params function from the cube_utils.url_parser module:

from cube_utils.url_parser import extract_url_params

url = "https://example.com/?foo=bar&baz=qux"
params = extract_url_params(url)
print(params)  # Output: {'foo': 'bar', 'baz': 'qux'}

Cube Package API

Cube Utils now includes the complete Cube package functionality, providing configuration management and template context handling capabilities.

Configuration Management

The config object allows you to set configuration options for your Cube deployment. You can import it directly from cube_utils:

from cube_utils import config

Direct Property Assignment

Set configuration properties directly:

config.base_path = '/cube-api'
config.api_secret = 'your-secret-key'
config.telemetry = False

Function-based Configuration

Assign functions to configuration properties:

config.context_to_app_id = lambda ctx: ctx['securityContext']['tenant_id']

# Or using a regular function
def get_app_id(context):
    return context['securityContext']['tenant_id']

config.context_to_app_id = get_app_id

Decorator-style Configuration

Use the @config decorator to configure properties:

# Using function name as property name
@config
def context_to_app_id(ctx):
    return ctx['securityContext']['tenant_id']

# Using custom property name
@config('context_to_app_id')
def app_id(ctx):
    return ctx['securityContext']['tenant_id']

Available Configuration Options

The configuration object supports numerous options including:

  • API Settings: base_path, api_secret, telemetry
  • Database: db_type, driver_factory
  • Authentication: check_auth, check_sql_auth, context_to_app_id
  • Caching: sql_cache, compiler_cache_size
  • WebSockets: web_sockets, web_sockets_base_path
  • Scheduled Refresh: scheduled_refresh_timer, scheduled_refresh_concurrency
  • Advanced: query_rewrite, pre_aggregations_schema, orchestrator_options

Template Context Management

The TemplateContext class provides template variable, function, and filter management for Jinja templates:

from cube_utils import TemplateContext

template = TemplateContext()

Variable Registration

Register variables that can be used in Jinja templates:

template.add_variable('my_var', 123)
template.add_variable('api_version', '2.0')
template.add_variable('feature_flags', {'new_ui': True})

Function Registration

Register functions that can be called from templates:

# Method 1: Direct registration
def get_user_data():
    return {'name': 'John', 'role': 'admin'}

template.add_function('get_user_data', get_user_data)

# Method 2: Using decorator
@template.function
def get_current_time():
    from datetime import datetime
    return datetime.now().isoformat()

# Method 3: Using decorator with custom name
@template.function('get_status')
def check_system_status():
    return 'active'

Filter Registration

Register custom Jinja filters:

# Method 1: Direct registration
def wrap_in_quotes(value):
    return f'"{value}"'

template.add_filter('quote', wrap_in_quotes)

# Method 2: Using decorator
@template.filter
def uppercase(value):
    return str(value).upper()

# Method 3: Using decorator with custom name
@template.filter('currency')
def format_currency(value):
    return f'${value:.2f}'

Context Functions

Mark functions as context functions using the context_func decorator:

from cube_utils import context_func

@context_func
def my_context_function():
    return 'This is a context function'

Safe Strings

Create safe strings for template rendering:

from cube_utils import SafeString

safe_html = SafeString('<b>Bold text</b>')
print(safe_html.is_safe)  # True

Backward Compatibility

The settings object is provided as an alias to config for backward compatibility:

from cube_utils import settings

settings.base_path = '/api'  # Same as config.base_path = '/api'

Additional Utilities

File Repository

Access file repository functionality for reading configuration files:

from cube_utils import file_repository

# Read all supported files from a directory
files = file_repository('/path/to/cube/schema')
# Returns list of dictionaries with 'fileName' and 'content' keys
# Supports: .js, .yml, .yaml, .jinja, .py files

Exception Handling

Handle configuration and template-related errors:

from cube_utils import ConfigurationException, TemplateException

try:
    @config('unknown_property')
    def invalid_config():
        pass
except ConfigurationException as e:
    print(f"Configuration error: {e}")

try:
    template.add_function('test', 'not a function')
except TemplateException as e:
    print(f"Template error: {e}")

API Reference

Query Parsing Functions

Function Description Module
extract_cubes Extract unique cube names from query payload cube_utils.query_parser
extract_members Extract all members from query payload cube_utils.query_parser
extract_filters_members Extract members from filters and segments only cube_utils.query_parser
extract_filters_members_with_values Extract filter members with their values cube_utils.query_parser
extract_members_from_expression Parse SQL expressions for member references cube_utils.query_parser
extract_url_params Extract URL query parameters cube_utils.url_parser

Cube Package Classes

Class/Object Description Import
config Global configuration object from cube_utils import config
TemplateContext Template management class from cube_utils import TemplateContext
context_func Context function decorator from cube_utils import context_func
SafeString Safe string class for templates from cube_utils import SafeString
settings Alias for config (backward compatibility) from cube_utils import settings

Running Tests

To run the tests, use the following command:

python -m unittest discover tests

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cube_utils-0.1.6.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cube_utils-0.1.6-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file cube_utils-0.1.6.tar.gz.

File metadata

  • Download URL: cube_utils-0.1.6.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.25.5 CPython/3.13.5 Linux/6.11.0-1018-azure

File hashes

Hashes for cube_utils-0.1.6.tar.gz
Algorithm Hash digest
SHA256 34686b88d155ab7e349e974a5ae2d6001eb8a37341ee4f1d1319b48f3d690038
MD5 5486db9f9e9d7e3dc134a3cd36060254
BLAKE2b-256 ac3ca05a466e58e626306af7ef7b0576d5a382e14e418e367ba185a185be828f

See more details on using hashes here.

File details

Details for the file cube_utils-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: cube_utils-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.25.5 CPython/3.13.5 Linux/6.11.0-1018-azure

File hashes

Hashes for cube_utils-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0f3ab168f3be6ce63be7d6b8fdeca143230c56a687f35e70180100f1cf834061
MD5 e6d8695d3f5e683fb1690e52b12b38a8
BLAKE2b-256 bf53e16a98041023a113b050b20b66edd225272882ce25a86f2734e68d1c48a7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page