Skip to main content

No project description provided

Project description

ckanext-toolbelt

Collection of different tools for daily use.

Requirements

CKAN version Compatible?
2.9 yes
2.10 yes
master yes

Content

Decorators (ckanext.toolbelt.decorators)

Collector

Creates a decorator that collects functions and returns them in a dictionary. Originally designed for actions, auth functions, validators and helpers.

:information_source: CKAN v2.10 has tk.blanket module. It does the same things in a bit different manner.

Can be used as decorator. Call Collector.get_collection when you need dictionary with names of helpers mapped to helper functions

helper = Collector()

@helper
def func():
	pass

###
# ITemplateHelpers
def get_helpers(self):
	return helper.get_collection()

Collector.split allows you to visually separate decorator from the method, that returns collection

action, get_actions = Collector().split()

@action
def func():
	pass

###
# IActions
def get_actions(self):
	return get_actions()

If you want your functions prefixed by the plugin name, provide this prefix as a first argument to the Collector's constructor. If particular items must remain unprefixed, you can specify what name to use, when decorating an item

validator, get_validators = Collector("toolbelt").split()

@validator
def func():
	"""I am toolbelt_func
	"""
	pass

@validator("custom_func")
def func():
	"""I am custom_func
	"""
	pass

###
# IValidators
def get_validators(self):
	return get_validators()

Back to content

Cache

Cache for functions.

Cache()
def func(v):
    return v * v

By default, cache is based on:

  • module, where function is defined
  • name of the function
  • positional arguments
  • named arguments

That means that following two invocations cached separately:

func(10)
func(v=10)

Cached data stored in redis as a JSON serialized structure. In order to use different serializers, you can specify dumper and loader parameters when creating Cache instance. Any function that accepts single value and returns str or bytes can be used as a dumper. Any function that accepts str or bytes and returns unserialized value can be used as loader.

from pickle import dumps, loads

@Cache(dumper=dumps, loader=loads)
def func(v):
    return v

As mentioned before, cache key computed using module, name of the function and parameters. It can be changed by passing a function as key argument to the Cache constructor. Expected signature is key_strategy(func, *args, **kwargs).

# this function will be called only once, because cache key is based on its name.
# And name will never change. Unless you change it
@Cache(key=lambda f, *a, **k: f.__name__)
def func(v):
    return v

Cache duration(in seconds) can be configured via duration parameter of the constructor(which can be a callable that returns comuted duration).

cache = Cache(duration=3600)

@cache
def func(v):
    return v + v

Back to content


Plugins

toolbelt_fdt_sqlalchemy

Adapter for Flask-SQLAlchemy. Enables SQLAlchemy panel on FlaskDebugToolbar. You have to install flask-sqlalchemy extra to use this plugin:

pip install 'ckanext-toolbelt[flask-sqlalchemy]'

toolbelt_cascade_organization_updates

Reindex all organization's datasets when organization updated. Requires background worker.

toolbelt_composite_groups / toolbelt_composite_organizations

Enable repeating subfields(ckanext-scheming) for organization and group schemas

Back to content

CLI

As soon as you've installed ckanext-toolbelt, it will register ckan toolbelt route for CLI. You don't have to add toolbelt to the list of enabled plugins.

Depending on the active plugins, extra subroutes may be added to the ckan toolbelt route.

In addition, there is global ctb command that allows to use this package without CKAN installed or without CKAN config file. But in this way some of commands (search-index for example) are not available, because they use CKAN core. ctb alias exists for setting up the CKAN or extensions and running generic services, that do not rely on CKAN instance.

Global commands, available via ctb and ckan toolbelt routes:

# create a generic configuration. Supported types:
# * deps-makefile  CKAN dependency manager
# * pre-commit     Pre-commit
# * pyproject      pyproject.toml
# * gulp-sass      gulpfile.js with SASS configuration
make config <type>

# create a configuration for GitHub Action. Supported types:
# * pypi-publish    Publish package to PyPI when vX.Y.Z tag added.
# * release-please  Create a PR that compiles changelog and publishes GitHub release.
# * test            Test workflow.
make gh-action <type>

# Generate parts of README.md
# Supported types:
# * config  Print declared config options for the given plugins.
make readme <type>

# Start mail server that will catch outcomming mails.
dev mail-server

Commands that depends on CKAN core and available only via ckan toolbelt route:

# Drop packages that are only in search index but not in DB.
search-index clear-missing

# Clean the DB, optionally keeping data in the given tables.
db clean --yes [-k user] [-k group] [-k ...]

Back to content


Misc

ckanext.toolbelt.utils.cache

DontCache

Cache

ckanext.toolbelt.utils.fs

StaticPath

No-op wrapper around filepath that can be used as a context manager:

with StaticPath("/tmp/x.txt") as path:
    with open(path) as src:
        ...
# nothing is changed

RemovablePath

Context manager that removes file on exit:

with RemovablePath("/tmp/x.txt") as path:
    with open(path) as src:
        ...
# /tmp/x.txt is removed

path_to_resource(res_dict, max_size=0)

Returns a filepath for a resource.

If resource is stored locally, return StaticPath. If resource stored remotely, download it to /tmp and return RemovablePath. Remote resources with size exceeding max_size are not downloaded and empty StaticPath returned.

Example:

with path_to_resource(resource) as path:
    with open(path) as src:
        print(src.read())

ckanext.toolbelt.utils.scheming

get_validation_schema

ckanext.toolbelt.utils.structures

Node

ckanext.toolbelt.utils.hierarchy

Node

Strategy

ParentReference

package_hierarchy

Back to content

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

ckanext_toolbelt-0.6.14.tar.gz (413.2 kB view details)

Uploaded Source

Built Distribution

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

ckanext_toolbelt-0.6.14-py3-none-any.whl (485.5 kB view details)

Uploaded Python 3

File details

Details for the file ckanext_toolbelt-0.6.14.tar.gz.

File metadata

  • Download URL: ckanext_toolbelt-0.6.14.tar.gz
  • Upload date:
  • Size: 413.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ckanext_toolbelt-0.6.14.tar.gz
Algorithm Hash digest
SHA256 672e17885e91e7cc8236cbabd35add7a7601b323e5330c5af9f6fc2bf97d475e
MD5 3012c79d15be87885e446d99e990d10c
BLAKE2b-256 8506857e50676e1741ca26276738b6cbe7b1660c5ba528a64a9f30182fe9b270

See more details on using hashes here.

File details

Details for the file ckanext_toolbelt-0.6.14-py3-none-any.whl.

File metadata

File hashes

Hashes for ckanext_toolbelt-0.6.14-py3-none-any.whl
Algorithm Hash digest
SHA256 8f40b95ac304061d4b4a1af868a7d586281d27eb44c9ed0dbb49338bcb1fbe24
MD5 b14e9e094ff6714670cbf0b5c0206fc4
BLAKE2b-256 e3f04dbad133cca0665f5dfd5b13437aeeb7d8ee6fe6bcdc7d37105b36d0a0e2

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