| 1 | from pypy.interpreter.mixedmodule import MixedModule |
| 2 | |
| 3 | |
| 4 | class Module(MixedModule): |
| 5 | """CSV parsing and writing. |
| 6 | |
| 7 | This module provides classes that assist in the reading and writing |
| 8 | of Comma Separated Value (CSV) files, and implements the interface |
| 9 | described by PEP 305. Although many CSV files are simple to parse, |
| 10 | the format is not formally defined by a stable specification and |
| 11 | is subtle enough that parsing lines of a CSV file with something |
| 12 | like line.split(\",\") is bound to fail. The module supports three |
| 13 | basic APIs: reading, writing, and registration of dialects. |
| 14 | |
| 15 | |
| 16 | DIALECT REGISTRATION: |
| 17 | |
| 18 | Readers and writers support a dialect argument, which is a convenient |
| 19 | handle on a group of settings. When the dialect argument is a string, |
| 20 | it identifies one of the dialects previously registered with the module. |
| 21 | If it is a class or instance, the attributes of the argument are used as |
| 22 | the settings for the reader or writer: |
| 23 | |
| 24 | class excel: |
| 25 | delimiter = ',' |
| 26 | quotechar = '\"' |
| 27 | escapechar = None |
| 28 | doublequote = True |
| 29 | skipinitialspace = False |
| 30 | lineterminator = '\\r\\n' |
| 31 | quoting = QUOTE_MINIMAL |
| 32 | |
| 33 | SETTINGS: |
| 34 | |
| 35 | * quotechar - specifies a one-character string to use as the |
| 36 | quoting character. It defaults to '\"'. |
| 37 | * delimiter - specifies a one-character string to use as the |
| 38 | field separator. It defaults to ','. |
| 39 | * skipinitialspace - specifies how to interpret whitespace which |
| 40 | immediately follows a delimiter. It defaults to False, which |
| 41 | means that whitespace immediately following a delimiter is part |
| 42 | of the following field. |
| 43 | * lineterminator - specifies the character sequence which should |
| 44 | terminate rows. |
| 45 | * quoting - controls when quotes should be generated by the writer. |
| 46 | It can take on any of the following module constants: |
| 47 | |
| 48 | csv.QUOTE_MINIMAL means only when required, for example, when a |
| 49 | field contains either the quotechar or the delimiter |
| 50 | csv.QUOTE_ALL means that quotes are always placed around fields. |
| 51 | csv.QUOTE_NONNUMERIC means that quotes are always placed around |
| 52 | fields which do not parse as integers or floating point |
| 53 | numbers. |
| 54 | csv.QUOTE_NONE means that quotes are never placed around fields. |
| 55 | * escapechar - specifies a one-character string used to escape |
| 56 | the delimiter when quoting is set to QUOTE_NONE. |
| 57 | * doublequote - controls the handling of quotes inside fields. When |
| 58 | True, two consecutive quotes are interpreted as one during read, |
| 59 | and when writing, each quote character embedded in the data is |
| 60 | written as two quotes. |
| 61 | """ |
| 62 | |
| 63 | appleveldefs = { |
| 64 | 'register_dialect': 'app_csv.register_dialect', |
| 65 | 'unregister_dialect': 'app_csv.unregister_dialect', |
| 66 | 'get_dialect': 'app_csv.get_dialect', |
| 67 | 'list_dialects': 'app_csv.list_dialects', |
| 68 | '_dialects': 'app_csv._dialects', |
| 69 | |
| 70 | 'Error': 'app_csv.Error', |
| 71 | } |
| 72 | |
| 73 | interpleveldefs = { |
| 74 | '__version__': 'space.wrap("1.0")', |
| 75 | |
| 76 | 'QUOTE_MINIMAL': 'space.wrap(interp_csv.QUOTE_MINIMAL)', |
| 77 | 'QUOTE_ALL': 'space.wrap(interp_csv.QUOTE_ALL)', |
| 78 | 'QUOTE_NONNUMERIC': 'space.wrap(interp_csv.QUOTE_NONNUMERIC)', |
| 79 | 'QUOTE_NONE': 'space.wrap(interp_csv.QUOTE_NONE)', |
| 80 | |
| 81 | 'Dialect': 'interp_csv.W_Dialect', |
| 82 | |
| 83 | 'reader': 'interp_reader.csv_reader', |
| 84 | 'field_size_limit': 'interp_reader.csv_field_size_limit', |
| 85 | |
| 86 | 'writer': 'interp_writer.csv_writer', |
| 87 | } |