File: ssv_csv.py

package info (click to toggle)
gvm-tools 25.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,480 kB
  • sloc: python: 10,611; xml: 445; makefile: 27
file content (442 lines) | stat: -rw-r--r-- 15,286 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/env python3
# coding: utf-8
# -
# Copyright © 2015, 2017, 2020, 2022
#       mirabilos <t.glaser@tarent.de>
# Licensor: tarent solutions GmbH
#
# Provided that these terms and disclaimer and all copyright notices
# are retained or reproduced in an accompanying document, permission
# is granted to deal in this work without restriction, including un‐
# limited rights to use, publicly perform, distribute, sell, modify,
# merge, give away, or sublicence.
#
# This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
# the utmost extent permitted by applicable law, neither express nor
# implied; without malicious intent or gross negligence. In no event
# may a licensor, author or contributor be held liable for indirect,
# direct, other damage, loss, or other issues arising in any way out
# of dealing in the work, even if advised of the possibility of such
# damage or existence of a defect, except proven that it results out
# of said person’s immediate fault when using the work as intended.

r"""SSV reader/writer and CSV writer library

This module offers the following classes:

- CSVInvalidCharacterError, CSVShapeError -- Exception classes
  that can be thrown by code from this library

- CSVPrinter -- configurable CSV row formatter and writer that
  ensures the output is quoted properly and in rectangular shape

- SSVPrinter -- CSVPrinter configured to produce SSV output

- CSVWriter, SSVWriter -- same but writing to a file-like object

- SSVReader -- class to read SSV files, returning lists of str|bytes
  (depending on the input file binary flag)

When run directly, it acts as SSV to CSV converter, which may be of
limited use but demonstrates how to use the module somewhat; -h for
usage (help).
"""

__all__ = [
    "CSVInvalidCharacterError",
    "CSVPrinter",
    "CSVShapeError",
    "CSVWriter",
    "SSVPrinter",
    "SSVReader",
    "SSVWriter",
]

import re
import sys
from typing import IO, AnyStr, List, Optional, TextIO


class CSVShapeError(Exception):
    r"""Error: data to write did not have a consistent amount of columns.

    The message string is descriptive, but the want and got fields of
    an object may be user-accessed.
    """

    def __init__(self, want: int, got: int) -> None:
        Exception.__init__(
            self, f"got {got} column{got != 1 and 's' or ''} but wanted {want}"
        )
        self.want = want  # type: int
        self.got = got  # type: int


class CSVInvalidCharacterError(Exception):
    r"""Error: disallowed characters in cell (writer) / row (reader).

    The message is deliberately constant in order to not show the actual
    cell or row content in logs, etc. (in case it’s a password) but the
    actual content is available in the questionable_content field.
    """

    def __init__(
        self, value: AnyStr, what: str = "prohibited character in cell"
    ) -> None:
        Exception.__init__(self, what)
        self.questionable_content = value  # type: Union[str, bytes]


class CSVPrinter(object):
    r"""CSV writer library, configurable.

    The defaults follow RFC 4180 and thus are suitable for use with most
    environments; newlines embedded in cell data are normalised (from
    ASCII/Unix/Mac to ASCII) by default, every cell data is quoted.

    The following arguments configure the writer instance:
    - sep -- output cell separator: ',' (default) or ';' or '\t'
    - quot -- output quote character (default '"'), escape by doubling;
        None to disable quoting and disallow embedded newlines (but not
        double quotes; the caller must not pass any if the result needs
        to conform to the RFC or just use default quoting of course)
    - eol -- output line terminator: '\r\n' or (Unix) '\n' or (Mac) '\r'
    - qnl -- output embedded newline, should match eol (default '\r\n');
        None to disable embedded newline normalisation

    These arguments must all be str, bytes is not supported. Cell data
    passed in that is not str will be stringified. Rows are written or
    returned as str; however, ASCII or a compatible encoding needs to
    be used (for conformance and portability); UTF-8 is ideal.

    Note: RFC 4180 permits only printable ASCII and space and, if quoted,
    newlines in cell data but this library permits any character except
    NUL, (unquoted) CR and LF.
    """

    # embedded NUL is never permitted
    _invf = re.compile("[\x00]")  # type: Pattern[str]
    # normalise embedded newlines (match)
    _nlf = re.compile("\r\n?|(?<!\r)\n")  # type: Pattern[str]
    # count to ensure rectangular shape of output
    _ncols = -1  # type: int

    # default line ending is ASCII (“DOS”)
    def __init__(
        self,
        sep: str = ",",
        quot: Optional[str] = '"',
        eol: str = "\r\n",
        qnl: Optional[str] = "\r\n",
    ) -> None:
        if quot is None:
            # cell joiner ('","')
            self._sep = sep  # type: str
            # one quote, e.g. for line beginning/end
            self._quots = ""  # type: str
            # two quotes to escape
            self._quotd = ""  # type: str
            # forbid newlines if we cannot quote them
            self._invf = re.compile("[\x00\r\n]")
        else:
            self._sep = quot + sep + quot
            self._quots = quot
            self._quotd = quot + quot
        # None if not quoting
        self._quot = quot  # type: Optional[str]
        # None or embedded newline replacement string
        self._nlrpl = qnl  # type: Optional[str]
        # EOL string
        self._eol = eol  # type: str

    def _mapcell(self, cell) -> str:
        if isinstance(cell, str):
            cstr = cell  # type: str
        else:
            cstr = str(cell)
        if self._invf.search(cstr) is not None:
            raise CSVInvalidCharacterError(cstr)
        if self._nlrpl is not None:
            cstr = self._nlf.sub(self._nlrpl, cstr)
        if self._quot is not None:
            cstr = cstr.replace(self._quots, self._quotd)
        return cstr

    def write(self, *args) -> None:
        r"""Print a CSV line (row) to standard output.

        - *args -- cell data by columns

        Note: reconfigures the newline mode of sys.stdout once,
        but make sure to run sys.stdout.reconfigure(newline='\n')
        e.g. when emitting an MS Excel sep= line and get the line
        ending for that right; see _main() for an example.
        """
        if hasattr(sys.stdout, "reconfigure"):
            sys.stdout.reconfigure(newline="\n")  # type: ignore
        setattr(CSVPrinter, "write", getattr(CSVPrinter, "_write"))
        delattr(CSVPrinter, "_write")
        return self.write(*args)

    def _write(self, *args) -> None:
        print(self.format(*args), end="")

    _write.__doc__ = write.__doc__

    def format(self, *args) -> str:
        r"""Produce a CSV row from cells.

        - *args -- cell data by columns

        Returns the row, including the trailing newline, as string.
        """
        if self._ncols == -1:
            self._ncols = len(args)
        elif self._ncols != len(args):
            raise CSVShapeError(self._ncols, len(args))
        cells = map(self._mapcell, args)
        return self._quots + self._sep.join(cells) + self._quots + self._eol


