Skip to main content

kahmi-dsl

This is a Python-based configuration language for the Kahmi build system that is heavily inspired by Groovy and Gradle.

Example:

buildscript {
  dependencies = ["kahmi-git", "kahmi-cxx"]
}

let cxx = load("kahmi-cxx")
let git = load("kahmi-git")

name = "myproject"
version = git.version()

cxx.build("main") {
  srcs = glob("src/*.cpp")
  type = "executable"
}

Syntax & Semantics

The Kahmi DSL is not a strict superset of the Python language, instead it wraps Python code and swaps between DSL parsing and Python code parsing.

Kahmi DSL Syntax

  1. Define a local variable with the let Keyword

    Local variables are defined using the let keyword. The variable can then be addressed in Python expressions or as call block targets (see below). The right hand side of the assignment must be a Python expression.

    let my_variable = 42
    
  2. Set a property on the current context object

    The same syntax but without the let keyword assigns the value to a member of the current context object instead of to a local variable.

    nmae = "my-project"
    version = git.version()
    
  3. Configure blocks

    A configure block basically generates a Python function, called the "closure", and passes it to the specified target. The closure that is defined after the target is passed to the target by either calling it's configure() method or calling the target directly.

    print("Hello, World!")  # Call without body
    
    buildscript {
      dependencies = ["kahmi-python"]
    }
    
    cxx.build("main") {
      srcs = glob("src/*.cpp")
    }
    

    At the root level, every Kahmi script is basically a closure that is executed against the main context object.

Python Syntax Extensions

When parsing a Python expression, Kahmi injects support for multi-line lambdas and macros.

  1. Multi-line lambdas

    The Kahmi DSL parser injects the ability to define multi-line lambdas in any Python expression. The lambda syntax is inspired by Javascript/Typescript and uses => as the lambda arrow operation to connect the argument definition with the lambda body.

    A lambda with braces requires a return statement, otherwhise the return value of the lambda will be None. Single-statement lambdas are not currently supported with this syntax (although you can always fall back to standard syntax lambda: <expr>).

    let myFunc = () => {
      import random
      return random.random()
    }
    
    print(myFunc())
    

    Nesting lambdas is supported and has the expected semantics except if used in comprehensions (as they introduce a new scope that can not be captured by the function definition that is a multi-line lambda is transpiled to).

  2. Macros

    Macros are plugins that can be enabled in the Kahmi DSL parser to implement custom parsing logic following a macro identifier. The Kahmi DSL parser comes with a YAML plugin out of the box:

    buildscript {
      dependencies = !yaml {
        - kahmi-git
        - kahmi-python
      }
    }
    
  3. Dynamic name lookup

    Names are resolved slightly different in Kahmi Python expressions. The local scope will always be resolved first. Subsequently, the current context object's members are checked, then the parent closure's local variables and context object, etc. Then finally, the global variables and builtins.

    dependencies = ["kahmi-python"]
    print(dependencies)
    print('dependencies' in locals())
    print('dependencies' in vars(self))
    

    Explanation: The property assignment sets the dependencies attribute on the current context object. Looking up the variable will first search it in the locals, but not find it there and subsequently find it in the context object (also referrable to as self).

Built-ins

Kahmi only provides two additional built-in functions on top of what is provided by Python, and they are only necessary for the execution of Kahmi's generated Python code.

Name Description
self The root context object for the script.
__lookup__(name, locals_, ctx) Helper function to resolve the targets of call blocks.

Under the hood

Kahmi comes with a simple cli that allows you to run any Kahmi script, but given the limited ability to override the root context object it is expected that it does not serve much use outside of debugging and development.

$ python -m kahmi.dsl examples/hello.kmi

Using the -E option, you can retrieve the Python code that a Kahmi file is transpiled to. This is especially useful to understand how Kahmi constructs are converted into Python. Below are some examples:

let msg = (name) => {
  return 'Hello, ' + name
}('World')

print(msg)
def lambda_stdin_1_10(name):
    return 'Hello, ' + name


msg = lambda_stdin_1_10('World')
__runtime__['print'](msg)

buildscript {
  dependencies = ["kahmi-python"]
}
@__runtime__.closure()
def __configure_buildscript(self):
    __runtime__.set_object_property(self, 'dependencies', ['kahmi-python'])


__configure_buildscript_self_target = __runtime__['buildscript']
with __runtime__.pushing(__runtime__['locals']()):
    __runtime__.configure_object(__configure_buildscript_self_target,
        __configure_buildscript)

Copyright © 2021 Niklas Rosenstein

Metadata

Release files for kahmi-dsl 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kahmi-dsl 0.1.2
File Size Uploaded
kahmi-dsl-0.1.2.tar.gz 16.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kahmi-dsl 0.1.2
File Interpreter ABI Platform
kahmi_dsl-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 33.7 kB

Release files / kahmi-dsl-0.1.2.tar.gz

Download URL kahmi-dsl-0.1.2.tar.gz
Size 16.6 kB
Tags Source
SHA-256 checksum
How to use checksums
41db5097df86a204b6cc740e2652bf522b915263889f3d72f45c27348a5b334b
BLAKE2b-256 checksum
How to use checksums
c7460aaa9afd85ed660ec1f54479cea658505aebfa734cc98796923792e932fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.3

Release files / kahmi_dsl-0.1.2-py3-none-any.whl

Download URL kahmi_dsl-0.1.2-py3-none-any.whl
Size 17.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3752270a24dbbe059b6215fd20dde4d566328e47de52a5bbfa2f6663bc620746
BLAKE2b-256 checksum
How to use checksums
35be28c3fc4949c96d875fb7e10a4c7b7a5f37b374393babe220d481efcca8f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page