Skip to content

Reading Device Configurations

A configuration records how to set up the hardware. Use a field for any reason. A field the build never reads is still worth storing: the instance keeps the answer, and another program can read it from the BOM.

A value can be read in three places. The device's own build instructions see it as a dict. The system build that placed the device reads it from the BOM. A program outside Harnice reads that same BOM file.

From the device's build instructions

Read configuration["field_id"] for any reason. One example is a signals list that follows the answers. Harnice builds the placed device before the system script runs, and injects configuration into that device's fileio.path("build instructions") (build.py). The key is the field id from fileio.path("configuration setup").

input_type = configuration["input_type"]
phantom = configuration["phantom_power"]
presets = configuration["presets"]   # dict, list, or scalar, from a json, yaml, or toml field
labels = configuration["labels"]     # list of strings

On a standalone device build, configuration is the setup defaults. On a system rebuild of a placed device, it is that instance's answers, with setup defaults filling any missing keys.

Harnice parses json, yaml, and toml text before the script runs.

Field type Value of configuration["field_id"]
text, textarea, select, radio, channel type, IP address, MAC address, URL, matrix string
number, slider, scale, rating number
boolean True or False
a field left unset None
list, multiselect list of strings
json, yaml, toml the loaded data: a dict, a list, a string, a number, a boolean, or None. Empty text is None.

Field types, validation, show_if, and a YAML example are on Device Configurations.

From the system build instructions

Read a placed device's answers for any reason. One example is choosing which channels to map. By the time the system script runs, Harnice has written the BOM and rebuilt each placed device. Harnice does not inject configuration into the system script. Read the placed device from the BOM:

import json

text = ""
for row in fileio.read_tsv("bom"):
    if row["device_refdes"] == "A1":
        text = (row.get("configuration") or "").strip()
        break

answers = json.loads(text) if text else {}

if answers.get("pairing") == "crossed":
    channel_map.map(("A1", "out1"), ("B1", "in1"))
else:
    channel_map.map(("A1", "out1"), ("B1", "in2"))

device_refdes is the instance name on the block diagram. answers is the saved object for that instance. An empty cell is no saved answers, and answers is {}.

answers[field_id] matches the table in "Reading one variable". A json, yaml, or toml field is still the document text on this path. Inside the device build, that same field is already parsed.

The channel_map.map calls above are one example. Use answers for whatever the system script needs. The channel id in that example is a channel on the signals list that device build wrote.

From the instances file

The system build also copies each placed device's answers onto its row in the instances file, in the device_configuration column. Read one answer with instances.configuration_value:

for instance in instances.read():
    ip_address = instances.configuration_value(instance, "ip_address")

configuration_value(instance, field_id) returns the saved answer, or "" when the instance has no saved answers or that field is unset. instance["device_configuration"] is the whole set of answers as a dict keyed by field id, or "" when there are none. The values match the BOM's configuration cell: a json, yaml, or toml field is still the document text.

How the instances file stores values

fileio.path("instances") is instances.json in the revision. It is one JSON object keyed by instance_name. Each instance is an object keyed by column name:

{
  "MIC-1": {
    "item_type": "device",
    "bom_line_number": "3",
    "project_editable_lib_modified": "True",
    "device_configuration": {
      "ip_address": "192.168.1.2",
      "U": 42
    },
    "lib_tools": [
      "M22520/2-01 crimp tool"
    ]
  },
  "MIC-1.node": {}
}
  • A plain column value is stored as text, the same as a TSV cell: 3 is "3" and True is "True".
  • A dict or list column is stored as a JSON object or array. Values inside it keep their type, so 42 stays a number. device_configuration, attributes_json, appearance, csys_children, lib_tools, and note_affected_instances are stored this way.
  • Every column, and every key or item inside a dict or list, is on its own line, so a diff shows only the values that changed.
  • A blank column is left out of the file. MIC-1.node above has no columns set.
  • instance_name is not repeated inside the object. It is the key.

instances.read() returns every column for every instance, in the standard column order. A column the file left out comes back as "".

From another program

fileio.path("bom") is lists/bom.tsv under the system revision. The file is tab-separated. One row is one placed device or harness. Find the device by device_refdes, the instance name on the block diagram.

Column Description
device_refdes Instance name on the block diagram. This is the row key.
one column per field id One column per configuration field, named after the field id, such as ip_address. The same answers as the configuration cell, one per column, for reading in a spreadsheet. A device without that field leaves it blank.
configuration The last column. One compact JSON object of that device's answers. An empty cell means the instance has no saved answers. Harness rows leave this cell empty.

The other BOM columns (MFG, MPN, lib_repo, lib_subpath, rev, harness, system) identify the part. A configuration field id is a key inside the configuration cell.

The field columns come after the standard columns and before configuration, in the order each field first appears on the block diagram. A column exists when at least one placed device has saved an answer for that field. A field id that matches a standard column name (such as rev) does not get its own column; read it from configuration. Each cell is text:

  • A string or number is written as is.
  • true and false are TRUE and FALSE.
  • An unset answer is blank.
  • A list, or the data of a json, yaml, or toml field, is compact JSON text.

A TSV cell cannot tell "1" from 1 or a blank from an unset answer. When the type matters, parse the configuration cell instead.

After a system build, the instances file copies that same JSON string into device_configuration. Parse that cell the same way.

Reading one variable

import csv
import json

with open(bom_path, newline="", encoding="utf-8") as f:
    rows = csv.DictReader(f, delimiter="\t")
    row = next(r for r in rows if r["device_refdes"] == refdes)

value = json.loads(row["configuration"])[variable]

value is the stored answer. json.loads turns JSON true, false, and null into True, False, and None.

Field type Value after json.loads
text, textarea, select, radio, channel type, IP address, MAC address, URL, matrix string
number, slider, scale, rating number
boolean True or False
a field left unset None
list, multiselect list of strings
json, yaml, toml the document text, still a string

A json, yaml, or toml field needs a second parse to get the data inside the document. document is the string from the step above.

Field type Second parse
json json.loads(document)
yaml yaml.safe_load(document)
toml tomllib.loads(document) (Python 3.11+)

An empty configuration cell has no object to parse. Skip that row, or treat it as no saved answers.