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