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