class CSVWriter(CSVPrinter):
    r"""CSV writer library, configurable.

    The defaults follow RFC 4180 and thus are suitable for use with most
    environments; newlines embedded in cell data are normalised (from
    ASCII/Unix/Mac to ASCII) by default, every cell data is quoted.

    The following arguments configure the writer instance:
    - file -- file-like object to output CSV to
    - sep -- output cell separator: ',' (default) or ';' or '\t'
    - quot -- output quote character (default '"'), escape by doubling;
        None to disable quoting and disallow embedded newlines (but not
        double quotes; the caller must not pass any if the result needs
        to conform to the RFC or just use default quoting of course)
    - eol -- output line terminator: '\r\n' or (Unix) '\n' or (Mac) '\r'
    - qnl -- output embedded newline, should match eol (default '\r\n');
        None to disable embedded newline normalisation

    These arguments must all be str, bytes is not supported. Cell data
    passed in that is not str will be stringified. Rows are written or
    returned as str; however, ASCII or a compatible encoding needs to
    be used (for conformance and portability); UTF-8 is ideal.

    Note: RFC 4180 permits only printable ASCII and space and, if quoted,
    newlines in cell data but this library permits any character except
    NUL, (unquoted) CR and LF.

    Note: the file argument will be reconfigured to disable automatic
    '\n' conversion; if prepending data (e.g. a sep= line for MS Excel)
    use the writeln() method.
    """

    def __init__(
        self,
        file: TextIO,
        sep: str = ",",
        quot: Optional[str] = '"',
        eol: str = "\r\n",
        qnl: Optional[str] = "\r\n",
    ) -> None:
        CSVPrinter.__init__(self, sep, quot, eol, qnl)
        # disable any automatic newline conversion if preset
        file.reconfigure(newline="\n")  # type: ignore
        self.outfile = file

    def write(self, *args) -> None:
        r"""Print a CSV line (row) to the output file.

        - *args -- cell data by columns
        """
        print(self.format(*args), end="", file=self.outfile)

    def writeln(self, line: str) -> None:
        r"""Print an arbitrary nōn-CSV line to the output file.

        - line -- str to output; trailing newline is automatically added
        """
        print(line, end=self._eol, file=self.outfile)


class SSVPrinter(CSVPrinter):
    r"""SSV writer library.

    This subclass sets up a CSVPrinter instance to produce SSV (see below).
    The writer supports str, or stringified arguments, only, not bytes.
    The caller must ensure the encoding is UTF-8 (ideally), or at least
    ASCII-compatible (CR, LF and \x1F require identity mapping), and that
    CR or LF characters output are not converted.

    shell-parseable separated values (or separator-separated values)
    is an idea to make CSV into something usable:

    • newline (\x0A) is row separator
    • unit separator (\x1F) is column separator
    • n̲o̲ quotes or escape characters
    • carriage return (\x0D) represents embedded newlines in cells

    Cell content is, in theory, arbitrary binary except NUL and
    the separators (\x1F and \x0A). In practice it should be UTF-8.

    SSV can be easily read from shell scripts:

        while IFS=$'\x1F' read -r col1 col2…; do
            # do something
        done
    """

    def __init__(self) -> None:
        CSVPrinter.__init__(self, sep="\x1f", quot=None, eol="\n", qnl="\r")
        # not permitted in SSV data
        self._invf = re.compile("[\x00\x1f]")


