Skip to content

Tool Template (Reference)

The sample tool beratools/core/tool_template.py is part of the frame: it uses only frame code (tool_base, tool_args, spatial_common), so it ships with the frame and changes with its tool API. Copy it into your module's folder to start a new tool.

Minimal template recap

tool_template.py demonstrates:

  • a standard tool API pattern with framework args (processes, call_mode, log_level)
  • metadata-driven argument composition via compose_tool_kwargs
  • optional multiprocessing through execute_multiprocessing
  • separation of lightweight tool wrappers from heavy core logic

Sample tools.json

The Feature Buffer tool in tool_template.py is not in the GUI toolbox. Its entry below is a sample module file in the format that modules use, checked by beratools/gui/assets/module.schema.json. The frame loads only the module files in beratools/modules/ (see How to Add a New Tool), so tool_template.py no longer runs on its own.

{
  "module": "sample",
  "version": "0.1.0",
  "requires_frame": ">=0.7",
  "category": "Sample",
  "description": "A sample module with one tool.",
  "tools": [
    {
      "name": "Feature Buffer",
      "info": "Buffer the features of a vector layer.",
      "tool_type": "python",
      "tool_api": "tool_template",
      "cli_name": "buffer",
      "entry": "sample.tool_template:tool_template",
      "icon": "tool_template.gif",
      "tech_link": "developer/tool_template.md",
      "parameters": [
        {
          "variable": "in_feature",
          "label": "Input Feature Class",
          "description": "Input vector layer.",
          "type": "file",
          "subtype": "vector",
          "default": "",
          "output": false,
          "optional": false
        },
        {
          "variable": "buffer_dist",
          "label": "Buffer Distance (m)",
          "description": "Buffer distance around each input feature.",
          "type": "number",
          "subtype": "float",
          "default": 10,
          "unit": "m",
          "output": false,
          "optional": false
        },
        {
          "variable": "out_feature",
          "label": "Output Buffered Features",
          "description": "Output buffered features.",
          "type": "file",
          "subtype": "vector",
          "default": "",
          "output": true,
          "optional": false
        }
      ]
    }
  ]
}

What the module-level fields and the tool fields cli_name and entry do:

Field Meaning
module The module's id and Python package name.
version The module's own version.
requires_frame The frame versions the module works with, as a PEP 440 specifier.
cli_name The bt subcommand, e.g. bt buffer. Unique across all modules.
entry package.module:function that the frame imports to run the tool.

The schema rejects unknown or misspelled keys, so check a new file against it before adding it.

PC2Products (R) tools

beratools/modules/pc2products/rpy_find_cellsize.py is the smallest complete PC2Products tool. It shows:

  • calling configure_r_environment() before importing r_common
  • checking the inputs and installing the R packages the tool needs
  • sourcing r_scripts_collection.r and calling an R function from robjects.globalenv
  • metadata-driven argument composition via compose_tool_kwargs

Where to place content

  • Put contributor step-by-step instructions in developer/add_new_tool.md
  • Put architecture/runtime mechanics in technical/architecture/tool_runtime_model.md