Marri Devender Rao | 44fd7e8 | 2020-03-08 09:51:34 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Matthew Barth | db440d4 | 2017-04-17 15:49:37 -0500 | [diff] [blame] | 2 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 3 | """Phosphor DBus Monitor YAML parser and code generator. |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [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 | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 16 | """ |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 17 | |
Matthew Barth | db440d4 | 2017-04-17 15:49:37 -0500 | [diff] [blame] | 18 | import os |
| 19 | import sys |
Matthew Barth | db440d4 | 2017-04-17 15:49:37 -0500 | [diff] [blame] | 20 | from argparse import ArgumentParser |
Patrick Williams | ab375ec | 2022-12-08 06:18:05 -0600 | [diff] [blame^] | 21 | |
| 22 | import mako.lookup |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 23 | import sdbusplus.property |
Patrick Williams | ab375ec | 2022-12-08 06:18:05 -0600 | [diff] [blame^] | 24 | import yaml |
| 25 | from sdbusplus.namedelement import NamedElement |
| 26 | from sdbusplus.renderer import Renderer |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 27 | |
| 28 | |
Patrick Williams | 18447ac | 2022-12-05 10:45:23 -0600 | [diff] [blame] | 29 | class InvalidConfigError(Exception): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 30 | """General purpose config file parsing error.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 31 | |
| 32 | def __init__(self, path, msg): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 33 | """Display configuration file with the syntax |
| 34 | error and the error message.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 35 | |
| 36 | self.config = path |
| 37 | self.msg = msg |
| 38 | |
| 39 | |
| 40 | class NotUniqueError(InvalidConfigError): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 41 | """Within a config file names must be unique. |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 42 | Display the config file with the duplicate and |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 43 | the duplicate itself.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 44 | |
| 45 | def __init__(self, path, cls, *names): |
| 46 | fmt = 'Duplicate {0}: "{1}"' |
| 47 | super(NotUniqueError, self).__init__( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 48 | path, fmt.format(cls, " ".join(names)) |
| 49 | ) |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 50 | |
| 51 | |
| 52 | def get_index(objs, cls, name, config=None): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 53 | """Items are usually rendered as C++ arrays and as |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 54 | such are stored in python lists. Given an item name |
| 55 | its class, and an optional config file filter, find |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 56 | the item index.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 57 | |
| 58 | for i, x in enumerate(objs.get(cls, [])): |
| 59 | if config and x.configfile != config: |
| 60 | continue |
| 61 | if x.name != name: |
| 62 | continue |
| 63 | |
| 64 | return i |
| 65 | raise InvalidConfigError(config, 'Could not find name: "{0}"'.format(name)) |
| 66 | |
| 67 | |
| 68 | def exists(objs, cls, name, config=None): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 69 | """Check to see if an item already exists in a list given |
| 70 | the item name.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 71 | |
| 72 | try: |
| 73 | get_index(objs, cls, name, config) |
Patrick Williams | 18447ac | 2022-12-05 10:45:23 -0600 | [diff] [blame] | 74 | except Exception: |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 75 | return False |
| 76 | |
| 77 | return True |
| 78 | |
| 79 | |
| 80 | def add_unique(obj, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 81 | """Add an item to one or more lists unless already present, |
| 82 | with an option to constrain the search to a specific config file.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 83 | |
| 84 | for container in a: |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 85 | if not exists(container, obj.cls, obj.name, config=kw.get("config")): |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 86 | container.setdefault(obj.cls, []).append(obj) |
Matthew Barth | db440d4 | 2017-04-17 15:49:37 -0500 | [diff] [blame] | 87 | |
| 88 | |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 89 | class Cast(object): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 90 | """Decorate an argument by casting it.""" |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 91 | |
| 92 | def __init__(self, cast, target): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 93 | """cast is the cast type (static, const, etc...). |
| 94 | target is the cast target type.""" |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 95 | self.cast = cast |
| 96 | self.target = target |
| 97 | |
| 98 | def __call__(self, arg): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 99 | return "{0}_cast<{1}>({2})".format(self.cast, self.target, arg) |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 100 | |
| 101 | |
| 102 | class Literal(object): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 103 | """Decorate an argument with a literal operator.""" |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 104 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 105 | integer_types = ["int16", "int32", "int64", "uint16", "uint32", "uint64"] |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 106 | |
| 107 | def __init__(self, type): |
| 108 | self.type = type |
| 109 | |
| 110 | def __call__(self, arg): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 111 | if "uint" in self.type: |
| 112 | arg = "{0}ull".format(arg) |
| 113 | elif "int" in self.type: |
| 114 | arg = "{0}ll".format(arg) |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 115 | |
| 116 | if self.type in self.integer_types: |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 117 | return Cast("static", "{0}_t".format(self.type))(arg) |
| 118 | elif self.type == "byte": |
Patrick Williams | 18447ac | 2022-12-05 10:45:23 -0600 | [diff] [blame] | 119 | return Cast("static", "uint8_t")(arg) |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 120 | elif self.type == "double": |
Patrick Williams | 18447ac | 2022-12-05 10:45:23 -0600 | [diff] [blame] | 121 | return Cast("static", "double")(arg) |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 122 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 123 | if self.type == "string": |
| 124 | return "{0}s".format(arg) |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 125 | |
| 126 | return arg |
| 127 | |
| 128 | |
| 129 | class FixBool(object): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 130 | """Un-capitalize booleans.""" |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 131 | |
| 132 | def __call__(self, arg): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 133 | return "{0}".format(arg.lower()) |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 134 | |
| 135 | |
| 136 | class Quote(object): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 137 | """Decorate an argument by quoting it.""" |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 138 | |
| 139 | def __call__(self, arg): |
| 140 | return '"{0}"'.format(arg) |
| 141 | |
| 142 | |
| 143 | class Argument(NamedElement, Renderer): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 144 | """Define argument type interface.""" |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 145 | |
| 146 | def __init__(self, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 147 | self.type = kw.pop("type", None) |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 148 | super(Argument, self).__init__(**kw) |
| 149 | |
| 150 | def argument(self, loader, indent): |
| 151 | raise NotImplementedError |
| 152 | |
| 153 | |
| 154 | class TrivialArgument(Argument): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 155 | """Non-array type arguments.""" |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 156 | |
| 157 | def __init__(self, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 158 | self.value = kw.pop("value") |
| 159 | self.decorators = kw.pop("decorators", []) |
| 160 | if kw.get("type", None): |
| 161 | self.decorators.insert(0, Literal(kw["type"])) |
| 162 | if kw.get("type", None) == "string": |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 163 | self.decorators.insert(0, Quote()) |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 164 | if kw.get("type", None) == "boolean": |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 165 | self.decorators.insert(0, FixBool()) |
| 166 | |
| 167 | super(TrivialArgument, self).__init__(**kw) |
| 168 | |
| 169 | def argument(self, loader, indent): |
| 170 | a = str(self.value) |
| 171 | for d in self.decorators: |
| 172 | a = d(a) |
| 173 | |
| 174 | return a |
| 175 | |
Matt Spinler | 80e9b65 | 2017-11-02 14:21:04 -0500 | [diff] [blame] | 176 | |
Gunnar Mills | 30474cf | 2017-08-11 09:38:54 -0500 | [diff] [blame] | 177 | class Metadata(Argument): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 178 | """Metadata type arguments.""" |
Gunnar Mills | 30474cf | 2017-08-11 09:38:54 -0500 | [diff] [blame] | 179 | |
| 180 | def __init__(self, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 181 | self.value = kw.pop("value") |
| 182 | self.decorators = kw.pop("decorators", []) |
| 183 | if kw.get("type", None) == "string": |
Gunnar Mills | 30474cf | 2017-08-11 09:38:54 -0500 | [diff] [blame] | 184 | self.decorators.insert(0, Quote()) |
| 185 | |
| 186 | super(Metadata, self).__init__(**kw) |
| 187 | |
| 188 | def argument(self, loader, indent): |
| 189 | a = str(self.value) |
| 190 | for d in self.decorators: |
| 191 | a = d(a) |
| 192 | |
| 193 | return a |
Brad Bishop | 0107989 | 2017-05-26 10:56:45 -0400 | [diff] [blame] | 194 | |
Matt Spinler | 80e9b65 | 2017-11-02 14:21:04 -0500 | [diff] [blame] | 195 | |
Matthew Barth | ae786ef | 2019-09-04 15:46:13 -0500 | [diff] [blame] | 196 | class OpArgument(Argument): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 197 | """Operation type arguments.""" |
Matthew Barth | ae786ef | 2019-09-04 15:46:13 -0500 | [diff] [blame] | 198 | |
| 199 | def __init__(self, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 200 | self.op = kw.pop("op") |
| 201 | self.bound = kw.pop("bound") |
| 202 | self.decorators = kw.pop("decorators", []) |
| 203 | if kw.get("type", None): |
| 204 | self.decorators.insert(0, Literal(kw["type"])) |
| 205 | if kw.get("type", None) == "string": |
Matthew Barth | ae786ef | 2019-09-04 15:46:13 -0500 | [diff] [blame] | 206 | self.decorators.insert(0, Quote()) |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 207 | if kw.get("type", None) == "boolean": |
Matthew Barth | ae786ef | 2019-09-04 15:46:13 -0500 | [diff] [blame] | 208 | self.decorators.insert(0, FixBool()) |
| 209 | |
| 210 | super(OpArgument, self).__init__(**kw) |
| 211 | |
| 212 | def argument(self, loader, indent): |
| 213 | a = str(self.bound) |
| 214 | for d in self.decorators: |
| 215 | a = d(a) |
| 216 | |
| 217 | return a |
| 218 | |
| 219 | |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 220 | class Indent(object): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 221 | """Help templates be depth agnostic.""" |
Matthew Barth | db440d4 | 2017-04-17 15:49:37 -0500 | [diff] [blame] | 222 | |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 223 | def __init__(self, depth=0): |
| 224 | self.depth = depth |
Matthew Barth | db440d4 | 2017-04-17 15:49:37 -0500 | [diff] [blame] | 225 | |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 226 | def __add__(self, depth): |
| 227 | return Indent(self.depth + depth) |
| 228 | |
| 229 | def __call__(self, depth): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 230 | """Render an indent at the current depth plus depth.""" |
| 231 | return 4 * " " * (depth + self.depth) |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 232 | |
| 233 | |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 234 | class ConfigEntry(NamedElement): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 235 | """Base interface for rendered items.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 236 | |
| 237 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 238 | """Pop the configfile/class/subclass keywords.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 239 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 240 | self.configfile = kw.pop("configfile") |
| 241 | self.cls = kw.pop("class") |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 242 | self.subclass = kw.pop(self.cls) |
Patrick Williams | cfdfa0d | 2021-09-20 18:32:17 -0500 | [diff] [blame] | 243 | |
| 244 | # TODO: NamedElement requires 'name' to be a string, but in many cases |
| 245 | # this script treats 'name' as a dict. Save the property off and |
| 246 | # insert it after ConfigEntry does its own thing to avoid |
| 247 | # exceptions. This should be refactored throughout the whole |
| 248 | # script to not overload 'name' as a dict. |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 249 | name_save = kw.pop("name") |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 250 | super(ConfigEntry, self).__init__(**kw) |
Patrick Williams | cfdfa0d | 2021-09-20 18:32:17 -0500 | [diff] [blame] | 251 | self.name = name_save |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 252 | |
| 253 | def factory(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 254 | """Optional factory interface for subclasses to add |
| 255 | additional items to be rendered.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 256 | |
| 257 | pass |
| 258 | |
| 259 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 260 | """Optional setup interface for subclasses, invoked |
| 261 | after all factory methods have been run.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 262 | |
| 263 | pass |
| 264 | |
| 265 | |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 266 | class Path(ConfigEntry): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 267 | """Path/metadata association.""" |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 268 | |
| 269 | def __init__(self, *a, **kw): |
| 270 | super(Path, self).__init__(**kw) |
| 271 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 272 | if self.name["meta"].upper() != self.name["meta"]: |
Brad Bishop | babf3b7 | 2017-05-31 19:44:53 -0400 | [diff] [blame] | 273 | raise InvalidConfigError( |
| 274 | self.configfile, |
| 275 | 'Metadata tag "{0}" must be upper case.'.format( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 276 | self.name["meta"] |
| 277 | ), |
| 278 | ) |
Brad Bishop | babf3b7 | 2017-05-31 19:44:53 -0400 | [diff] [blame] | 279 | |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 280 | def factory(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 281 | """Create path and metadata elements.""" |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 282 | |
| 283 | args = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 284 | "class": "pathname", |
| 285 | "pathname": "element", |
| 286 | "name": self.name["path"], |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 287 | } |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 288 | add_unique(ConfigEntry(configfile=self.configfile, **args), objs) |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 289 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 290 | args = {"class": "meta", "meta": "element", "name": self.name["meta"]} |
| 291 | add_unique(ConfigEntry(configfile=self.configfile, **args), objs) |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 292 | |
| 293 | super(Path, self).factory(objs) |
| 294 | |
| 295 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 296 | """Resolve path and metadata names to indices.""" |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 297 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 298 | self.path = get_index(objs, "pathname", self.name["path"]) |
| 299 | self.meta = get_index(objs, "meta", self.name["meta"]) |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 300 | |
| 301 | super(Path, self).setup(objs) |
| 302 | |
| 303 | |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 304 | class Property(ConfigEntry): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 305 | """Property/interface/metadata association.""" |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 306 | |
| 307 | def __init__(self, *a, **kw): |
| 308 | super(Property, self).__init__(**kw) |
| 309 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 310 | if self.name["meta"].upper() != self.name["meta"]: |
Brad Bishop | babf3b7 | 2017-05-31 19:44:53 -0400 | [diff] [blame] | 311 | raise InvalidConfigError( |
| 312 | self.configfile, |
| 313 | 'Metadata tag "{0}" must be upper case.'.format( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 314 | self.name["meta"] |
| 315 | ), |
| 316 | ) |
Brad Bishop | babf3b7 | 2017-05-31 19:44:53 -0400 | [diff] [blame] | 317 | |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 318 | def factory(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 319 | """Create interface, property name and metadata elements.""" |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 320 | |
| 321 | args = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 322 | "class": "interface", |
| 323 | "interface": "element", |
| 324 | "name": self.name["interface"], |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 325 | } |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 326 | add_unique(ConfigEntry(configfile=self.configfile, **args), objs) |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 327 | |
| 328 | args = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 329 | "class": "propertyname", |
| 330 | "propertyname": "element", |
| 331 | "name": self.name["property"], |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 332 | } |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 333 | add_unique(ConfigEntry(configfile=self.configfile, **args), objs) |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 334 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 335 | args = {"class": "meta", "meta": "element", "name": self.name["meta"]} |
| 336 | add_unique(ConfigEntry(configfile=self.configfile, **args), objs) |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 337 | |
| 338 | super(Property, self).factory(objs) |
| 339 | |
| 340 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 341 | """Resolve interface, property and metadata to indices.""" |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 342 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 343 | self.interface = get_index(objs, "interface", self.name["interface"]) |
| 344 | self.prop = get_index(objs, "propertyname", self.name["property"]) |
| 345 | self.meta = get_index(objs, "meta", self.name["meta"]) |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 346 | |
| 347 | super(Property, self).setup(objs) |
| 348 | |
| 349 | |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 350 | class Instance(ConfigEntry): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 351 | """Property/Path association.""" |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 352 | |
| 353 | def __init__(self, *a, **kw): |
| 354 | super(Instance, self).__init__(**kw) |
| 355 | |
| 356 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 357 | """Resolve elements to indices.""" |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 358 | |
| 359 | self.interface = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 360 | objs, "interface", self.name["property"]["interface"] |
| 361 | ) |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 362 | self.prop = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 363 | objs, "propertyname", self.name["property"]["property"] |
| 364 | ) |
| 365 | self.propmeta = get_index(objs, "meta", self.name["property"]["meta"]) |
| 366 | self.path = get_index(objs, "pathname", self.name["path"]["path"]) |
| 367 | self.pathmeta = get_index(objs, "meta", self.name["path"]["meta"]) |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 368 | |
| 369 | super(Instance, self).setup(objs) |
| 370 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 371 | |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 372 | class PathInstance(ConfigEntry): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 373 | """Path association.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 374 | |
| 375 | def __init__(self, *a, **kw): |
| 376 | super(PathInstance, self).__init__(**kw) |
| 377 | |
| 378 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 379 | """Resolve elements to indices.""" |
| 380 | self.path = self.name["path"]["path"] |
| 381 | self.pathmeta = self.name["path"]["meta"] |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 382 | super(PathInstance, self).setup(objs) |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 383 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 384 | |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 385 | class Group(ConfigEntry): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 386 | """Pop the members keyword for groups.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 387 | |
| 388 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 389 | self.members = kw.pop("members") |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 390 | super(Group, self).__init__(**kw) |
| 391 | |
| 392 | |
| 393 | class ImplicitGroup(Group): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 394 | """Provide a factory method for groups whose members are |
| 395 | not explicitly declared in the config files.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 396 | |
| 397 | def __init__(self, *a, **kw): |
| 398 | super(ImplicitGroup, self).__init__(**kw) |
| 399 | |
| 400 | def factory(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 401 | """Create group members.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 402 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 403 | factory = Everything.classmap(self.subclass, "element") |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 404 | for m in self.members: |
| 405 | args = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 406 | "class": self.subclass, |
| 407 | self.subclass: "element", |
| 408 | "name": m, |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | obj = factory(configfile=self.configfile, **args) |
| 412 | add_unique(obj, objs) |
| 413 | obj.factory(objs) |
| 414 | |
| 415 | super(ImplicitGroup, self).factory(objs) |
| 416 | |
| 417 | |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 418 | class GroupOfPaths(ImplicitGroup): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 419 | """Path group config file directive.""" |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 420 | |
| 421 | def __init__(self, *a, **kw): |
| 422 | super(GroupOfPaths, self).__init__(**kw) |
| 423 | |
| 424 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 425 | """Resolve group members.""" |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 426 | |
| 427 | def map_member(x): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 428 | path = get_index(objs, "pathname", x["path"]) |
| 429 | meta = get_index(objs, "meta", x["meta"]) |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 430 | return (path, meta) |
| 431 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 432 | self.members = map(map_member, self.members) |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 433 | |
| 434 | super(GroupOfPaths, self).setup(objs) |
| 435 | |
| 436 | |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 437 | class GroupOfProperties(ImplicitGroup): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 438 | """Property group config file directive.""" |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 439 | |
| 440 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 441 | self.type = kw.pop("type") |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 442 | self.datatype = sdbusplus.property.Property( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 443 | name=kw.get("name"), type=self.type |
| 444 | ).cppTypeName |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 445 | |
| 446 | super(GroupOfProperties, self).__init__(**kw) |
| 447 | |
| 448 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 449 | """Resolve group members.""" |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 450 | |
| 451 | def map_member(x): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 452 | iface = get_index(objs, "interface", x["interface"]) |
| 453 | prop = get_index(objs, "propertyname", x["property"]) |
| 454 | meta = get_index(objs, "meta", x["meta"]) |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 455 | |
| 456 | return (iface, prop, meta) |
| 457 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 458 | self.members = map(map_member, self.members) |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 459 | |
| 460 | super(GroupOfProperties, self).setup(objs) |
| 461 | |
| 462 | |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 463 | class GroupOfInstances(ImplicitGroup): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 464 | """A group of property instances.""" |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 465 | |
| 466 | def __init__(self, *a, **kw): |
| 467 | super(GroupOfInstances, self).__init__(**kw) |
| 468 | |
| 469 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 470 | """Resolve group members.""" |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 471 | |
| 472 | def map_member(x): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 473 | path = get_index(objs, "pathname", x["path"]["path"]) |
| 474 | pathmeta = get_index(objs, "meta", x["path"]["meta"]) |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 475 | interface = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 476 | objs, "interface", x["property"]["interface"] |
| 477 | ) |
| 478 | prop = get_index(objs, "propertyname", x["property"]["property"]) |
| 479 | propmeta = get_index(objs, "meta", x["property"]["meta"]) |
| 480 | instance = get_index(objs, "instance", x) |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 481 | |
| 482 | return (path, pathmeta, interface, prop, propmeta, instance) |
| 483 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 484 | self.members = map(map_member, self.members) |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 485 | |
| 486 | super(GroupOfInstances, self).setup(objs) |
| 487 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 488 | |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 489 | class GroupOfPathInstances(ImplicitGroup): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 490 | """A group of path instances.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 491 | |
| 492 | def __init__(self, *a, **kw): |
| 493 | super(GroupOfPathInstances, self).__init__(**kw) |
| 494 | |
| 495 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 496 | """Resolve group members.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 497 | |
| 498 | def map_member(x): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 499 | path = get_index(objs, "pathname", x["path"]["path"]) |
| 500 | pathmeta = get_index(objs, "meta", x["path"]["meta"]) |
| 501 | pathinstance = get_index(objs, "pathinstance", x) |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 502 | return (path, pathmeta, pathinstance) |
| 503 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 504 | self.members = map(map_member, self.members) |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 505 | |
| 506 | super(GroupOfPathInstances, self).setup(objs) |
| 507 | |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 508 | |
| 509 | class HasPropertyIndex(ConfigEntry): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 510 | """Handle config file directives that require an index to be |
| 511 | constructed.""" |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 512 | |
| 513 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 514 | self.paths = kw.pop("paths") |
| 515 | self.properties = kw.pop("properties") |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 516 | super(HasPropertyIndex, self).__init__(**kw) |
| 517 | |
| 518 | def factory(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 519 | """Create a group of instances for this index.""" |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 520 | |
| 521 | members = [] |
| 522 | path_group = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 523 | objs, "pathgroup", self.paths, config=self.configfile |
| 524 | ) |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 525 | property_group = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 526 | objs, "propertygroup", self.properties, config=self.configfile |
| 527 | ) |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 528 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 529 | for path in objs["pathgroup"][path_group].members: |
| 530 | for prop in objs["propertygroup"][property_group].members: |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 531 | member = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 532 | "path": path, |
| 533 | "property": prop, |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 534 | } |
| 535 | members.append(member) |
| 536 | |
| 537 | args = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 538 | "members": members, |
| 539 | "class": "instancegroup", |
| 540 | "instancegroup": "instance", |
| 541 | "name": "{0} {1}".format(self.paths, self.properties), |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | group = GroupOfInstances(configfile=self.configfile, **args) |
| 545 | add_unique(group, objs, config=self.configfile) |
| 546 | group.factory(objs) |
| 547 | |
| 548 | super(HasPropertyIndex, self).factory(objs) |
| 549 | |
| 550 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 551 | """Resolve path, property, and instance groups.""" |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 552 | |
| 553 | self.instances = get_index( |
| 554 | objs, |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 555 | "instancegroup", |
| 556 | "{0} {1}".format(self.paths, self.properties), |
| 557 | config=self.configfile, |
| 558 | ) |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 559 | self.paths = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 560 | objs, "pathgroup", self.paths, config=self.configfile |
| 561 | ) |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 562 | self.properties = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 563 | objs, "propertygroup", self.properties, config=self.configfile |
| 564 | ) |
| 565 | self.datatype = objs["propertygroup"][self.properties].datatype |
| 566 | self.type = objs["propertygroup"][self.properties].type |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 567 | |
| 568 | super(HasPropertyIndex, self).setup(objs) |
| 569 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 570 | |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 571 | class HasPathIndex(ConfigEntry): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 572 | """Handle config file directives that require an index to be |
| 573 | constructed.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 574 | |
| 575 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 576 | self.paths = kw.pop("paths") |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 577 | super(HasPathIndex, self).__init__(**kw) |
| 578 | |
| 579 | def factory(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 580 | """Create a group of instances for this index.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 581 | |
| 582 | members = [] |
| 583 | path_group = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 584 | objs, "pathgroup", self.paths, config=self.configfile |
| 585 | ) |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 586 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 587 | for path in objs["pathgroup"][path_group].members: |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 588 | member = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 589 | "path": path, |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 590 | } |
| 591 | members.append(member) |
| 592 | |
| 593 | args = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 594 | "members": members, |
| 595 | "class": "pathinstancegroup", |
| 596 | "pathinstancegroup": "pathinstance", |
| 597 | "name": "{0}".format(self.paths), |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | group = GroupOfPathInstances(configfile=self.configfile, **args) |
| 601 | add_unique(group, objs, config=self.configfile) |
| 602 | group.factory(objs) |
| 603 | |
| 604 | super(HasPathIndex, self).factory(objs) |
| 605 | |
| 606 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 607 | """Resolve path and instance groups.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 608 | |
| 609 | self.pathinstances = get_index( |
| 610 | objs, |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 611 | "pathinstancegroup", |
| 612 | "{0}".format(self.paths), |
| 613 | config=self.configfile, |
| 614 | ) |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 615 | self.paths = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 616 | objs, "pathgroup", self.paths, config=self.configfile |
| 617 | ) |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 618 | super(HasPathIndex, self).setup(objs) |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 619 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 620 | |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 621 | class GroupOfFilters(ConfigEntry): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 622 | """Handle config file directives that require an index for filters.""" |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 623 | |
| 624 | def __init__(self, *a, **kw): |
| 625 | # Pop filters data for adding to the available filters array |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 626 | self.type = kw.pop("type") |
| 627 | self.datatype = kw.pop("datatype", None) |
| 628 | self.filters = kw.pop("filters", None) |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 629 | |
| 630 | super(GroupOfFilters, self).__init__(**kw) |
| 631 | |
| 632 | def factory(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 633 | """Modify filters to add the property value type and |
| 634 | make them of operation argument type.""" |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 635 | if self.filters: |
| 636 | # 'type' used within OpArgument to generate filter |
| 637 | # argument values so add to each filter |
| 638 | for f in self.filters: |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 639 | f["type"] = self.type |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 640 | self.filters = [OpArgument(**x) for x in self.filters] |
| 641 | |
| 642 | super(GroupOfFilters, self).factory(objs) |
| 643 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 644 | |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 645 | class PropertyWatch(HasPropertyIndex): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 646 | """Handle the property watch config file directive.""" |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 647 | |
| 648 | def __init__(self, *a, **kw): |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 649 | # Pop optional filters for the properties being watched |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 650 | self.filters = kw.pop("filters", None) |
| 651 | self.callback = kw.pop("callback", None) |
| 652 | self.ignore_start_callback = kw.pop("ignore_start_callback", False) |
| 653 | self.ignore_start_callback = ( |
| 654 | "true" if self.ignore_start_callback else "false" |
| 655 | ) |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 656 | super(PropertyWatch, self).__init__(**kw) |
| 657 | |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 658 | def factory(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 659 | """Create any filters for this property watch.""" |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 660 | |
| 661 | if self.filters: |
| 662 | # Get the datatype(i.e. "int64_t") of the properties in this watch |
| 663 | # (Made available after all `super` classes init'd) |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 664 | datatype = objs["propertygroup"][ |
| 665 | get_index( |
| 666 | objs, |
| 667 | "propertygroup", |
| 668 | self.properties, |
| 669 | config=self.configfile, |
| 670 | ) |
| 671 | ].datatype |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 672 | # Get the type(i.e. "int64") of the properties in this watch |
| 673 | # (Made available after all `super` classes init'd) |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 674 | type = objs["propertygroup"][ |
| 675 | get_index( |
| 676 | objs, |
| 677 | "propertygroup", |
| 678 | self.properties, |
| 679 | config=self.configfile, |
| 680 | ) |
| 681 | ].type |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 682 | # Construct the data needed to make the filters for |
| 683 | # this watch available. |
| 684 | # *Note: 'class', 'subclass', 'name' are required for |
| 685 | # storing the filter data(i.e. 'type', 'datatype', & 'filters') |
| 686 | args = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 687 | "type": type, |
| 688 | "datatype": datatype, |
| 689 | "filters": self.filters, |
| 690 | "class": "filtersgroup", |
| 691 | "filtersgroup": "filters", |
| 692 | "name": self.name, |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 693 | } |
| 694 | # Init GroupOfFilters class with this watch's filters' arguments |
| 695 | group = GroupOfFilters(configfile=self.configfile, **args) |
| 696 | # Store this group of filters so it can be indexed later |
| 697 | add_unique(group, objs, config=self.configfile) |
| 698 | group.factory(objs) |
| 699 | |
| 700 | super(PropertyWatch, self).factory(objs) |
| 701 | |
Brad Bishop | fccdc39 | 2017-05-22 21:11:09 -0400 | [diff] [blame] | 702 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 703 | """Resolve optional filters and callback.""" |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 704 | |
| 705 | if self.filters: |
| 706 | # Watch has filters, provide array index to access them |
| 707 | self.filters = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 708 | objs, "filtersgroup", self.name, config=self.configfile |
| 709 | ) |
Brad Bishop | fccdc39 | 2017-05-22 21:11:09 -0400 | [diff] [blame] | 710 | |
| 711 | if self.callback: |
| 712 | self.callback = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 713 | objs, "callback", self.callback, config=self.configfile |
| 714 | ) |
Brad Bishop | fccdc39 | 2017-05-22 21:11:09 -0400 | [diff] [blame] | 715 | |
| 716 | super(PropertyWatch, self).setup(objs) |
| 717 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 718 | |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 719 | class PathWatch(HasPathIndex): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 720 | """Handle the path watch config file directive.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 721 | |
| 722 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 723 | self.pathcallback = kw.pop("pathcallback", None) |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 724 | super(PathWatch, self).__init__(**kw) |
| 725 | |
| 726 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 727 | """Resolve optional callback.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 728 | if self.pathcallback: |
| 729 | self.pathcallback = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 730 | objs, "pathcallback", self.pathcallback, config=self.configfile |
| 731 | ) |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 732 | super(PathWatch, self).setup(objs) |
| 733 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 734 | |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 735 | class Callback(HasPropertyIndex): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 736 | """Interface and common logic for callbacks.""" |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 737 | |
| 738 | def __init__(self, *a, **kw): |
| 739 | super(Callback, self).__init__(**kw) |
| 740 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 741 | |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 742 | class PathCallback(HasPathIndex): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 743 | """Interface and common logic for callbacks.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 744 | |
| 745 | def __init__(self, *a, **kw): |
| 746 | super(PathCallback, self).__init__(**kw) |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 747 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 748 | |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 749 | class ConditionCallback(ConfigEntry, Renderer): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 750 | """Handle the journal callback config file directive.""" |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 751 | |
| 752 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 753 | self.condition = kw.pop("condition") |
| 754 | self.instance = kw.pop("instance") |
| 755 | self.defer = kw.pop("defer", None) |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 756 | super(ConditionCallback, self).__init__(**kw) |
| 757 | |
| 758 | def factory(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 759 | """Create a graph instance for this callback.""" |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 760 | |
| 761 | args = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 762 | "configfile": self.configfile, |
| 763 | "members": [self.instance], |
| 764 | "class": "callbackgroup", |
| 765 | "callbackgroup": "callback", |
| 766 | "name": [self.instance], |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | entry = CallbackGraphEntry(**args) |
| 770 | add_unique(entry, objs, config=self.configfile) |
| 771 | |
| 772 | super(ConditionCallback, self).factory(objs) |
| 773 | |
| 774 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 775 | """Resolve condition and graph entry.""" |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 776 | |
| 777 | self.graph = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 778 | objs, "callbackgroup", [self.instance], config=self.configfile |
| 779 | ) |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 780 | |
| 781 | self.condition = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 782 | objs, "condition", self.name, config=self.configfile |
| 783 | ) |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 784 | |
| 785 | super(ConditionCallback, self).setup(objs) |
| 786 | |
| 787 | def construct(self, loader, indent): |
| 788 | return self.render( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 789 | loader, "conditional.mako.cpp", c=self, indent=indent |
| 790 | ) |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 791 | |
| 792 | |
| 793 | class Condition(HasPropertyIndex): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 794 | """Interface and common logic for conditions.""" |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 795 | |
| 796 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 797 | self.callback = kw.pop("callback") |
| 798 | self.defer = kw.pop("defer", None) |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 799 | super(Condition, self).__init__(**kw) |
| 800 | |
| 801 | def factory(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 802 | """Create a callback instance for this conditional.""" |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 803 | |
| 804 | args = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 805 | "configfile": self.configfile, |
| 806 | "condition": self.name, |
| 807 | "class": "callback", |
| 808 | "callback": "conditional", |
| 809 | "instance": self.callback, |
| 810 | "name": self.name, |
| 811 | "defer": self.defer, |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | callback = ConditionCallback(**args) |
| 815 | add_unique(callback, objs, config=self.configfile) |
| 816 | callback.factory(objs) |
| 817 | |
| 818 | super(Condition, self).factory(objs) |
| 819 | |
| 820 | |
| 821 | class CountCondition(Condition, Renderer): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 822 | """Handle the count condition config file directive.""" |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 823 | |
| 824 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 825 | self.countop = kw.pop("countop") |
| 826 | self.countbound = kw.pop("countbound") |
| 827 | self.op = kw.pop("op") |
| 828 | self.bound = kw.pop("bound") |
Matt Spinler | c458dee | 2018-02-19 13:09:10 -0600 | [diff] [blame] | 829 | self.oneshot = TrivialArgument( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 830 | type="boolean", value=kw.pop("oneshot", False) |
| 831 | ) |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 832 | super(CountCondition, self).__init__(**kw) |
| 833 | |
Brad Bishop | ec2ed2f | 2017-05-31 21:10:43 -0400 | [diff] [blame] | 834 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 835 | """Resolve type.""" |
Brad Bishop | ec2ed2f | 2017-05-31 21:10:43 -0400 | [diff] [blame] | 836 | |
| 837 | super(CountCondition, self).setup(objs) |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 838 | self.bound = TrivialArgument(type=self.type, value=self.bound) |
Brad Bishop | ec2ed2f | 2017-05-31 21:10:43 -0400 | [diff] [blame] | 839 | |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 840 | def construct(self, loader, indent): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 841 | return self.render(loader, "count.mako.cpp", c=self, indent=indent) |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 842 | |
| 843 | |
Matthew Barth | efdd03c | 2019-09-04 15:44:35 -0500 | [diff] [blame] | 844 | class MedianCondition(Condition, Renderer): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 845 | """Handle the median condition config file directive.""" |
Matthew Barth | efdd03c | 2019-09-04 15:44:35 -0500 | [diff] [blame] | 846 | |
| 847 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 848 | self.op = kw.pop("op") |
| 849 | self.bound = kw.pop("bound") |
Matthew Barth | efdd03c | 2019-09-04 15:44:35 -0500 | [diff] [blame] | 850 | self.oneshot = TrivialArgument( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 851 | type="boolean", value=kw.pop("oneshot", False) |
| 852 | ) |
Matthew Barth | efdd03c | 2019-09-04 15:44:35 -0500 | [diff] [blame] | 853 | super(MedianCondition, self).__init__(**kw) |
| 854 | |
| 855 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 856 | """Resolve type.""" |
Matthew Barth | efdd03c | 2019-09-04 15:44:35 -0500 | [diff] [blame] | 857 | |
| 858 | super(MedianCondition, self).setup(objs) |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 859 | self.bound = TrivialArgument(type=self.type, value=self.bound) |
Matthew Barth | efdd03c | 2019-09-04 15:44:35 -0500 | [diff] [blame] | 860 | |
| 861 | def construct(self, loader, indent): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 862 | return self.render(loader, "median.mako.cpp", c=self, indent=indent) |
Matthew Barth | efdd03c | 2019-09-04 15:44:35 -0500 | [diff] [blame] | 863 | |
| 864 | |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 865 | class Journal(Callback, Renderer): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 866 | """Handle the journal callback config file directive.""" |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 867 | |
| 868 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 869 | self.severity = kw.pop("severity") |
| 870 | self.message = kw.pop("message") |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 871 | super(Journal, self).__init__(**kw) |
| 872 | |
| 873 | def construct(self, loader, indent): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 874 | return self.render(loader, "journal.mako.cpp", c=self, indent=indent) |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 875 | |
| 876 | |
Gunnar Mills | d5faea3 | 2017-08-08 14:19:36 -0500 | [diff] [blame] | 877 | class Elog(Callback, Renderer): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 878 | """Handle the elog callback config file directive.""" |
Gunnar Mills | d5faea3 | 2017-08-08 14:19:36 -0500 | [diff] [blame] | 879 | |
| 880 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 881 | self.error = kw.pop("error") |
| 882 | self.metadata = [Metadata(**x) for x in kw.pop("metadata", {})] |
Gunnar Mills | d5faea3 | 2017-08-08 14:19:36 -0500 | [diff] [blame] | 883 | super(Elog, self).__init__(**kw) |
| 884 | |
| 885 | def construct(self, loader, indent): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 886 | with open(args.gen_errors, "a") as fd: |
| 887 | fd.write(self.render(loader, "errors.mako.hpp", c=self)) |
| 888 | return self.render(loader, "elog.mako.cpp", c=self, indent=indent) |
| 889 | |
Gunnar Mills | d5faea3 | 2017-08-08 14:19:36 -0500 | [diff] [blame] | 890 | |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 891 | class Event(Callback, Renderer): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 892 | """Handle the event callback config file directive.""" |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 893 | |
| 894 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 895 | self.eventName = kw.pop("eventName") |
| 896 | self.eventMessage = kw.pop("eventMessage") |
Ratan Gupta | 90bfaea | 2017-10-06 20:56:31 +0530 | [diff] [blame] | 897 | super(Event, self).__init__(**kw) |
| 898 | |
| 899 | def construct(self, loader, indent): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 900 | return self.render(loader, "event.mako.cpp", c=self, indent=indent) |
| 901 | |
Gunnar Mills | d5faea3 | 2017-08-08 14:19:36 -0500 | [diff] [blame] | 902 | |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 903 | class EventPath(PathCallback, Renderer): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 904 | """Handle the event path callback config file directive.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 905 | |
| 906 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 907 | self.eventType = kw.pop("eventType") |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 908 | super(EventPath, self).__init__(**kw) |
| 909 | |
| 910 | def construct(self, loader, indent): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 911 | return self.render(loader, "eventpath.mako.cpp", c=self, indent=indent) |
| 912 | |
Matt Spinler | 3c5318d | 2018-02-19 14:03:05 -0600 | [diff] [blame] | 913 | |
| 914 | class ElogWithMetadata(Callback, Renderer): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 915 | """Handle the elog_with_metadata callback config file directive.""" |
Matt Spinler | 3c5318d | 2018-02-19 14:03:05 -0600 | [diff] [blame] | 916 | |
| 917 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 918 | self.error = kw.pop("error") |
| 919 | self.metadata = kw.pop("metadata") |
Matt Spinler | 3c5318d | 2018-02-19 14:03:05 -0600 | [diff] [blame] | 920 | super(ElogWithMetadata, self).__init__(**kw) |
| 921 | |
| 922 | def construct(self, loader, indent): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 923 | with open(args.gen_errors, "a") as fd: |
| 924 | fd.write(self.render(loader, "errors.mako.hpp", c=self)) |
Matt Spinler | 3c5318d | 2018-02-19 14:03:05 -0600 | [diff] [blame] | 925 | return self.render( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 926 | loader, "elog_with_metadata.mako.cpp", c=self, indent=indent |
| 927 | ) |
Matt Spinler | 3c5318d | 2018-02-19 14:03:05 -0600 | [diff] [blame] | 928 | |
| 929 | |
Matt Spinler | 1d6ca48 | 2017-11-01 10:48:02 -0500 | [diff] [blame] | 930 | class ResolveCallout(Callback, Renderer): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 931 | """Handle the 'resolve callout' callback config file directive.""" |
Matt Spinler | 1d6ca48 | 2017-11-01 10:48:02 -0500 | [diff] [blame] | 932 | |
| 933 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 934 | self.callout = kw.pop("callout") |
Matt Spinler | 1d6ca48 | 2017-11-01 10:48:02 -0500 | [diff] [blame] | 935 | super(ResolveCallout, self).__init__(**kw) |
| 936 | |
| 937 | def construct(self, loader, indent): |
| 938 | return self.render( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 939 | loader, "resolve_errors.mako.cpp", c=self, indent=indent |
| 940 | ) |
Matt Spinler | 1d6ca48 | 2017-11-01 10:48:02 -0500 | [diff] [blame] | 941 | |
| 942 | |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 943 | class Method(ConfigEntry, Renderer): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 944 | """Handle the method callback config file directive.""" |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 945 | |
| 946 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 947 | self.service = kw.pop("service") |
| 948 | self.path = kw.pop("path") |
| 949 | self.interface = kw.pop("interface") |
| 950 | self.method = kw.pop("method") |
| 951 | self.args = [TrivialArgument(**x) for x in kw.pop("args", {})] |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 952 | super(Method, self).__init__(**kw) |
| 953 | |
| 954 | def factory(self, objs): |
| 955 | args = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 956 | "class": "interface", |
| 957 | "interface": "element", |
| 958 | "name": self.service, |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 959 | } |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 960 | add_unique(ConfigEntry(configfile=self.configfile, **args), objs) |
| 961 | |
| 962 | args = {"class": "pathname", "pathname": "element", "name": self.path} |
| 963 | add_unique(ConfigEntry(configfile=self.configfile, **args), objs) |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 964 | |
| 965 | args = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 966 | "class": "interface", |
| 967 | "interface": "element", |
| 968 | "name": self.interface, |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 969 | } |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 970 | add_unique(ConfigEntry(configfile=self.configfile, **args), objs) |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 971 | |
| 972 | args = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 973 | "class": "propertyname", |
| 974 | "propertyname": "element", |
| 975 | "name": self.method, |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 976 | } |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 977 | add_unique(ConfigEntry(configfile=self.configfile, **args), objs) |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 978 | |
| 979 | super(Method, self).factory(objs) |
| 980 | |
| 981 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 982 | """Resolve elements.""" |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 983 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 984 | self.service = get_index(objs, "interface", self.service) |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 985 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 986 | self.path = get_index(objs, "pathname", self.path) |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 987 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 988 | self.interface = get_index(objs, "interface", self.interface) |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 989 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 990 | self.method = get_index(objs, "propertyname", self.method) |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 991 | |
| 992 | super(Method, self).setup(objs) |
| 993 | |
| 994 | def construct(self, loader, indent): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 995 | return self.render(loader, "method.mako.cpp", c=self, indent=indent) |
Brad Bishop | 0df00be | 2017-05-25 23:38:37 -0400 | [diff] [blame] | 996 | |
| 997 | |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 998 | class CallbackGraphEntry(Group): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 999 | """An entry in a traversal list for groups of callbacks.""" |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1000 | |
| 1001 | def __init__(self, *a, **kw): |
| 1002 | super(CallbackGraphEntry, self).__init__(**kw) |
| 1003 | |
| 1004 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1005 | """Resolve group members.""" |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1006 | |
| 1007 | def map_member(x): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1008 | return get_index(objs, "callback", x, config=self.configfile) |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1009 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1010 | self.members = map(map_member, self.members) |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1011 | |
| 1012 | super(CallbackGraphEntry, self).setup(objs) |
| 1013 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1014 | |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1015 | class PathCallbackGraphEntry(Group): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1016 | """An entry in a traversal list for groups of callbacks.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1017 | |
| 1018 | def __init__(self, *a, **kw): |
| 1019 | super(PathCallbackGraphEntry, self).__init__(**kw) |
| 1020 | |
| 1021 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1022 | """Resolve group members.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1023 | |
| 1024 | def map_member(x): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1025 | return get_index(objs, "pathcallback", x, config=self.configfile) |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1026 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1027 | self.members = map(map_member, self.members) |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1028 | |
| 1029 | super(PathCallbackGraphEntry, self).setup(objs) |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1030 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1031 | |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1032 | class GroupOfCallbacks(ConfigEntry, Renderer): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1033 | """Handle the callback group config file directive.""" |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1034 | |
| 1035 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1036 | self.members = kw.pop("members") |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1037 | super(GroupOfCallbacks, self).__init__(**kw) |
| 1038 | |
| 1039 | def factory(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1040 | """Create a graph instance for this group of callbacks.""" |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1041 | |
| 1042 | args = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1043 | "configfile": self.configfile, |
| 1044 | "members": self.members, |
| 1045 | "class": "callbackgroup", |
| 1046 | "callbackgroup": "callback", |
| 1047 | "name": self.members, |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | entry = CallbackGraphEntry(**args) |
| 1051 | add_unique(entry, objs, config=self.configfile) |
| 1052 | |
| 1053 | super(GroupOfCallbacks, self).factory(objs) |
| 1054 | |
| 1055 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1056 | """Resolve graph entry.""" |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1057 | |
| 1058 | self.graph = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1059 | objs, "callbackgroup", self.members, config=self.configfile |
| 1060 | ) |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1061 | |
| 1062 | super(GroupOfCallbacks, self).setup(objs) |
| 1063 | |
| 1064 | def construct(self, loader, indent): |
| 1065 | return self.render( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1066 | loader, "callbackgroup.mako.cpp", c=self, indent=indent |
| 1067 | ) |
| 1068 | |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1069 | |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1070 | class GroupOfPathCallbacks(ConfigEntry, Renderer): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1071 | """Handle the callback group config file directive.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1072 | |
| 1073 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1074 | self.members = kw.pop("members") |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1075 | super(GroupOfPathCallbacks, self).__init__(**kw) |
| 1076 | |
| 1077 | def factory(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1078 | """Create a graph instance for this group of callbacks.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1079 | |
| 1080 | args = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1081 | "configfile": self.configfile, |
| 1082 | "members": self.members, |
| 1083 | "class": "pathcallbackgroup", |
| 1084 | "pathcallbackgroup": "pathcallback", |
| 1085 | "name": self.members, |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | entry = PathCallbackGraphEntry(**args) |
| 1089 | add_unique(entry, objs, config=self.configfile) |
| 1090 | super(GroupOfPathCallbacks, self).factory(objs) |
| 1091 | |
| 1092 | def setup(self, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1093 | """Resolve graph entry.""" |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1094 | |
| 1095 | self.graph = get_index( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1096 | objs, "callbackpathgroup", self.members, config=self.configfile |
| 1097 | ) |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1098 | |
| 1099 | super(GroupOfPathCallbacks, self).setup(objs) |
| 1100 | |
| 1101 | def construct(self, loader, indent): |
| 1102 | return self.render( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1103 | loader, "callbackpathgroup.mako.cpp", c=self, indent=indent |
| 1104 | ) |
| 1105 | |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1106 | |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1107 | class Everything(Renderer): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1108 | """Parse/render entry point.""" |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1109 | |
| 1110 | @staticmethod |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1111 | def classmap(cls, sub=None): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1112 | """Map render item class and subclass entries to the appropriate |
| 1113 | handler methods.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1114 | class_map = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1115 | "path": { |
| 1116 | "element": Path, |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 1117 | }, |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1118 | "pathgroup": { |
| 1119 | "path": GroupOfPaths, |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 1120 | }, |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1121 | "propertygroup": { |
| 1122 | "property": GroupOfProperties, |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 1123 | }, |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1124 | "property": { |
| 1125 | "element": Property, |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 1126 | }, |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1127 | "watch": { |
| 1128 | "property": PropertyWatch, |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 1129 | }, |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1130 | "pathwatch": { |
| 1131 | "path": PathWatch, |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1132 | }, |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1133 | "instance": { |
| 1134 | "element": Instance, |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 1135 | }, |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1136 | "pathinstance": { |
| 1137 | "element": PathInstance, |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1138 | }, |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1139 | "callback": { |
| 1140 | "journal": Journal, |
| 1141 | "elog": Elog, |
| 1142 | "elog_with_metadata": ElogWithMetadata, |
| 1143 | "event": Event, |
| 1144 | "group": GroupOfCallbacks, |
| 1145 | "method": Method, |
| 1146 | "resolve callout": ResolveCallout, |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 1147 | }, |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1148 | "pathcallback": { |
| 1149 | "eventpath": EventPath, |
| 1150 | "grouppath": GroupOfPathCallbacks, |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1151 | }, |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1152 | "condition": { |
| 1153 | "count": CountCondition, |
| 1154 | "median": MedianCondition, |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 1155 | }, |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | if cls not in class_map: |
| 1159 | raise NotImplementedError('Unknown class: "{0}"'.format(cls)) |
| 1160 | if sub not in class_map[cls]: |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1161 | raise NotImplementedError( |
| 1162 | 'Unknown {0} type: "{1}"'.format(cls, sub) |
| 1163 | ) |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1164 | |
| 1165 | return class_map[cls][sub] |
| 1166 | |
| 1167 | @staticmethod |
| 1168 | def load_one_yaml(path, fd, objs): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1169 | """Parse a single YAML file. Parsing occurs in three phases. |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1170 | In the first phase a factory method associated with each |
| 1171 | configuration file directive is invoked. These factory |
| 1172 | methods generate more factory methods. In the second |
| 1173 | phase the factory methods created in the first phase |
| 1174 | are invoked. In the last phase a callback is invoked on |
| 1175 | each object created in phase two. Typically the callback |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1176 | resolves references to other configuration file directives.""" |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1177 | |
| 1178 | factory_objs = {} |
| 1179 | for x in yaml.safe_load(fd.read()) or {}: |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1180 | # Create factory object for this config file directive. |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1181 | cls = x["class"] |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1182 | sub = x.get(cls) |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1183 | if cls == "group": |
| 1184 | cls = "{0}group".format(sub) |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1185 | |
| 1186 | factory = Everything.classmap(cls, sub) |
| 1187 | obj = factory(configfile=path, **x) |
| 1188 | |
| 1189 | # For a given class of directive, validate the file |
| 1190 | # doesn't have any duplicate names (duplicates are |
| 1191 | # ok across config files). |
| 1192 | if exists(factory_objs, obj.cls, obj.name, config=path): |
| 1193 | raise NotUniqueError(path, cls, obj.name) |
| 1194 | |
| 1195 | factory_objs.setdefault(cls, []).append(obj) |
| 1196 | objs.setdefault(cls, []).append(obj) |
| 1197 | |
| 1198 | for cls, items in factory_objs.items(): |
| 1199 | for obj in items: |
| 1200 | # Add objects for template consumption. |
| 1201 | obj.factory(objs) |
| 1202 | |
| 1203 | @staticmethod |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1204 | def load(args): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1205 | """Aggregate all the YAML in the input directory |
| 1206 | into a single aggregate.""" |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1207 | |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1208 | objs = {} |
| 1209 | yaml_files = filter( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1210 | lambda x: x.endswith(".yaml"), os.listdir(args.inputdir) |
| 1211 | ) |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1212 | |
Marri Devender Rao | 44fd7e8 | 2020-03-08 09:51:34 -0500 | [diff] [blame] | 1213 | for x in sorted(yaml_files): |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1214 | path = os.path.join(args.inputdir, x) |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1215 | with open(path, "r") as fd: |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1216 | Everything.load_one_yaml(path, fd, objs) |
| 1217 | |
| 1218 | # Configuration file directives reference each other via |
| 1219 | # the name attribute; however, when rendered the reference |
| 1220 | # is just an array index. |
| 1221 | # |
| 1222 | # At this point all objects have been created but references |
Gunnar Mills | 78199b4 | 2017-10-25 16:30:18 -0500 | [diff] [blame] | 1223 | # have not been resolved to array indices. Instruct objects |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1224 | # to do that now. |
| 1225 | for cls, items in objs.items(): |
| 1226 | for obj in items: |
| 1227 | obj.setup(objs) |
| 1228 | |
| 1229 | return Everything(**objs) |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1230 | |
| 1231 | def __init__(self, *a, **kw): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1232 | self.pathmeta = kw.pop("path", []) |
| 1233 | self.paths = kw.pop("pathname", []) |
| 1234 | self.meta = kw.pop("meta", []) |
| 1235 | self.pathgroups = kw.pop("pathgroup", []) |
| 1236 | self.interfaces = kw.pop("interface", []) |
| 1237 | self.properties = kw.pop("property", []) |
| 1238 | self.propertynames = kw.pop("propertyname", []) |
| 1239 | self.propertygroups = kw.pop("propertygroup", []) |
| 1240 | self.instances = kw.pop("instance", []) |
| 1241 | self.pathinstances = kw.pop("pathinstance", []) |
| 1242 | self.instancegroups = kw.pop("instancegroup", []) |
| 1243 | self.pathinstancegroups = kw.pop("pathinstancegroup", []) |
| 1244 | self.watches = kw.pop("watch", []) |
| 1245 | self.pathwatches = kw.pop("pathwatch", []) |
| 1246 | self.callbacks = kw.pop("callback", []) |
| 1247 | self.pathcallbacks = kw.pop("pathcallback", []) |
| 1248 | self.callbackgroups = kw.pop("callbackgroup", []) |
| 1249 | self.pathcallbackgroups = kw.pop("pathcallbackgroup", []) |
| 1250 | self.conditions = kw.pop("condition", []) |
| 1251 | self.filters = kw.pop("filtersgroup", []) |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 1252 | |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1253 | super(Everything, self).__init__(**kw) |
| 1254 | |
| 1255 | def generate_cpp(self, loader): |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1256 | """Render the template with the provided data.""" |
Gunnar Mills | d5faea3 | 2017-08-08 14:19:36 -0500 | [diff] [blame] | 1257 | # errors.hpp is used by generated.hpp to included any error.hpp files |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1258 | open(args.gen_errors, "w+") |
Gunnar Mills | d5faea3 | 2017-08-08 14:19:36 -0500 | [diff] [blame] | 1259 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1260 | with open(args.output, "w") as fd: |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1261 | fd.write( |
| 1262 | self.render( |
| 1263 | loader, |
Brad Bishop | e3a01af | 2017-05-15 17:09:04 -0400 | [diff] [blame] | 1264 | args.template, |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 1265 | meta=self.meta, |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 1266 | properties=self.properties, |
| 1267 | propertynames=self.propertynames, |
| 1268 | interfaces=self.interfaces, |
Brad Bishop | 0e7df13 | 2017-05-23 17:58:12 -0400 | [diff] [blame] | 1269 | paths=self.paths, |
| 1270 | pathmeta=self.pathmeta, |
| 1271 | pathgroups=self.pathgroups, |
Brad Bishop | e73b2c3 | 2017-05-23 18:01:54 -0400 | [diff] [blame] | 1272 | propertygroups=self.propertygroups, |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 1273 | instances=self.instances, |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1274 | pathinstances=self.pathinstances, |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 1275 | watches=self.watches, |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1276 | pathwatches=self.pathwatches, |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 1277 | instancegroups=self.instancegroups, |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1278 | pathinstancegroups=self.pathinstancegroups, |
Brad Bishop | c1283ae | 2017-05-20 21:42:38 -0400 | [diff] [blame] | 1279 | callbacks=self.callbacks, |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1280 | pathcallbacks=self.pathcallbacks, |
Brad Bishop | 49e6617 | 2017-05-23 19:16:21 -0400 | [diff] [blame] | 1281 | callbackgroups=self.callbackgroups, |
Marri Devender Rao | 80c7061 | 2018-04-12 09:22:55 -0500 | [diff] [blame] | 1282 | pathcallbackgroups=self.pathcallbackgroups, |
Brad Bishop | 4041d72 | 2017-05-21 10:06:07 -0400 | [diff] [blame] | 1283 | conditions=self.conditions, |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 1284 | filters=self.filters, |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1285 | indent=Indent(), |
| 1286 | ) |
| 1287 | ) |
Matthew Barth | db440d4 | 2017-04-17 15:49:37 -0500 | [diff] [blame] | 1288 | |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1289 | |
| 1290 | if __name__ == "__main__": |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1291 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 1292 | valid_commands = { |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1293 | "generate-cpp": "generate_cpp", |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | parser = ArgumentParser( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1297 | description=( |
| 1298 | "Phosphor DBus Monitor (PDM) YAML scanner and code generator." |
| 1299 | ) |
| 1300 | ) |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1301 | |
Matthew Barth | db440d4 | 2017-04-17 15:49:37 -0500 | [diff] [blame] | 1302 | parser.add_argument( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1303 | "-o", |
| 1304 | "--out", |
| 1305 | dest="output", |
| 1306 | default="generated.cpp", |
| 1307 | help="Generated output file name and path.", |
| 1308 | ) |
Brad Bishop | e3a01af | 2017-05-15 17:09:04 -0400 | [diff] [blame] | 1309 | parser.add_argument( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1310 | "-t", |
| 1311 | "--template", |
| 1312 | dest="template", |
| 1313 | default="generated.mako.hpp", |
| 1314 | help="The top level template to render.", |
| 1315 | ) |
Brad Bishop | e3a01af | 2017-05-15 17:09:04 -0400 | [diff] [blame] | 1316 | parser.add_argument( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1317 | "-e", |
| 1318 | "--errors", |
| 1319 | dest="gen_errors", |
| 1320 | default="errors.hpp", |
| 1321 | help="Generated errors.hpp output filename.", |
| 1322 | ) |
Matt Johnston | 04267b4 | 2022-08-04 15:05:11 +0800 | [diff] [blame] | 1323 | parser.add_argument( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1324 | "-p", |
| 1325 | "--template-path", |
| 1326 | dest="template_search", |
Brad Bishop | e3a01af | 2017-05-15 17:09:04 -0400 | [diff] [blame] | 1327 | default=script_dir, |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1328 | help="The space delimited mako template search path.", |
| 1329 | ) |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1330 | parser.add_argument( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1331 | "-d", |
| 1332 | "--dir", |
| 1333 | dest="inputdir", |
| 1334 | default=os.path.join(script_dir, "example"), |
| 1335 | help="Location of files to process.", |
| 1336 | ) |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1337 | parser.add_argument( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1338 | "command", |
| 1339 | metavar="COMMAND", |
| 1340 | type=str, |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1341 | choices=valid_commands.keys(), |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1342 | help="%s." % " | ".join(valid_commands.keys()), |
| 1343 | ) |
Matthew Barth | db440d4 | 2017-04-17 15:49:37 -0500 | [diff] [blame] | 1344 | |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1345 | args = parser.parse_args() |
| 1346 | |
| 1347 | if sys.version_info < (3, 0): |
| 1348 | lookup = mako.lookup.TemplateLookup( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1349 | directories=args.template_search.split(), disable_unicode=True |
| 1350 | ) |
Brad Bishop | 34a7acd | 2017-04-27 23:47:23 -0400 | [diff] [blame] | 1351 | else: |
| 1352 | lookup = mako.lookup.TemplateLookup( |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1353 | directories=args.template_search.split() |
| 1354 | ) |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1355 | try: |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1356 | function = getattr(Everything.load(args), valid_commands[args.command]) |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1357 | function(lookup) |
| 1358 | except InvalidConfigError as e: |
Patrick Williams | a1709e4 | 2022-12-05 10:43:55 -0600 | [diff] [blame] | 1359 | sys.stdout.write("{0}: {1}\n\n".format(e.config, e.msg)) |
Brad Bishop | 05b0c1e | 2017-05-23 00:24:01 -0400 | [diff] [blame] | 1360 | raise |