From 7bbaac30cdd78247c836c1349f5dcad23dd52c64 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Thu, 19 Apr 2018 19:01:44 +0200 Subject: 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. --- docs/csv2pskc.rst | 8 ++++++++ pskc/scripts/csv2pskc.py | 9 ++++++++- tests/test_csv2pskc.doctest | 49 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) 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 @@ -55,6 +55,9 @@ parser.add_argument( 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', @@ -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: +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 + + +... + +... + fGE+nCGU/32n9HcKsu1xIRH8vpU= +... + +... + S+YY40Wek2E3mUhUvD0uvkbzzOI= +... + + +>>> 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 + + +... + +... + fGE+nCGU/32n9HcKsu1xIRH8vpU= +... + +... + S+YY40Wek2E3mUhUvD0uvkbzzOI= +... + + + We can encrypt the resulting PSKC file with a passphrase. >>> f = tempfile.NamedTemporaryFile('w+t') -- cgit v1.2.3