Skip to main content

Converter between WoT TD and SDF (including protocol bindings). Work in Progress.

Project description

Build Status PyPI version codecov Code style: black

SDF-WoT-Converter (Python Edition)

This repository provides a Python-based converter from SDF to WoT TD including Thing Models.

The converter is both usable as a library and a command line tool. It is based on my Rust implementation but is (when it comes to the conversion from SDF to Thing Models) already more mature as development in Python turned out to be much faster. The final version of this converter, however, will be reimplemented in Rust once it is finished to also support more constrained environments.

The CI pipeline is set up to automatically convert all (valid) models from the oneDM playground to WoT Thing Models and upload to the results to this repository.

Installation

You can install the converter using pip:

pip install sdf-wot-converter

Afterwards, it can be used both as a command line tool and a library.

Using the command line tool

After installing the libary you should be able to call the converter in your terminal using sdf-wot-converter. You can display available parameters by typing sdf-wot-converter --help. So far, you can convert from SDF to WoT Thing Models and vice versa. Thing Descriptions are supposed to be added soon.

Example

# Convert an SDF model to a WoT Thing Model
sdf-wot-converter --from-sdf examples/sdf/example.sdf.json --to-tm converted-example.tm.json

# Convert a WoT Thing Model to an SDF model
sdf-wot-converter --from-tm examples/wot/example.tm.json --to-sdf converted-example.sdf.json

Using the library

With the converter installed, you can use also use it as a library in your own projects. Below you can see examples for how to convert an SDF model to a WoT Thing Model and back again. As you can see, nested definitions from sdfObjects or sdfThings a prefixed with the respective object or thing names. Moreover, an sdf:jsonPointer field is added to each affordance to enable roundtripping. More detailed mapping tables will be added to this readme soon.

from sdf_wot_converter import (
    convert_sdf_to_wot_tm,
    convert_wot_tm_to_sdf,
)

sdf_model = {
    "info": {
        "title": "Example file for OneDM Semantic Definition Format",
        "version": "2019-04-24",
        "copyright": "Copyright 2019 Example Corp. All rights reserved.",
        "license": "https://example.com/license",
    },
    "namespace": {"cap": "https://example.com/capability/cap"},
    "defaultNamespace": "cap",
    "sdfObject": {
        "Switch": {
            "sdfProperty": {
                "value": {
                    "description": "The state of the switch; false for off and true for on.",
                    "type": "boolean",
                }
            },
            "sdfAction": {
                "on": {
                    "description": "Turn the switch on; equivalent to setting value to true."
                },
                "off": {
                    "description": "Turn the switch off; equivalent to setting value to false."
                },
                "toggle": {
                    "description": "Toggle the switch; equivalent to setting value to its complement."
                },
            },
        }
    },
}

thing_model = convert_sdf_to_wot_tm(sdf_model)

expected_thing_model = {
    "@context": [
        "http://www.w3.org/ns/td",
        {"cap": "https://example.com/capability/cap", "sdf": "https://example.com/sdf"},
    ],
    "@type": "tm:ThingModel",
    "title": "Example file for OneDM Semantic Definition Format",
    "description": "Copyright 2019 Example Corp. All rights reserved.",
    "links": [{"href": "https://example.com/license", "rel": "license"}],
    "version": {"model": "2019-04-24"},
    "sdf:defaultNamespace": "cap",
    "actions": {
        "Switch_on": {
            "sdf:jsonPointer": "#/sdfObject/Switch/sdfAction/on",
            "description": "Turn the switch on; equivalent to setting value to true.",
        },
        "Switch_off": {
            "sdf:jsonPointer": "#/sdfObject/Switch/sdfAction/off",
            "description": "Turn the switch off; equivalent to setting value to false.",
        },
        "Switch_toggle": {
            "sdf:jsonPointer": "#/sdfObject/Switch/sdfAction/toggle",
            "description": "Toggle the switch; equivalent to setting value to its complement.",
        },
    },
    "properties": {
        "Switch_value": {
            "sdf:jsonPointer": "#/sdfObject/Switch/sdfProperty/value",
            "description": "The state of the switch; false for off and true for on.",
            "type": "boolean",
        }
    },
}

assert thing_model == expected_thing_model

sdf_roundtrip_model = convert_wot_tm_to_sdf(thing_model)

assert sdf_model == sdf_roundtrip_model

License

This project is licensed under the MIT license.

SPDX-License-Identifier: MIT

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

sdf-wot-converter-0.4.0.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

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

sdf_wot_converter-0.4.0-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

File details

Details for the file sdf-wot-converter-0.4.0.tar.gz.

File metadata

  • Download URL: sdf-wot-converter-0.4.0.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

File hashes

Hashes for sdf-wot-converter-0.4.0.tar.gz
Algorithm Hash digest
SHA256 5484199ce31a48aa977a701e08e40e0164afcd3e142dd10ee26816b963aea33b
MD5 54636fb97bc9daf24a1f183c0f606c9b
BLAKE2b-256 d0bdd883b4fa648740da07f656915f68f487f16dc62dce9d41cb4973e6387261

See more details on using hashes here.

File details

Details for the file sdf_wot_converter-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: sdf_wot_converter-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 23.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

File hashes

Hashes for sdf_wot_converter-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b349b6269c4c0007e33193020f3f378fcede4db70265fe432ddfd340075b838
MD5 ef40ddf0b157b33f7fb6b8f1ed5fdf8e
BLAKE2b-256 a60e14ae1f71428bfbe00762afa833ed4a78764b1b72a637d32b4869e2efa750

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