File: dtmi_conventions_sample.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (34 lines) | stat: -rw-r--r-- 1,608 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from azure.iot.modelsrepository import dtmi_conventions


def sample_is_valid_dtmi():
    """Check if a DTMI is valid or not using the .is_valid_dtmi() function"""
    # Returns True - this is a valid DTMI
    dtmi_conventions.is_valid_dtmi("dtmi:com:example:Thermostat;1")

    # Returns False - this is NOT a valid DTMI
    dtmi_conventions.is_valid_dtmi("dtmi:com:example:Thermostat")


def sample_get_model_uri():
    """Get a URI for a model in a Models Repository using the .get_model_uri() function"""
    dtmi = "dtmi:com:example:Thermostat;1"

    # Local repository example
    repo_uri = "file:///path/to/repository"
    print(dtmi_conventions.get_model_uri(dtmi, repo_uri))
    # Prints: "file:///path/to/repository/dtmi/com/example/thermostat-1.json"
    print(dtmi_conventions.get_model_uri(dtmi, repo_uri, expanded=True))
    # Prints: "file:///path/to/repository/dtmi/com/example/thermostat-1.expanded.json"

    # Remote repository example
    repo_uri = "https://contoso.com/models/"
    print(dtmi_conventions.get_model_uri(dtmi, repo_uri))
    # Prints: "https://contoso/com/models/dtmi/com/example/thermostat-1.json"
    print(dtmi_conventions.get_model_uri(dtmi, repo_uri, expanded=True))
    # Prints: "https://contoso/com/models/dtmi/com/example/thermostat-1.expanded.json"