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.8.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.8-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cube_utils-0.1.8.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for cube_utils-0.1.8.tar.gz
Algorithm Hash digest
SHA256 75161fc6b02e841528930668746f4648fc1b68df18e553251cf913612ae84a11
MD5 524f481bc619f29b10e72062d4460019
BLAKE2b-256 7e54b936a3841e3340a6a1ec69dfcb174a6d2a8ac06ede7d2ad7004b36df55f0

See more details on using hashes here.

Provenance

The following attestation bundles were made for cube_utils-0.1.8.tar.gz:

Publisher: publish.yml on paco-valdez/cube-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: cube_utils-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for cube_utils-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 3e8cc1c28cf09e9b3091c35c670ffeb7939b07a13b506361d37066537bccd7c5
MD5 6265e8eacacf4455ab109b3e1734ea2c
BLAKE2b-256 4fc232c74bb5748351e7d98d8f717ab41e2b48d42b97d35f95b59af1144db23f

See more details on using hashes here.

Provenance

The following attestation bundles were made for cube_utils-0.1.8-py3-none-any.whl:

Publisher: publish.yml on paco-valdez/cube-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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