class SSVWriter(CSVWriter):
    r"""SSV writer library (same as SSVPrinter except to file)"""

    def __init__(self, file: TextIO) -> None:
        # pylint: disable=C0301
        CSVWriter.__init__(
            self, file, sep="\x1f", quot=None, eol="\n", qnl="\r"
        )
        # not permitted in SSV data
        self._invf = re.compile("[\x00\x1f]")


if SSVPrinter.__doc__ is not None:
    SSVWriter.__doc__ = SSVPrinter.__doc__.replace("CSVPrinter", "CSVWriter")


class SSVReader(object):
    r"""SSV reader library.

    This library is initialised with a files-like object that must
    support .readline() and either must not use newline conversion
    or support .reconfigure() as in _io.TextIOWrapper, which is
    called with newline='\n' if it exists. SSVReader.read() will
    then proceed to read from it.

    See SSVPrinter about the SSV format.
    """

    def __init__(self, file: IO) -> None:
        if hasattr(file, "reconfigure"):
            # see https://bugs.python.org/issue46695 though
            file.reconfigure(newline="\n")  # type: ignore
        self.f = file  # type: IO

    @staticmethod
    def _read(
        line: AnyStr,
        lf: AnyStr,
        cr: AnyStr,
        us: AnyStr,
        nl: AnyStr,
        nul: AnyStr,
    ) -> List[AnyStr]:
        if line.find(nul) != -1:
            raise CSVInvalidCharacterError(line, "NUL in row")
        if line[-1:] != lf:
            raise CSVInvalidCharacterError(line, "unterminated row")
        line = line[:-1]
        if line.find(lf) != -1:
            raise CSVInvalidCharacterError(line, "LF in row")
        return line.replace(cr, nl).split(us)

    def read(self) -> Optional[List[AnyStr]]:
        r"""Read and decode one SSV line.

        Returns a list of cells, or None on EOF.
        """
        line = self.f.readline()  # type: Optional[AnyStr]
        if not line:
            return None
        if isinstance(line, str):
            return self._read(line, "\n", "\r", "\x1f", "\r\n", "\x00")
        if isinstance(line, bytes):
            return self._read(line, b"\n", b"\r", b"\x1f", b"\r\n", b"\x00")
        raise TypeError()


# mostly example of how to use this
def _main() -> None:
    # pylint: disable=C0103
    newline_ways = {
        "ascii": "\r\n",
        "unix": "\n",
        "mac": "\r",
    }
    p = argparse.ArgumentParser(
        description="Converts SSV to CSV.",
        # part of https://bugs.python.org/issue46700 workaround
        add_help=False,
    )
    g = p.add_argument_group("Options")  # issue46700
    g.add_argument("-h", action="help", help="show this help message and exit")
    g.add_argument(
        "-s",
        metavar="sep",
        help="cell separator, e.g. \x27,\x27 (default: tab)",
        default="\t",
    )
    g.add_argument(
        "-q",
        metavar="qch",
        help="quote character, e.g. \x27\x22\x27 (default: none)",
        default=None,
    )
    g.add_argument(
        "-n",
        metavar="eoltype",
        choices=list(newline_ways.keys()),
        help="line endings (ascii (default), unix, mac)",
        default="ascii",
    )
    g.add_argument(
        "-P",
        metavar="preset",
        choices=["std", "sep", "ssv"],
        help="predefined config (std=RFC 4180, sep=Excel header, ssv=SSV)",
    )
    g = p.add_argument_group("Arguments")  # issue46700
    g.add_argument(
        "file",
        nargs="?",
        help='SSV file to read, "-" for stdin (default)',
        default="-",
    )
    args = p.parse_args()
    if args.P in ("std", "sep"):
        args.s = ","
        args.q = '"'
        args.n = "ascii"
    nl = newline_ways[args.n]
    if hasattr(sys.stdout, "reconfigure"):
        sys.stdout.reconfigure(newline="\n")  # type: ignore
    if args.P == "sep":
        print(f"sep={args.s}", end=nl)
    if args.P != "ssv":
        w = CSVPrinter(args.s, args.q, nl, nl)
    else:
        w = SSVPrinter()

    def _convert(f):
        r = SSVReader(f)
        # no walrus in Python 3.7 yet ☹
        while True:
            row = r.read()
            if row is None:
                break
            w.write(*row)

    if args.file == "-":
        _convert(sys.stdin)
    else:
        with open(args.file, "r", encoding="utf-8") as file:
            _convert(file)


if __name__ == "__main__":
    import argparse

    _main()