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