|
| 1 | +/* Copyright (c) Jython Developers */ |
| 2 | +package org.python.modules._csv; |
| 3 | + |
| 4 | +import org.python.core.ArgParser; |
| 5 | +import org.python.core.Py; |
| 6 | +import org.python.core.PyBaseString; |
| 7 | +import org.python.core.PyInteger; |
| 8 | +import org.python.core.PyNewWrapper; |
| 9 | +import org.python.core.PyObject; |
| 10 | +import org.python.core.PyString; |
| 11 | +import org.python.core.PyType; |
| 12 | +import org.python.expose.ExposedDelete; |
| 13 | +import org.python.expose.ExposedGet; |
| 14 | +import org.python.expose.ExposedNew; |
| 15 | +import org.python.expose.ExposedSet; |
| 16 | +import org.python.expose.ExposedType; |
| 17 | + |
| 18 | +/** |
| 19 | + * The Python CSV Dialect type. |
| 20 | + */ |
| 21 | +@ExposedType(name = "_csv.Dialect") |
| 22 | +public class PyDialect extends PyObject { |
| 23 | + |
| 24 | + public static final PyType TYPE = PyType.fromClass(PyDialect.class); |
| 25 | + |
| 26 | + public PyString __doc__ = Py.newString( |
| 27 | + "CSV dialect\n" + |
| 28 | + "\n" + |
| 29 | + "The Dialect type records CSV parsing and generation options.\n"); |
| 30 | + |
| 31 | + /** Whether " is represented by "". */ |
| 32 | + @ExposedGet |
| 33 | + public boolean doublequote; |
| 34 | + |
| 35 | + /** Field separator. */ |
| 36 | + @ExposedGet |
| 37 | + public char delimiter; |
| 38 | + |
| 39 | + /** Quote character. */ |
| 40 | + public char quotechar; |
| 41 | + |
| 42 | + /** Escape character. */ |
| 43 | + public char escapechar; |
| 44 | + |
| 45 | + /** Ignore spaces following delimiter? */ |
| 46 | + @ExposedGet |
| 47 | + public boolean skipinitialspace; |
| 48 | + |
| 49 | + /** String to write between records. */ |
| 50 | + @ExposedGet |
| 51 | + public String lineterminator; |
| 52 | + |
| 53 | + /** Style of quoting to write. */ |
| 54 | + public QuoteStyle quoting; |
| 55 | + |
| 56 | + /** Whether an exception is raised on bad CSV. */ |
| 57 | + @ExposedGet |
| 58 | + public boolean strict; |
| 59 | + |
| 60 | + public PyDialect() { |
| 61 | + super(TYPE); |
| 62 | + } |
| 63 | + |
| 64 | + public PyDialect(PyType subType) { |
| 65 | + super(subType); |
| 66 | + } |
| 67 | + |
| 68 | + @ExposedNew |
| 69 | + final static PyObject Dialect___new__(PyNewWrapper new_, boolean init, PyType subtype, |
| 70 | + PyObject[] args, String[] keywords) { |
| 71 | + ArgParser ap = new ArgParser("__new__", args, keywords, |
| 72 | + new String[] {"dialect", "delimiter", "doublequote", |
| 73 | + "escapechar", "lineterminator", "quotechar", |
| 74 | + "quoting", "skipinitialspace", "strict"}); |
| 75 | + PyObject dialect = ap.getPyObject(0, null); |
| 76 | + PyObject delimiter = ap.getPyObject(1, null); |
| 77 | + PyObject doublequote = ap.getPyObject(2, null); |
| 78 | + PyObject escapechar = ap.getPyObject(3, null); |
| 79 | + PyObject lineterminator = ap.getPyObject(4, null); |
| 80 | + PyObject quotechar = ap.getPyObject(5, null); |
| 81 | + PyObject quoting = ap.getPyObject(6, null); |
| 82 | + PyObject skipinitialspace = ap.getPyObject(7, null); |
| 83 | + PyObject strict = ap.getPyObject(8, null); |
| 84 | + |
| 85 | + if (dialect instanceof PyString) { |
| 86 | + dialect = _csv.get_dialect_from_registry(dialect); |
| 87 | + } |
| 88 | + |
| 89 | + // Can we reuse this instance? |
| 90 | + if (dialect instanceof PyDialect && delimiter == null && doublequote == null |
| 91 | + && escapechar == null && lineterminator == null && quotechar == null && quoting == null |
| 92 | + && skipinitialspace == null && strict == null) { |
| 93 | + return dialect; |
| 94 | + } |
| 95 | + |
| 96 | + if (dialect != null) { |
| 97 | + delimiter = delimiter != null ? delimiter : dialect.__findattr__("delimiter"); |
| 98 | + doublequote = doublequote != null |
| 99 | + ? doublequote : dialect.__findattr__("doublequote"); |
| 100 | + escapechar = escapechar != null ? escapechar : dialect.__findattr__("escapechar"); |
| 101 | + lineterminator = lineterminator != null |
| 102 | + ? lineterminator : dialect.__findattr__("lineterminator"); |
| 103 | + quotechar = quotechar != null ? quotechar : dialect.__findattr__("quotechar"); |
| 104 | + quoting = quoting != null ? quoting : dialect.__findattr__("quoting"); |
| 105 | + skipinitialspace = skipinitialspace != null |
| 106 | + ? skipinitialspace : dialect.__findattr__("skipinitialspace"); |
| 107 | + strict = strict != null ? strict : dialect.__findattr__("strict"); |
| 108 | + } |
| 109 | + |
| 110 | + PyDialect self; |
| 111 | + if (new_.for_type == subtype) { |
| 112 | + self = new PyDialect(); |
| 113 | + } else { |
| 114 | + self = new PyDialectDerived(subtype); |
| 115 | + } |
| 116 | + |
| 117 | + // check types and convert to Java values |
| 118 | + int quotingOrdinal; |
| 119 | + self.delimiter = toChar("delimiter", delimiter, ','); |
| 120 | + self.doublequote = toBool("doublequote", doublequote, true); |
| 121 | + self.escapechar = toChar("escapechar", escapechar, '\0'); |
| 122 | + self.lineterminator = toStr("lineterminator", lineterminator, "\r\n"); |
| 123 | + self.quotechar = toChar("quotechar", quotechar, '"'); |
| 124 | + quotingOrdinal = toInt("quoting", quoting, QuoteStyle.QUOTE_MINIMAL.ordinal()); |
| 125 | + self.skipinitialspace = toBool("skipinitialspace", skipinitialspace, false); |
| 126 | + self.strict = toBool("strict", strict, false); |
| 127 | + |
| 128 | + // validate options |
| 129 | + self.quoting = QuoteStyle.fromOrdinal(quotingOrdinal); |
| 130 | + if (self.quoting == null) { |
| 131 | + throw Py.TypeError("bad \"quoting\" value"); |
| 132 | + } |
| 133 | + if (self.delimiter == '\0') { |
| 134 | + throw Py.TypeError("delimiter must be set"); |
| 135 | + } |
| 136 | + if (quotechar == Py.None && quoting == null) { |
| 137 | + self.quoting = QuoteStyle.QUOTE_NONE; |
| 138 | + } |
| 139 | + if (self.quoting != QuoteStyle.QUOTE_NONE && self.quotechar == '\0') { |
| 140 | + throw Py.TypeError("quotechar must be set if quoting enabled"); |
| 141 | + } |
| 142 | + if (self.lineterminator == null) { |
| 143 | + throw Py.TypeError("lineterminator must be set"); |
| 144 | + } |
| 145 | + |
| 146 | + return self; |
| 147 | + } |
| 148 | + |
| 149 | + private static boolean toBool(String name, PyObject src, boolean dflt) { |
| 150 | + return src == null ? dflt : src.__nonzero__(); |
| 151 | + } |
| 152 | + |
| 153 | + private static char toChar(String name, PyObject src, char dflt) { |
| 154 | + if (src == null) { |
| 155 | + return dflt; |
| 156 | + } |
| 157 | + boolean isStr = Py.isInstance(src, PyString.TYPE); |
| 158 | + if (src == Py.None || isStr && src.__len__() == 0) { |
| 159 | + return '\0'; |
| 160 | + } else if (!isStr || src.__len__() != 1) { |
| 161 | + throw Py.TypeError(String.format("\"%s\" must be an 1-character string", name)); |
| 162 | + } |
| 163 | + return src.toString().charAt(0); |
| 164 | + } |
| 165 | + |
| 166 | + private static int toInt(String name, PyObject src, int dflt) { |
| 167 | + if (src == null) { |
| 168 | + return dflt; |
| 169 | + } |
| 170 | + if (!(src instanceof PyInteger)) { |
| 171 | + throw Py.TypeError(String.format("\"%s\" must be an integer", name)); |
| 172 | + } |
| 173 | + return src.asInt(); |
| 174 | + } |
| 175 | + |
| 176 | + private static String toStr(String name, PyObject src, String dflt) { |
| 177 | + if (src == null) { |
| 178 | + return dflt; |
| 179 | + } |
| 180 | + if (src == Py.None) { |
| 181 | + return null; |
| 182 | + } |
| 183 | + if (!(src instanceof PyBaseString)) { |
| 184 | + throw Py.TypeError(String.format("\"%s\" must be an string", name)); |
| 185 | + } |
| 186 | + return src.toString(); |
| 187 | + } |
| 188 | + |
| 189 | + @ExposedGet(name = "escapechar") |
| 190 | + public PyObject getEscapechar() { |
| 191 | + return escapechar == '\0' ? Py.None : Py.newString(escapechar); |
| 192 | + } |
| 193 | + |
| 194 | + @ExposedGet(name = "quotechar") |
| 195 | + public PyObject getQuotechar() { |
| 196 | + return quotechar == '\0' ? Py.None : Py.newString(quotechar); |
| 197 | + } |
| 198 | + |
| 199 | + @ExposedGet(name = "quoting") |
| 200 | + public PyObject getQuoting() { |
| 201 | + return Py.newInteger(quoting.ordinal()); |
| 202 | + } |
| 203 | + |
| 204 | + // XXX: setQuoting and delQuoting are to make test_csv pass (currently we incorrectly |
| 205 | + // throw TypeErrors for get/set AttributeError failures) |
| 206 | + @ExposedSet(name = "quoting") |
| 207 | + public void setQuoting(PyObject obj) { |
| 208 | + throw Py.AttributeError(String.format("attribute '%s' of '%s' objects is not writable", |
| 209 | + "quoting", getType().fastGetName())); |
| 210 | + } |
| 211 | + |
| 212 | + @ExposedDelete(name = "quoting") |
| 213 | + public void delQuoting() { |
| 214 | + throw Py.AttributeError(String.format("attribute '%s' of '%s' objects is not writable", |
| 215 | + "quoting", getType().fastGetName())); |
| 216 | + } |
| 217 | +} |