xref: /linux/drivers/comedi/drivers/ni_routing/tools/csv_collection.py (revision 172cdcaefea5c297fdb3d20b7d5aff60ae4fbce6)
1# SPDX-License-Identifier: GPL-2.0+
2# vim: ts=2:sw=2:et:tw=80:nowrap
3
4import os, csv, glob
5
6class CSVCollection(dict):
7  delimiter=';'
8  quotechar='"'
9  source_column_name = 'Sources / Destinations'
10
11  """
12  This class is a dictionary representation of the collection of sheets that
13  exist in a given .ODS file.
14  """
15  def __init__(self, pattern, skip_commented_lines=True, strip_lines=True):
16    super(CSVCollection, self).__init__()
17    self.pattern = pattern
18    C = '#' if skip_commented_lines else 'blahblahblah'
19
20    if strip_lines:
21      strip = lambda s:s.strip()
22    else:
23      strip = lambda s:s
24
25    # load all CSV files
26    key = self.source_column_name
27    for fname in glob.glob(pattern):
28      with open(fname) as F:
29        dR = csv.DictReader(F, delimiter=self.delimiter,
30                            quotechar=self.quotechar)
31        name = os.path.basename(fname).partition('.')[0]
32        D = {
33          r[key]:{f:strip(c) for f,c in r.items()
34                  if f != key and f[:1] not in ['', C] and
35                     strip(c)[:1] not in ['', C]}
36          for r in dR if r[key][:1] not in ['', C]
37        }
38        # now, go back through and eliminate all empty dictionaries
39        D = {k:v for k,v in D.items() if v}
40        self[name] = D
41