Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 3 | '''Phosphor Inventory Manager YAML parser and code generator. |
| 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. |
| 16 | ''' |
| 17 | |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 18 | import sys |
| 19 | import os |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 20 | import argparse |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 21 | import subprocess |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 22 | import yaml |
| 23 | import mako.lookup |
| 24 | import sdbusplus.property |
| 25 | from sdbusplus.namedelement import NamedElement |
| 26 | from sdbusplus.renderer import Renderer |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 27 | |
| 28 | |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 29 | def cppTypeName(yaml_type): |
| 30 | ''' Convert yaml types to cpp types.''' |
| 31 | return sdbusplus.property.Property(type=yaml_type).cppTypeName |
| 32 | |
| 33 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 34 | class Interface(list): |
| 35 | '''Provide various interface transformations.''' |
| 36 | |
| 37 | def __init__(self, iface): |
| 38 | super(Interface, self).__init__(iface.split('.')) |
| 39 | |
| 40 | def namespace(self): |
| 41 | '''Represent as an sdbusplus namespace.''' |
| 42 | return '::'.join(['sdbusplus'] + self[:-1] + ['server', self[-1]]) |
| 43 | |
| 44 | def header(self): |
| 45 | '''Represent as an sdbusplus server binding header.''' |
| 46 | return os.sep.join(self + ['server.hpp']) |
| 47 | |
| 48 | def __str__(self): |
| 49 | return '.'.join(self) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 50 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 51 | |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 52 | class Indent(object): |
| 53 | '''Help templates be depth agnostic.''' |
| 54 | |
| 55 | def __init__(self, depth=0): |
| 56 | self.depth = depth |
| 57 | |
| 58 | def __add__(self, depth): |
| 59 | return Indent(self.depth + depth) |
| 60 | |
| 61 | def __call__(self, depth): |
| 62 | '''Render an indent at the current depth plus depth.''' |
| 63 | return 4*' '*(depth + self.depth) |
| 64 | |
| 65 | |
| 66 | class Template(NamedElement): |
| 67 | '''Associate a template name with its namespace.''' |
| 68 | |
| 69 | def __init__(self, **kw): |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 70 | self.namespace = kw.pop('namespace', []) |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 71 | super(Template, self).__init__(**kw) |
| 72 | |
| 73 | def qualified(self): |
| 74 | return '::'.join(self.namespace + [self.name]) |
| 75 | |
| 76 | |
Brad Bishop | db9b325 | 2017-01-30 08:58:40 -0500 | [diff] [blame] | 77 | class FixBool(object): |
| 78 | '''Un-capitalize booleans.''' |
| 79 | |
| 80 | def __call__(self, arg): |
| 81 | return '{0}'.format(arg.lower()) |
| 82 | |
| 83 | |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 84 | class Quote(object): |
| 85 | '''Decorate an argument by quoting it.''' |
| 86 | |
| 87 | def __call__(self, arg): |
| 88 | return '"{0}"'.format(arg) |
| 89 | |
| 90 | |
| 91 | class Cast(object): |
| 92 | '''Decorate an argument by casting it.''' |
| 93 | |
| 94 | def __init__(self, cast, target): |
| 95 | '''cast is the cast type (static, const, etc...). |
| 96 | target is the cast target type.''' |
| 97 | self.cast = cast |
| 98 | self.target = target |
| 99 | |
| 100 | def __call__(self, arg): |
| 101 | return '{0}_cast<{1}>({2})'.format(self.cast, self.target, arg) |
| 102 | |
| 103 | |
| 104 | class Literal(object): |
| 105 | '''Decorate an argument with a literal operator.''' |
| 106 | |
Brad Bishop | 134d2cb | 2017-02-23 12:37:56 -0500 | [diff] [blame] | 107 | integer_types = [ |
| 108 | 'int8', |
| 109 | 'int16', |
| 110 | 'int32', |
| 111 | 'int64', |
| 112 | 'uint8', |
| 113 | 'uint16', |
| 114 | 'uint32', |
| 115 | 'uint64' |
| 116 | ] |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 117 | |
| 118 | def __init__(self, type): |
| 119 | self.type = type |
| 120 | |
| 121 | def __call__(self, arg): |
Brad Bishop | 134d2cb | 2017-02-23 12:37:56 -0500 | [diff] [blame] | 122 | if 'uint' in self.type: |
| 123 | arg = '{0}ull'.format(arg) |
| 124 | elif 'int' in self.type: |
| 125 | arg = '{0}ll'.format(arg) |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 126 | |
Brad Bishop | 134d2cb | 2017-02-23 12:37:56 -0500 | [diff] [blame] | 127 | if self.type in self.integer_types: |
| 128 | return Cast('static', '{0}_t'.format(self.type))(arg) |
| 129 | |
| 130 | if self.type == 'string': |
| 131 | return '{0}s'.format(arg) |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 132 | |
| 133 | return arg |
| 134 | |
| 135 | |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 136 | class Argument(NamedElement, Renderer): |
| 137 | '''Define argument type inteface.''' |
| 138 | |
| 139 | def __init__(self, **kw): |
| 140 | self.type = kw.pop('type', None) |
| 141 | super(Argument, self).__init__(**kw) |
| 142 | |
| 143 | def argument(self, loader, indent): |
| 144 | raise NotImplementedError |
| 145 | |
| 146 | |
| 147 | class TrivialArgument(Argument): |
| 148 | '''Non-array type arguments.''' |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 149 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 150 | def __init__(self, **kw): |
| 151 | self.value = kw.pop('value') |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 152 | self.decorators = kw.pop('decorators', []) |
| 153 | if kw.get('type', None) == 'string': |
| 154 | self.decorators.insert(0, Quote()) |
Brad Bishop | db9b325 | 2017-01-30 08:58:40 -0500 | [diff] [blame] | 155 | if kw.get('type', None) == 'boolean': |
| 156 | self.decorators.insert(0, FixBool()) |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 157 | |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 158 | super(TrivialArgument, self).__init__(**kw) |
| 159 | |
| 160 | def argument(self, loader, indent): |
| 161 | a = str(self.value) |
| 162 | for d in self.decorators: |
| 163 | a = d(a) |
| 164 | |
| 165 | return a |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 166 | |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 167 | |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 168 | class InitializerList(Argument): |
| 169 | '''Initializer list arguments.''' |
| 170 | |
| 171 | def __init__(self, **kw): |
| 172 | self.values = kw.pop('values') |
| 173 | super(InitializerList, self).__init__(**kw) |
| 174 | |
| 175 | def argument(self, loader, indent): |
| 176 | return self.render( |
| 177 | loader, |
| 178 | 'argument.mako.cpp', |
| 179 | arg=self, |
| 180 | indent=indent) |
| 181 | |
| 182 | |
Brad Bishop | 75800cf | 2017-01-21 15:24:18 -0500 | [diff] [blame] | 183 | class DbusSignature(Argument): |
| 184 | '''DBus signature arguments.''' |
| 185 | |
| 186 | def __init__(self, **kw): |
| 187 | self.sig = {x: y for x, y in kw.iteritems()} |
| 188 | kw.clear() |
| 189 | super(DbusSignature, self).__init__(**kw) |
| 190 | |
| 191 | def argument(self, loader, indent): |
| 192 | return self.render( |
| 193 | loader, |
| 194 | 'signature.mako.cpp', |
| 195 | signature=self, |
| 196 | indent=indent) |
| 197 | |
| 198 | |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 199 | class MethodCall(Argument): |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 200 | '''Render syntatically correct c++ method calls.''' |
| 201 | |
| 202 | def __init__(self, **kw): |
| 203 | self.namespace = kw.pop('namespace', []) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 204 | self.templates = kw.pop('templates', []) |
| 205 | self.args = kw.pop('args', []) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 206 | super(MethodCall, self).__init__(**kw) |
| 207 | |
Brad Bishop | cab2bdd | 2017-01-21 15:00:54 -0500 | [diff] [blame] | 208 | def call(self, loader, indent): |
| 209 | return self.render( |
| 210 | loader, |
| 211 | 'method.mako.cpp', |
| 212 | method=self, |
| 213 | indent=indent) |
| 214 | |
| 215 | def argument(self, loader, indent): |
| 216 | return self.call(loader, indent) |
| 217 | |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 218 | |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 219 | class Vector(MethodCall): |
| 220 | '''Convenience type for vectors.''' |
| 221 | |
| 222 | def __init__(self, **kw): |
| 223 | kw['name'] = 'vector' |
| 224 | kw['namespace'] = ['std'] |
| 225 | kw['args'] = [InitializerList(values=kw.pop('args'))] |
| 226 | super(Vector, self).__init__(**kw) |
| 227 | |
| 228 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 229 | class Filter(MethodCall): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 230 | '''Convenience type for filters''' |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 231 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 232 | def __init__(self, **kw): |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 233 | kw['name'] = 'make_filter' |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 234 | super(Filter, self).__init__(**kw) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 235 | |
Brad Bishop | 0a6a479 | 2016-11-12 12:10:07 -0500 | [diff] [blame] | 236 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 237 | class Action(MethodCall): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 238 | '''Convenience type for actions''' |
Brad Bishop | 561a565 | 2016-10-26 21:13:32 -0500 | [diff] [blame] | 239 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 240 | def __init__(self, **kw): |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 241 | kw['name'] = 'make_action' |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 242 | super(Action, self).__init__(**kw) |
Brad Bishop | 92665b2 | 2016-10-26 20:51:16 -0500 | [diff] [blame] | 243 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 244 | |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 245 | class PathCondition(MethodCall): |
| 246 | '''Convenience type for path conditions''' |
| 247 | |
| 248 | def __init__(self, **kw): |
| 249 | kw['name'] = 'make_path_condition' |
| 250 | super(PathCondition, self).__init__(**kw) |
| 251 | |
| 252 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 253 | class CreateObjects(MethodCall): |
| 254 | '''Assemble a createObjects functor.''' |
Brad Bishop | db92c28 | 2017-01-21 23:44:28 -0500 | [diff] [blame] | 255 | |
| 256 | def __init__(self, **kw): |
| 257 | objs = [] |
| 258 | |
| 259 | for path, interfaces in kw.pop('objs').iteritems(): |
| 260 | key_o = TrivialArgument( |
| 261 | value=path, |
| 262 | type='string', |
| 263 | decorators=[Literal('string')]) |
| 264 | value_i = [] |
| 265 | |
| 266 | for interface, properties, in interfaces.iteritems(): |
| 267 | key_i = TrivialArgument(value=interface, type='string') |
| 268 | value_p = [] |
| 269 | |
| 270 | for prop, value in properties.iteritems(): |
| 271 | key_p = TrivialArgument(value=prop, type='string') |
| 272 | value_v = TrivialArgument( |
| 273 | decorators=[Literal(value.get('type', None))], |
| 274 | **value) |
| 275 | value_p.append(InitializerList(values=[key_p, value_v])) |
| 276 | |
| 277 | value_p = InitializerList(values=value_p) |
| 278 | value_i.append(InitializerList(values=[key_i, value_p])) |
| 279 | |
| 280 | value_i = InitializerList(values=value_i) |
| 281 | objs.append(InitializerList(values=[key_o, value_i])) |
| 282 | |
| 283 | kw['args'] = [InitializerList(values=objs)] |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 284 | kw['namespace'] = ['functor'] |
Brad Bishop | db92c28 | 2017-01-21 23:44:28 -0500 | [diff] [blame] | 285 | super(CreateObjects, self).__init__(**kw) |
| 286 | |
| 287 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 288 | class DestroyObjects(MethodCall): |
| 289 | '''Assemble a destroyObject functor.''' |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 290 | |
| 291 | def __init__(self, **kw): |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 292 | values = [{'value': x, 'type': 'string'} for x in kw.pop('paths')] |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 293 | conditions = [ |
| 294 | Event.functor_map[ |
| 295 | x['name']](**x) for x in kw.pop('conditions', [])] |
| 296 | conditions = [PathCondition(args=[x]) for x in conditions] |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 297 | args = [InitializerList( |
| 298 | values=[TrivialArgument(**x) for x in values])] |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 299 | args.append(InitializerList(values=conditions)) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 300 | kw['args'] = args |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 301 | kw['namespace'] = ['functor'] |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 302 | super(DestroyObjects, self).__init__(**kw) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 303 | |
| 304 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 305 | class SetProperty(MethodCall): |
| 306 | '''Assemble a setProperty functor.''' |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 307 | |
| 308 | def __init__(self, **kw): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 309 | args = [] |
| 310 | |
| 311 | value = kw.pop('value') |
| 312 | prop = kw.pop('property') |
| 313 | iface = kw.pop('interface') |
| 314 | iface = Interface(iface) |
| 315 | namespace = iface.namespace().split('::')[:-1] |
| 316 | name = iface[-1] |
| 317 | t = Template(namespace=namespace, name=iface[-1]) |
| 318 | |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 319 | member = '&%s' % '::'.join( |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 320 | namespace + [name, NamedElement(name=prop).camelCase]) |
| 321 | member_type = cppTypeName(value['type']) |
| 322 | member_cast = '{0} ({1}::*)({0})'.format(member_type, t.qualified()) |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 323 | |
Brad Bishop | 02ca021 | 2017-01-28 23:25:58 -0500 | [diff] [blame] | 324 | paths = [{'value': x, 'type': 'string'} for x in kw.pop('paths')] |
| 325 | args.append(InitializerList( |
| 326 | values=[TrivialArgument(**x) for x in paths])) |
| 327 | |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 328 | conditions = [ |
| 329 | Event.functor_map[ |
| 330 | x['name']](**x) for x in kw.pop('conditions', [])] |
| 331 | conditions = [PathCondition(args=[x]) for x in conditions] |
| 332 | |
| 333 | args.append(InitializerList(values=conditions)) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 334 | args.append(TrivialArgument(value=str(iface), type='string')) |
| 335 | args.append(TrivialArgument( |
| 336 | value=member, decorators=[Cast('static', member_cast)])) |
| 337 | args.append(TrivialArgument(**value)) |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 338 | |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 339 | kw['templates'] = [Template(name=name, namespace=namespace)] |
| 340 | kw['args'] = args |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 341 | kw['namespace'] = ['functor'] |
Brad Bishop | e2e402f | 2016-11-30 18:00:17 -0500 | [diff] [blame] | 342 | super(SetProperty, self).__init__(**kw) |
| 343 | |
| 344 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 345 | class PropertyChanged(MethodCall): |
| 346 | '''Assemble a propertyChanged functor.''' |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 347 | |
| 348 | def __init__(self, **kw): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 349 | args = [] |
| 350 | args.append(TrivialArgument(value=kw.pop('interface'), type='string')) |
| 351 | args.append(TrivialArgument(value=kw.pop('property'), type='string')) |
| 352 | args.append(TrivialArgument( |
| 353 | decorators=[ |
| 354 | Literal(kw['value'].get('type', None))], **kw.pop('value'))) |
| 355 | kw['args'] = args |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 356 | kw['namespace'] = ['functor'] |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 357 | super(PropertyChanged, self).__init__(**kw) |
| 358 | |
| 359 | |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 360 | class PropertyIs(MethodCall): |
| 361 | '''Assemble a propertyIs functor.''' |
Brad Bishop | 040e18b | 2017-01-21 22:04:00 -0500 | [diff] [blame] | 362 | |
| 363 | def __init__(self, **kw): |
| 364 | args = [] |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 365 | path = kw.pop('path', None) |
| 366 | if not path: |
| 367 | path = TrivialArgument(value='nullptr') |
| 368 | else: |
| 369 | path = TrivialArgument(value=path, type='string') |
| 370 | |
| 371 | args.append(path) |
Brad Bishop | 040e18b | 2017-01-21 22:04:00 -0500 | [diff] [blame] | 372 | args.append(TrivialArgument(value=kw.pop('interface'), type='string')) |
| 373 | args.append(TrivialArgument(value=kw.pop('property'), type='string')) |
| 374 | args.append(TrivialArgument( |
| 375 | decorators=[ |
| 376 | Literal(kw['value'].get('type', None))], **kw.pop('value'))) |
| 377 | |
| 378 | service = kw.pop('service', None) |
| 379 | if service: |
| 380 | args.append(TrivialArgument(value=service, type='string')) |
| 381 | |
| 382 | kw['args'] = args |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 383 | kw['namespace'] = ['functor'] |
Brad Bishop | 040e18b | 2017-01-21 22:04:00 -0500 | [diff] [blame] | 384 | super(PropertyIs, self).__init__(**kw) |
| 385 | |
| 386 | |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 387 | class Event(MethodCall): |
| 388 | '''Assemble an inventory manager event.''' |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 389 | |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 390 | functor_map = { |
Brad Bishop | 7b7e712 | 2017-01-21 21:21:46 -0500 | [diff] [blame] | 391 | 'destroyObjects': DestroyObjects, |
Brad Bishop | db92c28 | 2017-01-21 23:44:28 -0500 | [diff] [blame] | 392 | 'createObjects': CreateObjects, |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 393 | 'propertyChangedTo': PropertyChanged, |
Brad Bishop | 040e18b | 2017-01-21 22:04:00 -0500 | [diff] [blame] | 394 | 'propertyIs': PropertyIs, |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 395 | 'setProperty': SetProperty, |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 396 | } |
| 397 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 398 | def __init__(self, **kw): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 399 | self.summary = kw.pop('name') |
| 400 | |
| 401 | filters = [ |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 402 | self.functor_map[x['name']](**x) for x in kw.pop('filters', [])] |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 403 | filters = [Filter(args=[x]) for x in filters] |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 404 | filters = Vector( |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 405 | templates=[Template(name='Filter', namespace=[])], |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 406 | args=filters) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 407 | |
| 408 | event = MethodCall( |
| 409 | name='make_shared', |
| 410 | namespace=['std'], |
| 411 | templates=[Template( |
| 412 | name=kw.pop('event'), |
| 413 | namespace=kw.pop('event_namespace', []))], |
Brad Bishop | 064c94a | 2017-01-21 21:33:30 -0500 | [diff] [blame] | 414 | args=kw.pop('event_args', []) + [filters]) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 415 | |
| 416 | events = Vector( |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 417 | templates=[Template(name='EventBasePtr', namespace=[])], |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 418 | args=[event]) |
| 419 | |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 420 | action_type = Template(name='Action', namespace=[]) |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 421 | action_args = [ |
Brad Bishop | d0f48ad | 2017-01-30 08:52:26 -0500 | [diff] [blame] | 422 | self.functor_map[x['name']](**x) for x in kw.pop('actions', [])] |
Brad Bishop | c1f4798 | 2017-02-09 01:27:38 -0500 | [diff] [blame] | 423 | action_args = [Action(args=[x]) for x in action_args] |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 424 | actions = Vector( |
| 425 | templates=[action_type], |
| 426 | args=action_args) |
| 427 | |
| 428 | kw['name'] = 'make_tuple' |
| 429 | kw['namespace'] = ['std'] |
| 430 | kw['args'] = [events, actions] |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 431 | super(Event, self).__init__(**kw) |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 432 | |
Brad Bishop | cfb3c89 | 2016-11-12 11:43:37 -0500 | [diff] [blame] | 433 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 434 | class MatchEvent(Event): |
| 435 | '''Associate one or more dbus signal match signatures with |
| 436 | a filter.''' |
| 437 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 438 | def __init__(self, **kw): |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 439 | kw['event'] = 'DbusSignal' |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 440 | kw['event_namespace'] = [] |
Brad Bishop | c93bcc9 | 2017-01-21 16:23:39 -0500 | [diff] [blame] | 441 | kw['event_args'] = [ |
| 442 | DbusSignature(**x) for x in kw.pop('signatures', [])] |
| 443 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 444 | super(MatchEvent, self).__init__(**kw) |
| 445 | |
| 446 | |
Brad Bishop | 828df83 | 2017-01-21 22:20:43 -0500 | [diff] [blame] | 447 | class StartupEvent(Event): |
| 448 | '''Assemble a startup event.''' |
| 449 | |
| 450 | def __init__(self, **kw): |
| 451 | kw['event'] = 'StartupEvent' |
Brad Bishop | 12f8a3c | 2017-02-09 00:02:00 -0500 | [diff] [blame] | 452 | kw['event_namespace'] = [] |
Brad Bishop | 828df83 | 2017-01-21 22:20:43 -0500 | [diff] [blame] | 453 | super(StartupEvent, self).__init__(**kw) |
| 454 | |
| 455 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 456 | class Everything(Renderer): |
| 457 | '''Parse/render entry point.''' |
| 458 | |
| 459 | class_map = { |
| 460 | 'match': MatchEvent, |
Brad Bishop | 828df83 | 2017-01-21 22:20:43 -0500 | [diff] [blame] | 461 | 'startup': StartupEvent, |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | @staticmethod |
| 465 | def load(args): |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 466 | # Aggregate all the event YAML in the events.d directory |
| 467 | # into a single list of events. |
| 468 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 469 | events = [] |
Brad Bishop | a6fcd56 | 2017-02-03 11:00:27 -0500 | [diff] [blame] | 470 | events_dir = os.path.join(args.inputdir, 'events.d') |
| 471 | |
| 472 | if os.path.exists(events_dir): |
| 473 | yaml_files = filter( |
| 474 | lambda x: x.endswith('.yaml'), |
| 475 | os.listdir(events_dir)) |
| 476 | |
| 477 | for x in yaml_files: |
| 478 | with open(os.path.join(events_dir, x), 'r') as fd: |
| 479 | for e in yaml.safe_load(fd.read()).get('events', {}): |
| 480 | events.append(e) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 481 | |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 482 | interfaces = Everything.get_interfaces(args.ifacesdir) |
| 483 | extra_interfaces = Everything.get_interfaces( |
| 484 | os.path.join(args.inputdir, 'extra_interfaces.d')) |
| 485 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 486 | return Everything( |
| 487 | *events, |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 488 | interfaces=interfaces + extra_interfaces) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 489 | |
| 490 | @staticmethod |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 491 | def get_interfaces(targetdir): |
| 492 | '''Scan the interfaces directory for interfaces that PIM can create.''' |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 493 | |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 494 | yaml_files = [] |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 495 | interfaces = [] |
Brad Bishop | a6fcd56 | 2017-02-03 11:00:27 -0500 | [diff] [blame] | 496 | |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 497 | if targetdir and os.path.exists(targetdir): |
| 498 | for directory, _, files in os.walk(targetdir): |
| 499 | if not files: |
| 500 | continue |
| 501 | |
| 502 | yaml_files += map( |
| 503 | lambda f: os.path.relpath( |
| 504 | os.path.join(directory, f), |
| 505 | targetdir), |
| 506 | filter(lambda f: f.endswith('.interface.yaml'), files)) |
| 507 | |
| 508 | for y in yaml_files: |
Marri Devender Rao | 06e3d50 | 2017-06-09 11:33:38 -0500 | [diff] [blame^] | 509 | # parse only phosphor dbus related interface files |
| 510 | if not y.startswith('xyz'): |
| 511 | continue |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 512 | with open(os.path.join(targetdir, y)) as fd: |
| 513 | i = y.replace('.interface.yaml', '').replace(os.sep, '.') |
| 514 | |
| 515 | # PIM can't create interfaces with methods. |
| 516 | # PIM can't create interfaces without properties. |
| 517 | parsed = yaml.safe_load(fd.read()) |
| 518 | if parsed.get('methods', None): |
| 519 | continue |
| 520 | if not parsed.get('properties', None): |
| 521 | continue |
| 522 | |
| 523 | interfaces.append(i) |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 524 | |
| 525 | return interfaces |
| 526 | |
| 527 | def __init__(self, *a, **kw): |
| 528 | self.interfaces = \ |
| 529 | [Interface(x) for x in kw.pop('interfaces', [])] |
| 530 | self.events = [ |
| 531 | self.class_map[x['type']](**x) for x in a] |
| 532 | super(Everything, self).__init__(**kw) |
| 533 | |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 534 | def generate_cpp(self, loader): |
| 535 | '''Render the template with the provided events and interfaces.''' |
| 536 | with open(os.path.join( |
| 537 | args.outputdir, |
| 538 | 'generated.cpp'), 'w') as fd: |
| 539 | fd.write( |
| 540 | self.render( |
| 541 | loader, |
| 542 | 'generated.mako.cpp', |
| 543 | events=self.events, |
Brad Bishop | 9b5a12f | 2017-01-21 14:42:11 -0500 | [diff] [blame] | 544 | interfaces=self.interfaces, |
| 545 | indent=Indent())) |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 546 | |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 547 | |
| 548 | if __name__ == '__main__': |
| 549 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
Brad Bishop | 14a9fe5 | 2016-11-12 12:51:26 -0500 | [diff] [blame] | 550 | valid_commands = { |
| 551 | 'generate-cpp': 'generate_cpp', |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 552 | } |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 553 | |
| 554 | parser = argparse.ArgumentParser( |
| 555 | description='Phosphor Inventory Manager (PIM) YAML ' |
| 556 | 'scanner and code generator.') |
| 557 | parser.add_argument( |
| 558 | '-o', '--output-dir', dest='outputdir', |
| 559 | default='.', help='Output directory.') |
| 560 | parser.add_argument( |
Brad Bishop | 834989f | 2017-02-06 12:08:20 -0500 | [diff] [blame] | 561 | '-i', '--interfaces-dir', dest='ifacesdir', |
| 562 | help='Location of interfaces to be supported.') |
| 563 | parser.add_argument( |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 564 | '-d', '--dir', dest='inputdir', |
| 565 | default=os.path.join(script_dir, 'example'), |
| 566 | help='Location of files to process.') |
Brad Bishop | f4666f5 | 2016-11-12 12:44:42 -0500 | [diff] [blame] | 567 | parser.add_argument( |
| 568 | 'command', metavar='COMMAND', type=str, |
| 569 | choices=valid_commands.keys(), |
Brad Bishop | c029f6a | 2017-01-18 19:43:26 -0500 | [diff] [blame] | 570 | help='%s.' % " | ".join(valid_commands.keys())) |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 571 | |
| 572 | args = parser.parse_args() |
Brad Bishop | 22cfbe6 | 2016-11-30 13:25:10 -0500 | [diff] [blame] | 573 | |
| 574 | if sys.version_info < (3, 0): |
| 575 | lookup = mako.lookup.TemplateLookup( |
| 576 | directories=[script_dir], |
| 577 | disable_unicode=True) |
| 578 | else: |
| 579 | lookup = mako.lookup.TemplateLookup( |
| 580 | directories=[script_dir]) |
| 581 | |
| 582 | function = getattr( |
| 583 | Everything.load(args), |
| 584 | valid_commands[args.command]) |
| 585 | function(lookup) |
Brad Bishop | 95dd98f | 2016-11-12 12:39:15 -0500 | [diff] [blame] | 586 | |
| 587 | |
Brad Bishop | bf066a6 | 2016-10-19 08:09:44 -0400 | [diff] [blame] | 588 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |