Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 1 | import plyxproto.model as m |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 2 | from plyxproto.helpers import Visitor |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 3 | import argparse |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 4 | import plyxproto.parser as plyxproto |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 5 | import traceback |
| 6 | import sys |
| 7 | import jinja2 |
| 8 | import os |
Sapan Bhatia | 1e397df | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 9 | import copy |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 10 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 11 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 12 | def dotname_to_fqn(dotname): |
| 13 | b_names = [part.pval for part in dotname] |
| 14 | package = '.'.join(b_names[:-1]) |
| 15 | name = b_names[-1] |
| 16 | if package: |
| 17 | fqn = package + '.' + name |
| 18 | else: |
| 19 | fqn = name |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 20 | return {'name': name, 'fqn': fqn, 'package': package} |
| 21 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 22 | |
| 23 | def dotname_to_name(dotname): |
| 24 | b_names = [part.pval for part in dotname] |
| 25 | return '.'.join(b_names) |
| 26 | |
| 27 | |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 28 | def count_messages(body): |
| 29 | count = 0 |
| 30 | for e in body: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 31 | if (type(e) == m.MessageDefinition): |
| 32 | count += 1 |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 33 | return count |
| 34 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 35 | |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 36 | def count_fields(body): |
| 37 | count = 0 |
| 38 | for e in body: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 39 | if (type(e) in [m.LinkDefinition, m.FieldDefinition, m.LinkSpec]): |
| 40 | count += 1 |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 41 | return count |
| 42 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 43 | def compute_rlinks(messages, message_dict): |
Sapan Bhatia | 1e397df | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 44 | rev_links = {} |
| 45 | |
| 46 | link_opposite = { |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 47 | 'manytomany': 'manytomany', |
| 48 | 'manytoone': 'onetomany', |
| 49 | 'onetoone': 'onetoone', |
| 50 | 'onetomany': 'manytoone' |
Sapan Bhatia | 1e397df | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | for m in messages: |
| 54 | for l in m['links']: |
| 55 | rlink = copy.deepcopy(l) |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 56 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 57 | rlink['_type'] = 'rlink' # An implicit link, not declared in the model |
Sapan Bhatia | 1e397df | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 58 | rlink['src_port'] = l['dst_port'] |
| 59 | rlink['dst_port'] = l['src_port'] |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 60 | rlink['peer'] = {'name': m['name'], 'package': m['package'], 'fqn': m['fqn']} |
Sapan Bhatia | 1e397df | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 61 | rlink['link_type'] = link_opposite[l['link_type']] |
| 62 | |
| 63 | try: |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 64 | try: |
| 65 | rev_links[l['peer']['fqn']].append(rlink) |
| 66 | except TypeError: |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 67 | pass |
Sapan Bhatia | 1e397df | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 68 | except KeyError: |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 69 | rev_links[l['peer']['fqn']] = [rlink] |
Sapan Bhatia | 1e397df | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 70 | |
| 71 | for m in messages: |
| 72 | try: |
| 73 | m['rlinks'] = rev_links[m['name']] |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 74 | message_dict[m['name']]['rlinks'] = m['rlinks'] |
Sapan Bhatia | 1e397df | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 75 | except KeyError: |
| 76 | pass |
| 77 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 78 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 79 | def name_to_value(obj): |
| 80 | try: |
| 81 | value = obj.value.value.pval |
| 82 | except AttributeError: |
| 83 | try: |
| 84 | value = obj.value.value |
| 85 | except AttributeError: |
| 86 | value = obj.value.pval |
| 87 | |
| 88 | return value |
| 89 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 90 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 91 | class Stack(list): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 92 | def push(self, x): |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 93 | self.append(x) |
| 94 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 95 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 96 | ''' XOS2Jinja overrides the underlying visitor pattern to transform the tree |
| 97 | in addition to traversing it ''' |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 98 | |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 99 | class XOS2Jinja(Visitor): |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 100 | def __init__(self): |
| 101 | super(XOS2Jinja, self).__init__() |
| 102 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 103 | self.stack = Stack() |
| 104 | self.models = {} |
| 105 | self.options = {} |
| 106 | self.package = None |
| 107 | self.message_options = {} |
| 108 | self.count_stack = Stack() |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 109 | self.policies = {} |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 110 | self.content = "" |
| 111 | self.offset = 0 |
| 112 | self.current_message_name = None |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 113 | self.verbose = 0 |
| 114 | self.first_field = True |
| 115 | self.first_method = True |
| 116 | |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 117 | def visit_PolicyDefinition(self, obj): |
| 118 | if self.package: |
| 119 | pname = '.'.join([self.package, obj.name.value.pval]) |
| 120 | else: |
| 121 | pname = obj.name.value.pval |
| 122 | |
| 123 | self.policies[pname] = obj.body |
| 124 | |
| 125 | return True |
| 126 | |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 127 | def visit_PolicyDefinition(self, obj): |
| 128 | if self.package: |
| 129 | pname = '.'.join([self.package, obj.name.value.pval]) |
| 130 | else: |
| 131 | pname = obj.name.value.pval |
| 132 | |
| 133 | self.policies[pname] = obj.body |
| 134 | |
| 135 | return True |
| 136 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 137 | def visit_PackageStatement(self, obj): |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 138 | dotlist = obj.name.value |
| 139 | dotlist2 = [f.pval for f in dotlist] |
| 140 | dotstr = '.'.join(dotlist2) |
| 141 | self.package = dotstr |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 142 | return True |
| 143 | |
| 144 | def visit_ImportStatement(self, obj): |
| 145 | '''Ignore''' |
| 146 | return True |
| 147 | |
| 148 | def visit_OptionStatement(self, obj): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 149 | if not hasattr(obj, 'mark_for_deletion'): |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 150 | if (self.current_message_name): |
| 151 | self.message_options[obj.name.value.pval] = obj.value.value.pval |
| 152 | else: |
| 153 | self.options[obj.name.value.pval] = obj.value.value.pval |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 154 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 155 | return True |
| 156 | |
| 157 | def visit_LU(self, obj): |
| 158 | return True |
| 159 | |
| 160 | def visit_default(self, obj): |
| 161 | return True |
| 162 | |
| 163 | def visit_FieldDirective(self, obj): |
| 164 | return True |
| 165 | |
| 166 | def visit_FieldDirective_post(self, obj): |
| 167 | |
| 168 | try: |
| 169 | name = obj.name.value.pval |
| 170 | except AttributeError: |
| 171 | name = obj.name.value |
| 172 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 173 | if type(obj.value) == list: |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 174 | value = dotname_to_name(obj.value) |
| 175 | else: |
| 176 | value = name_to_value(obj) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 177 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 178 | self.stack.push([name, value]) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 179 | return True |
| 180 | |
| 181 | def visit_FieldType(self, obj): |
| 182 | '''Field type, if type is name, then it may need refactoring consistent with refactoring rules according to the table''' |
| 183 | return True |
| 184 | |
| 185 | def visit_LinkDefinition(self, obj): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 186 | s = {} |
Sapan Bhatia | 8a57c66 | 2017-04-11 10:39:47 -0700 | [diff] [blame] | 187 | |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 188 | try: |
| 189 | s['link_type'] = obj.link_type.pval |
| 190 | except AttributeError: |
| 191 | s['link_type'] = obj.link_type |
| 192 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 193 | s['src_port'] = obj.src_port.value.pval |
| 194 | s['name'] = obj.src_port.value.pval |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 195 | try: |
| 196 | s['policy'] = obj.policy.pval |
| 197 | except AttributeError: |
| 198 | s['policy'] = None |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 199 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 200 | try: |
| 201 | s['dst_port'] = obj.dst_port.value.pval |
| 202 | except AttributeError: |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 203 | s['dst_port'] = obj.dst_port |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 204 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 205 | if type(obj.through) == list: |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 206 | s['through'] = dotname_to_fqn(obj.through) |
| 207 | else: |
| 208 | try: |
| 209 | s['through'] = obj.through.pval |
| 210 | except AttributeError: |
| 211 | s['through'] = obj.through |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 212 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 213 | if type(obj.name) == list: |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 214 | s['peer'] = dotname_to_fqn(obj.name) |
| 215 | else: |
| 216 | try: |
| 217 | s['peer'] = obj.name.pval |
| 218 | except AttributeError: |
| 219 | s['peer'] = obj.name |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 220 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 221 | s['_type'] = 'link' |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 222 | s['options'] = {'modifier': 'optional'} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 223 | |
| 224 | self.stack.push(s) |
| 225 | return True |
| 226 | |
| 227 | def visit_FieldDefinition(self, obj): |
| 228 | self.count_stack.push(len(obj.fieldDirective)) |
| 229 | return True |
| 230 | |
| 231 | def visit_FieldDefinition_post(self, obj): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 232 | s = {} |
| 233 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 234 | if isinstance(obj.ftype, m.Name): |
| 235 | s['type'] = obj.ftype.value |
| 236 | else: |
| 237 | s['type'] = obj.ftype.name.pval |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 238 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 239 | s['name'] = obj.name.value.pval |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 240 | |
| 241 | try: |
| 242 | s['policy'] = obj.policy.pval |
| 243 | except AttributeError: |
| 244 | s['policy'] = None |
| 245 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 246 | s['modifier'] = obj.field_modifier.pval |
| 247 | s['id'] = obj.fieldId.pval |
| 248 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 249 | opts = {'modifier': s['modifier']} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 250 | n = self.count_stack.pop() |
| 251 | for i in range(0, n): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 252 | k, v = self.stack.pop() |
Sapan Bhatia | 5769d93 | 2017-05-17 11:10:54 +0200 | [diff] [blame] | 253 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 254 | # The two lines below may be added to eliminate "" around an option. |
Sapan Bhatia | 5769d93 | 2017-05-17 11:10:54 +0200 | [diff] [blame] | 255 | # Right now, this is handled in targets. FIXME |
| 256 | # |
| 257 | # if (v.startswith('"') and v.endswith('"')): |
| 258 | # v = v[1:-1] |
| 259 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 260 | opts[k] = v |
| 261 | |
| 262 | s['options'] = opts |
Sapan Bhatia | 8a57c66 | 2017-04-11 10:39:47 -0700 | [diff] [blame] | 263 | try: |
| 264 | last_link = self.stack[-1]['_type'] |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 265 | if (last_link == 'link'): |
Sapan Bhatia | 8a57c66 | 2017-04-11 10:39:47 -0700 | [diff] [blame] | 266 | s['link'] = True |
| 267 | except: |
| 268 | pass |
| 269 | s['_type'] = 'field' |
| 270 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 271 | self.stack.push(s) |
| 272 | return True |
| 273 | |
| 274 | def visit_EnumFieldDefinition(self, obj): |
| 275 | if self.verbose > 4: |
| 276 | print "\tEnumField: name=%s, %s" % (obj.name, obj) |
| 277 | |
| 278 | return True |
| 279 | |
| 280 | def visit_EnumDefinition(self, obj): |
| 281 | '''New enum definition, refactor name''' |
| 282 | if self.verbose > 3: |
| 283 | print "Enum, [%s] body=%s\n\n" % (obj.name, obj.body) |
| 284 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 285 | return True |
| 286 | |
| 287 | def visit_MessageDefinition(self, obj): |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 288 | self.current_message_name = obj.name.value.pval |
| 289 | self.message_options = {} |
| 290 | self.count_stack.push(count_fields(obj.body)) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 291 | return True |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 292 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 293 | def visit_MessageDefinition_post(self, obj): |
| 294 | stack_num = self.count_stack.pop() |
| 295 | fields = [] |
| 296 | links = [] |
| 297 | last_field = None |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 298 | try: |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 299 | obj.bases = map(dotname_to_fqn, obj.bases) |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 300 | except AttributeError: |
| 301 | pass |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 302 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 303 | last_field = {} |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 304 | for i in range(0, stack_num): |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 305 | f = self.stack.pop() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 306 | if (f['_type'] == 'link'): |
| 307 | f['options'] = {i: d[i] for d in [f['options'], last_field['options']] for i in d} |
| 308 | assert (last_field == fields[0]) |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 309 | fields[0].setdefault('options', {})['link_type'] = f['link_type'] |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 310 | links.insert(0, f) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 311 | else: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 312 | fields.insert(0, f) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 313 | last_field = f |
| 314 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 315 | if self.package: |
| 316 | model_name = '.'.join([self.package, obj.name.value.pval]) |
| 317 | else: |
| 318 | model_name = obj.name.value.pval |
| 319 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 320 | model_def = {'name':obj.name.value.pval,'fields':fields,'links':links, 'bases':obj.bases, 'options':self.message_options, 'package':self.package, 'fqn': model_name, 'rlinks': []} |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 321 | try: |
| 322 | model_def['policy'] = obj.policy.pval |
| 323 | except AttributeError: |
| 324 | model_def['policy'] = None |
| 325 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 326 | self.stack.push(model_def) |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 327 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 328 | self.models[model_name] = model_def |
| 329 | |
| 330 | # Set message options |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 331 | for k, v in self.options.iteritems(): |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 332 | try: |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 333 | if k not in self.message_options: |
| 334 | self.message_options[k] = v |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 335 | except KeyError: |
| 336 | pass |
| 337 | |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 338 | self.current_message_name = None |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 339 | return True |
| 340 | |
| 341 | def visit_MessageExtension(self, obj): |
| 342 | return True |
| 343 | |
| 344 | def visit_MethodDefinition(self, obj): |
| 345 | return True |
| 346 | |
| 347 | def visit_ServiceDefinition(self, obj): |
| 348 | return True |
| 349 | |
| 350 | def visit_ExtensionsDirective(self, obj): |
| 351 | return True |
| 352 | |
| 353 | def visit_Literal(self, obj): |
| 354 | return True |
| 355 | |
| 356 | def visit_Name(self, obj): |
| 357 | return True |
| 358 | |
| 359 | def visit_DotName(self, obj): |
| 360 | return True |
| 361 | |
| 362 | def visit_Proto(self, obj): |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 363 | self.count_stack.push(count_messages(obj.body)) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 364 | return True |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 365 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 366 | def visit_Proto_post(self, obj): |
| 367 | count = self.count_stack.pop() |
| 368 | messages = [] |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 369 | for i in range(0, count): |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 370 | try: |
| 371 | m = self.stack.pop() |
| 372 | except IndexError: |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 373 | pass |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 374 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 375 | messages.insert(0, m) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 376 | |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 377 | compute_rlinks(messages, self.models) |
| 378 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 379 | self.messages = messages |
| 380 | return True |
| 381 | |
| 382 | def visit_LinkSpec(self, obj): |
| 383 | count = self.count_stack.pop() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 384 | self.count_stack.push(count + 1) |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 385 | return True |