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