Some of the repetitive violations were suppressed.
3.26KiB; Python | 2018-01-28 18:59:31+01 | SLOC 82
1
#!/bin/env python
2
3
##
4
# Copyright(c) 2010-2015 Intel Corporation.
5
# Copyright(c) 2016-2018 Viosoft Corporation.
6
# All rights reserved.
7
#
8
# Redistribution and use in source and binary forms, with or without
9
# modification, are permitted provided that the following conditions
10
# are met:
11
#
12
#   * Redistributions of source code must retain the above copyright
13
#     notice, this list of conditions and the following disclaimer.
14
#   * Redistributions in binary form must reproduce the above copyright
15
#     notice, this list of conditions and the following disclaimer in
16
#     the documentation and/or other materials provided with the
17
#     distribution.
18
#   * Neither the name of Intel Corporation nor the names of its
19
#     contributors may be used to endorse or promote products derived
20
#     from this software without specific prior written permission.
21
#
22
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
##
34
35 11
from decimal import *
36
37
class CsvReaderError:
38
    def __init__(self, msg):
39 1
        self._msg = msg;
40
41
    def __str__(self):
42 1
        return self._msg;
43
44
class CsvReader:
45
    def __init__(self, fieldTypes = None):
46 1
        self._file_name = None;
47 1
        self._fieldTypes = fieldTypes;
48
49
    def open(self, file_name):
50 2
        self._file = open(file_name, 'r');
51 1
        self._file_name = file_name;
52
53
    def read(self):
54
        line = "#"
55
        while (len(line) != 0 and line[0] == "#"):
56 1
            line = self._file.readline();
57
58 2
        if (len(line) != 0):
59
            return self._lineToEntry(line)
60
        else:
61 1
            return None;
62
63
    def _lineToEntry(self, line):
64 1
        split = line.strip().split(',');
65 1
        if (self._fieldTypes is None):
66 1
            return split;
67
        have = len(split)
68
        expected = len(self._fieldTypes)
69 1
        if (have != expected):
70
            raise CsvReaderError("Invalid number of fields %d != %d" % (have, expected))
71
72
        entry = {};
73
        for i in range(len(self._fieldTypes)):
74
            curFieldType = self._fieldTypes[i][1]
75
            curFieldName = self._fieldTypes[i][0];
76 1
            if (curFieldType == "int"):
77
                entry[curFieldName] = int(split[i])
78 1
            elif (curFieldType == "Decimal"):
79
                entry[curFieldName] = Decimal(split[i])
80
            else:
81
                raise CsvReaderError("Invalid field type %s" % curFieldType);
82
        return entry;
83
84
    def readAll(self):
85
        ret = []
86
        line = self.read();
87 1
        while (line != None):
88
            ret.append(line);
89
            line = self.read();
90
        return ret;
91
92
    def close(self):
93
        self._file.close();
94 1
        self._file = None;