1 """
2 Class that implements the common functionality for reading and writing CSV-files
3 """
4
5 from optparse import OptionGroup
6
7 from os import path
8
9 from PyFoam.ThirdParty.six import print_
10
12 """ The class implement common functionality
13 """
14
16 calc=OptionGroup(self.parser,
17 "Calculations",
18 "Calculations to be performed on the data. Format is '<name>:::<expr>' (three colons should not appear in variable names). In the expressions there are variables that correspond to the column names. Also a variable 'data' that can be subscripted (for columns that are not valid variable names)")
19 calc.add_option("--recalc-columns",
20 action="append",
21 dest="recalcColumns",
22 default=[],
23 help="Columns that should be recalculated after reading. Can be specified more than once. In the expression a variable 'this' can reference the variable itself")
24 calc.add_option("--regular-expression-for-recalculation",
25 action="store_true",
26 dest="regularExpressionRecalc",
27 default=False,
28 help="The name in recalculations is a regular expression that must match the existing names")
29 calc.add_option("--raw-data-add-column",
30 action="append",
31 dest="rawAddColumns",
32 default=[],
33 help="Columns that should be added to the data after reading")
34 calc.add_option("--joined-data-add-column",
35 action="append",
36 dest="joinedAddColumns",
37 default=[],
38 help="Columns that should be added to the data before writing")
39 self.parser.add_option_group(calc)
40
41 info=OptionGroup(self.parser,
42 "Info",
43 "Information about the data")
44 info.add_option("--print-columns",
45 action="store_true",
46 dest="printColums",
47 default=False,
48 help="Print the column names found")
49 self.parser.add_option_group(info)
50
51 data=OptionGroup(self.parser,
52 "Data",
53 "Specification on the data that is read in")
54 self.parser.add_option_group(data)
55 data.add_option("--time-name",
56 action="store",
57 dest="time",
58 default=None,
59 help="Name of the time column")
60 data.add_option("--column-names",
61 action="append",
62 default=[],
63 dest="columns",
64 help="The columns (names) which should be copied to the CSV. All if unset")
65 data.add_option("--regexp-column-names",
66 action="store_true",
67 default=False,
68 dest="columnsRegexp",
69 help="The column names should be matched as regular expressions")
70
71 formt=OptionGroup(self.parser,
72 "Format",
73 "Specification on the format of the data read and written")
74 self.parser.add_option_group(formt)
75 formt.add_option("--write-excel-file",
76 action="store_true",
77 dest="writeExcel",
78 default=False,
79 help="Write to Excel-file instead of plain CSV. Onle works with the python-libraries pandas and xlwt")
80 formt.add_option("--read-excel-file",
81 action="store_true",
82 dest="readExcel",
83 default=False,
84 help="Read from Excel-file instead of plain CSV. Onle works with the python-libraries pandas and xlrd")
85 formt.add_option("--automatic-format",
86 action="store_true",
87 dest="automaticFormat",
88 default=False,
89 help="Determine from the file extension whether the files are CSV, Excel or plain text")
90 formt.add_option("--delimiter",
91 action="store",
92 dest="delimiter",
93 default=',',
94 help="Delimiter to be used between the values. Default: %default")
95
96
100
103
106
109
111 for s in specs:
112 try:
113 name,expr=s.split(":::")
114 except ValueError:
115 self.error(s,"can not be split correctly with ':::':",s.split(":::"))
116 if not create and self.opts.regularExpressionRecalc:
117 import re
118 rex=re.compile(name)
119 for n in data.names():
120 if rex.match(n):
121 data.recalcData(n,expr)
122 else:
123 data.recalcData(name,expr,create)
124
153
154
155