diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2018-04-19 19:01:44 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2018-04-21 11:23:29 +0200 |
commit | 7bbaac30cdd78247c836c1349f5dcad23dd52c64 (patch) | |
tree | 2ec3ac2272b89621328a3854c73f1d13e218afe4 | |
parent | 88002fc12d24add1876a169d2e310398c1348732 (diff) |
Add --skip-columns option
This option can be used to skip a number of rows in the CSV file before
the key data is read. If the number of rows to skip is 0, the column
interpretation should be provided using the --columns option.
-rw-r--r-- | docs/csv2pskc.rst | 8 | ||||
-rw-r--r-- | pskc/scripts/csv2pskc.py | 9 | ||||
-rw-r--r-- | tests/test_csv2pskc.doctest | 49 |
3 files changed, 65 insertions, 1 deletions
diff --git a/docs/csv2pskc.rst b/docs/csv2pskc.rst index 6f70cfb..e177136 100644 --- a/docs/csv2pskc.rst +++ b/docs/csv2pskc.rst @@ -51,6 +51,14 @@ Options properties (e.g. use of ``id+serial`` sets both the ID and device serial number to the value found in that column). +.. option:: --skip-rows N + + By default the first row is treated as a header which contains labels. + This option can be used to either skip more row (the first row of the CSV file will + still be treated as a header) or to indicate that there is no header row. + + In the latter case the :option:`--columns` option is required. + .. option:: -x COL=VALUE, --set COL=VALUE Specify properties that are added to all keys in the generated PSKC file. diff --git a/pskc/scripts/csv2pskc.py b/pskc/scripts/csv2pskc.py index 327e169..e1cf7b6 100644 --- a/pskc/scripts/csv2pskc.py +++ b/pskc/scripts/csv2pskc.py @@ -56,6 +56,9 @@ parser.add_argument( '-c', '--columns', metavar='COL|COL:LABEL,..', help='list of columns or label to column mapping to import') parser.add_argument( + '--skip-rows', metavar='N', type=int, default=1, + help='the number of rows before rows with key information start') +parser.add_argument( '-x', '--set', metavar='COL=VALUE', action='append', type=lambda x: x.split('=', 1), dest='extra_columns', help='add an extra value that is added to all key containers') @@ -111,7 +114,11 @@ def main(): # open the CSV file csvfile = open_csvfile(open(args.input, 'r') if args.input else sys.stdin) # figure out the meaning of the columns - columns = [x.lower().replace(' ', '_') for x in next(csvfile)] + columns = [] + if args.skip_rows > 0: + columns = [x.lower().replace(' ', '_') for x in next(csvfile)] + for i in range(args.skip_rows - 1): + next(csvfile) if args.columns: if ':' in args.columns: # --columns is a list of mappings diff --git a/tests/test_csv2pskc.doctest b/tests/test_csv2pskc.doctest index c01b32b..d67987c 100644 --- a/tests/test_csv2pskc.doctest +++ b/tests/test_csv2pskc.doctest @@ -439,6 +439,55 @@ to all keys in the PSKC file: </pskc:KeyContainer> +The --skip-rows option can be used to either not use the first row to denote +the key properties that are set (in which case the --columns option is +mandatory) or skip more rows at the beginning of the file. + +>>> f = tempfile.NamedTemporaryFile('w+t') +>>> x = f.write(''' +... 987654321,7c613e9c2194ff7da7f4770ab2ed712111fcbe95 +... 987654322,4be618e3459e936137994854bc3d2ebe46f3cce2 +... '''.lstrip()) +>>> f.flush() +>>> sys.argv = ['csv2pskc', f.name, '--skip-rows=0', '--columns=id+serial,secret'] +>>> main() #doctest: +ELLIPSIS +NORMALIZE_WHITESPACE +REPORT_UDIFF +<?xml version="1.0" encoding="UTF-8"?> +<pskc:KeyContainer xmlns:pskc="urn:ietf:params:xml:ns:keyprov:pskc" Version="1.0"> +... + <pskc:Key Id="987654321"> +... + <pskc:PlainValue>fGE+nCGU/32n9HcKsu1xIRH8vpU=</pskc:PlainValue> +... + <pskc:Key Id="987654322"> +... + <pskc:PlainValue>S+YY40Wek2E3mUhUvD0uvkbzzOI=</pskc:PlainValue> +... +</pskc:KeyContainer> + +>>> f = tempfile.NamedTemporaryFile('w+t') +>>> x = f.write(''' +... id+serial,secret +... IGNORED LINE +... 987654321,7c613e9c2194ff7da7f4770ab2ed712111fcbe95 +... 987654322,4be618e3459e936137994854bc3d2ebe46f3cce2 +... '''.lstrip()) +>>> f.flush() +>>> sys.argv = ['csv2pskc', f.name, '--skip-rows=2'] +>>> main() #doctest: +ELLIPSIS +NORMALIZE_WHITESPACE +REPORT_UDIFF +<?xml version="1.0" encoding="UTF-8"?> +<pskc:KeyContainer xmlns:pskc="urn:ietf:params:xml:ns:keyprov:pskc" Version="1.0"> +... + <pskc:Key Id="987654321"> +... + <pskc:PlainValue>fGE+nCGU/32n9HcKsu1xIRH8vpU=</pskc:PlainValue> +... + <pskc:Key Id="987654322"> +... + <pskc:PlainValue>S+YY40Wek2E3mUhUvD0uvkbzzOI=</pskc:PlainValue> +... +</pskc:KeyContainer> + + We can encrypt the resulting PSKC file with a passphrase. >>> f = tempfile.NamedTemporaryFile('w+t') |