Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 1 | import plyxproto.model as m |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 2 | import pdb |
| 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 |
| 9 | |
| 10 | class Stack(list): |
| 11 | def push(self,x): |
| 12 | self.append(x) |
| 13 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 14 | def str_to_dict(s): |
| 15 | lst = s.rsplit('.',1) |
| 16 | name = lst[-1] |
| 17 | |
| 18 | if len(lst)==2: |
| 19 | package = lst[0] |
| 20 | else: |
| 21 | package = '' |
| 22 | |
| 23 | return {'name': name, 'package': package, 'fqn': s} |
| 24 | |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 25 | def replace_link(obj): |
| 26 | try: |
| 27 | link = obj.link |
| 28 | try: |
| 29 | through = link['through'] |
| 30 | except KeyError: |
| 31 | through = None |
| 32 | |
| 33 | try: |
| 34 | through_str = through[1:-1] |
| 35 | except TypeError: |
| 36 | through_str = None |
| 37 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 38 | if through_str: |
| 39 | through_dict = str_to_dict(through_str) |
| 40 | else: |
| 41 | through_dict = {} |
| 42 | |
| 43 | model_dict = str_to_dict(link['model'][1:-1]) |
| 44 | |
| 45 | ls = m.LinkSpec(obj, m.LinkDefinition(link['link'][1:-1],obj.name,model_dict,link['port'][1:-1],through_dict)) |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 46 | return ls |
| 47 | except: |
| 48 | return obj |
| 49 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 50 | class Proto2XProto(m.Visitor): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 51 | def __init__(self): |
| 52 | super(Proto2XProto, self).__init__() |
| 53 | |
| 54 | self.stack = Stack() |
| 55 | self.count_stack = Stack() |
| 56 | self.content="" |
| 57 | self.offset=0 |
| 58 | self.statementsChanged=0 |
| 59 | self.message_options = {} |
| 60 | self.options = {} |
| 61 | self.current_message_name = None |
| 62 | |
| 63 | self.xproto_message_options = ['bases'] |
| 64 | self.xproto_field_options = ['model'] |
| 65 | self.verbose = 0 |
| 66 | self.first_field = True |
| 67 | self.first_method = True |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 68 | |
| 69 | def proto_to_xproto_field(self, obj): |
| 70 | try: |
| 71 | opts = {} |
| 72 | for fd in obj.fieldDirective: |
| 73 | k = fd.pval.name.value.pval |
| 74 | v = fd.pval.value.value.pval |
| 75 | opts[k]=v |
| 76 | |
| 77 | if ('model' in opts and 'link' in opts and 'port' in opts): |
| 78 | obj.link = opts |
| 79 | pass |
| 80 | except KeyError: |
| 81 | raise |
| 82 | |
| 83 | def proto_to_xproto_message(self, obj): |
| 84 | try: |
| 85 | bases = self.message_options['bases'].split(',') |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 86 | bases = map(lambda x:str_to_dict(x[1:-1]), bases) |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 87 | obj.bases = bases |
| 88 | except KeyError: |
| 89 | raise |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 90 | |
| 91 | def map_field(self, obj, s): |
| 92 | if 'model' in s: |
| 93 | link = m.LinkDefinition('onetoone','src','name','dst', obj.linespan, obj.lexspan, obj.p) |
| 94 | lspec = m.LinkSpec(link, obj) |
| 95 | else: |
| 96 | lspec = obj |
| 97 | return lspec |
| 98 | |
| 99 | |
| 100 | def get_stack(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 101 | return self.stack |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 102 | |
| 103 | def visit_PackageStatement(self, obj): |
| 104 | '''Ignore''' |
| 105 | return True |
| 106 | |
| 107 | def visit_ImportStatement(self, obj): |
| 108 | '''Ignore''' |
| 109 | return True |
| 110 | |
| 111 | def visit_OptionStatement(self, obj): |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 112 | if (self.current_message_name): |
| 113 | k = obj.name.value.pval |
| 114 | self.message_options[k] = obj.value.value.pval |
| 115 | if (k in self.xproto_message_options): |
| 116 | obj.mark_for_deletion = True |
| 117 | else: |
| 118 | self.options[obj.name.value.pval] = obj.value.value.pval |
| 119 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 120 | return True |
| 121 | |
| 122 | def visit_LU(self, obj): |
| 123 | return True |
| 124 | |
| 125 | def visit_default(self, obj): |
| 126 | return True |
| 127 | |
| 128 | def visit_FieldDirective(self, obj): |
| 129 | return True |
| 130 | |
| 131 | def visit_FieldDirective_post(self, obj): |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 132 | return True |
| 133 | |
| 134 | def visit_FieldType(self, obj): |
| 135 | return True |
| 136 | |
| 137 | def visit_LinkDefinition(self, obj): |
| 138 | return True |
| 139 | |
| 140 | def visit_FieldDefinition(self, obj): |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 141 | return True |
| 142 | |
| 143 | def visit_FieldDefinition_post(self, obj): |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 144 | self.proto_to_xproto_field(obj) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 145 | return True |
| 146 | |
| 147 | def visit_EnumFieldDefinition(self, obj): |
| 148 | return True |
| 149 | |
| 150 | def visit_EnumDefinition(self, obj): |
| 151 | return True |
| 152 | |
| 153 | def visit_MessageDefinition(self, obj): |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 154 | self.current_message_name = obj.name.value.pval |
| 155 | self.message_options = {} |
| 156 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 157 | return True |
| 158 | |
| 159 | def visit_MessageDefinition_post(self, obj): |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 160 | self.proto_to_xproto_message(obj) |
| 161 | obj.body = filter(lambda x:not hasattr(x, 'mark_for_deletion'), obj.body) |
| 162 | obj.body = map(replace_link, obj.body) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 163 | |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 164 | self.current_message_name = None |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 165 | return True |
| 166 | |
| 167 | def visit_MessageExtension(self, obj): |
| 168 | return True |
| 169 | |
| 170 | def visit_MethodDefinition(self, obj): |
| 171 | return True |
| 172 | |
| 173 | def visit_ServiceDefinition(self, obj): |
| 174 | return True |
| 175 | |
| 176 | def visit_ExtensionsDirective(self, obj): |
| 177 | return True |
| 178 | |
| 179 | def visit_Literal(self, obj): |
| 180 | return True |
| 181 | |
| 182 | def visit_Name(self, obj): |
| 183 | return True |
| 184 | |
| 185 | def visit_DotName(self, obj): |
| 186 | return True |
| 187 | |
| 188 | def visit_Proto(self, obj): |
| 189 | self.count_stack.push(len(obj.body)) |
| 190 | return True |
| 191 | |
| 192 | def visit_Proto_post(self, obj): |
| 193 | return True |
| 194 | |
| 195 | def visit_LinkSpec(self, obj): |
| 196 | return False |