28 import os |
28 import os |
29 from POULibrary import POULibrary |
29 from POULibrary import POULibrary |
30 from py_ext.PythonFileCTNMixin import PythonFileCTNMixin |
30 from py_ext.PythonFileCTNMixin import PythonFileCTNMixin |
31 import util.paths as paths |
31 import util.paths as paths |
32 |
32 |
|
33 pyext_python_lib_code = """ |
|
34 |
|
35 import csv |
|
36 from collections import OrderedDict |
|
37 |
|
38 csv_int_files = {} |
|
39 def CSVRdInt(fname, rowidx, colidx): |
|
40 \"\"\" |
|
41 Return value at row/column pointed by integer indexes |
|
42 Assumes data starts at first row and first column, no headers. |
|
43 \"\"\" |
|
44 global csv_int_files |
|
45 data = csv_int_files.get(fname, None) |
|
46 if data is None: |
|
47 data = list() |
|
48 try: |
|
49 csvfile = open(fname, 'rb') |
|
50 except IOError: |
|
51 return "#FILE_NOT_FOUND" |
|
52 try: |
|
53 dialect = csv.Sniffer().sniff(csvfile.read(1024)) |
|
54 csvfile.seek(0) |
|
55 reader = csv.reader(csvfile, dialect) |
|
56 for row in reader: |
|
57 data.append(row) |
|
58 except csv.Error: |
|
59 return "#CSV_ERROR" |
|
60 finally: |
|
61 csvfile.close() |
|
62 csv_int_files[fname] = data |
|
63 |
|
64 try: |
|
65 row = data[rowidx] |
|
66 except IndexError: |
|
67 return "#ROW_NOT_FOUND" |
|
68 |
|
69 try: |
|
70 return row[colidx] |
|
71 except IndexError: |
|
72 return "#COL_NOT_FOUND" |
|
73 |
|
74 |
|
75 csv_str_files = {} |
|
76 def CSVRdStr(fname, rowname, colname): |
|
77 \"\"\" |
|
78 Return value at row/column pointed by a pair of names as string |
|
79 Assumes first row is column headers and first column is row name. |
|
80 \"\"\" |
|
81 global csv_str_files |
|
82 entry = csv_str_files.get(fname, None) |
|
83 if entry is None: |
|
84 data = dict() |
|
85 try: |
|
86 csvfile = open(fname, 'rb') |
|
87 except IOError: |
|
88 return "#FILE_NOT_FOUND" |
|
89 try: |
|
90 dialect = csv.Sniffer().sniff(csvfile.read(1024)) |
|
91 csvfile.seek(0) |
|
92 reader = csv.reader(csvfile, dialect) |
|
93 headers = dict([(name, index) for index, name in enumerate(reader.next()[1:])]) |
|
94 for row in reader: |
|
95 data[row[0]] = row[1:] |
|
96 except csv.Error: |
|
97 return "#CSV_ERROR" |
|
98 finally: |
|
99 csvfile.close() |
|
100 csv_str_files[fname] = (headers, data) |
|
101 else: |
|
102 headers, data = entry |
|
103 |
|
104 try: |
|
105 row = data[rowname] |
|
106 except KeyError: |
|
107 return "#ROW_NOT_FOUND" |
|
108 |
|
109 try: |
|
110 colidx = headers[colname] |
|
111 except KeyError: |
|
112 return "#COL_NOT_FOUND" |
|
113 |
|
114 try: |
|
115 return row[colidx] |
|
116 except IndexError: |
|
117 return "#COL_NOT_FOUND" |
|
118 |
|
119 def pyext_csv_reload(): |
|
120 global csv_int_files, csv_str_files |
|
121 csv_int_files.clear() |
|
122 csv_str_files.clear() |
|
123 |
|
124 """ |
33 |
125 |
34 class PythonLibrary(POULibrary): |
126 class PythonLibrary(POULibrary): |
35 def GetLibraryPath(self): |
127 def GetLibraryPath(self): |
36 return paths.AbsNeighbourFile(__file__, "pous.xml") |
128 return paths.AbsNeighbourFile(__file__, "pous.xml") |
37 |
129 |
55 Gen_Pythonfile_path = os.path.join(buildpath, "py_ext.c") |
147 Gen_Pythonfile_path = os.path.join(buildpath, "py_ext.c") |
56 pythonfile = open(Gen_Pythonfile_path, 'w') |
148 pythonfile = open(Gen_Pythonfile_path, 'w') |
57 pythonfile.write(plc_python_code) |
149 pythonfile.write(plc_python_code) |
58 pythonfile.close() |
150 pythonfile.close() |
59 |
151 |
60 return (["py_ext"], [(Gen_Pythonfile_path, IECCFLAGS)], True), "" |
152 runtimefile_path = os.path.join(buildpath, "runtime_00_pyext.py") |
|
153 runtimefile = open(runtimefile_path, 'w') |
|
154 runtimefile.write(pyext_python_lib_code) |
|
155 runtimefile.close() |
|
156 return ((["py_ext"], [(Gen_Pythonfile_path, IECCFLAGS)], True), "", |
|
157 ("runtime_00_pyext.py", open(runtimefile_path, "rb"))) |
|
158 |
61 |
159 |
62 |
160 |
63 class PythonFile(PythonFileCTNMixin): |
161 class PythonFile(PythonFileCTNMixin): |
64 |
162 |
65 def GetIconName(self): |
163 def GetIconName(self): |