Mercurial > hg > freeDiameter
comparison contrib/tools/csv_to_fd @ 1506:2ab9d941bad5
csv_to_fd: improve validation error handling
author | Luke Mewburn <luke@mewburn.net> |
---|---|
date | Fri, 03 Apr 2020 17:04:13 +1100 |
parents | 01efba83bf60 |
children | 7c9214a5f095 |
comparison
equal
deleted
inserted
replaced
1505:b382cbd1a710 | 1506:2ab9d941bad5 |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 """ | 3 """ |
4 Convert CSV files containing RADIUS or Diameter AVP tables | 4 Convert 8 column CSV files containing RADIUS or Diameter AVP tables |
5 into various formats. | 5 into various formats. |
6 | 6 |
7 Format of the CSV files is one of: | 7 Format of the CSV files is one of: |
8 - Row per 3GPP AVP tables: | 8 - Row per 3GPP AVP tables: |
9 Name, Code, Section, DataType, Must, May, ShouldNot, MustNot [, extra] | 9 Name, Code, Section, DataType, Must, May, ShouldNot, MustNot |
10 - Name: | 10 - Name: |
11 AVP Name. String, validated as ALPHA *(ALPHA / DIGIT / "-") | 11 AVP Name. String, validated as ALPHA *(ALPHA / DIGIT / "-") |
12 per RFC 6733 section 3.2. | 12 per RFC 6733 section 3.2. |
13 May start with a DIGIT (e.g., "3GPP-IMSI"). | 13 May start with a DIGIT (e.g., "3GPP-IMSI"). |
14 - Code: | 14 - Code: |
598 # Process files | 598 # Process files |
599 for filename in args: | 599 for filename in args: |
600 avpproc.filename(filename) | 600 avpproc.filename(filename) |
601 with open(filename, 'r') as csvfile: | 601 with open(filename, 'r') as csvfile: |
602 csvdata = csv.DictReader(csvfile, CSV_COLUMN_NAMES, | 602 csvdata = csv.DictReader(csvfile, CSV_COLUMN_NAMES, |
603 restkey='extra_cells') | 603 restkey='extra_cells', restval='') |
604 standard = '' | 604 standard = '' |
605 vendor = 0 | 605 vendor = 0 |
606 errors = [] | 606 errors = [] |
607 for row in csvdata: | 607 for row in csvdata: |
608 try: | 608 try: |
609 if csvdata.restkey in row: | |
610 raise ValueError('Extra cells: {}'.format( | |
611 ','.join(row[csvdata.restkey]))) | |
609 if row['name'] in (None, '', 'Attribute Name'): | 612 if row['name'] in (None, '', 'Attribute Name'): |
610 continue | 613 continue |
611 elif row['name'].startswith('#'): | 614 elif row['name'].startswith('#'): |
612 comment = row['name'][1:] | 615 comment = row['name'][1:] |
613 avpproc.comment(comment, filename, csvdata.line_num) | 616 avpproc.comment(comment, filename, csvdata.line_num) |
638 conflict = avp_names[avp.vendor][avp.name] | 641 conflict = avp_names[avp.vendor][avp.name] |
639 avp_conflict('Name', avp, conflict) | 642 avp_conflict('Name', avp, conflict) |
640 avp_names[avp.vendor][avp.name] = avp | 643 avp_names[avp.vendor][avp.name] = avp |
641 # Process AVP | 644 # Process AVP |
642 avpproc.avp(avp) | 645 avpproc.avp(avp) |
643 except ValueError as e: | 646 except (TypeError, ValueError) as e: |
644 errors.append('CSV file "{}" line {}: {}\n'.format( | 647 errors.append('CSV file "{}" line {}: {}\n'.format( |
645 filename, csvdata.line_num, e)) | 648 filename, csvdata.line_num, e)) |
646 if errors: | 649 if errors: |
647 sys.stderr.write(''.join(errors)) | 650 sys.stderr.write(''.join(errors)) |
648 sys.exit(1) | 651 sys.exit(1) |