Describe a CSV dialect. This must be subclassed (see csv.excel). Valid attributes are: delimiter, quotechar, escapechar, doublequote, skipinitialspace, lineterminator, quoting, strict.
| 503 | |
| 504 | |
| 505 | class Dialect(): |
| 506 | """Describe a CSV dialect. |
| 507 | This must be subclassed (see csv.excel). Valid attributes are: |
| 508 | delimiter, quotechar, escapechar, doublequote, skipinitialspace, |
| 509 | lineterminator, quoting, strict. |
| 510 | """ |
| 511 | _name = "" |
| 512 | _valid = False |
| 513 | # placeholders |
| 514 | delimiter = None |
| 515 | quotechar = None |
| 516 | escapechar = None |
| 517 | doublequote = None |
| 518 | skipinitialspace = None |
| 519 | lineterminator = None |
| 520 | quoting = None |
| 521 | strict = None |
| 522 | |
| 523 | def __init__(self): |
| 524 | self.validate(self) |
| 525 | if self.__class__ != Dialect: |
| 526 | self._valid = True |
| 527 | |
| 528 | @classmethod |
| 529 | def validate(cls, dialect): |
| 530 | dialect = cls.extend(dialect) |
| 531 | |
| 532 | if not isinstance(dialect.quoting, int): |
| 533 | raise Error('"quoting" must be an integer') |
| 534 | |
| 535 | if dialect.delimiter is None: |
| 536 | raise Error('delimiter must be set') |
| 537 | cls.validate_text(dialect, 'delimiter') |
| 538 | |
| 539 | if dialect.lineterminator is None: |
| 540 | raise Error('lineterminator must be set') |
| 541 | if not isinstance(dialect.lineterminator, str): |
| 542 | raise Error('"lineterminator" must be a string') |
| 543 | |
| 544 | if dialect.quoting not in [ |
| 545 | QUOTE_NONE, QUOTE_MINIMAL, QUOTE_NONNUMERIC, QUOTE_ALL]: |
| 546 | raise Error('Invalid quoting specified') |
| 547 | |
| 548 | if dialect.quoting != QUOTE_NONE: |
| 549 | if dialect.quotechar is None and dialect.escapechar is None: |
| 550 | raise Error('quotechar must be set if quoting enabled') |
| 551 | if dialect.quotechar is not None: |
| 552 | cls.validate_text(dialect, 'quotechar') |
| 553 | |
| 554 | @staticmethod |
| 555 | def validate_text(dialect, attr): |
| 556 | val = getattr(dialect, attr) |
| 557 | if not isinstance(val, str): |
| 558 | if isinstance(val, bytes): |
| 559 | raise Error('"{0}" must be string, not bytes'.format(attr)) |
| 560 | raise Error('"{0}" must be string, not {1}'.format( |
| 561 | attr, type(val).__name__)) |
| 562 |
nothing calls this directly
no outgoing calls
no test coverage detected