Navigation


Changeset 1469:48fa8d70e6ad in freeDiameter


Ignore:
Timestamp:
Mar 18, 2020, 9:40:01 AM (4 years ago)
Author:
Luke Mewburn <luke@mewburn.net>
Branch:
default
Phase:
public
Message:

csv_to_fd: validate flags and other improvements

Validate the flags.
Allow more columns; ignore any past column 8 "Must Not".
Add basename of filenames to the regen command.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • contrib/tools/csv_to_fd

    r1464 r1469  
    77Format of the CSV files is one of:
    88- Row per 3GPP AVP tables:
    9     Name, Code, Section, DataType, Must, May, ShouldNot, MustNot [, ...]
     9    Name, Code, Section, DataType, Must, May, ShouldNot, MustNot [, extra]
    1010    - Name:
    1111        AVP Name. String, validated as ALPHA *(ALPHA / DIGIT / "-")
     
    4545import re
    4646import optparse
     47import os
    4748import sys
    4849
     
    5657    'shouldnot',
    5758    'mustnot',
    58     'encrypt',
    5959]
    6060
     
    8989    _name_re = re.compile(r'^[a-zA-Z0-9][a-zA-Z0-9-\.]*$')
    9090
     91    # Regex to validate flags: M, P, V, comma, space
     92    _flags_re = re.compile(r'^[MPV, ]*$')
     93
    9194    __slots__ = CSV_COLUMN_NAMES + [
    9295        'filename', 'linenum', 'standard', 'vendor', ]
    9396
    9497    def __init__(self, name, code, section, datatype,
    95                  must, may, shouldnot, mustnot, encrypt,
     98                 must, may, shouldnot, mustnot, extra_cells,
    9699                 filename='', linenum=0, standard='', vendor=0):
    97100        # Members from CSV row
     
    104107        self.shouldnot = shouldnot
    105108        self.mustnot = mustnot
    106         self.encrypt = encrypt
    107109        # Members from file state
    108110        self.filename = filename
     
    121123            raise ValueError('Invalid AVP data type "{}"'.format(
    122124                self.datatype))
    123 # TODO: validate must, may, shouldnot, mustnot
     125        for val, desc in [
     126                (self.must, 'Must'),
     127                (self.may, 'May'),
     128                (self.shouldnot, 'Should Not'),
     129                (self.mustnot, 'Must Not'),
     130                ]:
     131            if not self._flags_re.match(val):
     132                raise ValueError('Invalid AVP Flags {} "{}"'.format(desc, val))
    124133
    125134    @property
     
    217226
    218227    def __init__(self):
     228        self.filenames = []
    219229        self.lines = []
    220230
    221231    def next_file(self, filename):
    222         print('/* CSV file: {} */'.format(filename))
     232        self.filenames.append(os.path.basename(filename))
    223233
    224234    def avp(self, avp):
     
    277287        self.print_comment('')
    278288        self.print_comment('The following is created automatically with:')
    279         self.print_comment('    csv_to_fd -p {}'.format(self.cls_name()))
     289        self.print_comment('    csv_to_fd -p {} {}'.format(
     290            self.cls_name(), ' '.join(self.filenames)))
    280291        self.print_comment('Changes will be lost during the next update.')
    281292        self.print_comment('Do not modify;'
     
    412423        avpproc.next_file(filename)
    413424        with open(filename, 'r') as csvfile:
    414             csvdata = csv.DictReader(csvfile, CSV_COLUMN_NAMES)
     425            csvdata = csv.DictReader(csvfile, CSV_COLUMN_NAMES,
     426                                     restkey='extra_cells')
    415427            linenum = 0
    416428            standard = ''
Note: See TracChangeset for help on using the changeset viewer.