[3] | 1 | """ |
---|
| 2 | An extension function to iterate over a list of comma-separated values. |
---|
| 3 | |
---|
| 4 | Function 'csv_iterate' reads a file containing one or more rows of |
---|
| 5 | comma-separated columns, assigns them to col1...colN, and, for each row, |
---|
| 6 | executes the given twill script. |
---|
| 7 | """ |
---|
| 8 | |
---|
| 9 | __all__ = ['csv_iterate'] |
---|
| 10 | |
---|
| 11 | DEBUG=True |
---|
| 12 | |
---|
| 13 | import csv |
---|
| 14 | |
---|
| 15 | def csv_iterate(filename, scriptname): |
---|
| 16 | """ |
---|
| 17 | >> csv_iterate <csv_file> <script> |
---|
| 18 | |
---|
| 19 | For each line in <csv_file>, read in a list of comma-separated values, |
---|
| 20 | put them in $col1...$colN, and execute <script>. |
---|
| 21 | """ |
---|
| 22 | from twill import namespaces, execute_file, commands |
---|
| 23 | |
---|
| 24 | global_dict, local_dict = namespaces.get_twill_glocals() |
---|
| 25 | |
---|
| 26 | reader = csv.reader(open(filename, "rb")) |
---|
| 27 | for i, row in enumerate(reader): |
---|
| 28 | if DEBUG: |
---|
| 29 | print>>commands.OUT,'csv_iterate: on row %d of %s' % (i, filename,) |
---|
| 30 | for i, col in enumerate(row): |
---|
| 31 | global_dict["col%d" % (i + 1,)] = col |
---|
| 32 | |
---|
| 33 | execute_file(scriptname, no_reset=True) |
---|