未提供项目描述
项目描述
ckanext-toolbelt
日常使用工具集合。
需求
CKAN版本 | 兼容? |
---|---|
2.9 | yes |
2.10 | yes |
master | yes |
内容
装饰器(《ckanext.toolbelt.decorators》)
收集器
创建一个收集函数并返回字典的装饰器。最初设计用于操作、认证函数、验证器和辅助程序。
:information_source: CKAN v2.10有tk.blanket
模块。以略不同的方式做相同的事情。
可以作为装饰器使用。当需要将辅助程序名称映射到辅助程序函数的字典时,调用Collector.get_collection
helper = Collector()
@helper
def func():
pass
###
# ITemplateHelpers
def get_helpers(self):
return helper.get_collection()
Collector.split
允许您在装饰器和返回集合的方法之间进行视觉分离
action, get_actions = Collector().split()
@action
def func():
pass
###
# IActions
def get_actions(self):
return get_actions()
如果您希望函数以插件名称为前缀,请将此前缀作为第一个参数提供给Collector
构造函数。如果特定项必须保持无前缀,可以在装饰项时指定要使用的名称
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()
缓存
函数的缓存。
Cache()
def func(v):
return v * v
默认情况下,缓存基于
- 定义函数的模块
- 函数名称
- 位置参数
- 命名参数
这意味着以下两个调用被单独缓存
func(10)
func(v=10)
缓存数据以JSON序列化结构存储在redis中。为了使用不同的序列化器,可以在创建Cache
实例时指定dumper
和loader
参数。任何接受单个值并返回str
或bytes
的函数都可以用作dumper
。任何接受str
或bytes
并返回未序列化值的函数都可以用作loader。
from pickle import dumps, loads
@Cache(dumper=dumps, loader=loads)
def func(v):
return v
如前所述,缓存键是通过模块、函数名和参数计算的。可以通过将函数作为key
参数传递给Cache
构造函数来更改它。期望的签名是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
缓存持续时间(以秒为单位)可以通过构造函数的duration
参数进行配置(可以是返回计算持续时间的可调用函数)。
cache = Cache(duration=3600)
@cache
def func(v):
return v + v
插件
toolbelt_fdt_sqlalchemy
Flask-SQLAlchemy的适配器。[链接](https://sqlalchemy.flask.org.cn/en/3.0.x/)。使FlaskDebugToolbar具有SQLAlchemy面板。您必须安装flask-sqlalchemy
额外组件才能使用此插件。
pip install 'ckanext-toolbelt[flask-sqlalchemy]'
toolbelt_cascade_organization_updates
当组织更新时,重新索引所有组织的数据集。需要后台工作者。
toolbelt_composite_groups
/ toolbelt_composite_organizations
为组织和组模式启用重复子字段(ckanext-scheming)
返回内容
CLI
安装ckanext-toolbelt后,它将为CLI注册ckan toolbelt
路由。您不需要将toolbelt
添加到已启用插件的列表中。
根据活动的插件,可能会向ckan toolbelt
路由添加额外的子路由。
此外,还有一个全局的ctb
命令,允许在不安装CKAN或没有CKAN配置文件的情况下使用此包。但以这种方式,一些命令(例如search-index
)不可用,因为它们使用CKAN核心。ctb
别名用于设置CKAN或扩展以及运行不依赖于CKAN实例的通用服务。
全局命令,通过ctb
和ckan toolbelt
路由可用
# 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
依赖于CKAN核心并通过ckan toolbelt
路由可用的命令
# 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 ...]
杂项
ckanext.toolbelt.utils.cache
DontCache
缓存
ckanext.toolbelt.utils.fs
StaticPath
围绕文件路径的no-op包装器,可以用作上下文管理器
with StaticPath("/tmp/x.txt") as path:
with open(path) as src:
...
# nothing is changed
RemovablePath
退出时删除文件的上下文管理器
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)
返回资源的文件路径。
如果资源存储在本地,则返回StaticPath。如果资源存储在远程位置,则将其下载到/tmp并返回RemovablePath。超过max_size
大小的远程资源不会下载,并返回空StaticPath。
示例
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
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。