| 1 | import sys |
| 2 | from rpython.rlib import rutf8 |
| 3 | from pypy.interpreter.baseobjspace import W_Root |
| 4 | from pypy.interpreter.error import OperationError, oefmt |
| 5 | from pypy.interpreter.typedef import TypeDef, interp_attrproperty |
| 6 | from pypy.interpreter.typedef import GetSetProperty |
| 7 | from pypy.interpreter.gateway import interp2app |
| 8 | |
| 9 | |
| 10 | QUOTE_MINIMAL, QUOTE_ALL, QUOTE_NONNUMERIC, QUOTE_NONE = range(4) |
| 11 | |
| 12 | NOT_SET = sys.maxint |
| 13 | |
| 14 | class W_Dialect(W_Root): |
| 15 | _immutable_fields_ = [ |
| 16 | "dialect", |
| 17 | "delimiter", |
| 18 | "doublequote", |
| 19 | "escapechar", |
| 20 | "lineterminator", |
| 21 | "quotechar", |
| 22 | "quoting", |
| 23 | "skipinitialspace", |
| 24 | "strict", |
| 25 | ] |
| 26 | |
| 27 | def reduce_ex_w(self, space, w_protocol): |
| 28 | raise OperationError(space.w_TypeError, |
| 29 | space.newtext("can't pickle _csv.Dialect objects")) |
| 30 | |
| 31 | def _fetch(space, w_dialect, name): |
| 32 | return space.findattr(w_dialect, space.newtext(name)) |
| 33 | |
| 34 | def _get_bool(space, w_src, default): |
| 35 | if w_src is None: |
| 36 | return default |
| 37 | return space.is_true(w_src) |
| 38 | |
| 39 | def _get_int(space, w_src, default, attrname): |
| 40 | if w_src is None: |
| 41 | return default |
| 42 | try: |
| 43 | return space.int_w(w_src) |
| 44 | except OperationError as e: |
| 45 | if e.match(space, space.w_TypeError): |
| 46 | raise oefmt(space.w_TypeError, '"%s" must be an int', attrname) |
| 47 | raise |
| 48 | |
| 49 | def _get_str(space, w_src, default, attrname): |
| 50 | if w_src is None: |
| 51 | return default |
| 52 | try: |
| 53 | return space.text_w(w_src) |
| 54 | except OperationError as e: |
| 55 | if e.match(space, space.w_TypeError): |
| 56 | raise oefmt(space.w_TypeError, '"%s" must be a string', attrname) |
| 57 | raise |
| 58 | |
| 59 | def _get_codepoint(space, w_src, default, name, can_be_None=True, can_be_not_set=True): |
| 60 | if w_src is None: |
| 61 | return default |
| 62 | if space.is_w(w_src, space.w_None) and can_be_None: |
| 63 | return NOT_SET |
| 64 | if not space.isinstance_w(w_src, space.w_unicode): |
| 65 | if can_be_None: |
| 66 | raise oefmt(space.w_TypeError, '"%s" must be string or None, not %T', name, w_src) |
| 67 | raise oefmt(space.w_TypeError, '"%s" must be string, not %T', name, w_src) |
| 68 | src, length = space.utf8_len_w(w_src) |
| 69 | if length == 1: |
| 70 | res = rutf8.codepoint_at_pos(src, 0) |
| 71 | assert res >= 0 |
| 72 | return res |
| 73 | if len(src) == 0 and can_be_not_set: |
| 74 | return NOT_SET |
| 75 | raise oefmt(space.w_TypeError, '"%s" must be a 1-character string', name) |
| 76 | |
| 77 | def _build_dialect(space, w_dialect, w_delimiter, w_doublequote, |
| 78 | w_escapechar, w_lineterminator, w_quotechar, w_quoting, |
| 79 | w_skipinitialspace, w_strict): |
| 80 | if w_dialect is not None: |
| 81 | if space.isinstance_w(w_dialect, space.w_unicode): |
| 82 | w_module = space.getbuiltinmodule('_csv') |
| 83 | w_dialect = space.call_method(w_module, 'get_dialect', w_dialect) |
| 84 | |
| 85 | if (isinstance(w_dialect, W_Dialect) and |
| 86 | w_delimiter is None and |
| 87 | w_doublequote is None and |
| 88 | w_escapechar is None and |
| 89 | w_lineterminator is None and |
| 90 | w_quotechar is None and |
| 91 | w_quoting is None and |
| 92 | w_skipinitialspace is None and |
| 93 | w_strict is None): |
| 94 | return w_dialect |
| 95 | |
| 96 | if w_delimiter is None: |
| 97 | w_delimiter = _fetch(space, w_dialect, 'delimiter') |
| 98 | if w_doublequote is None: |
| 99 | w_doublequote = _fetch(space, w_dialect, 'doublequote') |
| 100 | if w_escapechar is None: |
| 101 | w_escapechar = _fetch(space, w_dialect, 'escapechar') |
| 102 | if w_lineterminator is None: |
| 103 | w_lineterminator = _fetch(space, w_dialect, 'lineterminator') |
| 104 | if w_quotechar is None: |
| 105 | w_quotechar = _fetch(space, w_dialect, 'quotechar') |
| 106 | if w_quoting is None: |
| 107 | w_quoting = _fetch(space, w_dialect, 'quoting') |
| 108 | if w_skipinitialspace is None: |
| 109 | w_skipinitialspace = _fetch(space, w_dialect, 'skipinitialspace') |
| 110 | if w_strict is None: |
| 111 | w_strict = _fetch(space, w_dialect, 'strict') |
| 112 | |
| 113 | dialect = W_Dialect() |
| 114 | dialect.delimiter = _get_codepoint(space, w_delimiter, ord(u','), 'delimiter', can_be_None=False) |
| 115 | dialect.doublequote = _get_bool(space, w_doublequote, True) |
| 116 | dialect.escapechar = _get_codepoint(space, w_escapechar, NOT_SET, 'escapechar', can_be_not_set=False) |
| 117 | dialect.lineterminator = _get_str(space, w_lineterminator, '\r\n', 'lineterminator') |
| 118 | tmp_quoting = _get_int(space, w_quoting, QUOTE_MINIMAL, 'quoting') |
| 119 | if not (0 <= tmp_quoting < 4): |
| 120 | raise oefmt(space.w_TypeError, 'bad "quoting" value') |
| 121 | if space.is_w(w_quotechar, space.w_None) and w_quoting is None: |
| 122 | tmp_quoting = QUOTE_NONE |
| 123 | dialect.quotechar = _get_codepoint(space, w_quotechar, ord(u'"'), 'quotechar', can_be_not_set=False) |
| 124 | dialect.skipinitialspace = _get_bool(space, w_skipinitialspace, False) |
| 125 | dialect.strict = _get_bool(space, w_strict, False) |
| 126 | |
| 127 | |
| 128 | if dialect.delimiter == NOT_SET: |
| 129 | raise oefmt(space.w_TypeError, |
| 130 | '"delimiter" must be a 1-character string') |
| 131 | |
| 132 | if tmp_quoting != QUOTE_NONE and dialect.quotechar == NOT_SET: |
| 133 | raise oefmt(space.w_TypeError, |
| 134 | "quotechar must be set if quoting enabled") |
| 135 | dialect.quoting = tmp_quoting |
| 136 | return dialect |
| 137 | |
| 138 | def W_Dialect___new__(space, w_subtype, w_dialect = None, |
| 139 | w_delimiter = None, |
| 140 | w_doublequote = None, |
| 141 | w_escapechar = None, |
| 142 | w_lineterminator = None, |
| 143 | w_quotechar = None, |
| 144 | w_quoting = None, |
| 145 | w_skipinitialspace = None, |
| 146 | w_strict = None, |
| 147 | ): |
| 148 | dialect = _build_dialect(space, w_dialect, w_delimiter, w_doublequote, |
| 149 | w_escapechar, w_lineterminator, w_quotechar, |
| 150 | w_quoting, w_skipinitialspace, w_strict) |
| 151 | if space.is_w(w_subtype, space.gettypeobject(W_Dialect.typedef)): |
| 152 | return dialect |
| 153 | else: |
| 154 | subdialect = space.allocate_instance(W_Dialect, w_subtype) |
| 155 | subdialect.delimiter = dialect.delimiter |
| 156 | subdialect.doublequote = dialect.doublequote |
| 157 | subdialect.escapechar = dialect.escapechar |
| 158 | subdialect.lineterminator = dialect.lineterminator |
| 159 | subdialect.quotechar = dialect.quotechar |
| 160 | subdialect.quoting = dialect.quoting |
| 161 | subdialect.skipinitialspace = dialect.skipinitialspace |
| 162 | subdialect.strict = dialect.strict |
| 163 | return subdialect |
| 164 | |
| 165 | |
| 166 | def _get_escapechar(space, dialect): |
| 167 | if dialect.escapechar == NOT_SET: |
| 168 | return space.w_None |
| 169 | s = rutf8.unichr_as_utf8(dialect.escapechar) |
| 170 | return space.newutf8(s, 1) |
| 171 | |
| 172 | def _get_quotechar(space, dialect): |
| 173 | if dialect.quotechar == NOT_SET: |
| 174 | return space.w_None |
| 175 | s = rutf8.unichr_as_utf8(dialect.quotechar) |
| 176 | return space.newutf8(s, 1) |
| 177 | |
| 178 | def _get_delimiter(space, dialect): |
| 179 | if dialect.delimiter == NOT_SET: |
| 180 | return space.w_None |
| 181 | s = rutf8.unichr_as_utf8(dialect.delimiter) |
| 182 | return space.newutf8(s, 1) |
| 183 | |
| 184 | |
| 185 | W_Dialect.typedef = TypeDef( |
| 186 | '_csv.Dialect', |
| 187 | __new__ = interp2app(W_Dialect___new__), |
| 188 | __reduce_ex__ = interp2app(W_Dialect.reduce_ex_w), |
| 189 | |
| 190 | delimiter = GetSetProperty(_get_delimiter, cls=W_Dialect), |
| 191 | doublequote = interp_attrproperty('doublequote', W_Dialect, |
| 192 | wrapfn='newbool'), |
| 193 | escapechar = GetSetProperty(_get_escapechar, cls=W_Dialect), |
| 194 | lineterminator = interp_attrproperty('lineterminator', W_Dialect, |
| 195 | wrapfn='newtext'), |
| 196 | quotechar = GetSetProperty(_get_quotechar, cls=W_Dialect), |
| 197 | quoting = interp_attrproperty('quoting', W_Dialect, |
| 198 | wrapfn='newint'), |
| 199 | skipinitialspace = interp_attrproperty('skipinitialspace', W_Dialect, |
| 200 | wrapfn='newbool'), |
| 201 | strict = interp_attrproperty('strict', W_Dialect, |
| 202 | wrapfn='newbool'), |
| 203 | |
| 204 | __doc__ = """CSV dialect |
| 205 | |
| 206 | The Dialect type records CSV parsing and generation options. |
| 207 | """) |