22 # You should have received a copy of the GNU General Public License |
22 # You should have received a copy of the GNU General Public License |
23 # along with this program; if not, write to the Free Software |
23 # along with this program; if not, write to the Free Software |
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
25 |
25 |
26 |
26 |
27 |
|
28 import os |
27 import os |
29 from POULibrary import POULibrary |
28 from POULibrary import POULibrary |
30 from py_ext.PythonFileCTNMixin import PythonFileCTNMixin |
29 from py_ext.PythonFileCTNMixin import PythonFileCTNMixin |
31 import util.paths as paths |
30 import util.paths as paths |
32 |
31 |
44 global csv_int_files |
43 global csv_int_files |
45 data = csv_int_files.get(fname, None) |
44 data = csv_int_files.get(fname, None) |
46 if data is None: |
45 if data is None: |
47 data = list() |
46 data = list() |
48 try: |
47 try: |
49 csvfile = open(fname, 'rb') |
48 csvfile = open(fname, 'rt', encoding='utf-8') |
50 except IOError: |
49 except IOError: |
51 return "#FILE_NOT_FOUND" |
50 return "#FILE_NOT_FOUND" |
52 try: |
51 try: |
53 dialect = csv.Sniffer().sniff(csvfile.read(1024)) |
52 dialect = csv.Sniffer().sniff(csvfile.read(1024)) |
54 csvfile.seek(0) |
53 csvfile.seek(0) |
55 reader = csv.reader(csvfile, dialect) |
54 reader = csv.reader(csvfile, dialect) |
56 for row in reader: |
55 for row in reader: |
57 data.append(row) |
56 data.append(row) |
58 except csv.Error: |
57 except csv.Error as e: |
59 return "#CSV_ERROR" |
58 return "#CSV_ERROR" |
60 finally: |
59 finally: |
61 csvfile.close() |
60 csvfile.close() |
62 csv_int_files[fname] = data |
61 csv_int_files[fname] = data |
63 |
62 |
64 try: |
63 try: |
65 row = data[rowidx] |
64 row = data[rowidx] |
66 except IndexError: |
65 except IndexError: |
67 return "#ROW_NOT_FOUND" |
66 return "#ROW_NOT_FOUND" |
68 |
67 |
81 global csv_str_files |
80 global csv_str_files |
82 entry = csv_str_files.get(fname, None) |
81 entry = csv_str_files.get(fname, None) |
83 if entry is None: |
82 if entry is None: |
84 data = dict() |
83 data = dict() |
85 try: |
84 try: |
86 csvfile = open(fname, 'rb') |
85 csvfile = open(fname, 'rt', encoding='utf-8') |
87 except IOError: |
86 except IOError: |
88 return "#FILE_NOT_FOUND" |
87 return "#FILE_NOT_FOUND" |
89 try: |
88 try: |
90 dialect = csv.Sniffer().sniff(csvfile.read(1024)) |
89 dialect = csv.Sniffer().sniff(csvfile.read(1024)) |
91 csvfile.seek(0) |
90 csvfile.seek(0) |
92 reader = csv.reader(csvfile, dialect) |
91 reader = csv.reader(csvfile, dialect) |
93 headers = dict([(name, index) for index, name in enumerate(reader.next()[1:])]) |
92 headers = dict([(name, index) for index, name in enumerate(reader.__next__()[1:])]) |
94 for row in reader: |
93 for row in reader: |
95 data[row[0]] = row[1:] |
94 data[row[0]] = row[1:] |
96 except csv.Error: |
95 except csv.Error: |
97 return "#CSV_ERROR" |
96 return "#CSV_ERROR" |
98 finally: |
97 finally: |
99 csvfile.close() |
98 csvfile.close() |
100 csv_str_files[fname] = (headers, data) |
99 csv_str_files[fname] = (headers, data) |
101 else: |
100 else: |
102 headers, data = entry |
101 headers, data = entry |
103 |
102 |
104 try: |
103 try: |
105 row = data[rowname] |
104 row = data[rowname] |
106 except KeyError: |
105 except KeyError: |
107 return "#ROW_NOT_FOUND" |
106 return "#ROW_NOT_FOUND" |
108 |
107 |
113 |
112 |
114 try: |
113 try: |
115 return row[colidx] |
114 return row[colidx] |
116 except IndexError: |
115 except IndexError: |
117 return "#COL_NOT_FOUND" |
116 return "#COL_NOT_FOUND" |
|
117 |
|
118 |
|
119 csv_int_files = {} |
|
120 def CSVWrInt(fname, rowidx, colidx, content, save): |
|
121 \"\"\" |
|
122 Update value at row/column pointed by integer indexes |
|
123 Assumes data starts at first row and first column, no headers. |
|
124 \"\"\" |
|
125 if save > 0: |
|
126 global csv_int_files |
|
127 data = csv_int_files.get(fname, None) |
|
128 if data is None: |
|
129 data = list() |
|
130 try: |
|
131 csvfile = open(fname, 'rt', encoding='utf-8') |
|
132 except IOError: |
|
133 return "#FILE_NOT_FOUND" |
|
134 try: |
|
135 dialect = csv.Sniffer().sniff(csvfile.read(1024)) |
|
136 csvfile.seek(0) |
|
137 reader = csv.reader(csvfile, dialect) |
|
138 for row in reader: |
|
139 data.append(row) |
|
140 except csv.Error as e: |
|
141 return "#CSV_ERROR" |
|
142 finally: |
|
143 csvfile.close() |
|
144 csv_int_files[fname] = data |
|
145 |
|
146 try: |
|
147 row = data[rowidx] |
|
148 except IndexError: |
|
149 return "#ROW_NOT_FOUND" |
|
150 |
|
151 try: |
|
152 row[colidx] = content |
|
153 except IndexError: |
|
154 return "#COL_NOT_FOUND" |
|
155 |
|
156 wfile = open(fname, 'rt+', encoding='utf-8') |
|
157 wdialect = csv.Sniffer().sniff(wfile.read(1024)) |
|
158 wfile.seek(0) |
|
159 writer = csv.writer(wfile, wdialect) |
|
160 for row in data: |
|
161 writer.writerow(row) |
|
162 wfile.truncate() |
|
163 wfile.close() |
|
164 |
|
165 return "#SUCCESS" |
|
166 |
118 |
167 |
119 def pyext_csv_reload(): |
168 def pyext_csv_reload(): |
120 global csv_int_files, csv_str_files |
169 global csv_int_files, csv_str_files |
121 csv_int_files.clear() |
170 csv_int_files.clear() |
122 csv_str_files.clear() |
171 csv_str_files.clear() |
123 |
172 |
124 """ |
173 """ |
|
174 |
125 |
175 |
126 class PythonLibrary(POULibrary): |
176 class PythonLibrary(POULibrary): |
127 def GetLibraryPath(self): |
177 def GetLibraryPath(self): |
128 return paths.AbsNeighbourFile(__file__, "pous.xml") |
178 return paths.AbsNeighbourFile(__file__, "pous.xml") |
129 |
179 |
155 runtimefile.close() |
205 runtimefile.close() |
156 return ((["py_ext"], [(Gen_Pythonfile_path, IECCFLAGS)], True), "", |
206 return ((["py_ext"], [(Gen_Pythonfile_path, IECCFLAGS)], True), "", |
157 ("runtime_00_pyext.py", open(runtimefile_path, "rb"))) |
207 ("runtime_00_pyext.py", open(runtimefile_path, "rb"))) |
158 |
208 |
159 |
209 |
160 |
|
161 class PythonFile(PythonFileCTNMixin): |
210 class PythonFile(PythonFileCTNMixin): |
162 |
211 |
163 def GetIconName(self): |
212 def GetIconName(self): |
164 return "Pyfile" |
213 return "Pyfile" |