Matthew Barth | f24d774 | 2020-03-17 16:12:15 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 2 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 3 | """ |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 4 | Phosphor Fan Presence (PFP) YAML parser and code generator. |
| 5 | |
| 6 | Parse the provided PFP configuration file and generate C++ code. |
| 7 | |
| 8 | The parser workflow is broken down as follows: |
| 9 | 1 - Import the YAML configuration file as native python type(s) |
| 10 | instance(s). |
| 11 | 2 - Create an instance of the Everything class from the |
| 12 | native python type instance(s) with the Everything.load |
| 13 | method. |
| 14 | 3 - The Everything class constructor orchestrates conversion of the |
| 15 | native python type(s) instances(s) to render helper types. |
| 16 | Each render helper type constructor imports its attributes |
| 17 | from the native python type(s) instances(s). |
| 18 | 4 - Present the converted YAML to the command processing method |
| 19 | requested by the script user. |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 20 | """ |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 21 | |
| 22 | import os |
| 23 | import sys |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 24 | from argparse import ArgumentParser |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 25 | |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 26 | import mako.lookup |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 27 | import yaml |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 28 | from sdbusplus.namedelement import NamedElement |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 29 | from sdbusplus.renderer import Renderer |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 30 | |
| 31 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 32 | class InvalidConfigError(Exception): |
| 33 | """General purpose config file parsing error.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 34 | |
| 35 | def __init__(self, path, msg): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 36 | """Display configuration file with the syntax |
| 37 | error and the error message.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 38 | |
| 39 | self.config = path |
| 40 | self.msg = msg |
| 41 | |
| 42 | |
| 43 | class NotUniqueError(InvalidConfigError): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 44 | """Within a config file names must be unique. |
| 45 | Display the duplicate item.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 46 | |
| 47 | def __init__(self, path, cls, *names): |
| 48 | fmt = 'Duplicate {0}: "{1}"' |
| 49 | super(NotUniqueError, self).__init__( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 50 | path, fmt.format(cls, " ".join(names)) |
| 51 | ) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 52 | |
| 53 | |
| 54 | def get_index(objs, cls, name): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 55 | """Items are usually rendered as C++ arrays and as |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 56 | such are stored in python lists. Given an item name |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 57 | its class, find the item index.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 58 | |
| 59 | for i, x in enumerate(objs.get(cls, [])): |
| 60 | if x.name != name: |
| 61 | continue |
| 62 | |
| 63 | return i |
| 64 | raise InvalidConfigError('Could not find name: "{0}"'.format(name)) |
| 65 | |
| 66 | |
| 67 | def exists(objs, cls, name): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 68 | """Check to see if an item already exists in a list given |
| 69 | the item name.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 70 | |
| 71 | try: |
| 72 | get_index(objs, cls, name) |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 73 | except Exception: |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 74 | return False |
| 75 | |
| 76 | return True |
| 77 | |
| 78 | |
| 79 | def add_unique(obj, *a, **kw): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 80 | """Add an item to one or more lists unless already present.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 81 | |
| 82 | for container in a: |
| 83 | if not exists(container, obj.cls, obj.name): |
| 84 | container.setdefault(obj.cls, []).append(obj) |
| 85 | |
| 86 | |
| 87 | class Indent(object): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 88 | """Help templates be depth agnostic.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 89 | |
| 90 | def __init__(self, depth=0): |
| 91 | self.depth = depth |
| 92 | |
| 93 | def __add__(self, depth): |
| 94 | return Indent(self.depth + depth) |
| 95 | |
| 96 | def __call__(self, depth): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 97 | """Render an indent at the current depth plus depth.""" |
| 98 | return 4 * " " * (depth + self.depth) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 99 | |
| 100 | |
| 101 | class ConfigEntry(NamedElement): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 102 | """Base interface for rendered items.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 103 | |
| 104 | def __init__(self, *a, **kw): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 105 | """Pop the class keyword.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 106 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 107 | self.cls = kw.pop("class") |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 108 | super(ConfigEntry, self).__init__(**kw) |
| 109 | |
| 110 | def factory(self, objs): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 111 | """Optional factory interface for subclasses to add |
| 112 | additional items to be rendered.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 113 | |
| 114 | pass |
| 115 | |
| 116 | def setup(self, objs): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 117 | """Optional setup interface for subclasses, invoked |
| 118 | after all factory methods have been run.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 119 | |
| 120 | pass |
| 121 | |
| 122 | |
| 123 | class Sensor(ConfigEntry): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 124 | """Convenience type for config file method:type handlers.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 125 | |
| 126 | def __init__(self, *a, **kw): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 127 | kw["class"] = "sensor" |
| 128 | kw.pop("type") |
| 129 | self.policy = kw.pop("policy") |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 130 | super(Sensor, self).__init__(**kw) |
| 131 | |
| 132 | def setup(self, objs): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 133 | """All sensors have an associated policy. Get the policy index.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 134 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 135 | self.policy = get_index(objs, "policy", self.policy) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 136 | |
| 137 | |
| 138 | class Gpio(Sensor, Renderer): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 139 | """Handler for method:type:gpio.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 140 | |
| 141 | def __init__(self, *a, **kw): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 142 | self.key = kw.pop("key") |
| 143 | self.physpath = kw.pop("physpath") |
| 144 | self.devpath = kw.pop("devpath") |
| 145 | kw["name"] = "gpio-{}".format(self.key) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 146 | super(Gpio, self).__init__(**kw) |
| 147 | |
| 148 | def construct(self, loader, indent): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 149 | return self.render(loader, "gpio.mako.hpp", g=self, indent=indent) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 150 | |
| 151 | def setup(self, objs): |
| 152 | super(Gpio, self).setup(objs) |
| 153 | |
| 154 | |
| 155 | class Tach(Sensor, Renderer): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 156 | """Handler for method:type:tach.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 157 | |
| 158 | def __init__(self, *a, **kw): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 159 | self.sensors = kw.pop("sensors") |
| 160 | kw["name"] = "tach-{}".format("-".join(self.sensors)) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 161 | super(Tach, self).__init__(**kw) |
| 162 | |
| 163 | def construct(self, loader, indent): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 164 | return self.render(loader, "tach.mako.hpp", t=self, indent=indent) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 165 | |
| 166 | def setup(self, objs): |
| 167 | super(Tach, self).setup(objs) |
| 168 | |
| 169 | |
| 170 | class Rpolicy(ConfigEntry): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 171 | """Convenience type for config file rpolicy:type handlers.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 172 | |
| 173 | def __init__(self, *a, **kw): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 174 | kw.pop("type", None) |
| 175 | self.fan = kw.pop("fan") |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 176 | self.sensors = [] |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 177 | kw["class"] = "policy" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 178 | super(Rpolicy, self).__init__(**kw) |
| 179 | |
| 180 | def setup(self, objs): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 181 | """All policies have an associated fan and methods. |
| 182 | Resolve the indices.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 183 | |
| 184 | sensors = [] |
| 185 | for s in self.sensors: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 186 | sensors.append(get_index(objs, "sensor", s)) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 187 | |
| 188 | self.sensors = sensors |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 189 | self.fan = get_index(objs, "fan", self.fan) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 190 | |
| 191 | |
Brad Bishop | fcbedca | 2017-07-25 19:59:46 -0400 | [diff] [blame] | 192 | class AnyOf(Rpolicy, Renderer): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 193 | """Default policy handler (policy:type:anyof).""" |
Brad Bishop | fcbedca | 2017-07-25 19:59:46 -0400 | [diff] [blame] | 194 | |
| 195 | def __init__(self, *a, **kw): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 196 | kw["name"] = "anyof-{}".format(kw["fan"]) |
Brad Bishop | fcbedca | 2017-07-25 19:59:46 -0400 | [diff] [blame] | 197 | super(AnyOf, self).__init__(**kw) |
| 198 | |
| 199 | def setup(self, objs): |
| 200 | super(AnyOf, self).setup(objs) |
| 201 | |
| 202 | def construct(self, loader, indent): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 203 | return self.render(loader, "anyof.mako.hpp", f=self, indent=indent) |
Brad Bishop | fcbedca | 2017-07-25 19:59:46 -0400 | [diff] [blame] | 204 | |
| 205 | |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 206 | class Fallback(Rpolicy, Renderer): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 207 | """Fallback policy handler (policy:type:fallback).""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 208 | |
| 209 | def __init__(self, *a, **kw): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 210 | kw["name"] = "fallback-{}".format(kw["fan"]) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 211 | super(Fallback, self).__init__(**kw) |
| 212 | |
| 213 | def setup(self, objs): |
| 214 | super(Fallback, self).setup(objs) |
| 215 | |
| 216 | def construct(self, loader, indent): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 217 | return self.render(loader, "fallback.mako.hpp", f=self, indent=indent) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 218 | |
| 219 | |
| 220 | class Fan(ConfigEntry): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 221 | """Fan directive handler. Fans entries consist of an inventory path, |
| 222 | optional redundancy policy and associated sensors.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 223 | |
| 224 | def __init__(self, *a, **kw): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 225 | self.path = kw.pop("path") |
| 226 | self.methods = kw.pop("methods") |
| 227 | self.rpolicy = kw.pop("rpolicy", None) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 228 | super(Fan, self).__init__(**kw) |
| 229 | |
| 230 | def factory(self, objs): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 231 | """Create rpolicy and sensor(s) objects.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 232 | |
| 233 | if self.rpolicy: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 234 | self.rpolicy["fan"] = self.name |
| 235 | factory = Everything.classmap(self.rpolicy["type"]) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 236 | rpolicy = factory(**self.rpolicy) |
| 237 | else: |
Brad Bishop | fcbedca | 2017-07-25 19:59:46 -0400 | [diff] [blame] | 238 | rpolicy = AnyOf(fan=self.name) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 239 | |
| 240 | for m in self.methods: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 241 | m["policy"] = rpolicy.name |
| 242 | factory = Everything.classmap(m["type"]) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 243 | sensor = factory(**m) |
| 244 | rpolicy.sensors.append(sensor.name) |
| 245 | add_unique(sensor, objs) |
| 246 | |
| 247 | add_unique(rpolicy, objs) |
| 248 | super(Fan, self).factory(objs) |
| 249 | |
| 250 | |
| 251 | class Everything(Renderer): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 252 | """Parse/render entry point.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 253 | |
| 254 | @staticmethod |
| 255 | def classmap(cls): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 256 | """Map render item class entries to the appropriate |
| 257 | handler methods.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 258 | |
| 259 | class_map = { |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 260 | "anyof": AnyOf, |
| 261 | "fan": Fan, |
| 262 | "fallback": Fallback, |
| 263 | "gpio": Gpio, |
| 264 | "tach": Tach, |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | if cls not in class_map: |
| 268 | raise NotImplementedError('Unknown class: "{0}"'.format(cls)) |
| 269 | |
| 270 | return class_map[cls] |
| 271 | |
| 272 | @staticmethod |
| 273 | def load(args): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 274 | """Load the configuration file. Parsing occurs in three phases. |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 275 | In the first phase a factory method associated with each |
| 276 | configuration file directive is invoked. These factory |
| 277 | methods generate more factory methods. In the second |
| 278 | phase the factory methods created in the first phase |
| 279 | are invoked. In the last phase a callback is invoked on |
| 280 | each object created in phase two. Typically the callback |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 281 | resolves references to other configuration file directives.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 282 | |
| 283 | factory_objs = {} |
| 284 | objs = {} |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 285 | with open(args.input, "r") as fd: |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 286 | for x in yaml.safe_load(fd.read()) or {}: |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 287 | # The top level elements all represent fans. |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 288 | x["class"] = "fan" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 289 | # Create factory object for this config file directive. |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 290 | factory = Everything.classmap(x["class"]) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 291 | obj = factory(**x) |
| 292 | |
| 293 | # For a given class of directive, validate the file |
| 294 | # doesn't have any duplicate names. |
| 295 | if exists(factory_objs, obj.cls, obj.name): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 296 | raise NotUniqueError(args.input, "fan", obj.name) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 297 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 298 | factory_objs.setdefault("fan", []).append(obj) |
| 299 | objs.setdefault("fan", []).append(obj) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 300 | |
Matthew Barth | 9dc3e0d | 2020-02-13 13:02:27 -0600 | [diff] [blame] | 301 | for cls, items in list(factory_objs.items()): |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 302 | for obj in items: |
| 303 | # Add objects for template consumption. |
| 304 | obj.factory(objs) |
| 305 | |
| 306 | # Configuration file directives reference each other via |
| 307 | # the name attribute; however, when rendered the reference |
| 308 | # is just an array index. |
| 309 | # |
| 310 | # At this point all objects have been created but references |
Gunnar Mills | 9a7688a | 2017-10-25 17:06:04 -0500 | [diff] [blame] | 311 | # have not been resolved to array indices. Instruct objects |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 312 | # to do that now. |
Matthew Barth | 9dc3e0d | 2020-02-13 13:02:27 -0600 | [diff] [blame] | 313 | for cls, items in list(objs.items()): |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 314 | for obj in items: |
| 315 | obj.setup(objs) |
| 316 | |
| 317 | return Everything(**objs) |
| 318 | |
| 319 | def __init__(self, *a, **kw): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 320 | self.fans = kw.pop("fan", []) |
| 321 | self.policies = kw.pop("policy", []) |
| 322 | self.sensors = kw.pop("sensor", []) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 323 | super(Everything, self).__init__(**kw) |
| 324 | |
| 325 | def generate_cpp(self, loader): |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 326 | """Render the template with the provided data.""" |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 327 | sys.stdout.write( |
| 328 | self.render( |
| 329 | loader, |
| 330 | args.template, |
| 331 | fans=self.fans, |
| 332 | sensors=self.sensors, |
| 333 | policies=self.policies, |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 334 | indent=Indent(), |
| 335 | ) |
| 336 | ) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 337 | |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 338 | |
| 339 | if __name__ == "__main__": |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 340 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 341 | valid_commands = { |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 342 | "generate-cpp": "generate_cpp", |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | parser = ArgumentParser( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 346 | description=( |
| 347 | "Phosphor Fan Presence (PFP) YAML scanner and code generator." |
| 348 | ) |
| 349 | ) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 350 | |
| 351 | parser.add_argument( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 352 | "-i", |
| 353 | "--input", |
| 354 | dest="input", |
| 355 | default=os.path.join(script_dir, "example", "example.yaml"), |
| 356 | help="Location of config file to process.", |
| 357 | ) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 358 | parser.add_argument( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 359 | "-t", |
| 360 | "--template", |
| 361 | dest="template", |
| 362 | default="generated.mako.hpp", |
| 363 | help="The top level template to render.", |
| 364 | ) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 365 | parser.add_argument( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 366 | "-p", |
| 367 | "--template-path", |
| 368 | dest="template_search", |
| 369 | default=os.path.join(script_dir, "templates"), |
| 370 | help="The space delimited mako template search path.", |
| 371 | ) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 372 | parser.add_argument( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 373 | "command", |
| 374 | metavar="COMMAND", |
| 375 | type=str, |
Matthew Barth | 9dc3e0d | 2020-02-13 13:02:27 -0600 | [diff] [blame] | 376 | choices=list(valid_commands.keys()), |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 377 | help="%s." % " | ".join(list(valid_commands.keys())), |
| 378 | ) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 379 | |
| 380 | args = parser.parse_args() |
| 381 | |
| 382 | if sys.version_info < (3, 0): |
| 383 | lookup = mako.lookup.TemplateLookup( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 384 | directories=args.template_search.split(), disable_unicode=True |
| 385 | ) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 386 | else: |
| 387 | lookup = mako.lookup.TemplateLookup( |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 388 | directories=args.template_search.split() |
| 389 | ) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 390 | try: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 391 | function = getattr(Everything.load(args), valid_commands[args.command]) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 392 | function(lookup) |
| 393 | except InvalidConfigError as e: |
Patrick Williams | 0f2588f | 2022-12-05 10:17:03 -0600 | [diff] [blame] | 394 | sys.stderr.write("{0}: {1}\n\n".format(e.config, e.msg)) |
Brad Bishop | 5593560 | 2017-06-13 13:31:24 -0400 | [diff] [blame] | 395 | raise |