diff --git a/app_state.py b/app_state.py index ab66fdf..ecb93c3 100644 --- a/app_state.py +++ b/app_state.py @@ -8,7 +8,7 @@ so later rebinding would only ever be seen by the module that did it. import configparser import os -VERSION = "0.0.0.089" +VERSION = "0.0.0.092" PROPERTIES_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "editor.properties") DEFAULT_GRID_SIZE = 10 DEFAULT_ADD_TIMESTAMP = True diff --git a/device_session.py b/device_session.py index 1fc0865..244b3c9 100644 --- a/device_session.py +++ b/device_session.py @@ -13,12 +13,22 @@ import schema from sox import SoxClient, SoxComponent, TREE, CONFIG, RUNTIME, LINKS, ALL -def _fmt_float(val): - """Format a 32-bit float for display: 6 significant digits, always with a decimal point.""" - s = f"{val:.6g}" - if "." not in s and "e" not in s and "E" not in s: - s += ".0" - return s +def _fmt_float(val, precision=2): + """Format a float for display with *precision* decimal places (default 2).""" + return f"{val:.{precision}f}" + + +def _slot_precision(type_name, slot_name): + """Looks up the precision facet for a slot, returning 2 as the default.""" + reg = app_state.final_components_registry.get(type_name) + if reg: + for s in reg: + if s["name"] == slot_name: + p = s.get("facets", {}).get("precision") + if isinstance(p, int) and 0 <= p <= 12: + return p + break + return 2 class DeviceSession: @@ -100,7 +110,7 @@ class DeviceSession: for type_elem in manifest.iter("type"): if type_elem.get("name") == "Component": slots = [] - for slot_elem in type_elem.iter("slot"): + for slot_elem in type_elem.findall("slot"): slot_name = slot_elem.get("name", "") slot_id = int(slot_elem.get("id", "-1")) slot_type = slot_elem.get("type", "int") @@ -127,7 +137,7 @@ class DeviceSession: if type_id_val < 0: continue slots = [] - for slot_elem in type_elem.iter("slot"): + for slot_elem in type_elem.findall("slot"): slot_name = slot_elem.get("name", "") slot_id = int(slot_elem.get("id", "-1")) slot_type = slot_elem.get("type", "int") @@ -276,7 +286,7 @@ class DeviceSession: if name in sox_comp.values: val = sox_comp.values[name] if isinstance(val, float): - val_str = _fmt_float(val) + val_str = _fmt_float(val, _slot_precision(type_qname, name)) else: val_str = str(val) ET.SubElement(comp_elem, "prop", @@ -364,6 +374,7 @@ class DeviceSession: if comp_elem is None: return None + type_name = sox_comp.type_name slots = self.sox_client.slot_registry.get( (sox_comp.kit_id, sox_comp.type_id)) if slots: @@ -374,7 +385,10 @@ class DeviceSession: name = slot["name"] if name in sox_comp.values: raw = sox_comp.values[name] - val = _fmt_float(raw) if isinstance(raw, float) else str(raw) + if isinstance(raw, float): + val = _fmt_float(raw, _slot_precision(type_name, name)) + else: + val = str(raw) if name in existing_props: existing_props[name].set("val", val) else: diff --git a/properties.py b/properties.py index 34bd296..2cf51e3 100644 --- a/properties.py +++ b/properties.py @@ -374,7 +374,7 @@ def display_text(slot, raw): precision = slot["facets"].get("precision") if isinstance(precision, int) and 0 <= precision <= 12: return f"{number:.{precision}f}" - return str(number) + return f"{number:.2f}" if slot_type in INTEGER_TYPES: try: diff --git a/sox.py b/sox.py index d45bf02..c1bcf56 100644 --- a/sox.py +++ b/sox.py @@ -831,9 +831,8 @@ class SoxClient: continue if operator_only and not slot.get("is_operator"): continue - is_config = slot.get("is_config", True) - is_runtime = slot.get("is_runtime", False) - if what_char == 'c' and is_runtime: + is_config = slot.get("is_config", False) + if what_char == 'c' and not is_config: continue if what_char == 'r' and is_config: continue