Changeset 1479:c0aa1e66c12e in freeDiameter
- Timestamp:
- Mar 26, 2020, 8:28:16 AM (4 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
contrib/tools/csv_to_fd
r1469 r1479 24 24 25 25 - Comment row. First cell: 26 # Comment text Comment text27 #= Header row of ====26 # Comment text 'Comment text' 27 #= '/*========*/' 28 28 # Blank line 29 29 … … 93 93 94 94 __slots__ = CSV_COLUMN_NAMES + [ 95 'filename', 'line num', 'standard', 'vendor', ]95 'filename', 'line_num', 'standard', 'vendor', ] 96 96 97 97 def __init__(self, name, code, section, datatype, 98 must, may, shouldnot, mustnot, extra_cells ,99 filename='', line num=0, standard='', vendor=0):98 must, may, shouldnot, mustnot, extra_cells=[], 99 filename='', line_num=0, standard='', vendor=0): 100 100 # Members from CSV row 101 101 self.name = name … … 109 109 # Members from file state 110 110 self.filename = filename 111 self.line num = linenum111 self.line_num = line_num 112 112 self.standard = standard 113 113 self.vendor = vendor … … 116 116 raise ValueError('Invalid AVP name "{}"'.format(self.name)) 117 117 if (self.code < 0 or self.code > 4294967295): 118 raise ValueError('Invalid AVP code {}'.format(self.code)) 118 raise ValueError('AVP "{}" invalid code {}'.format( 119 self.name, self.code)) 119 120 if (self.datatype not in ( 120 121 'OctetString', 'Integer32', 'Integer64', 'Unsigned32', 121 122 'Unsigned64', 'Float32', 'Float64', 'Grouped') 122 123 and self.datatype not in DERIVED_TO_BASE): 123 raise ValueError('Invalid AVP data type "{}"'.format( 124 self.datatype)) 124 raise ValueError('AVP "{}" invalid data type "{}"'.format( 125 self.name, self.datatype)) 126 # Validate flags 127 flags = collections.Counter() 125 128 for val, desc in [ 126 129 (self.must, 'Must'), … … 130 133 ]: 131 134 if not self._flags_re.match(val): 132 raise ValueError('Invalid AVP Flags {} "{}"'.format(desc, val)) 135 raise ValueError('AVP "{}" invalid {} Flags "{}"'.format( 136 self.name, desc, val)) 137 flags.update(val) 138 # Check occurrence of M,V in Must,May,ShouldNot,MustNot 139 for flag in 'MV': 140 # TODO: can AVP flags not appear at all? 141 # if flags[flag] == 0: 142 # raise ValueError('AVP "{}" Flag "{}" not set'.format( 143 # self.name, flag)) 144 if flags[flag] > 1: 145 raise ValueError('AVP "{}" Flag "{}" set {} times'.format( 146 self.name, flag, flags[flag])) 147 # Compare V presence against vendor 148 if 'V' in self.must: 149 if self.vendor == 0: 150 raise ValueError('AVP "{}" Flag "V" set for vendor 0'.format( 151 self.name)) 152 else: 153 if self.vendor != 0: 154 raise ValueError( 155 'AVP "{}" Flag "V" not set for vendor {}'.format( 156 self.name, self.vendor)) 133 157 134 158 @property … … 169 193 170 194 @abc.abstractmethod 171 def comment(self, comment, filename, line num):195 def comment(self, comment, filename, line_num): 172 196 """Process a comment row: 173 197 #comment, … … 191 215 print('AVP: {name}, {code}, {datatype}'.format(**avpdict)) 192 216 193 def comment(self, comment, filename, line num):217 def comment(self, comment, filename, line_num): 194 218 print('Comment: {}'.format(comment)) 195 219 … … 207 231 pass 208 232 209 def comment(self, comment, filename, line num):233 def comment(self, comment, filename, line_num): 210 234 pass 211 235 … … 272 296 self.add('') 273 297 274 def comment(self, comment, filename, line num):298 def comment(self, comment, filename, line_num): 275 299 if comment == '': 276 300 self.add('') … … 353 377 self.avps.append(row) 354 378 355 def comment(self, comment, filename, line num):379 def comment(self, comment, filename, line_num): 356 380 pass 357 381 … … 419 443 avp_codes = collections.defaultdict(dict) 420 444 445 # dict of [vendor][name] : Avp 446 avp_names = collections.defaultdict(dict) 447 421 448 # Process files 422 449 for filename in args: … … 425 452 csvdata = csv.DictReader(csvfile, CSV_COLUMN_NAMES, 426 453 restkey='extra_cells') 427 linenum = 0428 454 standard = '' 429 455 vendor = 0 430 456 for row in csvdata: 431 linenum += 1432 457 try: 433 458 if row['name'] in (None, '', 'Attribute Name'): … … 435 460 elif row['name'].startswith('#'): 436 461 comment = row['name'][1:] 437 avpproc.comment(comment, filename, linenum)462 avpproc.comment(comment, filename, csvdata.line_num) 438 463 elif row['name'].startswith('@'): 439 464 parameter = row['name'][1:] … … 449 474 parameter)) 450 475 else: 451 avp = Avp(filename=filename, line num=linenum,476 avp = Avp(filename=filename, line_num=csvdata.line_num, 452 477 standard=standard, vendor=vendor, 453 478 **row) … … 459 484 ' in file "{}" line {}'.format( 460 485 avp.vendor, avp.code, 461 conflict.filename, conflict.line num))486 conflict.filename, conflict.line_num)) 462 487 avp_codes[avp.vendor][avp.code] = avp 488 # Ensure AVP vendor/name not already defined 489 if avp.name in avp_names[avp.vendor]: 490 conflict = avp_names[avp.vendor][avp.name] 491 raise ValueError( 492 'AVP vendor {} name "{}" already present' 493 ' in file "{}" line {}'.format( 494 avp.vendor, avp.name, 495 conflict.filename, conflict.line_num)) 496 avp_names[avp.vendor][avp.name] = avp 463 497 # Process AVP 464 498 avpproc.avp(avp) 465 499 except ValueError as e: 466 sys.stderr.write('CSV file "{}" line {}: {} : {}\n'.format(467 filename, linenum, e.__class__.__name__, e))500 sys.stderr.write('CSV file "{}" line {}: {}\n'.format( 501 filename, csvdata.line_num, e)) 468 502 sys.exit(1) 469 503
Note: See TracChangeset
for help on using the changeset viewer.