Debian Codegraph @

src/python/qutesheet.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # This file defines the QuteSheet API
3 # It is part of QuteCsound and is licensed under the GPLv3 or LGPLv2
4 # Please see QuteCsound for more information
5 # qutecsound.sourceforge.net
6
7 majorversion = 1
8 minorversion = 0
9 subversion = 0
10
11 version_text = str(majorversion) + '.' + str(minorversion) + '.' + str(subversion)
12
13 class InconsistentData(Exception):
14 """Exception raised for errors in the input.
15
16 Attributes:
17 expr -- input expression in which the error occurred
18 msg -- explanation of the error
19 """
20
21 def __init__(self, expr, msg):
22 self.expr = expr
23 self.msg = msg
24
25 class QuteSheet:
26 """The QuteSheet API"""
27 def __init__(self, qutesheet_data):
28 self.data = qutesheet_data
29 cons = self._test_consistency()
30 self.out_filename = qutesheet_data.out_filename
31 self.first_row = qutesheet_data.row
32 self.first_col = qutesheet_data.col
33 self.num_rows = qutesheet_data.num_rows
34 self.num_cols = qutesheet_data.num_cols
35 self.total_rows = qutesheet_data.total_rows
36 self.total_cols = qutesheet_data.total_cols
37
38 def text(self):
39 return self._rows_to_text(self.data.data_all);
40
41 def selection_text(self):
42 return self._rows_to_text(self.selection_by_rows());
43
44 # By rows
45 def selection_by_rows(self):
46 return self._some_cols(self.data.data_all, self.first_row, self.num_rows,
47 self.first_col, self.num_cols)
48
49 def selection_full_rows(self):
50 return self._all_cols(self.data.data_all, self.first_row, self.num_rows)
51
52 def selection_full_rows_sorted(self):
53 return self.sort_by_start(self._all_cols(self.data.data_all,
54 self.first_row, self.num_rows))
55
56 def all_rows(self):
57 return self.data.data_all
58
59 def all_rows_sorted(self):
60 return self.sort_by_start(self.data.data_all)
61
62 # By Columns
63 def selection_by_cols(self):
64 return self._transpose(self._some_cols(self.data.data_all, self.first_row, self.num_rows,
65 self.first_col, self.num_cols))
66
67 def selection_full_cols(self):
68 return self._transpose(self._some_cols(self.data.data_all, 0, self.total_rows,
69 self.first_col, self.num_cols))
70
71 def all_cols(self):
72 return self._transpose(self.data.data_all);
73
74 # By Cells
75 def cells_selection_by_row(self):
76 return self._get_cells_by_row(self.selection_by_rows());
77
78 def cells_all_by_row(self):
79 return self._get_cells_by_row(self.data.data_all);
80
81 def cells_selection_by_col(self):
82 return self._get_cells_by_row(self._transpose(self.selection_by_rows()));
83
84 def cells_all_by_col(self):
85 return self._get_cells_by_row(self._transpose(self.data.data_all));
86
87 # ----------------
88 # Data setting functions (for data output from script)
89
90 def set_rows(self, new_data, new_first_row = -1, new_first_col = -1, new_num_rows = -1, new_num_cols = -1):
91 """Set the data for the text output organized by rows"""
92 if new_first_row == -1:
93 new_first_row = self.first_row
94 if new_first_col == -1:
95 new_first_col = self.first_col
96 if new_num_rows == -1:
97 new_num_rows = self.num_rows
98 if new_num_cols == -1:
99 new_num_cols = self.num_cols
100 out_text = '__@ ' + str(new_first_row) + ' ' + str(new_first_col)
101 out_text += ' ' + str(new_num_rows) + ' ' + str(new_num_cols) + '\n'
102 out_text += self._rows_to_text(new_data)
103 self._write_out_file(out_text)
104 sys.exit()
105
106 def set_cols(self, new_data, new_first_row = -1, new_first_col = -1, new_num_rows = -1, new_num_cols = -1):
107 """Set the data for the text output organized by columns"""
108 self.set_rows(self._transpose(new_data), new_first_row, new_first_col, new_num_rows, new_num_cols)
109
110 def set_cells_by_row(self, new_data, new_first_row = -1, new_first_col = -1, new_num_rows = -1, new_num_cols = -1):
111 """Set the data for the text output as a list of individual cells organized by rows"""
112 if new_first_row == -1:
113 new_first_row = self.first_row
114 if new_first_col == -1:
115 new_first_col = self.first_col
116 if new_num_rows == -1:
117 new_num_rows = self.num_rows
118 if new_num_cols == -1:
119 new_num_cols = self.num_cols
120 out_data = []
121 for i in range(new_num_rows):
122 out_data.append([])
123 for j in range(new_num_cols):
124 if len(new_data) > 0:
125 out_data[i].append(new_data.pop(0))
126 set_rows(out_data, new_first_row, new_first_col, new_num_rows, new_num_cols)
127
128 def set_cells_by_col(self, new_data, new_first_row = -1, new_first_col = -1, new_num_rows = -1, new_num_cols = -1):
129 """Set the data for the text output as a list of individual cells organized by columns"""
130 U print "Warning! set_cells_by_col() not implemented yet!"
131
132 def set_text(self, text, new_first_row = -1, new_first_col = -1, new_num_rows = -1, new_num_cols = -1):
133 """Set data from a text string, each line is interpreted as a row and each cell must be separated by a space."""
134 if new_first_row == -1:
135 new_first_row = self.first_row
136 if new_first_col == -1:
137 new_first_col = self.first_col
138 if new_num_rows == -1:
139 new_num_rows = self.num_rows
140 if new_num_cols == -1:
141 new_num_cols = self.num_cols
142 out_text = '__@ ' + str(new_first_row) + ' ' + str(new_first_col)
143 out_text += ' ' + str(new_num_rows) + str(new_num_cols) + '\n'
144 out_text += text
145 _write_out_file(out_text)
146
147 # ----------------
148 # Utility functions
149
150 def sort_by_start(self, rows, p_field = 2):
151 """Sort rows by start time (p2 by default)"""
152 new_data = []
153 for r in rows:
154 if len(r) < p_field + 1:
155 new_data.append(r)
156 else:
157 count = 0
158 while count < len(new_data):
159 if type(r[p_field]) != str and r[p_field] <= new_data[count][p_field]:
160 break
161 count += 1
162 new_data.insert(count, r)
163 return new_data
164
165 # ----------------
166 # internal functions
167
168 def _test_consistency(self):
169 d = self.data
170 if (d.total_rows != len(d.data_all)):
171 raise InconsistentData(d.num_rows, "Inconsistent number of rows.")
172 if (d.total_cols != len(self._transpose(d.data_all))):
173 raise InconsistentData(d.num_cols, "Inconsistent number of columns.")
174
175 def _get_cells_by_row(self, rows):
176 cells = []
177 for r in rows:
178 for c in r:
179 cells.append(c)
180 return cells
181
182 def _transpose(self, mtx):
183 return zip(*mtx)
184
185 def _some_cols(self, data, row, num_rows, col, num_cols):
186 rows = []
187 for r in range(row, row + num_rows):
188 new_row = []
189 for c in range(col, col + num_cols):
190 new_row.append(data[r][c])
191 rows.append(new_row)
192 return rows
193
194 def _all_cols(self, data, row, num_rows):
195 rows = []
196 for r in range(row, row + num_rows):
197 rows.append(data[r])
198 return rows
199
200 def _rows_to_text(self, rows):
201 out_text = ""
202 for row in rows:
203 for cell in row:
204 if type(cell)==str:
205 out_text += "" + cell + " "
206 else:
207 out_text += str(cell) + " "
208 out_text += '\n'
209 return out_text
210
211 def _write_out_file(self, text):
212 f = open(self.out_filename, "w")
213 f.write(text);
214 f.close()
215
216 #-----------------------------------------------------------------------------
217
218
219 import sys
220
221 try:
222 import qutesheet_data
223 except ImportError:
224 print "QuteSheet: Can't find Event Sheet Data. Aborting!"
225 sys.exit()
226
227 _defObj = QuteSheet(qutesheet_data)
228 #_defObj.set_out_filename(qutesheet_data.out_filename)
229
230
231 # ----------------
232 # Data properties
233
234 first_col = _defObj.first_col
235 first_row = _defObj.first_row
236 num_rows = _defObj.num_rows
237 num_cols = _defObj.num_cols
238 total_rows = _defObj.total_rows
239 total_cols = _defObj.total_cols
240
241 def text():
242 """Get the text of all data"""
243 return _defObj.text()
244
245 def selection_text():
246 """Get the selected data text"""
247 return _defObj.selection_text()
248
249 # Data by rows
250 def selection_by_rows():
251 """Get the selected data grouped in rows"""
252 return _defObj.selection_by_rows()
253
254 def selection_full_rows():
255 """Get the selected data grouped in rows containing all (even non selected) columns"""
256 return _defObj.selection_full_rows()
257
258 def selection_full_rows_sorted():
259 """Get the selected data grouped in rows containing all (even non selected) columns, sorted
260 according to the third element (p2)"""
261 return _defObj.selection_full_rows_sorted()
262
263 def all_rows():
264 """Get all the data grouped in rows"""
265 return _defObj.all_rows()
266
267 def all_rows_sorted():
268 """Get all the data grouped in rows, sorted according to the third element (p2)"""
269 return _defObj.all_rows_sorted()
270
271 # By Columns
272 def selection_by_cols():
273 """Get the selected data grouped in columns"""
274 return _defObj.selection_by_cols()
275
276 def selection_full_cols():
277 """Get the selected data grouped in columns, containing all rows even non-selected ones"""
278 return _defObj.selection_full_cols()
279
280 def all_cols():
281 """Get all the data grouped in columns"""
282 return _defObj.all_cols()
283
284 # By Cells
285 def cells_selection_by_row():
286 """Get the selection as separate elements, read row by row"""
287 return _defObj.cells_selection_by_row()
288
289 def cells_all_by_row():
290 """Get all the data as separate elements, read row by row"""
291 return _defObj.cells_all_by_row()
292
293 def cells_selection_by_col():
294 """Get the selection as separate elements, read column by column"""
295 return _defObj.cells_selection_by_col()
296
297 def cells_all_by_col():
298 """Get all the data as separate elements, read column by column"""
299 return _defObj.cells_all_by_col()
300
301 # ----------------
302 # Data setting functions (for data output from script)
303
304 def set_rows(new_data, new_first_row = -1, new_first_col = -1, new_num_rows = -1, new_num_cols = -1):
305 """Set the data for the text output organized by rows"""
306 _defObj.set_rows(new_data, new_first_row, new_first_col, new_num_rows, new_num_cols)
307
308 def set_cols(new_data, new_first_row = -1, new_first_col = -1, new_num_rows = -1, new_num_cols = -1):
309 """Set the data for the text output organized by columns"""
310 _defObj.set_cols(new_data, new_first_row, new_first_col, new_num_rows, new_num_cols)
311
312 def set_cells_by_row(new_data, new_first_row = -1, new_first_col = -1, new_num_rows = -1, new_num_cols = -1):
313 """Set the data for the text output as a list of individual cells organized by rows"""
314 _defObj.set_cells_by_row(new_data, new_first_row, new_first_col, new_num_rows, new_num_cols)
315
316 def set_cells_by_col(new_data, new_first_row = -1, new_first_col = -1, new_num_rows = -1, new_num_cols = -1):
317 """Set the data for the text output as a list of individual cells organized by columns"""
318 _defObj.set_cells_by_col(new_data, new_first_row, new_first_col, new_num_rows, new_num_cols)
319
320 def set_text(text, new_first_row = -1, new_first_col = -1, new_num_rows = -1, new_num_cols = -1):
321 """Set data from a text string, each line is interpreted as a row and each cell must be separated by a space."""
322 _defObj.set_text(text, new_first_row, new_first_col, new_num_rows, new_num_cols)
323
324 # ----------------
325 # Utility functions
326
327 def sort_by_start(rows, p_field = 2):
328 _defObj.sort_by_start(rows, p_field)
329
330 if __name__ == '__main__':
331 print "QuteSheet python module. Part of QuteCsound."