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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
import os
from dbus_fast import (
ArgDirection,
InvalidMemberNameError,
PropertyAccess,
SignatureType,
)
from dbus_fast import introspection as intr
with open(f"{os.path.dirname(__file__)}/data/strict-introspection.xml") as f:
strict_data = f.read()
with open(f"{os.path.dirname(__file__)}/data/sloppy-introspection.xml") as f:
sloppy_data = f.read()
def test_introspection_from_xml_sloppy():
intr.Node.parse(sloppy_data, validate_property_names=False)
def test_introspection_from_xml_strict():
try:
node = intr.Node.parse(sloppy_data)
except InvalidMemberNameError:
pass
else:
assert False, "Expected an AssertionError"
node = intr.Node.parse(strict_data)
assert len(node.interfaces) == 1
interface = node.interfaces[0]
assert len(node.nodes) == 2
assert len(interface.methods) == 3
assert len(interface.signals) == 2
assert len(interface.properties) == 1
assert type(node.nodes[0]) is intr.Node
assert node.nodes[0].name == "child_of_sample_object"
assert type(node.nodes[1]) is intr.Node
assert node.nodes[1].name == "another_child_of_sample_object"
assert interface.name == "com.example.SampleInterface0"
frobate = interface.methods[0]
assert type(frobate) is intr.Method
assert frobate.name == "Frobate"
assert len(frobate.in_args) == 1
assert len(frobate.out_args) == 2
foo = frobate.in_args[0]
assert type(foo) is intr.Arg
assert foo.name == "foo"
assert foo.direction == ArgDirection.IN
assert foo.signature == "i"
assert type(foo.type) is SignatureType
assert foo.type.token == "i"
bar = frobate.out_args[0]
assert type(bar) is intr.Arg
assert bar.name == "bar"
assert bar.direction == ArgDirection.OUT
assert bar.signature == "s"
assert type(bar.type) is SignatureType
assert bar.type.token == "s"
prop = interface.properties[0]
assert type(prop) is intr.Property
assert prop.name == "Bar"
assert prop.signature == "y"
assert type(prop.type) is SignatureType
assert prop.type.token == "y"
assert prop.access == PropertyAccess.WRITE
changed = interface.signals[0]
assert type(changed) is intr.Signal
assert changed.name == "Changed"
assert len(changed.args) == 1
new_value = changed.args[0]
assert type(new_value) is intr.Arg
assert new_value.name == "0-new_value"
assert new_value.signature == "b"
def test_example_introspection_to_xml():
node = intr.Node.parse(strict_data)
tree = node.to_xml()
assert tree.tag == "node"
assert tree.attrib.get("name") == "/com/example/sample_object0"
assert len(tree) == 3
interface = tree[0]
assert interface.tag == "interface"
assert interface.get("name") == "com.example.SampleInterface0"
assert len(interface) == 6
method = interface[0]
assert method.tag == "method"
assert method.get("name") == "Frobate"
assert len(method) == 4
arg = method[0]
assert arg.tag == "arg"
assert arg.attrib.get("name") == "foo"
assert arg.attrib.get("type") == "i"
assert arg.attrib.get("direction") == "in"
annotation = method[3]
assert annotation.tag == "annotation"
assert annotation.attrib.get("name") == "org.freedesktop.DBus.Deprecated"
assert annotation.attrib.get("value") == "true"
signal = interface[3]
assert signal.tag == "signal"
assert signal.attrib.get("name") == "Changed"
assert len(signal) == 1
arg = signal[0]
assert arg.tag == "arg"
assert arg.attrib.get("name") == "0-new_value"
assert arg.attrib.get("type") == "b"
signal = interface[4]
assert signal.tag == "signal"
assert signal.attrib.get("name") == "ChangedMulti"
assert len(signal) == 2
arg = signal[0]
assert arg.tag == "arg"
assert arg.attrib.get("name") == "new_value1"
assert arg.attrib.get("type") == "b"
arg = signal[1]
assert arg.tag == "arg"
assert arg.attrib.get("name") == "0-new_value2"
assert arg.attrib.get("type") == "y"
prop = interface[5]
assert prop.attrib.get("name") == "Bar"
assert prop.attrib.get("type") == "y"
assert prop.attrib.get("access") == "write"
def test_default_interfaces():
# just make sure it doesn't throw
default = intr.Node.default()
assert type(default) is intr.Node
|