Patrick Williams | 5587898 | 2020-04-03 15:24:57 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 2 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 3 | """Phosphor Inventory Manager YAML parser and code generator. |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 4 | |
| 5 | The parser workflow is broken down as follows: |
| 6 | 1 - Import YAML files as native python type(s) instance(s). |
| 7 | 2 - Create an instance of the Everything class from the |
| 8 | native python type instance(s) with the Everything.load |
| 9 | method. |
| 10 | 3 - The Everything class constructor orchestrates conversion of the |
| 11 | native python type(s) instances(s) to render helper types. |
| 12 | Each render helper type constructor imports its attributes |
| 13 | from the native python type(s) instances(s). |
| 14 | 4 - Present the converted YAML to the command processing method |
| 15 | requested by the script user. |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 16 | """ |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 17 | |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 18 | import argparse |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 19 | import os |
| 20 | import sys |
| 21 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 22 | import mako.lookup |
| 23 | import sdbusplus.property |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 24 | import yaml |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 25 | from sdbusplus.namedelement import NamedElement |
| 26 | from sdbusplus.renderer import Renderer |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 27 | |
Matthew Barth | 979eb59 | 2018-10-05 15:29:26 -0500 | [diff] [blame] | 28 | # Global busname for use within classes where necessary |
| 29 | busname = "xyz.openbmc_project.Inventory.Manager" |
| 30 | |
| 31 | |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 32 | def cppTypeName(yaml_type): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 33 | """Convert yaml types to cpp types.""" |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 34 | return sdbusplus.property.Property(type=yaml_type).cppTypeName |
| 35 | |
| 36 | |
Deepak Kodihalli | ef550b1 | 2017-08-03 14:00:17 -0500 | [diff] [blame] | 37 | class InterfaceComposite(object): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 38 | """Compose interface properties.""" |
Deepak Kodihalli | ef550b1 | 2017-08-03 14:00:17 -0500 | [diff] [blame] | 39 | |
| 40 | def __init__(self, dict): |
| 41 | self.dict = dict |
| 42 | |
Deepak Kodihalli | ef550b1 | 2017-08-03 14:00:17 -0500 | [diff] [blame] | 43 | def interfaces(self): |
Patrick Williams | 5587898 | 2020-04-03 15:24:57 -0500 | [diff] [blame] | 44 | return list(self.dict.keys()) |
Deepak Kodihalli | ef550b1 | 2017-08-03 14:00:17 -0500 | [diff] [blame] | 45 | |
| 46 | def names(self, interface): |
Marri Devender Rao | fa23d70 | 2017-09-02 04:43:42 -0500 | [diff] [blame] | 47 | names = [] |
| 48 | if self.dict[interface]: |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 49 | names = [ |
| 50 | NamedElement(name=x["name"]) for x in self.dict[interface] |
| 51 | ] |
Deepak Kodihalli | ef550b1 | 2017-08-03 14:00:17 -0500 | [diff] [blame] | 52 | return names |
| 53 | |
| 54 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 55 | class Interface(list): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 56 | """Provide various interface transformations.""" |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 57 | |
| 58 | def __init__(self, iface): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 59 | super(Interface, self).__init__(iface.split(".")) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 60 | |
| 61 | def namespace(self): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 62 | """Represent as an sdbusplus namespace.""" |
| 63 | return "::".join(["sdbusplus"] + self[:-1] + ["server", self[-1]]) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 64 | |
| 65 | def header(self): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 66 | """Represent as an sdbusplus server binding header.""" |
| 67 | return os.sep.join(self + ["server.hpp"]) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 68 | |
| 69 | def __str__(self): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 70 | return ".".join(self) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 71 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 72 | |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 73 | class Indent(object): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 74 | """Help templates be depth agnostic.""" |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 75 | |
| 76 | def __init__(self, depth=0): |
| 77 | self.depth = depth |
| 78 | |
| 79 | def __add__(self, depth): |
| 80 | return Indent(self.depth + depth) |
| 81 | |
| 82 | def __call__(self, depth): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 83 | """Render an indent at the current depth plus depth.""" |
| 84 | return 4 * " " * (depth + self.depth) |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 85 | |
| 86 | |
| 87 | class Template(NamedElement): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 88 | """Associate a template name with its namespace.""" |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 89 | |
| 90 | def __init__(self, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 91 | self.namespace = kw.pop("namespace", []) |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 92 | super(Template, self).__init__(**kw) |
| 93 | |
| 94 | def qualified(self): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 95 | return "::".join(self.namespace + [self.name]) |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 96 | |
| 97 | |
Brad Bishop | db9b325 | 2017-01-30 08:58:40 -0500 | [diff] [blame] | 98 | class FixBool(object): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 99 | """Un-capitalize booleans.""" |
Brad Bishop | db9b325 | 2017-01-30 08:58:40 -0500 | [diff] [blame] | 100 | |
| 101 | def __call__(self, arg): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 102 | return "{0}".format(arg.lower()) |
Brad Bishop | db9b325 | 2017-01-30 08:58:40 -0500 | [diff] [blame] | 103 | |
| 104 | |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 105 | class Quote(object): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 106 | """Decorate an argument by quoting it.""" |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 107 | |
| 108 | def __call__(self, arg): |
| 109 | return '"{0}"'.format(arg) |
| 110 | |
| 111 | |
| 112 | class Cast(object): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 113 | """Decorate an argument by casting it.""" |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 114 | |
| 115 | def __init__(self, cast, target): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 116 | """cast is the cast type (static, const, etc...). |
| 117 | target is the cast target type.""" |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 118 | self.cast = cast |
| 119 | self.target = target |
| 120 | |
| 121 | def __call__(self, arg): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 122 | return "{0}_cast<{1}>({2})".format(self.cast, self.target, arg) |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 123 | |
| 124 | |
| 125 | class Literal(object): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 126 | """Decorate an argument with a literal operator.""" |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 127 | |
Brad Bishop | 134d2cb | 2017-02-23 12:37:56 -0500 | [diff] [blame] | 128 | integer_types = [ |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 129 | "int8", |
| 130 | "int16", |
| 131 | "int32", |
| 132 | "int64", |
| 133 | "uint8", |
| 134 | "uint16", |
| 135 | "uint32", |
| 136 | "uint64", |
Brad Bishop | 134d2cb | 2017-02-23 12:37:56 -0500 | [diff] [blame] | 137 | ] |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 138 | |
| 139 | def __init__(self, type): |
| 140 | self.type = type |
| 141 | |
| 142 | def __call__(self, arg): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 143 | if "uint" in self.type: |
| 144 | arg = "{0}ull".format(arg) |
| 145 | elif "int" in self.type: |
| 146 | arg = "{0}ll".format(arg) |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 147 | |
Brad Bishop | 134d2cb | 2017-02-23 12:37:56 -0500 | [diff] [blame] | 148 | if self.type in self.integer_types: |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 149 | return Cast("static", "{0}_t".format(self.type))(arg) |
Brad Bishop | 134d2cb | 2017-02-23 12:37:56 -0500 | [diff] [blame] | 150 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 151 | if self.type == "string": |
| 152 | return "{0}s".format(arg) |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 153 | |
| 154 | return arg |
| 155 | |
| 156 | |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 157 | class Argument(NamedElement, Renderer): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 158 | """Define argument type inteface.""" |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 159 | |
| 160 | def __init__(self, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 161 | self.type = kw.pop("type", None) |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 162 | super(Argument, self).__init__(**kw) |
| 163 | |
| 164 | def argument(self, loader, indent): |
| 165 | raise NotImplementedError |
| 166 | |
| 167 | |
| 168 | class TrivialArgument(Argument): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 169 | """Non-array type arguments.""" |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 170 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 171 | def __init__(self, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 172 | self.value = kw.pop("value") |
| 173 | self.decorators = kw.pop("decorators", []) |
| 174 | if kw.get("type", None) == "string": |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 175 | self.decorators.insert(0, Quote()) |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 176 | if kw.get("type", None) == "boolean": |
Brad Bishop | db9b325 | 2017-01-30 08:58:40 -0500 | [diff] [blame] | 177 | self.decorators.insert(0, FixBool()) |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 178 | |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 179 | super(TrivialArgument, self).__init__(**kw) |
| 180 | |
| 181 | def argument(self, loader, indent): |
| 182 | a = str(self.value) |
| 183 | for d in self.decorators: |
| 184 | a = d(a) |
| 185 | |
| 186 | return a |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 187 | |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 188 | |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 189 | class InitializerList(Argument): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 190 | """Initializer list arguments.""" |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 191 | |
| 192 | def __init__(self, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 193 | self.values = kw.pop("values") |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 194 | super(InitializerList, self).__init__(**kw) |
| 195 | |
| 196 | def argument(self, loader, indent): |
| 197 | return self.render( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 198 | loader, "argument.mako.cpp", arg=self, indent=indent |
| 199 | ) |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 200 | |
| 201 | |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 202 | class DbusSignature(Argument): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 203 | """DBus signature arguments.""" |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 204 | |
| 205 | def __init__(self, **kw): |
Patrick Williams | 5587898 | 2020-04-03 15:24:57 -0500 | [diff] [blame] | 206 | self.sig = {x: y for x, y in kw.items()} |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 207 | kw.clear() |
| 208 | super(DbusSignature, self).__init__(**kw) |
| 209 | |
| 210 | def argument(self, loader, indent): |
| 211 | return self.render( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 212 | loader, "signature.mako.cpp", signature=self, indent=indent |
| 213 | ) |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 214 | |
| 215 | |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 216 | class MethodCall(Argument): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 217 | """Render syntatically correct c++ method calls.""" |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 218 | |
| 219 | def __init__(self, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 220 | self.namespace = kw.pop("namespace", []) |
| 221 | self.templates = kw.pop("templates", []) |
| 222 | self.args = kw.pop("args", []) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 223 | super(MethodCall, self).__init__(**kw) |
| 224 | |
Brad Bishop | cab2bdd | 2017-01-21 15:00:54 -0500 | [diff] [blame] | 225 | def call(self, loader, indent): |
| 226 | return self.render( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 227 | loader, "method.mako.cpp", method=self, indent=indent |
| 228 | ) |
Brad Bishop | cab2bdd | 2017-01-21 15:00:54 -0500 | [diff] [blame] | 229 | |
| 230 | def argument(self, loader, indent): |
| 231 | return self.call(loader, indent) |
| 232 | |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 233 | |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 234 | class Vector(MethodCall): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 235 | """Convenience type for vectors.""" |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 236 | |
| 237 | def __init__(self, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 238 | kw["name"] = "vector" |
| 239 | kw["namespace"] = ["std"] |
| 240 | kw["args"] = [InitializerList(values=kw.pop("args"))] |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 241 | super(Vector, self).__init__(**kw) |
| 242 | |
| 243 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 244 | class Filter(MethodCall): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 245 | """Convenience type for filters""" |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 246 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 247 | def __init__(self, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 248 | kw["name"] = "make_filter" |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 249 | super(Filter, self).__init__(**kw) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 250 | |
Brad Bishop | 0a6a479 | 2016-11-12 12:10:07 -0500 | [diff] [blame] | 251 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 252 | class Action(MethodCall): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 253 | """Convenience type for actions""" |
Brad Bishop | 561a565 | 2016-10-26 21:13:32 -0500 | [diff] [blame] | 254 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 255 | def __init__(self, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 256 | kw["name"] = "make_action" |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 257 | super(Action, self).__init__(**kw) |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 258 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 259 | |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 260 | class PathCondition(MethodCall): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 261 | """Convenience type for path conditions""" |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 262 | |
| 263 | def __init__(self, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 264 | kw["name"] = "make_path_condition" |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 265 | super(PathCondition, self).__init__(**kw) |
| 266 | |
| 267 | |
Matthew Barth | 979eb59 | 2018-10-05 15:29:26 -0500 | [diff] [blame] | 268 | class GetProperty(MethodCall): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 269 | """Convenience type for getting inventory properties""" |
Matthew Barth | 979eb59 | 2018-10-05 15:29:26 -0500 | [diff] [blame] | 270 | |
| 271 | def __init__(self, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 272 | kw["name"] = "make_get_property" |
Matthew Barth | 979eb59 | 2018-10-05 15:29:26 -0500 | [diff] [blame] | 273 | super(GetProperty, self).__init__(**kw) |
| 274 | |
| 275 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 276 | class CreateObjects(MethodCall): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 277 | """Assemble a createObjects functor.""" |
Brad Bishop | db92c28 | 2017-01-21 23:44:28 -0500 | [diff] [blame] | 278 | |
| 279 | def __init__(self, **kw): |
| 280 | objs = [] |
| 281 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 282 | for path, interfaces in kw.pop("objs").items(): |
Brad Bishop | db92c28 | 2017-01-21 23:44:28 -0500 | [diff] [blame] | 283 | key_o = TrivialArgument( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 284 | value=path, type="string", decorators=[Literal("string")] |
| 285 | ) |
Brad Bishop | db92c28 | 2017-01-21 23:44:28 -0500 | [diff] [blame] | 286 | value_i = [] |
| 287 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 288 | for ( |
| 289 | interface, |
| 290 | properties, |
| 291 | ) in interfaces.items(): |
| 292 | key_i = TrivialArgument(value=interface, type="string") |
Brad Bishop | db92c28 | 2017-01-21 23:44:28 -0500 | [diff] [blame] | 293 | value_p = [] |
Marri Devender Rao | fa23d70 | 2017-09-02 04:43:42 -0500 | [diff] [blame] | 294 | if properties: |
Patrick Williams | 5587898 | 2020-04-03 15:24:57 -0500 | [diff] [blame] | 295 | for prop, value in properties.items(): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 296 | key_p = TrivialArgument(value=prop, type="string") |
Marri Devender Rao | fa23d70 | 2017-09-02 04:43:42 -0500 | [diff] [blame] | 297 | value_v = TrivialArgument( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 298 | decorators=[Literal(value.get("type", None))], |
| 299 | **value |
| 300 | ) |
| 301 | value_p.append( |
| 302 | InitializerList(values=[key_p, value_v]) |
| 303 | ) |
Brad Bishop | db92c28 | 2017-01-21 23:44:28 -0500 | [diff] [blame] | 304 | |
| 305 | value_p = InitializerList(values=value_p) |
| 306 | value_i.append(InitializerList(values=[key_i, value_p])) |
| 307 | |
| 308 | value_i = InitializerList(values=value_i) |
| 309 | objs.append(InitializerList(values=[key_o, value_i])) |
| 310 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 311 | kw["args"] = [InitializerList(values=objs)] |
| 312 | kw["namespace"] = ["functor"] |
Brad Bishop | db92c28 | 2017-01-21 23:44:28 -0500 | [diff] [blame] | 313 | super(CreateObjects, self).__init__(**kw) |
| 314 | |
| 315 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 316 | class DestroyObjects(MethodCall): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 317 | """Assemble a destroyObject functor.""" |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 318 | |
| 319 | def __init__(self, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 320 | values = [{"value": x, "type": "string"} for x in kw.pop("paths")] |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 321 | conditions = [ |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 322 | Event.functor_map[x["name"]](**x) for x in kw.pop("conditions", []) |
| 323 | ] |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 324 | conditions = [PathCondition(args=[x]) for x in conditions] |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 325 | args = [InitializerList(values=[TrivialArgument(**x) for x in values])] |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 326 | args.append(InitializerList(values=conditions)) |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 327 | kw["args"] = args |
| 328 | kw["namespace"] = ["functor"] |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 329 | super(DestroyObjects, self).__init__(**kw) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 330 | |
| 331 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 332 | class SetProperty(MethodCall): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 333 | """Assemble a setProperty functor.""" |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 334 | |
| 335 | def __init__(self, **kw): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 336 | args = [] |
| 337 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 338 | value = kw.pop("value") |
| 339 | prop = kw.pop("property") |
| 340 | iface = kw.pop("interface") |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 341 | iface = Interface(iface) |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 342 | namespace = iface.namespace().split("::")[:-1] |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 343 | name = iface[-1] |
| 344 | t = Template(namespace=namespace, name=iface[-1]) |
| 345 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 346 | member = "&%s" % "::".join( |
| 347 | namespace + [name, NamedElement(name=prop).camelCase] |
| 348 | ) |
| 349 | member_type = cppTypeName(value["type"]) |
| 350 | member_cast = "{0} ({1}::*)({0})".format(member_type, t.qualified()) |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 351 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 352 | paths = [{"value": x, "type": "string"} for x in kw.pop("paths")] |
| 353 | args.append( |
| 354 | InitializerList(values=[TrivialArgument(**x) for x in paths]) |
| 355 | ) |
Brad Bishop | 02ca021 | 2017-01-28 23:25:58 -0500 | [diff] [blame] | 356 | |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 357 | conditions = [ |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 358 | Event.functor_map[x["name"]](**x) for x in kw.pop("conditions", []) |
| 359 | ] |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 360 | conditions = [PathCondition(args=[x]) for x in conditions] |
| 361 | |
| 362 | args.append(InitializerList(values=conditions)) |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 363 | args.append(TrivialArgument(value=str(iface), type="string")) |
| 364 | args.append( |
| 365 | TrivialArgument( |
| 366 | value=member, decorators=[Cast("static", member_cast)] |
| 367 | ) |
| 368 | ) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 369 | args.append(TrivialArgument(**value)) |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 370 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 371 | kw["templates"] = [Template(name=name, namespace=namespace)] |
| 372 | kw["args"] = args |
| 373 | kw["namespace"] = ["functor"] |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 374 | super(SetProperty, self).__init__(**kw) |
| 375 | |
| 376 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 377 | class PropertyChanged(MethodCall): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 378 | """Assemble a propertyChanged functor.""" |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 379 | |
| 380 | def __init__(self, **kw): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 381 | args = [] |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 382 | args.append(TrivialArgument(value=kw.pop("interface"), type="string")) |
| 383 | args.append(TrivialArgument(value=kw.pop("property"), type="string")) |
| 384 | args.append( |
| 385 | TrivialArgument( |
| 386 | decorators=[Literal(kw["value"].get("type", None))], |
| 387 | **kw.pop("value") |
| 388 | ) |
| 389 | ) |
| 390 | kw["args"] = args |
| 391 | kw["namespace"] = ["functor"] |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 392 | super(PropertyChanged, self).__init__(**kw) |
| 393 | |
| 394 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 395 | class PropertyIs(MethodCall): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 396 | """Assemble a propertyIs functor.""" |
Brad Bishop | 040e18b | 2017-01-21 22:04:00 -0500 | [diff] [blame] | 397 | |
| 398 | def __init__(self, **kw): |
| 399 | args = [] |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 400 | path = kw.pop("path", None) |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 401 | if not path: |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 402 | path = TrivialArgument(value="nullptr") |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 403 | else: |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 404 | path = TrivialArgument(value=path, type="string") |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 405 | |
| 406 | args.append(path) |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 407 | iface = TrivialArgument(value=kw.pop("interface"), type="string") |
Matthew Barth | 979eb59 | 2018-10-05 15:29:26 -0500 | [diff] [blame] | 408 | args.append(iface) |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 409 | prop = TrivialArgument(value=kw.pop("property"), type="string") |
Matthew Barth | 979eb59 | 2018-10-05 15:29:26 -0500 | [diff] [blame] | 410 | args.append(prop) |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 411 | args.append( |
| 412 | TrivialArgument( |
| 413 | decorators=[Literal(kw["value"].get("type", None))], |
| 414 | **kw.pop("value") |
| 415 | ) |
| 416 | ) |
Brad Bishop | 040e18b | 2017-01-21 22:04:00 -0500 | [diff] [blame] | 417 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 418 | service = kw.pop("service", None) |
Brad Bishop | 040e18b | 2017-01-21 22:04:00 -0500 | [diff] [blame] | 419 | if service: |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 420 | args.append(TrivialArgument(value=service, type="string")) |
Brad Bishop | 040e18b | 2017-01-21 22:04:00 -0500 | [diff] [blame] | 421 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 422 | dbusMember = kw.pop("dbusMember", None) |
Matthew Barth | 979eb59 | 2018-10-05 15:29:26 -0500 | [diff] [blame] | 423 | if dbusMember: |
| 424 | # Inventory manager's service name is required |
| 425 | if not service or service != busname: |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 426 | args.append(TrivialArgument(value=busname, type="string")) |
Matthew Barth | 979eb59 | 2018-10-05 15:29:26 -0500 | [diff] [blame] | 427 | |
| 428 | gpArgs = [] |
| 429 | gpArgs.append(path) |
| 430 | gpArgs.append(iface) |
| 431 | # Prepend '&' and append 'getPropertyByName' function on dbusMember |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 432 | gpArgs.append( |
| 433 | TrivialArgument(value="&" + dbusMember + "::getPropertyByName") |
| 434 | ) |
Matthew Barth | 979eb59 | 2018-10-05 15:29:26 -0500 | [diff] [blame] | 435 | gpArgs.append(prop) |
| 436 | fArg = MethodCall( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 437 | name="getProperty", |
| 438 | namespace=["functor"], |
| 439 | templates=[Template(name=dbusMember, namespace=[])], |
| 440 | args=gpArgs, |
| 441 | ) |
Matthew Barth | 979eb59 | 2018-10-05 15:29:26 -0500 | [diff] [blame] | 442 | |
| 443 | # Append getProperty functor |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 444 | args.append( |
| 445 | GetProperty( |
| 446 | templates=[ |
| 447 | Template( |
| 448 | name=dbusMember + "::PropertiesVariant", |
| 449 | namespace=[], |
| 450 | ) |
| 451 | ], |
| 452 | args=[fArg], |
| 453 | ) |
| 454 | ) |
Matthew Barth | 979eb59 | 2018-10-05 15:29:26 -0500 | [diff] [blame] | 455 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 456 | kw["args"] = args |
| 457 | kw["namespace"] = ["functor"] |
Brad Bishop | 040e18b | 2017-01-21 22:04:00 -0500 | [diff] [blame] | 458 | super(PropertyIs, self).__init__(**kw) |
| 459 | |
| 460 | |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 461 | class Event(MethodCall): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 462 | """Assemble an inventory manager event.""" |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 463 | |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 464 | functor_map = { |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 465 | "destroyObjects": DestroyObjects, |
| 466 | "createObjects": CreateObjects, |
| 467 | "propertyChangedTo": PropertyChanged, |
| 468 | "propertyIs": PropertyIs, |
| 469 | "setProperty": SetProperty, |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 470 | } |
| 471 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 472 | def __init__(self, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 473 | self.summary = kw.pop("name") |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 474 | |
| 475 | filters = [ |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 476 | self.functor_map[x["name"]](**x) for x in kw.pop("filters", []) |
| 477 | ] |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 478 | filters = [Filter(args=[x]) for x in filters] |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 479 | filters = Vector( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 480 | templates=[Template(name="Filter", namespace=[])], args=filters |
| 481 | ) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 482 | |
| 483 | event = MethodCall( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 484 | name="make_shared", |
| 485 | namespace=["std"], |
| 486 | templates=[ |
| 487 | Template( |
| 488 | name=kw.pop("event"), |
| 489 | namespace=kw.pop("event_namespace", []), |
| 490 | ) |
| 491 | ], |
| 492 | args=kw.pop("event_args", []) + [filters], |
| 493 | ) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 494 | |
| 495 | events = Vector( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 496 | templates=[Template(name="EventBasePtr", namespace=[])], |
| 497 | args=[event], |
| 498 | ) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 499 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 500 | action_type = Template(name="Action", namespace=[]) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 501 | action_args = [ |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 502 | self.functor_map[x["name"]](**x) for x in kw.pop("actions", []) |
| 503 | ] |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 504 | action_args = [Action(args=[x]) for x in action_args] |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 505 | actions = Vector(templates=[action_type], args=action_args) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 506 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 507 | kw["name"] = "make_tuple" |
| 508 | kw["namespace"] = ["std"] |
| 509 | kw["args"] = [events, actions] |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 510 | super(Event, self).__init__(**kw) |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 511 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 512 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 513 | class MatchEvent(Event): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 514 | """Associate one or more dbus signal match signatures with |
| 515 | a filter.""" |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 516 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 517 | def __init__(self, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 518 | kw["event"] = "DbusSignal" |
| 519 | kw["event_namespace"] = [] |
| 520 | kw["event_args"] = [ |
| 521 | DbusSignature(**x) for x in kw.pop("signatures", []) |
| 522 | ] |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 523 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 524 | super(MatchEvent, self).__init__(**kw) |
| 525 | |
| 526 | |
Brad Bishop | 828df83 | 2017-01-21 22:20:43 -0500 | [diff] [blame] | 527 | class StartupEvent(Event): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 528 | """Assemble a startup event.""" |
Brad Bishop | 828df83 | 2017-01-21 22:20:43 -0500 | [diff] [blame] | 529 | |
| 530 | def __init__(self, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 531 | kw["event"] = "StartupEvent" |
| 532 | kw["event_namespace"] = [] |
Brad Bishop | 828df83 | 2017-01-21 22:20:43 -0500 | [diff] [blame] | 533 | super(StartupEvent, self).__init__(**kw) |
| 534 | |
| 535 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 536 | class Everything(Renderer): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 537 | """Parse/render entry point.""" |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 538 | |
| 539 | class_map = { |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 540 | "match": MatchEvent, |
| 541 | "startup": StartupEvent, |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | @staticmethod |
| 545 | def load(args): |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 546 | # Aggregate all the event YAML in the events.d directory |
| 547 | # into a single list of events. |
| 548 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 549 | events = [] |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 550 | events_dir = os.path.join(args.inputdir, "events.d") |
Brad Bishop | a6fcd56 | 2017-02-03 11:00:27 -0500 | [diff] [blame] | 551 | |
| 552 | if os.path.exists(events_dir): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 553 | yaml_files = [ |
| 554 | x for x in os.listdir(events_dir) if x.endswith(".yaml") |
| 555 | ] |
Brad Bishop | a6fcd56 | 2017-02-03 11:00:27 -0500 | [diff] [blame] | 556 | |
| 557 | for x in yaml_files: |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 558 | with open(os.path.join(events_dir, x), "r") as fd: |
| 559 | for e in yaml.safe_load(fd.read()).get("events", {}): |
Brad Bishop | a6fcd56 | 2017-02-03 11:00:27 -0500 | [diff] [blame] | 560 | events.append(e) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 561 | |
Deepak Kodihalli | ef550b1 | 2017-08-03 14:00:17 -0500 | [diff] [blame] | 562 | interfaces, interface_composite = Everything.get_interfaces( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 563 | args.ifacesdir |
| 564 | ) |
| 565 | ( |
| 566 | extra_interfaces, |
| 567 | extra_interface_composite, |
| 568 | ) = Everything.get_interfaces( |
| 569 | os.path.join(args.inputdir, "extra_interfaces.d") |
| 570 | ) |
Deepak Kodihalli | ef550b1 | 2017-08-03 14:00:17 -0500 | [diff] [blame] | 571 | interface_composite.update(extra_interface_composite) |
| 572 | interface_composite = InterfaceComposite(interface_composite) |
Matthew Barth | 979eb59 | 2018-10-05 15:29:26 -0500 | [diff] [blame] | 573 | # Update busname if configured differenly than the default |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 574 | global busname |
Matthew Barth | 979eb59 | 2018-10-05 15:29:26 -0500 | [diff] [blame] | 575 | busname = args.busname |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 576 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 577 | return Everything( |
| 578 | *events, |
Deepak Kodihalli | ef550b1 | 2017-08-03 14:00:17 -0500 | [diff] [blame] | 579 | interfaces=interfaces + extra_interfaces, |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 580 | interface_composite=interface_composite |
| 581 | ) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 582 | |
| 583 | @staticmethod |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 584 | def get_interfaces(targetdir): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 585 | """Scan the interfaces directory for interfaces that PIM can create.""" |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 586 | |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 587 | yaml_files = [] |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 588 | interfaces = [] |
Deepak Kodihalli | ef550b1 | 2017-08-03 14:00:17 -0500 | [diff] [blame] | 589 | interface_composite = {} |
Brad Bishop | a6fcd56 | 2017-02-03 11:00:27 -0500 | [diff] [blame] | 590 | |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 591 | if targetdir and os.path.exists(targetdir): |
| 592 | for directory, _, files in os.walk(targetdir): |
| 593 | if not files: |
| 594 | continue |
| 595 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 596 | yaml_files += [ |
| 597 | os.path.relpath(os.path.join(directory, f), targetdir) |
| 598 | for f in [ |
| 599 | f for f in files if f.endswith(".interface.yaml") |
| 600 | ] |
| 601 | ] |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 602 | |
| 603 | for y in yaml_files: |
Marri Devender Rao | 06e3d50 | 2017-06-09 11:33:38 -0500 | [diff] [blame] | 604 | # parse only phosphor dbus related interface files |
Sunny Srivastava | 3ce5930 | 2022-05-12 20:21:45 +0530 | [diff] [blame] | 605 | if not ( |
| 606 | y.startswith("xyz") |
| 607 | or y.startswith("com/ibm/ipzvpd") |
| 608 | or y.startswith("com/ibm/Control/Host") |
| 609 | ): |
Marri Devender Rao | 06e3d50 | 2017-06-09 11:33:38 -0500 | [diff] [blame] | 610 | continue |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 611 | with open(os.path.join(targetdir, y)) as fd: |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 612 | i = y.replace(".interface.yaml", "").replace(os.sep, ".") |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 613 | |
| 614 | # PIM can't create interfaces with methods. |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 615 | parsed = yaml.safe_load(fd.read()) |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 616 | if parsed.get("methods", None): |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 617 | continue |
Deepak Kodihalli | 0b6ca10 | 2017-08-09 04:39:43 -0500 | [diff] [blame] | 618 | # Cereal can't understand the type sdbusplus::object_path. This |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 619 | # type is a wrapper around std::string. Ignore interfaces |
| 620 | # having a property of this type for now. The only interface |
| 621 | # that has a property of this type now is |
| 622 | # xyz.openbmc_project.Association, which is an unused |
| 623 | # interface. No inventory objects implement this interface. |
Deepak Kodihalli | 0b6ca10 | 2017-08-09 04:39:43 -0500 | [diff] [blame] | 624 | # TODO via openbmc/openbmc#2123 : figure out how to make Cereal |
| 625 | # understand sdbusplus::object_path. |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 626 | properties = parsed.get("properties", None) |
Marri Devender Rao | fa23d70 | 2017-09-02 04:43:42 -0500 | [diff] [blame] | 627 | if properties: |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 628 | if any("path" in p["type"] for p in properties): |
Marri Devender Rao | fa23d70 | 2017-09-02 04:43:42 -0500 | [diff] [blame] | 629 | continue |
Deepak Kodihalli | ef550b1 | 2017-08-03 14:00:17 -0500 | [diff] [blame] | 630 | interface_composite[i] = properties |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 631 | interfaces.append(i) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 632 | |
Deepak Kodihalli | ef550b1 | 2017-08-03 14:00:17 -0500 | [diff] [blame] | 633 | return interfaces, interface_composite |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 634 | |
| 635 | def __init__(self, *a, **kw): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 636 | self.interfaces = [Interface(x) for x in kw.pop("interfaces", [])] |
| 637 | self.interface_composite = kw.pop("interface_composite", {}) |
| 638 | self.events = [self.class_map[x["type"]](**x) for x in a] |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 639 | super(Everything, self).__init__(**kw) |
| 640 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 641 | def generate_cpp(self, loader): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 642 | """Render the template with the provided events and interfaces.""" |
| 643 | with open(os.path.join(args.outputdir, "generated.cpp"), "w") as fd: |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 644 | fd.write( |
| 645 | self.render( |
| 646 | loader, |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 647 | "generated.mako.cpp", |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 648 | events=self.events, |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 649 | interfaces=self.interfaces, |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 650 | indent=Indent(), |
| 651 | ) |
| 652 | ) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 653 | |
Deepak Kodihalli | f7b0399 | 2017-08-04 11:25:41 -0500 | [diff] [blame] | 654 | def generate_serialization(self, loader): |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 655 | with open( |
| 656 | os.path.join(args.outputdir, "gen_serialization.hpp"), "w" |
| 657 | ) as fd: |
Deepak Kodihalli | f7b0399 | 2017-08-04 11:25:41 -0500 | [diff] [blame] | 658 | fd.write( |
| 659 | self.render( |
| 660 | loader, |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 661 | "gen_serialization.mako.hpp", |
Deepak Kodihalli | f7b0399 | 2017-08-04 11:25:41 -0500 | [diff] [blame] | 662 | interfaces=self.interfaces, |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 663 | interface_composite=self.interface_composite, |
| 664 | ) |
| 665 | ) |
Deepak Kodihalli | f7b0399 | 2017-08-04 11:25:41 -0500 | [diff] [blame] | 666 | |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 667 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 668 | if __name__ == "__main__": |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 669 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 670 | valid_commands = { |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 671 | "generate-cpp": "generate_cpp", |
| 672 | "generate-serialization": "generate_serialization", |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 673 | } |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 674 | |
| 675 | parser = argparse.ArgumentParser( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 676 | description=( |
| 677 | "Phosphor Inventory Manager (PIM) YAML scanner and code generator." |
| 678 | ) |
| 679 | ) |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 680 | parser.add_argument( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 681 | "-o", |
| 682 | "--output-dir", |
| 683 | dest="outputdir", |
| 684 | default=".", |
| 685 | help="Output directory.", |
| 686 | ) |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 687 | parser.add_argument( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 688 | "-i", |
| 689 | "--interfaces-dir", |
| 690 | dest="ifacesdir", |
| 691 | help="Location of interfaces to be supported.", |
| 692 | ) |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 693 | parser.add_argument( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 694 | "-d", |
| 695 | "--dir", |
| 696 | dest="inputdir", |
| 697 | default=os.path.join(script_dir, "example"), |
| 698 | help="Location of files to process.", |
| 699 | ) |
Brad Bishop | f4666f5 | 2016-11-12 12:44:42 -0500 | [diff] [blame] | 700 | parser.add_argument( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 701 | "-b", |
| 702 | "--bus-name", |
| 703 | dest="busname", |
| 704 | default="xyz.openbmc_project.Inventory.Manager", |
| 705 | help="Inventory manager busname.", |
| 706 | ) |
Matthew Barth | 979eb59 | 2018-10-05 15:29:26 -0500 | [diff] [blame] | 707 | parser.add_argument( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 708 | "command", |
| 709 | metavar="COMMAND", |
| 710 | type=str, |
Patrick Williams | 5587898 | 2020-04-03 15:24:57 -0500 | [diff] [blame] | 711 | choices=list(valid_commands.keys()), |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 712 | help="%s." % " | ".join(list(valid_commands.keys())), |
| 713 | ) |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 714 | |
| 715 | args = parser.parse_args() |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 716 | |
| 717 | if sys.version_info < (3, 0): |
| 718 | lookup = mako.lookup.TemplateLookup( |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 719 | directories=[script_dir], disable_unicode=True |
| 720 | ) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 721 | else: |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 722 | lookup = mako.lookup.TemplateLookup(directories=[script_dir]) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 723 | |
Patrick Williams | 5de46b3 | 2022-12-05 10:03:44 -0600 | [diff] [blame] | 724 | function = getattr(Everything.load(args), valid_commands[args.command]) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 725 | function(lookup) |